0

I want to change the color of my BufferdImage. When I do it like in this post: BufferedImage draw white when I say red it works only in my method which constructs my JFrame with all the components.

But when I want to use it in an ActionEvent in my Controller class, it does nothing, but the rest of the ActionEvent works, like for example change text in text field.

Methods in the JPanel class:

public void testImage(){
    System.out.println("Methode suc. called");
    Graphics g = image.getGraphics();
    tempC = Color.GREEN;
    g.setColor(tempC);
    g.fillRect(150, 300, 100, 100);
    tempC = Color.CYAN;
    g.setColor(tempC);
    g.fillOval(0, 0, 100, 100);
    g.dispose();
    if(image == null){System.out.println("Image is null");} 
}

public void clearImage(){
    Graphics g = image.getGraphics();
    tempC = Color.WHITE;
    g.setColor(tempC);
    g.fillRect(0, 0, darstellungsBreite, darstellungsHoehe);
    g.dispose();
}

The ActionListener:

public void actionPerformed(ActionEvent arg0) {
    if(arg0.getActionCommand().equals(view.ACTION_CLEAR))
    {
        //Clear Graphics
        view.drawArea.clearImage();
        //view.useClear();
    }
Community
  • 1
  • 1
Ticius
  • 1
  • 2
  • 1
    Depending on the rest of your code, you may need to call `repaint()` on the component that displays the image. The image is its own entity, not the pixels on the screen. – kiheru Jul 09 '15 at 13:45
  • jep, solved the problem! I only had to replace g.dispose with repaint() :) – Ticius Jul 09 '15 at 14:02
  • It's still good practice to dispose the graphics objects you create (like those obtained with `Image.getGraphics()`). So don't replace it, just add the repaint call. – kiheru Jul 09 '15 at 14:04

0 Answers0