拖动鼠标时Java Swing重绘

时间:2012-10-04 20:08:01

标签: java swing drawing mouseevent

当我将鼠标拖过它时,我很难让自定义面板重新绘制。

基本上,我在自定义面板上绘制自由形式的线条,并附带以下处理程序:

MouseInputAdapter mia = new MouseInputAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(_app_split_right_buttons_radioInkBtn.isSelected()) {
                    _app_split_right_journal.StartLine();
                    _app_split_right_journal.AddLineSegment(e.getX(), e.getY());
                }
                _app_split_right_journal.repaint();
            }
            @Override
            public void mouseReleased(MouseEvent e) {
                if(_app_split_right_buttons_radioInkBtn.isSelected()) {
                    _app_split_right_journal.AddLineSegment(e.getX(), e.getY());
                    _app_split_right_journal.EndLine();
                }
                _app_split_right_journal.repaint();
            }
            @Override
            public void mouseDragged(MouseEvent e) {
                if(_app_split_right_buttons_radioInkBtn.isSelected()) {
                    _app_split_right_journal.AddLineSegment(e.getX(), e.getY());
                }
                _app_split_right_journal.repaint();
            }
        };
        _app_split_right_journal.addMouseListener(mia);
        _app_split_right_journal.addMouseMotionListener(mia);

然而,在我拖动鼠标时,我似乎无法重新绘制它,只有在我释放它之后(线条正确绘制)。奇怪的是,如果在我的拖动过程中,我右键单击,当我拖动时,该线将突然开始绘制。

任何帮助都将不胜感激。

编辑:为清楚起见,StartLine()/ AddLineSegment()/ EndLine()函数只是将点添加到Point对象的ArrayList中,并且重写了repaint()函数以循环遍历这些点并在两者之间绘制线段每。代码如下:

public void DrawShapes(Graphics g) {
        g.setColor(Color.black);
        for(Geometry.Shape shape : _shapeList) {
            if(shape instanceof Geometry.Line) {
                ArrayList<Point> points = ((Line) shape).GetPointList();
                Point p1 = points.get(0);
                for(int i=1; i<points.size(); i++) {
                    Point p = points.get(i);
                    g.drawLine(p1.x, p1.y, p.x, p.y);
                    p1 = p;
                }
            }
}

编辑:想出来。这是因为我在循环列表中循环,但是我忘记了在我释放鼠标并调用EndLine()之前,我没有添加我正在绘制到该列表的行。我必须在repaint()方法中放置单独的代码来绘制我当前在绘图中间的形状。代码添加到repaint():

if(_currentShape instanceof Geometry.Line) {
            ArrayList<Point> points = ((Line)_currentShape).GetPointList();
            Point p1 = points.get(0);
            for(int i=1; i<points.size(); i++) {
                Point p = points.get(i);
                g.drawLine(p1.x, p1.y, p.x, p.y);
                p1 = p;
            }
        }

1 个答案:

答案 0 :(得分:5)

这是我为另一个问题做的一个例子,但它使用鼠标拖动在组件上绘制线条......

public class PaintingExample {

    private BufferedImage bImage;
    private ImageIcon image;
    private JLabel imageLabel;
    private int xClicked = 0;
    private int yClicked = 0;
    private int xDragged = 0;
    private int yDragged = 0;
    private MouseAdapter mouseListener =
        new MouseAdapter() {
            private boolean paint = false;
            @Override
            public void mousePressed(MouseEvent me) {

                xClicked = me.getX();
                yClicked = me.getY();
                xDragged = xClicked;
                yDragged = yClicked;

                paint = true;

            }

            @Override
            public void mouseReleased(MouseEvent e) {

                xClicked = -1;
                xClicked = -1;
                xDragged = -1;
                yDragged = -1;

                paint = false;

            }

            @Override
            public void mouseMoved(MouseEvent me) {
            }

            @Override
            public void mouseDragged(MouseEvent me) {

                if (paint) {

                    xClicked = xDragged;
                    yClicked = yDragged;

                    xDragged = me.getX();
                    yDragged = me.getY();

                    xDragged = me.getX();
                    yDragged = me.getY();

                    Graphics2D g2 = bImage.createGraphics();
                    g2.setColor(Color.WHITE);
                    g2.drawLine(xClicked, yClicked, xDragged, yDragged);
                    g2.dispose();
                    imageLabel.setIcon(new ImageIcon(bImage));

                    me.getComponent().repaint();

                }

            }
        };

    public PaintingExample() {
        try {
            bImage = ImageIO.read(new URL(
                            "http://gagandeepbali.uk.to/"
                            + "gaganisonline/images/planetbackground.jpg"));
            image = new ImageIcon(bImage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Painting on Image");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        imageLabel = new JLabel(image);
        imageLabel.addMouseListener(mouseListener);
        imageLabel.addMouseMotionListener(mouseListener);

        contentPane.add(imageLabel);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new PaintingExample().displayGUI();
            }
        });
    }
}