Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-14-2010, 06:56 PM   PM User | #1
frederic202
New to the CF scene

 
Join Date: Mar 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
frederic202 is an unknown quantity at this point
Question PaintComponent

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?


Code:
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
		}
	}




}


}
frederic202 is offline   Reply With Quote
Old 03-14-2010, 07:09 PM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
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?
brad211987 is offline   Reply With Quote
Old 03-14-2010, 07:15 PM   PM User | #3
frederic202
New to the CF scene

 
Join Date: Mar 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
frederic202 is an unknown quantity at this point
The problem is this piece of code:

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
frederic202 is offline   Reply With Quote
Reply

Bookmarks

Tags
graphics, grid, jpanel, paintcomponent

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:03 PM.


Advertisement
Log in to turn off these ads.