• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Painting with a custom JComponent

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic