Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: Draw a Checkerboard

Java: Draw a Checkerboard
{filelink=10236}

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CheckerboardExample extends JFrame
{
    public CheckerboardExample()
    {
    	JFrame f = new JFrame("Checkerboard in java");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(250, 200);
        JPanel damier = new Checkerboard();
        f.add(damier);
        f.setVisible(true);
    }
	public static void main(String argv[])
    {
        Runnable showMyGUI = new Runnable() {
            public void run() {
                new CheckerboardExample();
            }
        };
        SwingUtilities.invokeLater(showMyGUI);
    }
}
 class Checkerboard extends JPanel
 {
        static final int CHECKER_SIZE = 60;
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.white);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.BLACK);
            for (int stripeX = 0; stripeX < getWidth(); stripeX += CHECKER_SIZE) {
                for (int y = 0, row = 0; y < getHeight(); y += CHECKER_SIZE/2, ++row) {
                    int x = (row % 2 == 0) ? stripeX : (stripeX + CHECKER_SIZE/2);
                    g.fillRect(x, y, CHECKER_SIZE/2, CHECKER_SIZE/2);
                }
            }
        }
    }

Users who found this page were searching for:

  • make a checkerboard with java 2D graphics
  • perl check if exists key in hash
  • python extract url
  • print date java
  • method checker board java
  • python change numerical value in dictionary
  • python static method class method
  • how to check for positive numbers in python
  • Drawing a Checkerboard in Java
  • regex replace lowercase a-z

No comments yet.

Leave a Comment