简体   繁体   中英

Can't change color

Hello I am creating a application ( exercise ) in Java where I have to change the color of a variable in my drawing class. when the application starts and I run a sysout on the color variable it says null but when I press my right mouse button for example it changes color in the controller class but not in my drawing class.. Can someone take a look and tell me what i am doing wrong?

here is a piece of code

this is the relevant part of the drawing class

    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);
    }
}

and this is the relevant code of my controller.

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();
}

I hope someone can figure out what i'm doing wrong..

You never actually assign an initial value to color anywhere in the code that I can see. You only set it when a mouse event occurs.

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);
}

I assume that in addition to printing out this color you also want to set it to your Drawing class, then trigger a repaint.

Add a setter method to your class Drawing and pass the actual color after it has been calculated on a right-mouse-button click:

public void setColor(Color color) {
   this.color = color;
}

And in the controller:

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);
}

Since they are separate classes, color in each of them is a separate object.
If in your Drawing class you change color to public, you can then set color to the new color that is created within the controller.

color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
draw.color = color;

You could also create a setter in the Drawing class and use that to set the color from the controller.

public setColor(Color color) {
    this.color = color;
}

In addition, setting the color to any value in your constructor will stop it from printing as null.

In your controller, you have a color property, and on right click, you set it, but you never set it on your Drawing class. try :

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);
draw.setColor(color);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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