Solved

JPanel paintComponent method

Posted on 2008-10-26
3
1,239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22809465
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
ID: 22809488
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 60

Expert Comment

by:Kevin Cross
ID: 22809932
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

717 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