Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Painting with a custom JComponent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Painting with a custom JComponent" Watch "Painting with a custom JComponent" New topic
Author

Painting with a custom JComponent

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I'm trying to do some custom drawing via a JComponent. I can get the frame to show, but unfortunately I can't seem to get the graphics to appear. Here is a minimal version of my code.



What do I need to do to get the rectangle to be drawn? Also, setting the minumum and preferred size of the JComponent seem to be to only way to get the JFrame to render in a non-minimized form. Are there better ways?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You fill the shape but you never draw the shape. You need to add g2d.draw(r)

As far as the size thing, you are calling pack(). If you want to control the size remove the call to pack() and call frame.setSize(...).


GenRocket - Experts at Building Test Data
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Thanks for the tip about not calling pack() and calling setSize() on the JFrame, that's exactly what I was looking for. However even after calling draw(Shape) I'm still not getting an image rendered.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You aren't telling the system where to draw the rectangle.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Thanks for the help Gregg. The problem was actually something different, and a little wierder to me. It wasn't that the image wasn't getting rendered, it had to do with the WIDTH and HEIGHT constants. I added a some logging statements and got this:



Output:


But this produces the output I expected:


Output:


Can anyone figure out why this is?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Not sure of the EXACT issue but looking at the API docs, WIDTH and HEIGHT are fields inherited from interface java.awt.image.ImageObserver.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I just saw the same thing. I guess that's one more reason enum types are superior to int constants in new code.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Gregg Bolinger wrote:Not sure of the EXACT issue but looking at the API docs, WIDTH and HEIGHT are fields inherited from interface java.awt.image.ImageObserver.

We have a winner!

Garrett, if you're always drawing the same rectangle, why not create the rectangle outside of the component?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Painting with a custom JComponent
 
Similar Threads
Autoscrolling for marquee selection
GridBagLayout with glue, w/o gaps
constructing a component (beginner)
Calling an applet from another class of the same package
JFrame