Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By JosAH

Thread: Can you help me understand this basic GUI concept?

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Rep Power

    Default Can you help me understand this basic GUI concept?

    Ok so this is the basic skelington for a GUI

    Java Code:
    package chapter_iv;
    import java.awt.Graphics; 
    import javax.swing.JPanel;
    public class GUIGraphics extends JPanel
       public void paintComponent(Graphics g)
          super.paintComponent( g );
          int width = getWidth();   
          int height = getHeight();
          g.drawLine(0, 0, width, height);

    Java Code:
    package chapter_iv;
    import javax.swing.JFrame;
    public class GUIGraphicsTest
    public static void main( String[] args )
       GUIGraphics panel = new GUIGraphics();
       JFrame application = new JFrame();
       application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
       application.add( panel );     
       application.setSize(250, 250 );
       application.setVisible( true ); 
    I was hoping you guys could make this clear to me.

    From what I understand we have a JFrame which is a window right? so this is the main structure... you then want to give the JFrame a Jpanel object.
    Does that mean Jpanel is like a whiteboard you stick onto the window so that you can draw on it?

    I know that I have created a GUI which has inherited from Jpanal, so I override its paintComponent method which renders the window.

    what confuses me is how am I actually using it? all I do in my main method is instantiate my new Jpanel object and add it to the window, I dont even give GUI any arguments... how does it know to run paintcomponent()? I dont tell application to do it either

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Can you help me understand this basic GUI concept?

    The Swing framework runs its own Thread, the EDT (Event Dispatch Thread); it calls the paint( ... ) method for each component that needs to be repainted. Swing components split up the responsibility for painting their borders, children and themselves; the latter is done by the paintComponent( ... ) method; you have overridden it in your class so whenever your panel needs to be painted its paintComponent( ... ) method is called. You did it correctly.

    kind regards,

    EscSequenceAlpha likes this.
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Java concept help
    By DMarsh12 in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 08:06 PM
  2. Array Concept
    By sandeep43 in forum New To Java
    Replies: 11
    Last Post: 08-04-2011, 12:19 PM
  3. Map concept in JAVA
    By Mathan in forum New To Java
    Replies: 19
    Last Post: 06-27-2008, 06:02 AM
  4. mail concept
    By indirani in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:30 PM
  5. mail concept
    By thamizhisai in forum Advanced Java
    Replies: 4
    Last Post: 04-11-2008, 07:19 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