Solved

JPanel paintComponent method

Posted on 2008-10-26
3
1,227 Views
Last Modified: 2013-12-14
I am creating a frame that draws a picture (which is a JPanel) 4 times using a gridLayout(2,2). For some reason, the background rectangle which should be white, is only drawing itself in the top left hand corner and not in the others. Any suggestions?
public class CanadaFlag extends JPanel {
 

    public CanadaFlag() {

    }

    

    public void paintComponent(Graphics g) {

    	

    	Graphics2D g2 = (Graphics2D)g;  

    	Rectangle flag = new Rectangle();

    	flag.setBounds(this.getBounds());

    	

    	int h = (int)flag.getHeight();

   	int w = (int)flag.getWidth();

    	int xCoord = (int)flag.getX();

    	int yCoord = (int)flag.getY();

    	
 

    	g2.setColor(Color.WHITE);

    	g2.fillRect(xCoord,yCoord,w,h); //this only draws it in the 

//topleft.
 

//here is where i call the panel:
 

public class FlagsFrame extends JFrame {
 

    public FlagsFrame() {

    	Toolkit toolkit = Toolkit.getDefaultToolkit();

    	Dimension dimensions = toolkit.getScreenSize();

    	int height = dimensions.height;

    	setBounds(0,0,height,height);

    	setLayout(new GridLayout(2,2));

    	add(new CanadaFlag());

    	add(new CanadaFlag());

    	add(new CanadaFlag());	

    	add(new CanadaFlag());

    	setTitle("Designed by Eric Totte: Canadian-Olympic"); 	

    }    

}

Open in new window

0
Comment
Question by:etotte1
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
Think the x and y coordinates are coming back the same for each of the four flags; therefore, I would pass the x and y coordinates in the constructor of flag something like this:

(these may not be the right location points, but using as an example)
add(new CanadaFlag(0, 0)); // top left
          add(new CanadaFlag(0, 20)); // bottom left
          add(new CanadaFlag(20, 0)); // top right      
          add(new CanadaFlag(20, 20)); // bottom right

Hope that makes sense and helps.
0
 

Author Comment

by:etotte1
Comment Utility
When i use methods like fillPolygon() the polygon shows up 4 times so the x and y coordinates are updating themselves as the coordinates used for the polygon are relative to the panel
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
I don't see any panels above, so I am not sure what you are trying to convey with your last statement.  If you saying that the new CanadaFlag() doesn't need x, y coordinates since it takes it from panel, then you must ensure that have JPanels added at the correct x, y coordinates and then add CanadaFlag() to each of the four JPanels you create.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now