简体   繁体   English

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

[英]Java Swing - Clicking on a shape to change its color

I need to make a Java Swing application that displays a rectangle on screen and if it is being clicked, it should change its color to black if it's white or white if it's black. 我需要制作一个Java Swing应用程序,该应用程序在屏幕上显示一个矩形,如果单击它,则应该将其颜色更改为黑色(如果是白色),或者将白色更改为黑色(如果是黑色)。 The problem is that it is a class that needs to extend JComponent and override paintComponent. 问题在于,这是一个需要扩展JComponent并覆盖paintComponent的类。 I got everything done except the clicking part. 除了点击部分,我已完成所有工作。 For some reason I cannot make it so that it only changes colors when it is clicked. 由于某种原因,我无法制作它,因此仅在单击它时才更改颜色。 It also changes colors when the background apart from it is being clicked. 当单击除其以外的背景时,它也会更改颜色。

Here is the code: 这是代码:

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

I cannot seem to get it working using the e.getPoint() method. 我似乎无法使用e.getPoint()方法使其正常工作。 I also tried using coordinates and e.getX() and e.getY() and that works as long as the shape is in its default position. 我还尝试使用坐标以及e.getX()和e.getY(),只要形状处于其默认位置即可。 However, if the shape is being moved to the center, it does no longer work. 但是,如果将形状移到中心,它将不再起作用。

Here is the method that I tried: 这是我尝试的方法:

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

How can I make it so that it only changes colors when it is clicked? 我如何才能使其仅在单击时才更改颜色? I am really out of ideas and I could not find any way to do this. 我真的没有想法,我找不到任何方法可以做到这一点。

Ok, so it seems the code shared above is fine with a small issue that you are taking x and y of the component instead of the Rectangle painted on Component while comparing in mousePressed . 好的,因此似乎上面共享的代码很好,但有一个小问题:在mousePressed进行比较时,您要获取component x和y而不是在Component绘制的Rectangle So what you can do is create two functions to return x and y of rectangle and then use those functions where ever you need to check the coordinates of rectangle. 因此,您可以做的是创建两个函数以返回rectangle x和y,然后在需要检查矩形坐标的地方使用这些函数。

Please find the updated code below: 请在下面找到更新的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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