Draw a chess board using nested for loop - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Draw a chess board using nested for loop

Demo Code

     /*from w ww  .  j a v  a2s .  com*/
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame
{

     public static void main(String arg[]) 
     {
         JFrame frame = new Main();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         frame.setSize(new Dimension(400,400));
         frame.pack();
         frame.setVisible(true);
       }
     
     public Main()
     {
          JPanel panel = new DrawStuff();
          panel.setPreferredSize(new Dimension(400,400));
          add(panel);
     }
     
     class DrawStuff extends JPanel
     {
          int nPoints = 1000;

          public DrawStuff()
          {
               
          }
          
           public void paintComponent(Graphics g) 
           {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D)g;
                g2.translate(20,20);
                boolean black = false;
                for(int row = 0; row < 8; row++)
                {
                     for(int column = 0; column < 8; column++)
                     {
                          Rectangle2D.Double rect = new Rectangle2D.Double(column*40,row*40,40,40);
                          if(black)
                               g2.fill(rect);
                          else
                               g2.draw(rect);
                          black = !black;
                     }
                     black = !black;
                }
           }
     }
}

Related Tutorials