Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Painting with a custom JComponent

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(...).
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't telling the system where to draw the rectangle.

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just saw the same thing. I guess that's one more reason enum types are superior to int constants in new code.
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic