Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I can't draw anything with Container and Graphics class

 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i m new to Java and i tried to draw rectangles, round rectangles, ovals but failed.
The only method from Graphics class did work was drawString() method.
i typed the code exactly from my textbook and that's why i m more confused.
Here's one of many codes I failed to draw figures.



If i execute this program, then i can only see light-gray colored frame, not two rectangles.

Could somebody help me understand why i can't draw any figures?
Sorry for my poor English
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
The correct way to achieve custom painting in Swing is by overriding the paintComponent method.
Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/painting/
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getGraphics() will return null if the JFrame is still not visible. If you want to draw in the JFrame, it better to override paint(Graphics g) method.
Or you can get the graphics object after the JFrame is visible. You can do it by sleeping the main thread for a while after you call the setVisible(true) method.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Pramuditha Rathnayake wrote:getGraphics() will return null if the JFrame is still not visible. If you want to draw in the JFrame, it better to override paint(Graphics g) method.
Or you can get the graphics object after the JFrame is visible. You can do it by sleeping the main thread for a while after you call the setVisible(true) method.

No. That is not the correct way to do it. Please refer the link I posted in my reply.
 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your help
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
No. That is not the correct way to do it. Please refer the link I posted in my reply.


I told here the way to get the graphic object of the frame. But it is not the correct way...

And it is better to draw the drawing not in the JFrame, but you can do it inside a JPanel and add it to JFrame.. When do drawing, it's better to override paintComponent(Graphics g) method rather than paint(Graphics g) method..
 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Pramuditha Rathnayake wrote:
Maneesh Godbole wrote:
No. That is not the correct way to do it. Please refer the link I posted in my reply.


I told here the way to get the graphic object of the frame. But it is not the correct way...

And it is better to draw the drawing not in the JFrame, but you can do it inside a JPanel and add it to JFrame.. When do drawing, it's better to override paintComponent(Graphics g) method rather than paint(Graphics g) method..


I see. Thank you very much. You've been very helpful!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic