3

I want to display boxes that are actually image placeholders in Swing. They are fixed size and the image, once loaded into the placeholder, will scale to fit the placeholder. I'd also like to be able to paint over them. Nothing grand. Just simple dots and lines, done with clicks outside the image (in some JButton, say), no brushes.

I originally thought of using a JLabel then setIcon from it. But that won't scale would it? Besides, I don't think it'd allow me to paint over them.

I've thought of extending JComponent, using ImageIO.read to get an image, then displaying it with paintComponent. But I'm not sure if, again, I'd be able to paint over the image. Besides, I find this GUI component to be pretty common that I may be reinventing the wheel.

Any suggestions?

skytreader
  • 11,467
  • 7
  • 43
  • 61

1 Answers1

3

I would advice:

  • A BufferedImage that contains your image and edited pixels
  • A component that extends JLabel and override the paintComponent method. You may pass the original image at the beginning to your JLabel or you can leave it empty (you may then need to set the preferred size).
  • in paintComponent just paint your buffered image.

Anthony

Anthony
  • 1,245
  • 1
  • 16
  • 15
  • 1
    there isn't needed to override method paintComponent() for put Image/ImageIcon into JLabel, just JLabel#setIcon() +1 – mKorbel Aug 14 '11 at 09:47
  • 2
    +1 See also [`RotatableImage`](http://stackoverflow.com/questions/3405799/how-to-rotate-an-image-gradually-in-swing/3420651#3420651). – trashgod Aug 14 '11 at 12:30