当鼠标释放时,线条在绘图程序中消失

时间:2013-03-29 03:56:57

标签: java swing line paint repaint

下面写在BasicLinePix类中我尝试创建一个程序,允许用户通过按住shift键同时按住鼠标按钮并将其拖动到端点来绘制线条。问题是,当鼠标被释放时,该行消失。我试图让线条在面板中显示,以及以相同的方式显示多条线。

我的代码如下:

    // this method overrides the paint method defined in JFrame
    public void paint(Graphics g) {
        super.paint(g);


    }


    // Inner class - instances of this class handle action events
    private class EventHandler implements ActionListener, MouseListener,
            MouseMotionListener {

        private Point startPoint = null; // line's start point
        private Point endPoint = null; // line's most recent end point

        public void actionPerformed(ActionEvent arg0) {
            if (arg0.getActionCommand().equals("Exit")) {
                statusLabel.setText("Exiting program...");
                System.exit(0);
            }

        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {

            if (e.isShiftDown()) {

                // record starting point for line
                startPoint = new Point(e.getX(), e.getY());

                // initialize endPoint
                endPoint = startPoint;
            }

            if (e.isControlDown()) {
                Graphics g = drawingPanel.getGraphics();
                g.drawString("Hello", e.getX(), e.getY());
            }

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            //repaint the frame and its contents
            //this executes the paint method defined above
            repaint();
        }

        @Override
        public void mouseDragged(MouseEvent e) {

            if (e.isShiftDown()) {
                // Implement rubber-band cursor
                Graphics g = drawingPanel.getGraphics();
                g.setColor(Color.black);

                g.setXORMode(drawingPanel.getBackground());

                // REDRAW the line that was drawn
                // most recently during this drag
                // XOR mode means that yellow pixels turn black
                // essentially erasing the existing line
                drawLine(g, startPoint, endPoint);

                // Update the end point of the line to current mouse position
                endPoint = new Point(e.getX(), e.getY());

                // Draw line to current mouse position
                // XOR mode: yellow pixels become black
                // black pixels, like those from existing lines, temporarily
                // become
                // yellow
                drawLine(g, startPoint, endPoint);

            }

        }

        @Override
        public void mouseMoved(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        private void drawLine(Graphics g, Point start, Point end) {
            if (startPoint != null && endPoint != null) {
                int startX = ((Double) start.getX()).intValue();
                int startY = ((Double) start.getY()).intValue();

                int endX = ((Double) end.getX()).intValue();
                int endY = ((Double) end.getY()).intValue();

                g.drawLine(startX, startY, endX, endY);
            }
        }

    }

}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:6)

不要对组件使用getGraphics()调用来获取组件的图形。用paintComponent(...)方法绘制。您可以在本网站和图形教程中多次找到此建议。

问题是从组件上调用getGraphics()获得的Graphics对象不是一个持久的Graphics对象,并且会在下一次重绘时消失。

请注意,您发布的代码中有80%与您的问题无关,而且完全不必要且分散注意力。

编辑:请注意,您的程序看起来可能会受益于使用BufferedImage,绘制它,然后在JPanel的paintComponent方法中显示BufferedImage。如果你选择了这条路线,那么可以通过getGraphics()获得 BufferedImage的图形对象,只要你在完成它时就把它丢弃了。

编辑2:尽管删除不相关的代码是个好主意,但不要删除太多使代码无法编译且无法运行的代码。请查看而不是创建sscce