简体   繁体   中英

How to drag and draw line between 2 points in Java Swing

I would like to draw a line between two xy coordinate with mouse drag, but cannot get anything to draw

its a gui application using swing and awt, I currently have the mouse log the initial and final xy positions using mouse events which are stored in an array as [x1,y1,x2,y2] , however, cannot get a line to draw between them.

The drawline is its own function called into the main

edit: say I have 2 classes;

public class mainApp extends JFrame implements ActionListener, Runnable {

    private JPanel jpanel = new JPanel();

    private mainApp(String title) throws HeadlessException {
        super(title);
    }

    private void createGUI() {
        // TODO
        // ...

        // cannot call unless is static
        drawStraightLine.drawLine(jpanel);

        this.pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {}

    @Override
    public void run() {createGUI();}

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        SwingUtilities.invokeLater(new mainApp("drawline"));
    }
}
public class drawStraightLine extends JPanel {

    public static void drawLine(JPanel jpanel) {
        // content which conceivably works
        // mouselisteners and repaint()

        public void paintComponent (Graphics g){
            super.paintComponent(g);
            if (check != null) {
                Color purple = new Color(128, 0, 128);
                g.setColor(purple);
                g.drawLine(x1, y1, x2, y2);
        }
    }
}

i cannot call drawline(jpanel) unless it is a static function, but making it static causes the mouselisteners and repaint to become invalid.

while as long as Graphics g is inside a function and not directly in the class it will become an invalid symbol (ignoring check and xy values as placeholders) 在此处输入图片说明

you don't need to have arrays or even X & Y.you can use the getPoint() method of mouseEvent. try this:

public static void main(String args[]) throws Exception {
    JFrame f = new JFrame("Draw a Line");
    f.setSize(300, 300);
    f.setLocation(300, 300);
    f.setResizable(false);
    JPanel p = new JPanel() {
        Point pointStart = null;
        Point pointEnd   = null;
        {
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    pointStart = e.getPoint();
                }

                public void mouseReleased(MouseEvent e) {
                    pointStart = null;
                }
            });
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    pointEnd = e.getPoint();
                }

                public void mouseDragged(MouseEvent e) {
                    pointEnd = e.getPoint();
                    repaint();
                }
            });
        }
        public void paint(Graphics g) {
            super.paint(g);
            if (pointStart != null) {
                g.setColor("put your color here");
                g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
            }
        }
    };
    f.add(p);
    f.setVisible(true); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM