Java: mouseDragged and moving around in a graphical interface

前端 未结 1 2017
耶瑟儿~
耶瑟儿~ 2020-11-29 13:07

newbie programmer here.

I\'m making a program that renders user-inputted equations in a Cartesian coordinate system. At the moment I\'m having some issues with letti

相关标签:
1条回答
  • 2020-11-29 13:30

    Based on this example, the following program allows the user to drag the axes' intersection to an arbitrary point, origin, which starts at the center of the panel.

    iamge

    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    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;
    
    /**
     * @see https://stackoverflow.com/a/15576413/230513
     * @see https://stackoverflow.com/a/5312702/230513
     */
    public class MouseDragTest extends JPanel {
    
        private static final String TITLE = "Drag me!";
        private static final int W = 640;
        private static final int H = 480;
        private Point origin = new Point(W / 2, H / 2);
        private Point mousePt;
    
        public MouseDragTest() {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            this.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    mousePt = e.getPoint();
                    repaint();
                }
            });
            this.addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    int dx = e.getX() - mousePt.x;
                    int dy = e.getY() - mousePt.y;
                    origin.setLocation(origin.x + dx, origin.y + dy);
                    mousePt = e.getPoint();
                    repaint();
                }
            });
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(W, H);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawLine(0, origin.y, getWidth(), origin.y);
            g.drawLine(origin.x, 0, origin.x, getHeight());
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame f = new JFrame(TITLE);
                    f.add(new MouseDragTest());
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.pack();
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题