Java: paintComponent not recognized

Started by
4 comments, last by Xiachunyi 18 years, 5 months ago
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.
Advertisement
Did you want paintComponent() or paintComponents()?
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
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?
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).
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.

This topic is closed to new replies.

Advertisement