Results 1 to 4 of 4
  1. #1
    p.pavlov is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default extending JPanel issue

    Hello,

    I'm new to java and I'm having bit time trouble starting with Graphics class...

    I have to do some custom graphics and other 2D figures. I decided to extend a basic component and just add the drawing features "on top", but hit a wall...
    Okay so I use NetBeans7.0 and so far I
    1) Created following class GraphicsPanel.
    Java Code:
    package gui;
    import javax.swing.*; // For JPanel, etc.
    import java.awt.*; // For Graphics, etc.
    
    
    public class GraphicsPanel extends JPanel {
       private Graphics g1;    
        @Override
        public void paintComponent(Graphics g) {
            g1 = g;
            clear(g);
            g.drawLine (10, 0, 10, 100);
            g.drawLine (250, 100, 10, 100);
             
    
    }
        public void AddLine() {
            g1.drawLine (100, 100, 10, 100);       
            super.paintComponent(g1);
            System.out.println("AddLine()");
        }
        // super.paintComponent clears off screen pixmap,
        // since we're using double buffering by default.
        protected void clear(Graphics g) {
        super.paintComponent(g);
        }
    }
    2) Added new JPanel to my applicaton's gui.
    Since NetBeans doesn't allow modifications to variable declaration code, I used default
    Java Code:
    private javax.swing.JPanel TestGraphic;
    but changed the initialization code to :
    Java Code:
    TestGraphic = new GraphicsPanel();
    3) So now when I run the application I can see the result from
    g.drawLine (10, 0, 10, 100); and g.drawLine (250, 100, 10, 100);
    in public void paintComponent(Graphics g)

    Picture - ImageShack® - Online Photo and Video Hosting

    4) Now I'd like to have a method that enables me to add the actual content to this graphic using public void AddLine(), so I created the jButton with following code
    Java Code:
           private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        GraphicsPanel myTestGraphic = (GraphicsPanel)TestGraphic; 
        myTestGraphic.AddLine();
        
        }
    The method is called and System.out.println("AddLine()") is executed(I see result in console), but nothing happens in the graphical interface... I guess I shouldn't use g1 this way, but still can't figure out what should i do.

    I've already spent half day on this and checked couple books and web tutorials, but still can't figure out what I'm missing... thank you..

    Regards,
    Pavel

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default

    No, you absolutely should not be using Graphics like that.

    What you want to be doing is keeping a List of lines. In paintComponent(), just paint all the lines in the List. In the addLine() method, add a line to that List, then call repaint().

    My best advice to you is to ditch the GUI builder altogether.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    p.pavlov is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Hello Kevin,

    Thank you for the advice it was priceless!!
    I solved the problem by doing following changes:

    1) Defined line, because I couldn't find a predefined one.

    Java Code:
    public class Line {
        public int[] xy = new int[4];
        
        	public Line() {
    		xy[0] = 0; //x1
                    xy[1] = 0; //y1
    		xy[2] = 0; //x2
                    xy[3] = 0; //y2                
                    
    	}
            
        	public Line(int X1,int Y1,int X2,int Y2) {
    		this.xy[0] = X1; this.xy[1] = Y1;
    		this.xy[2] = X2; this.xy[3] = Y2;
    	}   
            
    }
    2) Modified my GraphicsPanel
    Java Code:
    package gui;
    import javax.swing.*; // For JPanel, etc.
    import java.awt.*; // For Graphics, etc.
    
    
    public class GraphicsPanel extends JPanel {
    
        int[] ln = new int[4];
        java.util.LinkedList<core.Line> ll = new java.util.LinkedList<core.Line>();
        
        public GraphicsPanel(){ 
            ll.add(new core.Line(10, 0, 10, 100));
            ll.add(new core.Line(10, 100, 400, 100));
        }
    
        @Override
        public void paintComponent(Graphics g) {
            // Clear off-screen bitmap
            super.paintComponent(g);   
            // Cast Graphics to Graphics2D
            Graphics2D g2d = (Graphics2D)g;      
            //print blue frame
            g2d.setPaint(Color.BLUE);
            for(int i = 0; i< 2; i++) {
                ln = ll.get(i).xy;
                g2d.drawLine(ln[0], ln[1], ln[2], ln[3]);
            }  
            // print red lines with the data 
            g2d.setPaint(Color.RED);
            for(int i = 2; i< ll.size(); i++) {
                ln = ll.get(i).xy;
                g2d.drawLine(ln[0], ln[1], ln[2], ln[3]);
            }            
        }    
     
        public void AddLine(core.Line l) {
            ll.add(l);        
            this.repaint();
        }
    
        public void AddLine(int x1, int y1, int x2, int y2) {
            ll.add(new core.Line(x1, y1, x2, y2));
            this.repaint();
        }    
    }
    3) Changed my JButton a little
    Java Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            GraphicsPanel myTestGraphic = (GraphicsPanel)TestGraphic; 
            myTestGraphic.AddLine(new core.Line(10, 100, 200, 50));
            myTestGraphic.AddLine(140, 100, 340, 50);
        
        }
    Now I have
    initial state - ImageShack® - Online Photo and Video Hosting
    after jButton1 is clicked ImageShack® - Online Photo and Video Hosting

    About dropping the GUI Builder... I already got warned that it's better to avoid it, but for a java-noob (especially coming from MS Visual Studio) doing GUI with text editor is a big cultural shock, I guess I'll need some time to agree to do that.. :D

    Once again thanks for your response!
    If I made big mistake again please do not hesitate to correct me:rolleyes:

    Regards,
    Pavel
    Last edited by p.pavlov; 04-28-2011 at 08:20 PM. Reason: corrections

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default

    Much better.

    The only thing I'd like to point out is standard naming conventions- variables and methods should always start with a lower-case letter, classes start with a capital letter. It sounds picky, but it makes your code much easier to read.

    Also, there already is a Line class in the standard API. Actually there are 3! Line2D (which is abstract), Line2D.Double, and Line2D.Float. You might use Line2D.Double instead of a custom Line class.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Adding jscrollpane when extending jpanel
    By phil128 in forum AWT / Swing
    Replies: 4
    Last Post: 01-19-2011, 12:30 AM
  2. Extending Classes and What is Necessary
    By GavinCash in forum New To Java
    Replies: 10
    Last Post: 10-11-2010, 08:07 AM
  3. JPanel/JScrollPane issue
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 09-23-2010, 05:02 AM
  4. Possible to do JPanel drawing without extending?
    By SM2010 in forum AWT / Swing
    Replies: 7
    Last Post: 07-03-2010, 12:30 AM
  5. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 10:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •