Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// ==============================================================================================
- /// Author: Eric Ho
- /// Date: Jan 24, 2015
- /// Class: CSE142
- /// TA: Christina Polyukh
- /// Assignment: #3B
- /// Description: This class will draw an cafe wall illusion, exactly as in the prompt.
- /// ==============================================================================================
- import java.awt.*;
- public class CafeWall
- {
- public static final int MORTAR_HEIGHT = 2; // The distance, in pixels, separating the rows of a grid.
- // Represents the mortar glueing bricks together in a real wall.
- public static void main(String[] args)
- {
- //-- Set up the panel to draw on
- DrawingPanel panel = new DrawingPanel(650, 400);
- Graphics pen = panel.getGraphics();
- panel.setBackground(Color.GRAY);
- //-- Draw the 2 row of boxes, as in the prompt
- drawRowOfBoxes( 0, 0, 4, 20, pen);
- drawRowOfBoxes( 50, 70, 5, 30, pen);
- //-- Draw the 4 grid of boxes, as in the prompt
- drawGridOfBoxes( 10, 150, 4, 25, 0, pen);
- drawGridOfBoxes( 250, 200, 3, 25, 10, pen);
- drawGridOfBoxes( 425, 180, 5, 20, 10, pen);
- drawGridOfBoxes( 400, 20, 2, 35, 35, pen);
- }
- //
- // Method: Draws a grid of boxes, using drawRowOfBoxes, with mortar between each row.
- // Parameters:
- // int x, y - starting coordinates; the top left corner of the grid
- // int pairs - number of pairs of boxes in each row. Also determines the number of rows.
- // int size - size of each box, in pixels
- // int offset - the horizontal offset between alternating rows, in pixels
- // Graphics pen - graphics to draw with
- //
- public static void drawGridOfBoxes(int x, int y, int pairs, int size, int offset, Graphics pen)
- {
- for (int row = 0; row < (2 * pairs); row ++) // Draws (2 * pairs) rows of boxes
- {
- int rowX = x + (row % 2) * offset; // Starting X for this row. Adds offset to even numbered rows.
- int rowY = y + row * (size + MORTAR_HEIGHT); // Starting Y for this row. Adds height to leave mortar between rows.
- drawRowOfBoxes(rowX, rowY, pairs, size, pen);
- }
- }
- //
- // Method: Draws a row of boxes, using drawPairOfBoxes.
- // Parameters:
- // int x, y - starting coordinates; the top left corner of the row
- // int pairs - number of pairs of boxes in the row
- // int size - size of each box, in pixels
- // Graphics pen - graphics to draw with
- //
- public static void drawRowOfBoxes(int x, int y, int pairs, int size, Graphics pen)
- {
- for (int loop = 0; loop < pairs; loop ++) // Draws (pairs) pairs of boxes
- {
- int pairX = x + (2 * loop * size); // Starting X coordinate for the pair of boxes.
- drawPairOfBoxes(pairX, y, size, pen);
- }
- }
- //
- // Method: Draws a pair of boxes, one black with a blue X across it,
- // one white of the same size directly to the right of it.
- // Parameter:
- // int x, y - starting coordinates; the top left corner of the black box
- // int size - size of each box, in pixels
- // Graphics pen - graphics to draw with.
- //
- public static void drawPairOfBoxes(int x, int y, int size, Graphics pen)
- {
- //-- Draw a black box
- pen.setColor(Color.BLACK);
- pen.fillRect(x, y, size, size);
- //-- Draw a blue X over the black box
- pen.setColor(Color.BLUE);
- pen.drawLine(x, y, x + size, y + size);
- pen.drawLine(x, y + size, x + size, y);
- //-- Draw a white box
- pen.setColor(Color.WHITE);
- pen.fillRect(x + size, y, size, size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement