双击时,圆需要消失

When double-clicked, circle needs to disappear

我的代码中的问题是,我试图制作一个程序,当您单击网格中的一个单元格时,该单元格内应该出现一个圆圈。我对此很满意。但是,当您第二次单击时,圆圈应该会消失。我不知道该怎么做。

我尝试在实现的方法中重绘圆圈为与背景相同的颜色,按下鼠标,但这不是很有效。当你按下它时它只会 "disappears" ,但我希望它在被点击时消失。

我写成mouse-pressed,因为我不知道在mouse-clicked方法中怎么用。

这是我的代码:

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

/**
 * In this program, when user clicks a square, a circle appears until the user clicks it again.
 */
public class DD_GridFiller extends JFrame {

    private int gridRows;
    private int gridColumns;

    private Color[][] circleColor;  //color of circles
    private Color lineColor;       //color of lines

    /**
     * constructor
     */
    public DD_GridFiller() {
        setTitle("My Grid Filler");
        setSize(600,600);
        setLayout(new GridLayout(4,4));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        circleColor = new Color[4][4]; //stores the colors in arrays
        gridRows = 4;
        gridColumns = 4;
        lineColor = Color.RED;
        setPreferredSize( new Dimension(90*4, 90*4) );
        setBackground(Color.BLACK); // set the background color for this panel.
        addMouseListener(new MouseListener());

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int top, left;  // position for top left corner of the window
        left = ( screenSize.width - getWidth() ) / 2;
        top = ( screenSize.height - getHeight() ) / 2;
        setLocation(left,top);
        setResizable(false);

        setVisible(true);
        pack();
    }

    public void paint(Graphics g) {
        g.setColor(getBackground());
        g.fillRect(0,0,getWidth(),getHeight());
        int row, col;
        double cellWidth = (double)getWidth() / gridColumns;  //get cell width
        double cellHeight = (double)getHeight() / gridRows;   //get cell height

        //create circles in every cell
        for (row = 0; row < gridRows; row++) {
            for (col = 0; col < gridColumns; col++) {
                if (circleColor[row][col] != null) {
                    int x1 = (int)(col*cellWidth);
                    int y1 = (int)(row*cellHeight);
                    g.setColor(circleColor[row][col]);
                    g.fillOval(x1-2, y1-2, 23*4, 23*4);
                }
            }
        }
        //CREATES THE LINES
        if (lineColor != null) {
            g.setColor(lineColor);
            for (row = 1; row < gridRows; row++) {
                int y = (int)(row*cellHeight);
                g.drawLine(0,y,getWidth(),y);
            }
            for (col = 1; col < gridRows; col++) {
                int x = (int)(col*cellWidth);
                g.drawLine(x,0,x,getHeight());
            }
        }
    }
    /**
     * Finds the row
     * @param pixelY location on x-axis
     * @return rows
     */
    private int findRow(int pixelY) {
        return (int)(((double)pixelY)/getHeight()*gridRows);
    }

    /**
     * Finds the column
     * @param pixelX location of y-axis
     * @return columns
     */
    private int findColumn(int pixelX) {
        return (int)(((double)pixelX)/getWidth()* gridColumns);
    }

    private class MouseListener implements java.awt.event.MouseListener {
         @Override
    public void mouseClicked(MouseEvent e) {
        int row, col; // the row and column in the grid of squares where the user clicked.
        row = findRow( e.getY() ); col = findColumn( e.getX() );  //find the location of cells clicked

        circleColor[row][col] = new Color(0,223,197);
        repaint(); // redraw the panel by calling the paintComponent method.
    }

    @Override
    public void mousePressed(MouseEvent e) {
        int row, col; // the row and column in the grid of squares where the user clicked.
        row = findRow( e.getY() ); col = findColumn( e.getX() ); //find the location of cells clicked
        circleColor[row][col] = new Color(0);
        repaint(); // redraw the panel by calling the paintComponent method.
    }
        @Override public void mouseReleased(MouseEvent e) { }
        @Override public void mouseEntered(MouseEvent e) { }
        @Override public void mouseExited(MouseEvent e) { }
    }
    public static void main (String[] args) {
        new DD_GridFiller();
    }
}

您的 paint 方法测试颜色的 row/col 位置;如果它是非空的,它会画一个圆,对吗?也许在 mousePressed 中,你可以测试那个位置的 circleColor 是否为非空,如果是,则将其设为空。

我不清楚重绘是否填充了单元格;绘制圆后可能需要这样做以覆盖圆。

在像这样的应用程序中,通常会计算需要重绘的最小矩形并只重绘它 -- 您可以通过计算该矩形并将其坐标传递给重绘方法,然后只绘制组件的一部分来实现(s) 与更改后的矩形相交。

您的问题是 mousePressed 一直将颜色设置为黑色。即使您检测到当您按下圆圈时颜色不是黑色,在 mousePressed 中您再次将其设置为黑色,然后再次绘制圆圈,如此循环。

其实解决方法很简单:

  1. 删除mousePressed中的所有内容。我们不需要它,mouseClicked 本质上已经只是 mousePressed + mouseReleased。
  2. 将此添加到您的 mouseClicked 方法中:

    @Override
    public void mouseClicked(MouseEvent e) {
        int row, col; // the row and column in the grid of squares where the user clicked.
        row = findRow( e.getY() ); col = findColumn( e.getX() );  //find the location of cells clicked
    
        System.out.println("Cell color: " + circleColor[row][col]); //will let you see whats happening
        if (circleColor[row][col] == null) {
            circleColor[row][col] = new Color(0,223,197);
        } else {
            circleColor[row][col] = null;
        }
    
        repaint(); // redraw the panel by calling the paintComponent method.
    }
    

我们在做什么 - 最初我们所有的颜色都是空的(之前,在您的代码中,mousePressed 将它们设置为 RGB [0,0,0],即黑色)。因此,当我们第一次单击单元格并看到单元格颜色为 "null",即它是空的时,我们将圆圈颜色设置为我们的新颜色并绘制圆圈。如果我们再次按下,我们检测到颜色不再是 "null",即单元格内部有一个圆圈 - 然后我们将单元格设置回空。

有些人可能不喜欢 "null" 颜色的概念 - 如果您想要 RGB [0, 0, 0] 而不是 null,只需将 null 的任何初始出现转换为 RGB [0 , 0, 0] 然后使用:

public void mouseClicked(MouseEvent e) {
    ...

    //initial setup
    if (circleColor[row][col] == null) {
        circleColor[row][col] = new Color(0);
    }

    System.out.println("Cell color: " + circleColor[row][col]); //will let you see whats happening
    if (circleColor[row][col].equals(Color.getHSBColor(0,0,0))) {
        circleColor[row][col] = new Color(0,223,197);
    } else {
        circleColor[row][col] = new Color(0) ;
    }

    repaint(); // redraw the panel by calling the paintComponent method.
}