Java Swing - 单击形状以更改其颜色

时间:2018-04-17 23:28:57

标签: java swing shape

我需要创建一个Java Swing应用程序,在屏幕上显示一个矩形,如果它被点击,它应该将其颜色改为黑色,如果它是白色或白色,如果它是黑色的。问题是它是一个需要扩展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();
}

如何制作它以便在点击时只改变颜色?我真的没有想法,我找不到任何办法。

1 个答案:

答案 0 :(得分:0)

好的,所以看起来上面分享的代码很好,只有一个小问题,你在component而不是在Rectangle上绘制的Component的x和y mousePressed。所以你可以做的是创建两个函数来返回rectangle的x和y,然后在需要检查矩形坐标时使用这些函数。

请在下面找到更新的代码:

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