简体   繁体   English

如何在Java Swing中的2点之间拖动和画线

[英]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 我想用鼠标拖动在两个xy坐标之间绘制一条线,但无法绘制任何内容

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. 它是一个使用swing和awt的gui应用程序,目前我使用鼠标事件将鼠标事件的初始和最终xy位置记录为[x1,y1,x2,y2] ,但是无法在两者之间绘制线条他们。

The drawline is its own function called into the main 画线是自己的函数,称为主函数

edit: say I have 2 classes; 编辑:说我有2节课;

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. 我不能调用drawline(jpanel),除非它是一个静态函数,但是将其设为静态会导致mouselisteners和repaint变得无效。

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) 只要Graphics g在函数内部而不是直接在类中,它将变为无效符号(忽略check和xy值作为占位符) 在此处输入图片说明

you don't need to have arrays or even X & Y.you can use the getPoint() method of mouseEvent. 您不需要数组甚至X&Y。您可以使用mouseEvent的getPoint()方法。 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); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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