I am pretty new to java, and I am trying to make a MouseListener so that I can draw lines in a JFrame. Here is my code:
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Liner extends JComponent implements MouseListener{
static int MouseX1, MouseY1, MouseX2, MouseY2;
static JFrame window = new JFrame();
static JPanel panel = new JPanel();
public static void main(String[] args){
panel.addMouseListener(this);
window.add(panel);
window.setVisible(true);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g){
g.drawLine(MouseX1,MouseY1,MouseX2,MouseY2);
}
public void mousePressed(MouseEvent e) {
MouseX1 = e.getX();
MouseY1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
MouseX2 = e.getX();
MouseY2 = e.getY();
}
}
I have been getting the same errors a lot, including
error: non-static variable this cannot be referenced from a static context panel.addMouseListener(this);
and
error: cannot find symbol public void mouseReleased(MouseEvent e) {
(the second one refers to "MouseEvent").