document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.List;
  4. import java.util.*;
  5. /**
  6. * Create Canvas
  7. *
  8. * @author (Hendra Ramadani 05111740000055)
  9. *
  10. * @version (0.0001)
  11. */
  12. public class Canvas
  13. {
  14. // Note: The implementation of this class (specifically the handling of
  15. // shape identity and colors) is slightly more complex than necessary. This
  16. // is done on purpose to keep the interface and instance fields of the
  17. // shape objects in this project clean and simple for educational purposes.
  18. private static Canvas canvasSingleton;
  19. /**
  20. * Factory method to get the canvas singleton object.
  21. */
  22. public static Canvas getCanvas()
  23. {
  24. if(canvasSingleton == null) {
  25. canvasSingleton = new Canvas("BlueJ Shapes Demo", 1000, 800, Color.white);
  26. }
  27. canvasSingleton.setVisible(true);
  28. return canvasSingleton;
  29. }
  30. // ----- instance part -----
  31. private JFrame frame;
  32. private CanvasPane canvas;
  33. private Graphics2D graphic;
  34. private Color backgroundColour;
  35. private Image canvasImage;
  36. private List objects;
  37. private HashMap shapes;
  38. /**
  39. * Create a Canvas.
  40. * @param title title to appear in Canvas Frame
  41. * @param width the desired width for the canvas
  42. * @param height the desired height for the canvas
  43. * @param bgClour the desired background colour of the canvas
  44. */
  45. private Canvas(String title, int width, int height, Color bgColour)
  46. {
  47. frame = new JFrame();
  48. canvas = new CanvasPane();
  49. frame.setContentPane(canvas);
  50. frame.setTitle(title);
  51. canvas.setPreferredSize(new Dimension(width, height));
  52. backgroundColour = bgColour;
  53. frame.pack();
  54. objects = new ArrayList();
  55. shapes = new HashMap();
  56. }
  57. /**
  58. * Set the canvas visibility and brings canvas to the front of screen
  59. * when made visible. This method can also be used to bring an already
  60. * visible canvas to the front of other windows.
  61. * @param visible boolean value representing the desired visibility of
  62. * the canvas (true or false)
  63. */
  64. public void setVisible(boolean visible)
  65. {
  66. if(graphic == null) {
  67. // first time: instantiate the offscreen image and fill it with
  68. // the background colour
  69. Dimension size = canvas.getSize();
  70. canvasImage = canvas.createImage(size.width, size.height);
  71. graphic = (Graphics2D)canvasImage.getGraphics();
  72. graphic.setColor(backgroundColour);
  73. graphic.fillRect(0, 0, size.width, size.height);
  74. graphic.setColor(Color.black);
  75. }
  76. frame.setVisible(visible);
  77. }
  78. /**
  79. * Draw a given shape onto the canvas.
  80. * @param referenceObject an object to define identity for this shape
  81. * @param color the color of the shape
  82. * @param shape the shape object to be drawn on the canvas
  83. */
  84. // Note: this is a slightly backwards way of maintaining the shape
  85. // objects. It is carefully designed to keep the visible shape interfaces
  86. // in this project clean and simple for educational purposes.
  87. public void draw(Object referenceObject, String color, Shape shape)
  88. {
  89. objects.remove(referenceObject); // just in case it was already there
  90. objects.add(referenceObject); // add at the end
  91. shapes.put(referenceObject, new ShapeDescription(shape, color));
  92. redraw();
  93. }
  94. /**
  95. * Erase a given shape's from the screen.
  96. * @param referenceObject the shape object to be erased
  97. */
  98. public void erase(Object referenceObject)
  99. {
  100. objects.remove(referenceObject); // just in case it was already there
  101. shapes.remove(referenceObject);
  102. redraw();
  103. }
  104. /**
  105. * Set the foreground colour of the Canvas.
  106. * @param newColour the new colour for the foreground of the Canvas
  107. */
  108. public void setForegroundColor(String colorString)
  109. {
  110. if(colorString.equals("red"))
  111. graphic.setColor(Color.red);
  112. else if(colorString.equals("black"))
  113. graphic.setColor(Color.black);
  114. else if(colorString.equals("cyan"))
  115. graphic.setColor(Color.cyan);
  116. else if(colorString.equals("blue"))
  117. graphic.setColor(Color.blue);
  118. else if(colorString.equals("yellow"))
  119. graphic.setColor(Color.yellow);
  120. else if(colorString.equals("green"))
  121. graphic.setColor(Color.green);
  122. else if(colorString.equals("magenta"))
  123. graphic.setColor(Color.magenta);
  124. else if(colorString.equals("white"))
  125. graphic.setColor(Color.white);
  126. else if(colorString.equals("light brown"))
  127. graphic.setColor(new Color(153,102,0));
  128. else if(colorString.equals("brown"))
  129. graphic.setColor(new Color(102,51,0));
  130. else if(colorString.equals("grey"))
  131. graphic.setColor(new Color(190,190,190));
  132. else if(colorString.equals("light blue"))
  133. graphic.setColor(new Color(0,191,255));
  134. else
  135. graphic.setColor(Color.black);
  136. }
  137. /**
  138. * Wait for a specified number of milliseconds before finishing.
  139. * This provides an easy way to specify a small delay which can be
  140. * used when producing animations.
  141. * @param milliseconds the number
  142. */
  143. public void wait(int milliseconds)
  144. {
  145. try
  146. {
  147. Thread.sleep(milliseconds);
  148. }
  149. catch (Exception e)
  150. {
  151. // ignoring exception at the moment
  152. }
  153. }
  154. /**
  155. * Redraw ell shapes currently on the Canvas.
  156. */
  157. private void redraw()
  158. {
  159. erase();
  160. for(Iterator i=objects.iterator(); i.hasNext(); ) {
  161. ((ShapeDescription)shapes.get(i.next())).draw(graphic);
  162. }
  163. canvas.repaint();
  164. }
  165. /**
  166. * Erase the whole canvas. (Does not repaint.)
  167. */
  168. private void erase()
  169. {
  170. Color original = graphic.getColor();
  171. graphic.setColor(backgroundColour);
  172. Dimension size = canvas.getSize();
  173. graphic.fill(new Rectangle(0, 0, size.width, size.height));
  174. graphic.setColor(original);
  175. }
  176. /************************************************************************
  177. * Inner class CanvasPane - the actual canvas component contained in the
  178. * Canvas frame. This is essentially a JPanel with added capability to
  179. * refresh the image drawn on it.
  180. */
  181. private class CanvasPane extends JPanel
  182. {
  183. public void paint(Graphics g)
  184. {
  185. g.drawImage(canvasImage, 0, 0, null);
  186. }
  187. }
  188. /************************************************************************
  189. * Inner class CanvasPane - the actual canvas component contained in the
  190. * Canvas frame. This is essentially a JPanel with added capability to
  191. * refresh the image drawn on it.
  192. */
  193. private class ShapeDescription
  194. {
  195. private Shape shape;
  196. private String colorString;
  197. public ShapeDescription(Shape shape, String color)
  198. {
  199. this.shape = shape;
  200. colorString = color;
  201. }
  202. public void draw(Graphics2D graphic)
  203. {
  204. setForegroundColor(colorString);
  205. graphic.fill(shape);
  206. }
  207. }
  208. }
');