双击时,圆圈需要消失

问题描述 投票:1回答:2

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

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

我把它写成鼠标按下,因为我不知道如何在鼠标点击方法中使用它。

这是我的代码:

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();
    }
}
java swing jframe mouseevent mouselistener
2个回答
2
投票

你的问题是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. }

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

有些人可能不喜欢颜色的“null”概念 - 如果不是null你想要RGB [0,0,0],只需将任何初始出现的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.
}

0
投票

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

我不清楚重画是否填充细胞;绘制圆形后,可能需要这样做才能覆盖圆圈。

在这样的应用程序中通常计算需要重新绘制的最小矩形并仅重新绘制它 - 通过计算该矩形并将其坐标传递到重绘方法,然后仅绘制相交的组件部分来实现使用更改的矩形。

© www.soinside.com 2019 - 2024. All rights reserved.