creepyToast

Untitled

Nov 19th, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. import java.awt.Color;
  2.  
  3. import acm.graphics.GLabel;
  4. import acm.graphics.GRect;
  5. import acm.program.GraphicsProgram;
  6.  
  7. /**
  8.  *
  9.  * Draws a chessboard with gray and white Rectangles and even the row and column
  10.  * designators.
  11.  *
  12.  */
  13. public class Chessboard extends GraphicsProgram {
  14.  
  15.     private static final String[] DESIGNATOR_NUM = { "0", "8", "7", "6", "5", "4", "3", "2", "1" };
  16.     private static final String[] DESIGNATOR_LET = { "0", "A", "B", "C", "D", "E", "F", "G", "H" };
  17.  
  18.     public static final int FIELD_SIZE = 100;
  19.  
  20.     @Override
  21.     public void run() {
  22.         setSize(1000, 1000);
  23.         drawChessboard(FIELD_SIZE);
  24.         super.run();
  25.     }
  26.  
  27.     /**
  28.      *
  29.      * Draws the chestboard with a size that can be set by the user as well as the
  30.      * Designators to the sides of the playfield.
  31.      *
  32.      * @param size size of the rectangles that should be drawn.
  33.      */
  34.     public void drawChessboard(int size) {
  35.         // Draw the Chessfield with Changing colors
  36.         for (int x = 1; x <= 8; x++) {
  37.             for (int y = 1; y <= 8; y++) {
  38.                 if (y % 2 == 0 && x % 2 != 0 || y % 2 != 0 && x % 2 == 0) {
  39.                     GRect rect = new GRect(x * size, y * size, size, size);
  40.                     rect.setFilled(true);
  41.                     rect.setFillColor(Color.GRAY);
  42.                     add(rect);
  43.                 }
  44.             }
  45.         }
  46.         // Draw Designators A to H at the top row
  47.         for (int i = 1; i <= 8; i++) {
  48.             GLabel label = new GLabel(DESIGNATOR_LET[i], i * FIELD_SIZE + FIELD_SIZE / 2, 75);
  49.             label.setFont("SansSerif-30");
  50.             add(label);
  51.         }
  52.         // Draw Designators A to H at the bottom row
  53.         for (int i = 1; i <= 8; i++) {
  54.             GLabel label = new GLabel(DESIGNATOR_LET[i], i * FIELD_SIZE + FIELD_SIZE / 2, 950);
  55.             label.setFont("SansSerif-30");
  56.             add(label);
  57.         }
  58.         // Draw Designators 1 to 8 on the left side
  59.         for (int i = 1; i <= 8; i++) {
  60.             GLabel label = new GLabel(DESIGNATOR_NUM[i], 50, i * FIELD_SIZE + FIELD_SIZE / 2);
  61.             label.setFont("SansSerif-30");
  62.             add(label);
  63.         }
  64.         // Draw Designators 1 to 8 on the right side
  65.         for (int i = 1; i <= 8; i++) {
  66.             GLabel label = new GLabel(DESIGNATOR_NUM[i], 925, i * FIELD_SIZE + FIELD_SIZE / 2);
  67.             label.setFont("SansSerif-30");
  68.             add(label);
  69.         }
  70.     }
  71.  
  72. }
Add Comment
Please, Sign In to add comment