I have worked on this for hours upon hours and it doesn't work.
I want to have a pixel appear on the screen, but the paint component doesn't work I don't understand why.
The setupFrame method is called from the main method.
public static void setupFrame()
{
JFrame frame = new JFrame("graphicsTest");
JPanel panel = new JPanel();
panel.setBounds(0, 0, 1080, 1080/2);
frame.add(panel);
frame.pack();
frame.setSize(1080, 1080/2);
frame.setVisible(true);
frame.setResizable(false);
frame.repaint();
}
@Override
public void paintComponent(Graphics g) {
g.fillRect(0, 0, 50, 50);
}
kenny_k
4,0005 gold badges34 silver badges43 bronze badges
-
2For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example.Andrew Thompson– Andrew Thompson2019年10月02日 10:15:37 +00:00Commented Oct 2, 2019 at 10:15
1 Answer 1
You're overloading the paintComponent() in the class you've created (let's call it MyPanel) but you're creating an instance of another class:
JPanel panel = new JPanel();
So there's no instance of MyPanel here and the method is not called. You need to use the class where you've overloaded the paintComponent method:
MyPanel panel = new MyPanel();
Or (this should be equivalent):
JPanel panel = new MyPanel();
answered Oct 2, 2019 at 10:36
Guillaume
14.7k3 gold badges46 silver badges42 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-java