Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import acm.graphics.GLabel;
- import acm.graphics.GRect;
- import acm.program.GraphicsProgram;
- /**
- *
- * Draws a chessboard with gray and white Rectangles and even the row and column
- * designators.
- *
- */
- public class Chessboard extends GraphicsProgram {
- private static final String[] DESIGNATOR_NUM = { "0", "8", "7", "6", "5", "4", "3", "2", "1" };
- private static final String[] DESIGNATOR_LET = { "0", "A", "B", "C", "D", "E", "F", "G", "H" };
- public static final int FIELD_SIZE = 100;
- @Override
- public void run() {
- setSize(1000, 1000);
- drawChessboard(FIELD_SIZE);
- super.run();
- }
- /**
- *
- * Draws the chestboard with a size that can be set by the user as well as the
- * Designators to the sides of the playfield.
- *
- * @param size size of the rectangles that should be drawn.
- */
- public void drawChessboard(int size) {
- // Draw the Chessfield with Changing colors
- for (int x = 1; x <= 8; x++) {
- for (int y = 1; y <= 8; y++) {
- if (y % 2 == 0 && x % 2 != 0 || y % 2 != 0 && x % 2 == 0) {
- GRect rect = new GRect(x * size, y * size, size, size);
- rect.setFilled(true);
- rect.setFillColor(Color.GRAY);
- add(rect);
- }
- }
- }
- // Draw Designators A to H at the top row
- for (int i = 1; i <= 8; i++) {
- GLabel label = new GLabel(DESIGNATOR_LET[i], i * FIELD_SIZE + FIELD_SIZE / 2, 75);
- label.setFont("SansSerif-30");
- add(label);
- }
- // Draw Designators A to H at the bottom row
- for (int i = 1; i <= 8; i++) {
- GLabel label = new GLabel(DESIGNATOR_LET[i], i * FIELD_SIZE + FIELD_SIZE / 2, 950);
- label.setFont("SansSerif-30");
- add(label);
- }
- // Draw Designators 1 to 8 on the left side
- for (int i = 1; i <= 8; i++) {
- GLabel label = new GLabel(DESIGNATOR_NUM[i], 50, i * FIELD_SIZE + FIELD_SIZE / 2);
- label.setFont("SansSerif-30");
- add(label);
- }
- // Draw Designators 1 to 8 on the right side
- for (int i = 1; i <= 8; i++) {
- GLabel label = new GLabel(DESIGNATOR_NUM[i], 925, i * FIELD_SIZE + FIELD_SIZE / 2);
- label.setFont("SansSerif-30");
- add(label);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement