Java减少mouseMotionListener间隔之间的时间

时间:2018-10-18 10:12:46

标签: java swing mousemotionlistener

我想在Java中创建一个简单的绘图程序,该程序目前仅使用Graphics.fillOval()mouseMotionListener()绘制一条线。问题是,如果快速移动鼠标,线条的精度会降低,椭圆形(在这种情况下为圆形)会散开。

代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Drawing 
{
     private JFrame window;
     private Graphics g;
     public Drawing()
     {
        window=new JFrame();
        window.setTitle("Paint_window");
        window.setSize(1000,700);
        window.setVisible(true);
        window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
        g=window.getGraphics();
        window.addMouseMotionListener(new MouseMotionAdapter()
            {
                public void mouseDragged(MouseEvent e)
                {
                 if(SwingUtilities.isLeftMouseButton(e)
                {
                        g.fillOval((int)e.getX(),(int)e.getY(),10,10);
                    }
                }
            });
    }
}

是否有改进方法或更好的方法?

1 个答案:

答案 0 :(得分:1)

g=window.getGraphics();

首先,您不应该使用组件的getGraphics()。您所做的任何绘制都只是临时的,并且在Swing第一次确定需要重新绘制组件时将被擦除。在上面的示例中,只需尝试调整框架的大小即可。

进行自定义绘制的正确方法是重写JPanel的paintComponent(...)方法并将面板添加到框架中。有关更多信息,请参见Custom Painting

  

问题是,如果您快速移动鼠标,线条会变得不那么精确,并且椭圆形(在这种情况下为圆形)会分开

您将无法为鼠标移动的每个像素生成事件。

相反,您需要能够在拖动鼠标时在连续的点之间“画一条线”。

因此,您需要将每个点存储在ArrayList中,并在自定义绘制代码中迭代所有点并绘制一条线。

一个入门的基本示例:

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

class DrawingPanel extends JPanel
{
    private ArrayList<ArrayList<Point>> previous = new ArrayList<ArrayList<Point>>();
    private ArrayList<Point> current = new ArrayList<Point>();
    private BasicStroke basicStroke;

    public DrawingPanel(int strokeSize)
    {
        basicStroke = new BasicStroke(strokeSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);

        MouseAdapter ma = new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                current.add( new Point(e.getX(), e.getY()) );
            }

            @Override
            public void mouseDragged(MouseEvent e)
            {
                current.add( new Point(e.getX(), e.getY()) );
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent e)
            {
                if (current.size() > 1)
                {
                    previous.add( current );
                }

                current = new ArrayList<Point>();
            }
        };

        addMouseMotionListener( ma );
        addMouseListener( ma );
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke( basicStroke );

        //  Paint lines from previous drags

        for (int i = 0; i < previous.size(); i++)
        {
            drawLines(g, previous.get(i));
        }

        //  Paint line from current drag

        drawLines(g, current);
    }

    private void drawLines(Graphics g, ArrayList<Point> points)
    {
        for (int i = 0; i < points.size() - 1; i++)
        {
            int x = (int) points.get(i).getX();
            int y = (int) points.get(i).getY();
            int x2 = (int) points.get(i + 1).getX();
            int y2 = (int) points.get(i + 1).getY();
            g.drawLine(x, y, x2, y2);
        }
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Drawing Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DrawingPanel(15));
        frame.setSize(400, 400);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

使用上述方法,您将在每次重新绘制组件时重新绘制线条。

另一种方法是绘制到BufferedImage,然后在面板上绘制BufferedImage。您可以查看Custom Painting Approaches以获得该方法的示例。