跟踪鼠标坐标并允许在窗口中绘制线条的程序

时间:2017-06-06 13:14:36

标签: java jframe jpanel drawing mouseevent

该计划应该做到以下几点:

  1. 打开一个窗口。获取鼠标在Window(x和y坐标)中的位置,并将它们设置为窗口的标题。
  2. 如果单击并按住鼠标左键,则应该能够画一条线。
  3. 问题是当我运行程序时没有。 1工作,但不是没有。 2.在调整窗口大小时,坐标会冻结,但我可以绘制线条。

    希望有人可以帮忙解决这个问题。

    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Point;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Main extends JPanel {
    
        private Point lastPoint;
    
    
        public Main() {
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    lastPoint = new Point(e.getX(), e.getY());
                }
            });
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    Graphics g = getGraphics();
                    g.drawLine(lastPoint.x, lastPoint.y, e.getX(), e.getY());
                    lastPoint = new Point(e.getX(), e.getY());
                    g.dispose();
                }
            });
        }
    
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);
            frame.setResizable(true);
            frame.setVisible(true);
            frame.getContentPane().add(new Main(), BorderLayout.CENTER);
            frame.addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    int x = e.getX();
                    int y = e.getY();
                    String xstring = String.valueOf(x);
                    String ystring = String.valueOf(y);
                    frame.setTitle(xstring + ", " +  ystring);
            }
            });
            }
    
    }
    

2 个答案:

答案 0 :(得分:0)

如果将以下内容添加到MouseMotionAdapter,您可以在拖动鼠标时看到如何调整标题。 也许你可以从那里发牢骚。

public void mouseDragged(MouseEvent e) {
  int x = e.getX();
  int y = e.getY();
  String xstring = String.valueOf(x);
  String ystring = String.valueOf(y);
  frame.setTitle("dragged - " + xstring + ", " + ystring);
}

尝试一点然后导致这个代码:

public class Draw {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setResizable(true);
        frame.setVisible(true);
        JPanel myPanel = new MyPanel();
        frame.getContentPane().add(myPanel, BorderLayout.CENTER);
        myPanel.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseMoved(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                String xstring = String.valueOf(x);
                String ystring = String.valueOf(y);
                frame.setTitle(xstring + ", " + ystring);
                ((MyPanel) myPanel).setLastPoint(e);
            }

            public void mouseDragged(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                String xstring = String.valueOf(x);
                String ystring = String.valueOf(y);
                frame.setTitle("dragged - " + xstring + ", " + ystring);

                Graphics g = myPanel.getGraphics();
                Point lastPoint = ((MyPanel) myPanel).getLastPoint();
                g.drawLine(lastPoint.x, lastPoint.y, e.getX(), e.getY());
                ((MyPanel) myPanel).setLastPoint(new Point(e.getX(), e.getY()));
                g.dispose();
            }
        });
    }
}

class MyPanel extends JPanel {
    private Point lastPoint;

    public Point getLastPoint() {
        return lastPoint;
    }

    public void setLastPoint(Point point) {
        lastPoint = point;

    }

    public void setLastPoint(MouseEvent e) {
        lastPoint = new Point(e.getX(), e.getY());
    }
}

答案 1 :(得分:0)

这里有几件事:

如果要自定义Component的绘画,请覆盖其paintComponent方法,请勿尝试使用Graphics获取其getGraphics对象。< / p>

使用repaint()重新制作Component(即paintComponent将被调用)。

只有在添加了所有其他组件后,才能使JFrame可见。

以下是您的代码的更改方式:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

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

public class Main extends JPanel {

    private Point lastPoint = new Point();
    private Point currentPoint = new Point();

    public Main() {
        addMouseListener(new MouseAdapter() {
            public void mousePressed(final MouseEvent e) {
                lastPoint = new Point(e.getX(), e.getY());
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(final MouseEvent e) {

                currentPoint = new Point(e.getX(), e.getY());
                repaint();

            }
        });
    }

    @Override
    public void paintComponent(final Graphics g) {

        super.paintComponent(g);

        g.drawLine(lastPoint.x, lastPoint.y, currentPoint.x, currentPoint.y);

    }

    public static void main(final String[] args) {
        // TODO Auto-generated method stub
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setResizable(true);

        frame.getContentPane().add(new Main(), BorderLayout.CENTER);
        frame.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseMoved(final MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                String xstring = String.valueOf(x);
                String ystring = String.valueOf(y);
                frame.setTitle(xstring + ", " + ystring);
            }
        });
        frame.setVisible(true);
    }

}