Java: Dessiner un damier

Author:

 list, list, set, swing, awt, JPanel, JFrame
Download

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 Damier extends JFrame
{
    public Damier()
    {
    	JFrame f = new JFrame("Jeux de Dame en 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 Damier();
            }
        };
        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);
                }
            }
        }
    }