在java中绘制/擦除按钮

时间:2016-12-10 20:24:59

标签: java netbeans

我正在努力找到一种方法来实现一种方法,无论何时单击绘图按钮,它都将决定您是绘图还是删除。

 private void drawButtonActionPerformed (java.awt.event.ActionEvent evt) {

        solveButton.setEnabled(true);

        slider.setEnabled(true);

        captureButton.setEnabled(true);

    } 

此刻我的代码会根据您是否有点击的障碍自动决定是否绘制或删除

 private class MouseHandler implements MouseListener, MouseMotionListener {

        int cur_row, cur_col, cur_val;



        @Override
        public void mousePressed(MouseEvent evt) {


            int row = (evt.getY() - 10) / squareSize;
            int col = (evt.getX() - 10) / squareSize;
            if (row >= 0 && row < rows && col >= 0 && col < columns) {

                cur_row = row;
                cur_col = col;
                cur_val = grid[row][col];

                if (cur_val == EMPTY) {
                    grid[row][col] = OBST;
                }
                if (cur_val == OBST) {
                    grid[row][col] = EMPTY;
                }
                repaint();
            }
        }

        @Override
        public void mouseDragged(MouseEvent evt) {

            int row = (evt.getY() - 10) / squareSize;
            int col = (evt.getX() - 10) / squareSize;
            if (row >= 0 && row < rows && col >= 0 && col < columns) {

                if ((row * columns + col != cur_row * columns + cur_col) && (cur_val == STARTNODE || cur_val == ENDNODE)) {
                    int new_val = grid[row][col];
                    if (new_val == EMPTY) {
                        grid[row][col] = cur_val;
                        if (cur_val == STARTNODE) {
                            startnodeStart.row = row;
                            startnodeStart.col = col;
                        } else {
                            endnodePos.row = row;
                            endnodePos.col = col;
                        }
                        grid[cur_row][cur_col] = new_val;
                        cur_row = row;
                        cur_col = col;
                        if (cur_val == STARTNODE) {
                            startnodeStart.row = cur_row;
                            startnodeStart.col = cur_col;
                        } else {
                            endnodePos.row = cur_row;
                            endnodePos.col = cur_col;
                        }
                        cur_val = grid[row][col];
                    }
                } else if (grid[row][col] != STARTNODE && grid[row][col] != ENDNODE) {
                    grid[row][col] = OBST;
                }

            }

            repaint();

        }

        @Override
        public void mouseReleased(MouseEvent evt) {
        }

        @Override
        public void mouseEntered(MouseEvent evt) {
        }

        @Override
        public void mouseExited(MouseEvent evt) {
        }

        @Override
        public void mouseMoved(MouseEvent evt) {
        }

        @Override
        public void mouseClicked(MouseEvent evt) {
        }

    }

请帮助我。

1 个答案:

答案 0 :(得分:0)

我没有读过代码(它很长很不清楚),但是如果你想做我认为你想做的事情(你可以绘制一个程序,然后画出你绘制的内容)你可以当按下鼠标按钮时,在鼠标的x和y中绘制某种颜色的圆圈。当你想要简化时,你只需要用背景颜色做同样的事情。