aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes I can't draw anything with Container and Graphics class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "I can Watch "I can New topic
Author

I can't draw anything with Container and Graphics class

Matthew Park
Greenhorn

Joined: Aug 07, 2013
Posts: 13
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


CS major in Soongsil University, Seoul, South Korea
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

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/


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 175
    
    1

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.


Ramesh-X
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

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

Joined: Aug 07, 2013
Posts: 13
Thank you all for your help
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 175
    
    1

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

Joined: Aug 07, 2013
Posts: 13
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I can't draw anything with Container and Graphics class