Sign in to follow this  
Xiachunyi

Java: paintComponent not recognized

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this