Results 1 to 2 of 2
- 04-08-2012, 06:54 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Can you help me understand this basic GUI concept?
Ok so this is the basic skelington for a GUI
andJava 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); } }
I was hoping you guys could make this clear to me.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 ); } }
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
- 04-08-2012, 08:33 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Java concept help
By DMarsh12 in forum New To JavaReplies: 1Last Post: 02-15-2012, 07:06 PM -
Array Concept
By sandeep43 in forum New To JavaReplies: 11Last Post: 08-04-2011, 12:19 PM -
Map concept in JAVA
By Mathan in forum New To JavaReplies: 19Last Post: 06-27-2008, 06:02 AM -
mail concept
By indirani in forum New To JavaReplies: 3Last Post: 04-16-2008, 01:30 PM -
mail concept
By thamizhisai in forum Advanced JavaReplies: 4Last Post: 04-11-2008, 07:19 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks