Public Group

# Java: paintComponent not recognized

This topic is 4791 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, for some reason, something simple probably, I can not seem to find what is the problem of painComponent not being recognized. The following is my program in all its glory so far:
import java.awt.*;
import javax.swing.*;

/*==============================================================================
Designation: Program's Main Class
Use: Holds the entry-point, drawing, and timing.
==============================================================================*/
class Fan extends JFrame
{
//Variables

//=====Entry Point
public static void main(String args[])
{
//Setup Main Window
JFrame jfWindow = new JFrame("Fan Control");

jfWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfWindow.setResizable(false);
jfWindow.setSize(640, 480);

//Center Main Window to Screen
Dimension dimScreenSize = Toolkit.getDefaultToolkit().getScreenSize();

int iPosX = (dimScreenSize.width - jfWindow.getWidth()) / 2;
int iPosY = (dimScreenSize.height - jfWindow.getHeight()) / 2;

jfWindow.setLocation(iPosX, iPosY);

//Display Window
jfWindow.setVisible(true);
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);

}
}


Whenever I try to compile the source, my compiler would return, "cannot resolve symbol method paintComponent(java.awt.Graphics)". I checked over and over to make sure I have included the necessary files and the spelling is right. Is there some simple thing that I have simply forgotton? Thank you.

##### Share on other sites
Did you want paintComponent() or paintComponents()?

##### Share on other sites
There is no method called paintComponent in JFrame (see this ). There is a method called paintComponents (notices the last 's') in java.awt.Container which JFrame inherit's from. Still I think what you are looking for is update() in JFrame and paint in java.awt.Container

Lizard

##### Share on other sites
Thank you both for the quick replies.

I was trying to overload the method provided from this since it has a "protected void paintComponent(Graphics g): Calls the UI delegate's paint method, if the UI delegate is non-null." method.

The reason being is that, from my reading, it seems that this is automatically called whenever the frame is resized, moved, and so on.

Is "paintComponents()" the same thing?

##### Share on other sites
Quote:
 I was trying to overload the method provided from this since it has a "protected void paintComponent(Graphics g): Calls the UI delegate's paint method, if the UI delegate is non-null." method.

JFrame doesn't inherit from JComponent, so it doesn't have that method.

Is there a reason you're not just overriding paint() ?

Also, I find it works better if you make a custom JComponent, and set that to be the content pane of the frame using JFrame.setContentPane(). I forget what exactly this fixes, but I was having some weird problem and this made things work normally.

I think it might have been the borders: if you draw straight from the JFrame, then the top left corner (0,0) is actually under the title bar that's part of the OS's UI, and somewhere around (0, 30) is the top-left corner of the visible area. Whereas if you use a custom content pane, (0,0) is the top-left visible point (as you would expect).

##### Share on other sites
Now I know what I am doing wrong, I am trying to "class Fan extends JPanel" but got confused because I am creating a new JFrame object. doh!

Thank you all for the help.

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013707
×