Java Can';不要变色

Java Can';不要变色,java,Java,您好,我正在用Java创建一个应用程序(练习),我必须在绘图类中更改变量的颜色。当应用程序启动,我在颜色变量上运行sysout时,它会显示null,但当我按下鼠标右键时,例如,它会在控制器类中更改颜色,但不会在绘图类中更改颜色。。有人能看一下并告诉我我做错了什么吗 这是一段代码 这是绘图类的相关部分 private Color color; private ArrayList<Point> p = new ArrayList<Point>(); public D

您好,我正在用Java创建一个应用程序(练习),我必须在绘图类中更改变量的颜色。当应用程序启动,我在颜色变量上运行sysout时,它会显示null,但当我按下鼠标右键时,例如,它会在控制器类中更改颜色,但不会在绘图类中更改颜色。。有人能看一下并告诉我我做错了什么吗

这是一段代码

这是绘图类的相关部分

    private Color color;
private ArrayList<Point> p = new ArrayList<Point>();

public Drawing(Color color) {
    this.color = color;
    System.out.println("color  " + color);
}

public void draw(Graphics g) {
    for(int i = 0; i < p.size(); i++) {
        g.setColor(color);
        g.fillRect(p.get(i).x, p.get(i).y, 10, 10);
    }
}
私人色彩;
private ArrayList p=new ArrayList();
公共绘图(彩色){
这个颜色=颜色;
System.out.println(“颜色”+颜色);
}
公共空间绘制(图g){
对于(int i=0;i
这是我的控制器的相关代码

Color color; // kleur vasthouden
Drawing draw; // class definieren
private ArrayList<Drawing> tekening = new ArrayList<Drawing>();
int x, y;

public DrawingPanel() {
    setBackground(Color.WHITE); // zorg voor een witte achtergrond.
    this.addMouseListener(this); // control de mouselistener
    draw = new Drawing(color);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    draw.draw(g);
}

@Override
public void mouseClicked(MouseEvent e) {

    if(e.getButton() == MouseEvent.BUTTON1) {
        Point k = new Point(e.getX(), e.getY());

        draw.addPoint(k);
        System.out.println("punt gezet op " + k);
    }
    if(e.getButton() == MouseEvent.BUTTON3) {
        color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
        System.out.println("new color " + color);
    }
    repaint();
}
color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
draw.color = color;
public setColor(Color color) {
    this.color = color;
}
Color颜色;//克勒瓦斯托登
绘图绘图;//类定义者
private ArrayList tekening=新ArrayList();
int x,y;
公共绘图面板(){
挫折背景(颜色。白色);//佐尔格·沃尔·伊恩·维特·阿奇特格隆。
this.addMouseListener(this);//控件de mouselistener
图纸=新图纸(颜色);
}
公共组件(图形g){
超级组件(g);
抽签,抽签(g);
}
@凌驾
公共无效mouseClicked(MouseEvent e){
如果(例如getButton()==MouseEvent.BUTTON1){
点k=新点(e.getX(),e.getY());
绘制添加点(k);
System.out.println(“punt gezet op”+k);
}
如果(例如getButton()==MouseEvent.BUTTON3){
颜色=新颜色(r.nextInt(255-0+1)+0,r.nextInt(255-0+1)+0,r.nextInt(255-0+1)+0);
System.out.println(“新颜色”+颜色);
}
重新油漆();
}

我希望有人能找出我做错了什么。

在代码中我能看到的任何地方,你都不会给
color
赋值。您仅在鼠标事件发生时设置它

if(e.getButton() == MouseEvent.BUTTON3) {
    color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
    System.out.println("new color " + color);
}

我假设,除了打印出此颜色之外,您还希望将其设置为绘图类,然后触发重新绘制。

您实际上从未将初始值指定给我可以看到的代码中的任何地方。您仅在鼠标事件发生时设置它

if(e.getButton() == MouseEvent.BUTTON3) {
    color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
    System.out.println("new color " + color);
}
我假设除了打印此颜色外,您还希望将其设置为绘图类,然后触发重新绘制。

向类
绘图添加setter方法,并在鼠标右键单击计算实际颜色后传递它:

public void setColor(Color color) {
   this.color = color;
}
在控制器中:

if(e.getButton() == MouseEvent.BUTTON3) {
    color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
    System.out.println("new color " + color);
    draw.setColor(color);
}
将setter方法添加到类
绘图中
,并在鼠标右键单击计算出实际颜色后传递该颜色:

public void setColor(Color color) {
   this.color = color;
}
在控制器中:

if(e.getButton() == MouseEvent.BUTTON3) {
    color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
    System.out.println("new color " + color);
    draw.setColor(color);
}

因为它们是独立的类,所以它们中的
color
是一个独立的对象。
如果在
绘图
类中将
颜色
更改为公共,则可以将
颜色
设置为控制器内创建的新颜色

Color color; // kleur vasthouden
Drawing draw; // class definieren
private ArrayList<Drawing> tekening = new ArrayList<Drawing>();
int x, y;

public DrawingPanel() {
    setBackground(Color.WHITE); // zorg voor een witte achtergrond.
    this.addMouseListener(this); // control de mouselistener
    draw = new Drawing(color);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    draw.draw(g);
}

@Override
public void mouseClicked(MouseEvent e) {

    if(e.getButton() == MouseEvent.BUTTON1) {
        Point k = new Point(e.getX(), e.getY());

        draw.addPoint(k);
        System.out.println("punt gezet op " + k);
    }
    if(e.getButton() == MouseEvent.BUTTON3) {
        color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
        System.out.println("new color " + color);
    }
    repaint();
}
color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
draw.color = color;
public setColor(Color color) {
    this.color = color;
}
您还可以在绘图类中创建一个setter,并使用它从控制器设置颜色

Color color; // kleur vasthouden
Drawing draw; // class definieren
private ArrayList<Drawing> tekening = new ArrayList<Drawing>();
int x, y;

public DrawingPanel() {
    setBackground(Color.WHITE); // zorg voor een witte achtergrond.
    this.addMouseListener(this); // control de mouselistener
    draw = new Drawing(color);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    draw.draw(g);
}

@Override
public void mouseClicked(MouseEvent e) {

    if(e.getButton() == MouseEvent.BUTTON1) {
        Point k = new Point(e.getX(), e.getY());

        draw.addPoint(k);
        System.out.println("punt gezet op " + k);
    }
    if(e.getButton() == MouseEvent.BUTTON3) {
        color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
        System.out.println("new color " + color);
    }
    repaint();
}
color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
draw.color = color;
public setColor(Color color) {
    this.color = color;
}

此外,将颜色设置为构造函数中的任何值都会阻止它作为null打印。

因为它们是单独的类,所以每个类中的
color
都是单独的对象。
如果在
绘图
类中将
颜色
更改为公共,则可以将
颜色
设置为控制器内创建的新颜色

Color color; // kleur vasthouden
Drawing draw; // class definieren
private ArrayList<Drawing> tekening = new ArrayList<Drawing>();
int x, y;

public DrawingPanel() {
    setBackground(Color.WHITE); // zorg voor een witte achtergrond.
    this.addMouseListener(this); // control de mouselistener
    draw = new Drawing(color);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    draw.draw(g);
}

@Override
public void mouseClicked(MouseEvent e) {

    if(e.getButton() == MouseEvent.BUTTON1) {
        Point k = new Point(e.getX(), e.getY());

        draw.addPoint(k);
        System.out.println("punt gezet op " + k);
    }
    if(e.getButton() == MouseEvent.BUTTON3) {
        color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
        System.out.println("new color " + color);
    }
    repaint();
}
color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
draw.color = color;
public setColor(Color color) {
    this.color = color;
}
您还可以在绘图类中创建一个setter,并使用它从控制器设置颜色

Color color; // kleur vasthouden
Drawing draw; // class definieren
private ArrayList<Drawing> tekening = new ArrayList<Drawing>();
int x, y;

public DrawingPanel() {
    setBackground(Color.WHITE); // zorg voor een witte achtergrond.
    this.addMouseListener(this); // control de mouselistener
    draw = new Drawing(color);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    draw.draw(g);
}

@Override
public void mouseClicked(MouseEvent e) {

    if(e.getButton() == MouseEvent.BUTTON1) {
        Point k = new Point(e.getX(), e.getY());

        draw.addPoint(k);
        System.out.println("punt gezet op " + k);
    }
    if(e.getButton() == MouseEvent.BUTTON3) {
        color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
        System.out.println("new color " + color);
    }
    repaint();
}
color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
draw.color = color;
public setColor(Color color) {
    this.color = color;
}

此外,将颜色设置为构造函数中的任何值都会阻止它作为null打印。

在控制器中,您有一个颜色属性,右键单击可以设置它,但从未在绘图类中设置它。 尝试:


在控制器中,您有一个颜色属性,单击鼠标右键可以设置该属性,但从未在图形类中设置该属性。 尝试:


我必须设置初始值吗?(所以不为null)假设您不希望初始值为null,这将是一个好主意是的!我必须设置初始值吗?(所以不为null)假设您不希望初始值为null,这将是一个好主意是的!通过setter还是更改变量更好?setter可能是更好的选择。只允许类对自己的变量进行操作通常是好的。通过setter或更改变量的方式更好吗?setter可能是更好的选择。只允许类对自己的变量进行操作通常是好的。