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)