简体   繁体   English

repaint()方法不适用于色彩图像

[英]repaint() method not working for tint image

Full project here https://github.com/jafetrd/easyImageEditor 完整项目在这里https://github.com/jafetrd/easyImageEditor

I'm trying to apply a color to an image using the paintComponent(), this is the class I use 我正在尝试使用paintComponent()将颜色应用于图像,这是我使用的类

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. 我从另一个类调用setColor()来发送颜色对象并使用paint组件内部的XOR重新绘制图像,但是它不起作用。 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. 在这里,我从JColorChooser中获取颜色,并将其发送到setColor()方法并重新绘制图像,但是它根本不起作用。

Your problem is a basic example of a bad and misguided design. 您的问题是不良和错误设计的基本示例。

In your Colores you create a new instance of Metodos ... 在您的Colores ,创建一个新的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 您需要将Metodos的参考MetodosColores

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 并更新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... JColorChooser实际上具有内置的对话框支持...

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 如果用户不想选择颜色,则可以取消对话框

Here is an example of a program that works. 这是一个有效程序的示例。 You need a png file to see the results. 您需要一个png文件才能查看结果。

import javax.swing.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class TintPicture{

    static Color[] colors = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW};

    public static void main(String[] args) throws Exception{
        JFrame frame = new JFrame("tinted");
        BufferedImage img = ImageIO.read(new File("sample.png"));
        int[] index = {0};
        JPanel pane = new JPanel(){
            @Override
            public void paintComponent(Graphics g){
                Graphics2D g2d = (Graphics2D)g;
                g2d.setXORMode(colors[index[0]]);
                g2d.drawImage(img, 0, 0, this);
            }
        };

        pane.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent evt){
                index[0] = (index[0] + 1)%colors.length;
                pane.repaint();
            }
        });

        frame.setContentPane(pane);

        frame.setSize(700, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

It includes using the xor, and changing/updating the display. 它包括使用xor,以及更改/更新显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM