Results 1 to 9 of 9
Thread: paintComponent function problem
- 02-21-2012, 07:58 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
paintComponent function problem
Hi guys I'm learing about Graphic in Java and I have a problem , because my paintComponent
doesn't work I mean that complier doesn't see ma function and program is performed without paint function
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Figure extends JPanel{
private JPanel panel;
Figure()
{
setLayout( new BorderLayout() );
panel = new JPanel();
panel.setBackground(Color.YELLOW);
add(panel, BorderLayout.CENTER);
}
public void paintComponent (Graphics g )
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(45, 45, 45, 45);
}
public static void main( String [] args )
{
JFrame f = new JFrame("Name");
Figure k = new Figure();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 200);
f.setVisible(true);
f.add(k);
}
}
- 02-21-2012, 08:03 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: paintComponent function problem
How do you know it "doesn't work"? Add some println's in the paintComponent method and see if the what you add is printed to the console.Hi guys I'm learing about Graphic in Java and I have a problem , because my paintComponent
doesn't work I mean that complier doesn't see ma function and program is performed without paint function
- 02-21-2012, 08:13 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: paintComponent function problem
I tried but sill is yellow panel without any drawing text or shapesHow do you know it "doesn't work"? Add some println's in the paintComponent method and see if the what you add is printed to the console.
- 02-21-2012, 09:11 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: paintComponent function problem
Adding a System.out.println statement won't fix it...it lets you know whether paintComponent is being called or not and is an invaluable tool in stepping through and debugging code...in your case you think paintComponent isn't being called - this will tell you whether it is or not. Post back the result. Whether it does or not will indicate what you should do to fix the problem.
- 02-21-2012, 09:35 PM #5
Re: paintComponent function problem
Add components to a GUI hierarchy before, rather than after, making it visible.
dbJava Code:// f.setVisible(true); f.add(k); f.setVisible(true);
Why do they call it rush hour when nothing moves? - Robin Williams
- 02-21-2012, 11:50 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: paintComponent function problem
I tried both this methods but it wasn't change anything , but when I changed paintComponent() to paint(), it works correctly drawing this things what I want,"Adding a System.out.println statement won't fix it..."
"Add components to a GUI hierarchy before, rather than after, making it visible.
Java Code:
// f.setVisible(true);
f.add(k);
f.setVisible(true);"
but all this stuff are drawing at default JFrame background rather then yellow JPanel
- 02-22-2012, 12:22 AM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: paintComponent function problem
Did you read my post? Adding println' won't fix it, but give you information to help you 'fix' it. And yes, changing paintComponent to paint will 'fix' the problem given the inherent way that paint works (calling super.paint at the start of the paint method will paint the children before you paint over the children - one of the reasons its advised not to override the paint method). I am not sure why you are adding a JPanel to make the background Yellow instead of just painting yellow.
- 02-22-2012, 12:50 AM #8
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: paintComponent function problem
I want to do a simple game which will consist of to parts first - label (JLabel) which will be show a score and second part will be (JPanel) area where when a player click on viewing element they change result on a label score. But now I am not sure if I must put this two part together to JFrame or it is anther way to do this.I am not sure why you are adding a JPanel to make the background Yellow instead of just painting yellow.
- 02-22-2012, 01:05 AM #9
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: paintComponent function problem
If I understand correctly, then combine the components using an appropriate LayoutManager. It should not be necessary to rely on Components painting over one another as the example above demonstrates, rather create each one and rely on a LayoutManager to place them accordingly.
Similar Threads
-
paintComponent problem
By luke in forum New To JavaReplies: 5Last Post: 04-02-2011, 02:07 PM -
Problem regarding function
By sudarson in forum New To JavaReplies: 8Last Post: 02-26-2011, 10:28 PM -
Problem with split function
By a.tajj in forum New To JavaReplies: 4Last Post: 04-14-2009, 03:30 AM -
Problem going outside paintComponent
By Thez in forum Java 2DReplies: 9Last Post: 12-08-2007, 04:59 PM -
Function declaration problem.
By snooze-g in forum Advanced JavaReplies: 3Last Post: 07-18-2007, 09:15 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks