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