Java Swing - 点击形状改变颜色

Java Swing - Clicking on a shape to change its color

我需要制作一个 Java Swing 应用程序,它在屏幕上显示一个矩形,如果它被单击,它应该将其颜色更改为黑色(如果它是白色)或白色(如果它是黑色)。问题是它是一个 class 需要扩展 JComponent 并覆盖 paintComponent。除了点击部分,我都完成了。出于某种原因,我不能让它只在单击时改变颜色。当点击它以外的背景时,它也会改变颜色。

代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class RectangleC extends JComponent implements MouseListener{
    private int width, height;
    private Color color;

    public RectangleC(int w, int h, Color c){
        width = w;
        height = h;
        color = c;
        this.addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(color);
        g.fillRect(0, 0, width, height);
    }

    @Override
    public void mousePressed(MouseEvent e){
        if(this.contains(e.getPoint())){
            if(color == Color.WHITE) {
                color = Color.BLACK;
            }
            else {
                color = Color.WHITE;
            }
        }
        repaint();
    }

    public void mouseClicked(MouseEvent e){};
    public void mouseReleased(MouseEvent e){};
    public void mouseEntered(MouseEvent e){};
    public void mouseExited(MouseEvent e){};

    public static void main(String args[]){
        JFrame frame = new JFrame("Rectangle Component");
        RectangleC rectangle2 = new RectangleC(300, 500, Color.BLACK);
        frame.add(rectangle2);
        frame.setSize(600,600);;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

我似乎无法使用 e.getPoint() 方法让它工作。我还尝试使用坐标和 e.getX() 和 e.getY() 并且只要形状处于默认位置就可以使用。但是,如果形状被移动到中心,它就不再起作用了。

这是我试过的方法:

    @Override
public void mousePressed(MouseEvent e)
{
    int currentX = e.getX();
    int currentY = e.getY();
    if(currentX > this.getX() && currentX < this.getX() + width && currentY > this.getY() && currentY < this.getY() + height ){        
        if(color != Color.WHITE)
            color = Color.WHITE;
        else
            color = Color.black;
    }
    repaint();
}

我怎样才能让它只在被点击时改变颜色?我真的没有主意,我找不到任何方法来做到这一点。

好的,所以上面共享的代码似乎没有问题,你正在使用 component 的 x 和 y 而不是 Component 上绘制的 Rectangle 而在 mousePressed 中比较。所以你可以做的是创建两个函数 return x 和 y of rectangle 然后在你需要检查矩形坐标的地方使用这些函数。

请在下方找到更新后的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class RectangleC extends JComponent implements MouseListener{
    private int width, height;
    int x,y;
    private Color color;

    public RectangleC(int w, int h, Color c){
        width = w;
        height = h;

        //Given x and y some default position. This can be changed as required
        x = 20;
        y = 20;

        color = c;
        this.addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(color);
        g.fillRect(x, y, width, height);
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        int currentX = e.getX();
        int currentY = e.getY();
        if(currentX > this.getRectX() && currentX < this.getRectX() + width && currentY > this.getRectY() && currentY < this.getRectY() + height ){        
            if(color != Color.WHITE)
                color = Color.WHITE;
            else
                color = Color.black;
        }
        repaint();
    }

    // Function to return rectangle coordinate
    private int getRectX() {
        return this.getX()+x;
    }


    private int getRectY() {
        return this.getY()+y;
    }
public void mouseClicked(MouseEvent e){};
    public void mouseReleased(MouseEvent e){};
    public void mouseEntered(MouseEvent e){};
    public void mouseExited(MouseEvent e){};

    public static void main(String args[]){
        JFrame frame = new JFrame("Rectangle Component");
        RectangleC rectangle2 = new RectangleC(300, 500, Color.BLACK);
        frame.add(rectangle2);
        frame.setSize(600,600);;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}