PDA

View Full Version : PaintComponent


frederic202
03-14-2010, 06:56 PM
What's wrong with this code. There is an unknown problem..
It's just a grid, that works fine, but at the end there is a piece of code that calls another paintComponent method.. Is this a mistake?


package domein;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JPanel;

public class Grid extends JPanel
{
private int kolommen,rijen;
private Cel[][] hetBord;

public Grid(int zijde)
{
this.rijen=zijde;
this.kolommen=zijde;

}

public void setHetBord(Cel[][] cel)
{
hetBord=cel;
}

public void paintComponent(Graphics g)
{

int wide = getWidth(); //breedte van de frame
int tall = getHeight(); //hoogte van de frame
double rowH = getHeight() / (double)rijen; //hoogte van een vierkant
double colH=getWidth()/ (double)kolommen; //breedte van een vierkant
double breedteMuur=10;
int doorsnedePion=(int)colH/2;
Color brown=new Color(80,0,0);
g.setColor(brown); //achtergrondkleur bepalen!
g.fillRect(0, 0, wide, tall); //achtergrond helemaal opvullen


Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black); //kleur van de streep
g2.setStroke(new BasicStroke(2)); //dikte van de streep

for (int i = 1; i < kolommen; i++) //verticale lijnen cellen
{
Line2D verticaal1;

verticaal1 = new Line2D.Double(i*colH, 0.0,i*colH, (double)getHeight()); //werken met double voor nauwkeurigheid
g2.draw(verticaal1);
}

for (int i = 1; i < kolommen; i++) //verticale lijnen muren
{
Line2D verticaal2;
verticaal2 = new Line2D.Double(i*colH+breedteMuur, 0.0,i*colH+breedteMuur, (double)getHeight());

g2.draw(verticaal2);
}



for (int i = 1; i < rijen; i++) //horizontale lijnen cellen
{
Line2D horizontaal1;
horizontaal1 = new Line2D.Double(0.0, (double) (i * rowH),(double) getWidth(), (double) (i * rowH));

g2.draw(horizontaal1);
}

for (int i = 1; i < rijen; i++) //horizontale lijnen muren
{
Line2D horizontaal2;
horizontaal2 = new Line2D.Double(0.0, (double) (i * rowH+breedteMuur),(double) getWidth(), (double) (i * rowH+breedteMuur));

g2.draw(horizontaal2);
}
g.setColor(Color.red);

for(int i=0;i<hetBord.length;i++)
{
for(int j=0;j<hetBord[i].length;j++)
{
hetBord[i][j].getVoorwerp().paintComponent(g); //hetBord contains objects of the type Voorwerp, who's a subclass of JPanel
}
}




}


}

brad211987
03-14-2010, 07:09 PM
What problem are you having with it? You will need to be much more specific.

Do you get any errors?
What line/lines seem to be the problem?
Is the behavior as expected, if not where does it differ?

frederic202
03-14-2010, 07:15 PM
The problem is this piece of code:

for(int i=0;i<hetBord.length;i++)
{
for(int j=0;j<hetBord[i].length;j++)
{
hetBord[i][j].getVoorwerp().paintComponent(g); //hetBord contains objects of the type Voorwerp, who's a subclass of JPanel
}
}

Without this piece, I get a grid in my JFrame, but if I add this piece, the JFrame is just filled with a white color. And the console application tells me that there is an unknown error.

I need this piece to draw shapes for some pawns on my grid:)