drawing graphics in java using Netbeans with source codes

java graphics drawing
java graphics drawing

This is a simple java graphics drawing tutorial. Here I show you how drawing a chessboard using graphics in java. And here I use Netbeans IDE for this tutorial.

Do you need help to remotely set up my any project on your machine or customize any project with your requirement? please contact me at syntech1994@gmail.com

package chessboard;

import javax.swing.JFrame;


import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 *
 * @author ccs
 */
public class ChessBoard extends JPanel{

   
    
    public void paint(Graphics gp)
    {
    
        gp.fillRect(50, 50, 600, 600);
        for(int x=50; x<=600; x+=150)
        {
        
            for(int y=50; y<=600; y+=150)
            {
                gp.clearRect(x, y, 75, 75);
            }
        }
        
    
         for(int x=125; x<=600; x+=150)
        {
        
            for(int y=125; y<=600; y+=150)
            {
                gp.clearRect(x, y, 75, 75);
            }
        }
        
    }
    
    
    public static void main(String[] args) {
       
        JFrame jf=new JFrame();
        jf.setSize(700, 700);
        jf.setTitle("Draw Chess Board");
        jf.getContentPane().add(new ChessBoard());
        jf.setLocationRelativeTo(null);
        jf.setBackground(Color.PINK);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);     
    } 
}

One more tutorial with Graphics 2D : Click Here

Visit to code store: Click here>>

Don’t Forget Subscribe Us

5 Comments

  1. This is really great do you plan on adding in pieces, since I want to do something like this for my coding assessment which is very soon.

Leave a Reply

Your email address will not be published.


*