Advertisement
Guest User

Untitled

a guest
Jan 24th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. /// ==============================================================================================
  2. /// Author: Eric Ho
  3. /// Date: Jan 24, 2015
  4. /// Class: CSE142
  5. /// TA: Christina Polyukh
  6. /// Assignment: #3B
  7. /// Description: This class will draw an cafe wall illusion, exactly as in the prompt.
  8. /// ==============================================================================================
  9.  
  10. import java.awt.*;
  11.  
  12. public class CafeWall
  13. {
  14. public static final int MORTAR_HEIGHT = 2; // The distance, in pixels, separating the rows of a grid.
  15. // Represents the mortar glueing bricks together in a real wall.
  16.  
  17. public static void main(String[] args)
  18. {
  19. //-- Set up the panel to draw on
  20. DrawingPanel panel = new DrawingPanel(650, 400);
  21. Graphics pen = panel.getGraphics();
  22. panel.setBackground(Color.GRAY);
  23.  
  24. //-- Draw the 2 row of boxes, as in the prompt
  25. drawRowOfBoxes( 0, 0, 4, 20, pen);
  26. drawRowOfBoxes( 50, 70, 5, 30, pen);
  27.  
  28. //-- Draw the 4 grid of boxes, as in the prompt
  29. drawGridOfBoxes( 10, 150, 4, 25, 0, pen);
  30. drawGridOfBoxes( 250, 200, 3, 25, 10, pen);
  31. drawGridOfBoxes( 425, 180, 5, 20, 10, pen);
  32. drawGridOfBoxes( 400, 20, 2, 35, 35, pen);
  33. }
  34.  
  35.  
  36.  
  37. //
  38. // Method: Draws a grid of boxes, using drawRowOfBoxes, with mortar between each row.
  39. // Parameters:
  40. // int x, y - starting coordinates; the top left corner of the grid
  41. // int pairs - number of pairs of boxes in each row. Also determines the number of rows.
  42. // int size - size of each box, in pixels
  43. // int offset - the horizontal offset between alternating rows, in pixels
  44. // Graphics pen - graphics to draw with
  45. //
  46. public static void drawGridOfBoxes(int x, int y, int pairs, int size, int offset, Graphics pen)
  47. {
  48. for (int row = 0; row < (2 * pairs); row ++) // Draws (2 * pairs) rows of boxes
  49. {
  50. int rowX = x + (row % 2) * offset; // Starting X for this row. Adds offset to even numbered rows.
  51. int rowY = y + row * (size + MORTAR_HEIGHT); // Starting Y for this row. Adds height to leave mortar between rows.
  52.  
  53. drawRowOfBoxes(rowX, rowY, pairs, size, pen);
  54. }
  55. }
  56.  
  57.  
  58.  
  59. //
  60. // Method: Draws a row of boxes, using drawPairOfBoxes.
  61. // Parameters:
  62. // int x, y - starting coordinates; the top left corner of the row
  63. // int pairs - number of pairs of boxes in the row
  64. // int size - size of each box, in pixels
  65. // Graphics pen - graphics to draw with
  66. //
  67. public static void drawRowOfBoxes(int x, int y, int pairs, int size, Graphics pen)
  68. {
  69. for (int loop = 0; loop < pairs; loop ++) // Draws (pairs) pairs of boxes
  70. {
  71. int pairX = x + (2 * loop * size); // Starting X coordinate for the pair of boxes.
  72.  
  73. drawPairOfBoxes(pairX, y, size, pen);
  74. }
  75. }
  76.  
  77.  
  78.  
  79. //
  80. // Method: Draws a pair of boxes, one black with a blue X across it,
  81. // one white of the same size directly to the right of it.
  82. // Parameter:
  83. // int x, y - starting coordinates; the top left corner of the black box
  84. // int size - size of each box, in pixels
  85. // Graphics pen - graphics to draw with.
  86. //
  87. public static void drawPairOfBoxes(int x, int y, int size, Graphics pen)
  88. {
  89. //-- Draw a black box
  90. pen.setColor(Color.BLACK);
  91. pen.fillRect(x, y, size, size);
  92.  
  93. //-- Draw a blue X over the black box
  94. pen.setColor(Color.BLUE);
  95. pen.drawLine(x, y, x + size, y + size);
  96. pen.drawLine(x, y + size, x + size, y);
  97.  
  98. //-- Draw a white box
  99. pen.setColor(Color.WHITE);
  100. pen.fillRect(x + size, y, size, size);
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement