paintComponent telling me im using an object not a graphic

3.3k views Asked by At

Basically i have this code

ImageIcon image = new ImageIcon(this.getClass().getResource("http://i.imgur.com/UKmK7j0.jpg")); //Image is just an example

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    image.paintIcon(null, g, x, y);
}

but it tells me

"The method paintComponent(Graphics) is undefined for the type Object"

What am i doing wrong?? Please help me

2

There are 2 answers

2
jl2975 On BEST ANSWER

This is most likely because you are not extending the correct class. You need to extend JPanel or any other class that contains it in order to call this super class. Right now it is looking for the paintComponent() method in the default super class which is Object and there is no paintComponent() method there.

public class myClass extends JPanel {
    ...
}
1
Paul Samsotha On

Your call to super.paintComponent(g) is looking for a paintComponent() method from the super class. It's probably because you're trying to use this method in a class that is not a subclass of JComponent, which is the class with the paintComponent() method. See JComponent.

More than likely, you want to make your class a subclass of JPanel, or any other class that is a subclass of JComponent