0

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) enter image description here

Anan
  • 81
  • 8
  • @AndrewThompson better? – Anan May 14 '19 at 06:51
  • Better that you **read** the links offered and ask about anything you don't understand! – Andrew Thompson May 14 '19 at 06:51
  • 1
    See [Custom Painting Approaches](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/). It demonstrates how to draw multiple rectangles on a panel. The concept would be the same except you draw a line instead of a rectangle. – camickr May 14 '19 at 14:31
  • Possible duplicate of [Move an Oval in java](https://stackoverflow.com/questions/20792947/move-an-oval-in-java) – Frakcool May 14 '19 at 19:08
  • Another possible duplicate: https://stackoverflow.com/questions/47369565/connect-two-circles-with-a-line/47371140#47371140 – Frakcool May 14 '19 at 19:09

1 Answers1

3

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); 
}
Davood
  • 72
  • 8
  • so I've tried adapting it into a class that extends Canvas to draw on (.paint and repaint() didnt like it otherwise) then into its own call-able function, but the g part of graphics g stopped working (cannot resolve symbol), which was part of the initial issue, how do i get around that? Then there are complaints about it needing to be static, which makes the mouse listeners inactive – Anan May 14 '19 at 07:15
  • 2
    `public void paint(Graphics g) {` Don't override `paint` in a `JComponent`. The correct method is `paintComponent`. – Andrew Thompson May 14 '19 at 08:46