1

I am painting on a Panel by overriding the paintComponent -method.

class Canvas extends Panel ...
...
override def paintComponent(g: Graphics2D) {
    super.paintComponent(g)
    for (cmd <- commands.get) cmd.execute(g, this)
}

Okay. Inside one of the commands is this one.

case class TestCommand() {
  def execute(g: java.awt.Graphics2D, c: Canvas) = {
    val img = new BufferedImage(c.width, c.height, BufferedImage.TYPE_INT_RGB) 
    g.drawImage(img, null, 0, 0)
}

What im trying to do there is create a BufferedImage from the Graphics2D -object, so i can read the value of some pixel at location x, y.

However when this command is executed, i notice that the drawImage -method causes my panel to turn completely black, wiping everything i've drawn on it so far. I want the command to simply create a BufferedImage of what i've drawn on the panel, instead of somehow affecting the Graphics2D -object itself. What am i doing wrong here?

  • 1
    *"I want the command to simply create a BufferedImage of what i've drawn on the panel"* I find it easier to create the custom painting in the graphics object of the *image*, then simply draw the image to the component (or perhaps just display the image in a label). – Andrew Thompson Apr 05 '15 at 05:45
  • 1
    *"create a BufferedImage of what i've drawn on the panel"* It sounds like you want to draw the panel to the image but right now you're drawing the (blank) image to the panel. I also agree with Andrew's comment. – Radiodef Apr 05 '15 at 05:53
  • Thank you for the comments. You're right, i am drawing the blank image to the panel. I do not understand why the drawImage -method is doing it though: "Renders a BufferedImage that is filtered with a BufferedImageOp. The rendering attributes applied include the Clip, Transform and Composite attributes." -Oracle docs. I do not understand Andrews suggestion, could you give me an example? – user3723987 Apr 05 '15 at 06:14
  • 1
    Tip: Add @Radiodef (or whoever, the `@` is important) to *notify* the person of a new comment. *"could you give me an example?"* See this [2D animation in a buffered image](http://stackoverflow.com/a/14575043/418556), this [animated stroked border](http://stackoverflow.com/a/9772978/418556) & this [animation of dots and lines](http://stackoverflow.com/a/10628553/418556).. To get a `Graphics2D` object from a `BufferedImage` it is as simple as calling `createGraphics()`. – Andrew Thompson Apr 05 '15 at 07:15

1 Answers1

3

A Graphics2D is connected to some canvas or image. You might think of it like a brush or a pen. In this case, the graphics object you have is tied to your panel. Calling drawImage draws the image on to the panel.

If you want to draw the panel on to the image, you need a Graphics for the BufferedImage:

Graphics2D ig = img.createGraphics();

Then you need to paint the panel to the graphics. java.awt.Component provides a number of convenience methods for this type of thing, for example printAll:

c.printAll(ig);
ig.dispose(); // don't forget to dispose Graphics you've created

(Sorry, I don't really know Scala syntax, if it's different.)

Without being too presumptuous, there may be better ways to approach what you're doing. E.g. if you're doing painting to the panel, as mentioned in the comments, it's usually a good idea to do your painting on an image to begin with, then paint the image to the panel. Then you always have the image.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • Thanks @Radiodef. This all finally makes sense. I got the "think of it like a brush or a pen" -part but i wasn't aware it was connected to some canvas or image. – user3723987 Apr 06 '15 at 17:43