Your code dosent run as expected beacuse in
public class theGame {
public static void main(String[] args) {
lineTest gameBoard = new lineTest();
}
You are creating a new instance of lineTest, but in the lineTest class you do
public class lineTest extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
g2d.drawLine(100, 100, 100, 200);
}
public static void main(String[] args) {
lineTest points = new lineTest();
JFrame frame = new JFrame("Points");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(points);
frame.setSize(250, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Which if you didn't notice the mistake, you added a main method and not a constructor, you need to change it to:
public lineTest() {
lineTest points = new lineTest();
JFrame frame = new JFrame("Points");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(points);
frame.setSize(250, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Also, another problem is, that you are casting Graphics to Graphics2D, which is completley unnecesary.
So change
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
g2d.drawLine(100, 100, 100, 200);
}
to
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(100, 100, 100, 200);
}
(adding the @Override anottation is good practice)