1

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
asked Oct 2, 2019 at 8:49
1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.