repaint() 方法不适用于着色图像
repaint() method not working for tint image
Full project here
https://github.com/jafetrd/easyImageEditor
我正在尝试使用 paintComponent() 将颜色应用于图像,这是 class 我使用
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();
}
}
我从另一个 class 调用 setColor() 来发送颜色对象并使用绘制组件内的 XOR 重新绘制图像,但它不起作用。我发送颜色的 class 看起来像这样:
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.
这是一个有效的程序示例。您需要 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);
}
}
它包括使用异或,changing/updating显示。
您的问题是糟糕和错误设计的一个基本示例。
在您的 Colores
中,您创建了 Metodos
...
的新实例
public final class Colores extends JFrame{
JColorChooser jc;
private Metodos m;
public Colores(){
componentes();
inicio();
m = new Metodos();
}
这与您之前创建并显示在屏幕上的实例有什么关系?
您需要将 Metodos
的引用传递给 Colores
查看 Passing Information to a Method or a Constructor 了解更多详情
public final class Colores extends JFrame {
JColorChooser jc;
private Metodos m;
public Colores(Metodos m) {
componentes();
inicio();
this.m = m;
}
并更新Metodos
case Colores:
new Colores(this).setVisible(true);
break;
事物不会神奇地结合在一起,您实际上需要向您的程序提供有效信息才能运行。
不过,我鼓励您使用不同的机制。
JColorChooser
实际上内置了对话框支持...
case Colores:
Color newColor = JColorChooser.showDialog(this, "Colors", color);
if (newColor != null){
color = newColor;
repaint();
}
break;
这允许用户在不想select颜色
时取消对话框
Full project here https://github.com/jafetrd/easyImageEditor
我正在尝试使用 paintComponent() 将颜色应用于图像,这是 class 我使用
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();
}
}
我从另一个 class 调用 setColor() 来发送颜色对象并使用绘制组件内的 XOR 重新绘制图像,但它不起作用。我发送颜色的 class 看起来像这样:
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.
这是一个有效的程序示例。您需要 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);
}
}
它包括使用异或,changing/updating显示。
您的问题是糟糕和错误设计的一个基本示例。
在您的 Colores
中,您创建了 Metodos
...
public final class Colores extends JFrame{
JColorChooser jc;
private Metodos m;
public Colores(){
componentes();
inicio();
m = new Metodos();
}
这与您之前创建并显示在屏幕上的实例有什么关系?
您需要将 Metodos
的引用传递给 Colores
查看 Passing Information to a Method or a Constructor 了解更多详情
public final class Colores extends JFrame {
JColorChooser jc;
private Metodos m;
public Colores(Metodos m) {
componentes();
inicio();
this.m = m;
}
并更新Metodos
case Colores:
new Colores(this).setVisible(true);
break;
事物不会神奇地结合在一起,您实际上需要向您的程序提供有效信息才能运行。
不过,我鼓励您使用不同的机制。
JColorChooser
实际上内置了对话框支持...
case Colores:
Color newColor = JColorChooser.showDialog(this, "Colors", color);
if (newColor != null){
color = newColor;
repaint();
}
break;
这允许用户在不想select颜色
时取消对话框