Search code examples
javapaintcomponentgraphics2drepaint

repaint() method not working for tint image


Full project here https://github.com/jafetrd/easyImageEditor

I'm trying to apply a color to an image using the paintComponent(), this is the class I use

public class Metodos extends JPanel{
...............................
........more code..........
................

public void setColor(Color color) {
    this.color = color;
    System.out.println("entrecolor");
    repaint();
}
@Override
protected void paintComponent(Graphics g) {
        if(imagen != null){
        super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g.create();
              g2d.setXORMode(color); //this is the filter i want to repaint
                if(detectar==false){
                     g2d.drawImage(imagen, getWidth()/2 - nuevoTamaño.width/2, getHeight()/2 - nuevoTamaño.height/2, nuevoTamaño.width, nuevoTamaño.height, this);
                }else{
                     g2d.drawImage(imagen, posX-nuevoTamaño.width/2, posY-nuevoTamaño.height/2, nuevoTamaño.width, nuevoTamaño.height,this);
                }
           g2d.dispose();
        }
}

I call to setColor() from another class to send the color object and repaint the image with the XOR inside the paint component, but it's not working. The class from where I send the color looks like this:

 public final class Colores extends JFrame{
  JColorChooser jc;
  private Metodos m;
public Colores(){
        componentes();
        inicio();    
   m = new Metodos();
}

public final void componentes(){
    setLayout(new BorderLayout());
   // Metodos a = new Metodos();
    jc = new JColorChooser();
    jc.setPreviewPanel(new JPanel());
    jc.getSelectionModel().addChangeListener((ChangeEvent arg0) -> {
        m.setColor(jc.getColor());
        super.repaint();
    });

    add(jc);
    pack();
}
.........................
.........more code.........
...................................

Here I take the color from the JColorChooser and send to the method setColor() and repaint the image, but it does not work at all.


Solution

  • Your problem is a basic example of a bad and misguided design.

    In your Colores you create a new instance of Metodos...

    public final class Colores extends JFrame{
    
        JColorChooser jc;
        private Metodos m;
        public Colores(){
           componentes();
           inicio();    
           m = new Metodos();
        }
    

    In what way does this have anything to do with the instance which you created earlier and put on the screen?

    You need to pass a reference of Metodos to Colores

    Have a look at Passing Information to a Method or a Constructor for more details

    public final class Colores extends JFrame {
    
        JColorChooser jc;
        private Metodos m;
    
        public Colores(Metodos m) {
            componentes();
            inicio();
            this.m = m;
        }
    

    And update Metodos

    case Colores:
        new Colores(this).setVisible(true);
        break;
    

    Things don't magically bind together, you actually need to provide the valid information to you program before it can work.

    I would, however, encourage you to use a different mechanism.

    JColorChooser actually has it's dialog support built in...

    case Colores:
        Color newColor = JColorChooser.showDialog(this, "Colors", color);
        if (newColor != null){
            color = newColor;
            repaint();
        }
        break;
    

    This allows the user to cancel the dialog if they don't want to select a color