79754679

Date: 2025-09-03 13:57:17
Score: 0.5
Natty:
Report link

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)

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Override
  • Low reputation (1):
Posted by: Alex