有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我需要制作一个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) 个答案

  1. # 1 楼答案

    好的,看来上面共享的代码可以解决一个小问题,即在mousePressed中比较时,使用component的x和y,而不是Rectangle上绘制的Component。所以你可以创建两个函数来返回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);
        }
    }