Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Chessboard filling issue

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Chessboard filling issue

    Hi,

    I'm trying to create an 8x8 chessboard using GeneralPath. I've been able to make the outline of the "matrix" itself, but I am having trouble filling it. I've tried filling it with both windingrules, so I guess my question is as follows: Is there any way I can make the board I've created using a path into one whole figure which allows me to fill every other square with the color black?

    My code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.geom.*;
     
    public class chessBoard extends JApplet{
     
    	public static void main (String []args) {
    		JFrame frame = new JFrame();
    		frame.setTitle("Chessboard");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JApplet applet = new chessBoard();
    		applet.init();
    		frame.getContentPane().add(applet);
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public void init() {
    		JPanel panel = new chessBoardPanel();
    		getContentPane().add(panel);
    	}
    }
     
    class chessBoardPanel extends JPanel {
     
    	public chessBoardPanel() {
    		setBackground(Color.white);
    		setPreferredSize(new Dimension(600, 400));
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D)g;
    		GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
    		g2.setColor(Color.black);
     
    		int w = this.getWidth();
    		int h = this.getHeight();
    		float x0 = 0.1f*w;
    		float y0 = 0.1f*h;
    		float y = y0;
    		float x = x0;
     
    		path.reset();
    		for (int i = 0; i <= 8; i++) {
    			path.moveTo(x, y0);
    			path.lineTo(x, h - y0);
    			x += x0;
    		}
    		for (int i = 0; i <= 8; i++) {
    			path.moveTo(x0, y);
    			path.lineTo(w - x0, y);
    			y += y0;
    		}
    		path.closePath();
    		g2.draw(path);
    	}
    }

    What I've done is created 9 lines vertically and horizonally, merging into an 8x8 matrix.

    I've also tried adding this after I'm done drawing with"path":
    AffineTransform tr = new AffineTransform();
    		Shape pathshape;
    		path.setWindingRule(GeneralPath.WIND_EVEN_ODD);
    		pathshape = (Shape)tr.createTransformedShape(path);
    		g2.draw(pathshape);

    Current output:


    Very grateful for any response.

    Best regards
    Ole Martin
    Last edited by olemagro; January 22nd, 2010 at 12:27 PM. Reason: wrong attribute in the code


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Chessboard filling issue

    You could just iterate over the grid, calling g.fillRect when necessary. Creating objects and moving them via other objects looks like a bit of work for a drawing routine. Something like this:
    //you need to deal with the x and y increments during this loop in order to specify the right coordinates for fillRect
    for ( int i = 0; i < 8; i++ ){		     
    	for ( int j = 0; j < 8; j++ ){         
    		if ( (i % 2 == 0 && j % 2 == 0) ||  ( i % 2 == 1 &&  j % 2 == 1)  ){
                         g.fillRect(....);
                    }
    	}
     
    }

    Once you've got the boxes filled, you really only need to draw the outline around the board and not around every box (the filled boxes already provide their outline)

  3. The Following User Says Thank You to copeg For This Useful Post:

    olemagro (January 22nd, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Chessboard filling issue

    Ah, clever. I'll give it a shot!

  5. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Chessboard filling issue

    I definetly got the matrix generated with your double for-loop example and simply adding a border around it all. However, the border doesn't "stick" to the filled squares when you try to adjust the with and height of the image after running it. I'm just wondering if there is any way I could make it do that?

    My current paintComponent-method:
    public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D)g;
    		GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
    		g2.setColor(Color.black);
     
    		int w = this.getWidth();
    		int h = this.getHeight();
     
    		int x0 = w/10;
    		int y0 = h/10;
    		int y = y0;
    		int x = x0;
     
    		//Draws matrix, filling every other square
    		for ( int i = 0; i < 8; i++ ){
    			for ( int j = 0; j < 8; j++ ){         
    				if ( (i % 2 == 0 && j % 2 == 0) ||  ( i % 2 == 1 &&  j % 2 == 1)  ){
    		                    g2.fillRect(x, y, x0, y0);
    				}
    				x += x0;
    			}
    			x = x0;
    			y += y0;
    		}
     
    		//Draws outline
    		g2.drawLine(x0, y0, x0, h - y0);
    		g2.drawLine(x0, h - y0, w - x0, h - y0);
    		g2.drawLine(w - x0, h - y0, w - x0, y0);
    		g2.drawLine(w - x0, y0, x0, y0);
    	}

    Thanks in advance
    //Ole Martin

  6. #5
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Chessboard filling issue

    This is only a minor thing, but you can simply your if statement. Instead of...
    if ( (i % 2 == 0 && j % 2 == 0) ||  ( i % 2 == 1 &&  j % 2 == 1)  )

    ...you can just write
    if((i+j)%2 == 0)

  7. The Following User Says Thank You to Shambolic For This Useful Post:

    copeg (January 23rd, 2010)

  8. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Chessboard filling issue

    Thanks Shambolic.

    olemagro, you can get them to 'stick' if you draw the boundary relative to the width of the board, not the screen.

    g2.drawLine(x0, y0, x0, 9 * y0);

    Because your widths are all divided by 10 in integer math, the board will only resize every 10 pixels of window resizing. You might be able to fix this by using a Shape object (aka GeneralPath) that uses floating points.

  9. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Chessboard filling issue

    A better way is to use a grid layout. Setup your JPanel to accept a 8*8 set of components, and then each component can be drawn separately.

    public class GUI
    {
         public static void main(String[] args)
         {
              JPanel board = new JPanel(new GridLayout(8,8,0,0));
              for (int i = 0; i < 63; i++)
              {
                   if (i % 2 == 0)
                   {
                        board.add(new Square(Color.black);
                   }
                   else
                   {
                        board.add(new Square(Color.white);
                   }
              }
              JFrame mainFrame = new JFrame(board);
              mainFrame.setDefaultCloseOperation(JFrame.CLOSE_ON_EXIT);
              mainFrame.setVisible(true);
         }
    }
    public class Square extends JComponent
    {
         protected Color color;
     
         public Square(Color color)
         {
              this.color = color;
         }
     
         protected void PaintComponent(Graphics g)
         {
              super();
              Graphics ours = g.create(); // so we don't mess-up the original graphics
              ours.setColor(this.color);
              ours.fillRect(0,0,this.getWidth(), this.getHeight()); // I may have it drawing the wrong direction. If so, that's a simple fix
         }
    }

  10. #8
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Chessboard filling issue

    Thanks a lot for all the response! I'll give it a shot when I've gotten some rest.

    //Ole Martin

Similar Threads

  1. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 08:28 PM
  2. url pattern issue - please help me...
    By bharathik in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 9th, 2009, 04:28 AM
  3. Repeating program issue
    By Bill_H in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2009, 10:58 AM
  4. Filling an Array?
    By Bascotie in forum Collections and Generics
    Replies: 5
    Last Post: October 14th, 2009, 06:27 PM
  5. Issues with Tomcat 6.0
    By sanyog24681 in forum Java Servlet
    Replies: 0
    Last Post: October 21st, 2008, 07:55 AM