Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: PaintComponent

  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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?

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •