Advertisement
Guest User

Untitled

a guest
May 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.geom.*;
  4. import java.util.ArrayList;
  5. import javax.swing.*;
  6.  
  7. class Draw extends JFrame implements ActionListener, ItemListener {
  8.  
  9. static final ArrayList<MyShape> shapes = new ArrayList<>();
  10.  
  11. // Initial Frame size
  12. static final int FWIDTH = 600; // frame width
  13. static final int FHEIGHT = 450; // frame height
  14.  
  15. // Color choices
  16. static final String COLOR_NAMES[] = {"None", "Red", "Blue", "Green"};
  17. static final Color COLORS[] = {null, Color.red, Color.blue, Color.green};
  18.  
  19. // Button control
  20. JButton circle;
  21. JButton roundRec;
  22. JButton threeDRec;
  23.  
  24. // Color choice box
  25. JComboBox<String> colorChoice;
  26. JList<String> jList1;
  27. JScrollPane jScrollPane2;
  28.  
  29. // the canvas
  30. DrawCanvas canvas;
  31.  
  32. /**
  33. * Constructor
  34. */
  35. public
  36. Draw() {
  37. super("Java Draw");
  38. setLayout(new BorderLayout());
  39.  
  40. // create panel for controls
  41. JPanel topPanel = new JPanel(new GridLayout(2, 1));
  42. add(topPanel, BorderLayout.NORTH);
  43.  
  44. // create button control
  45. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  46. topPanel.add(buttonPanel);
  47.  
  48. circle = new JButton("Circle");
  49. buttonPanel.add(circle);
  50. roundRec = new JButton("Rounded Rectangle");
  51. buttonPanel.add(roundRec);
  52. threeDRec = new JButton("3D Rectangle");
  53. buttonPanel.add(threeDRec);
  54.  
  55. // add button listener
  56. circle.addActionListener(this);
  57. roundRec.addActionListener(this);
  58. threeDRec.addActionListener(this);
  59.  
  60. // create panel for color choices
  61. JPanel colorPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  62. topPanel.add(colorPanel);
  63. JLabel label = new JLabel("Filled Color:");
  64. colorPanel.add(label);
  65. colorChoice = new JComboBox();
  66.  
  67. for (int i = 0; i < COLOR_NAMES.length; i++) {
  68. colorChoice.addItem(COLOR_NAMES[i]);
  69. }
  70. colorPanel.add(colorChoice);
  71. colorChoice.addItemListener(this);
  72.  
  73. JPanel listPanel = getListPanel();
  74. add(listPanel, BorderLayout.WEST);
  75.  
  76. // create the canvas
  77. canvas = new DrawCanvas();
  78. add(canvas, BorderLayout.CENTER);
  79. } // end of constructor
  80.  
  81. /**
  82. * Implementing ActionListener
  83. */
  84. @Override public void actionPerformed(ActionEvent event) {
  85. if (event.getSource() == circle) { // circle button
  86. canvas.setShape(DrawCanvas.CIRCLE);
  87. } else if (event.getSource() == roundRec) { // rounded rectangle button
  88. canvas.setShape(DrawCanvas.ROUNDED_RECTANGLE);
  89. } else if (event.getSource() == threeDRec) { // 3D rectangle button
  90. canvas.setShape(DrawCanvas.RECTANGLE_3D);
  91. } else if (event.getActionCommand().equals("Clear All")) {
  92. for (int i = 0; i < shapes.size(); i++) {
  93. shapes.remove(i);
  94. }
  95. updateShapeList();
  96. canvas.repaint();
  97. } else if (event.getActionCommand().equals("Remove Shape")) {
  98. String name = jList1.getSelectedValue();
  99. for (int i = 0; i < shapes.size(); i++) {
  100. if (shapes.get(i).toString().equals(name)) {
  101. shapes.remove(i);
  102. break;
  103. }
  104. }
  105. updateShapeList();
  106. canvas.repaint();
  107. }
  108. }
  109.  
  110. /**
  111. * Implementing ItemListener
  112. * @param event
  113. */
  114. @Override public void itemStateChanged(ItemEvent event) {
  115. Color color = COLORS[colorChoice.getSelectedIndex()];
  116. canvas.setFilledColor(color);
  117. }
  118.  
  119. /**
  120. * the main method
  121. */
  122. public
  123. static void main(String[] argv) {
  124. // Create a frame
  125. Draw frame = new Draw();
  126. frame.setSize(FWIDTH, FHEIGHT);
  127. frame.setLocation(150, 100);
  128.  
  129. // add window closing listener
  130. frame.addWindowListener(new WindowAdapter() {
  131. public void windowClosing(WindowEvent event) {
  132. System.exit(0);
  133. }
  134. });
  135.  
  136. // Show the frame
  137. frame.setVisible(true);
  138. }
  139.  
  140. private
  141. JPanel getListPanel() {
  142. JPanel p = new JPanel(new GridLayout(3, 1));
  143. p.setSize(100, 300);
  144. jScrollPane2 = new JScrollPane();
  145. jList1 = new JList<>();
  146.  
  147. updateShapeList();
  148. jScrollPane2.setViewportView(jList1);
  149. p.add(jScrollPane2);
  150.  
  151. JButton removeShape = new JButton("Remove Shape");
  152. removeShape.addActionListener(this);
  153. p.add(removeShape);
  154.  
  155. JButton Clearall = new JButton("Clear All");
  156. Clearall.addActionListener(this);
  157. p.add(Clearall);
  158. return p;
  159. }
  160.  
  161. /**
  162. * When ever a new item added or removed from the list
  163. * then we need to update the model of the list.
  164. */
  165. public
  166. void updateShapeList() {
  167. jList1.setModel(new javax.swing.AbstractListModel<String>() {
  168. String[] shapenames = getMenuNames();
  169. private
  170. String[] getMenuNames() {
  171. int i = 0;
  172. String[] names = new String[shapes.size() + 1];
  173. for (MyShape m : shapes) {
  174. names[i++] = m.toString();
  175. }
  176. return names;
  177. }
  178.  
  179. public
  180. int getSize() { return shapenames.length; }
  181. @Override public String getElementAt(int i) {
  182. return shapenames[i];
  183. }
  184. });
  185. }
  186.  
  187. class DrawCanvas extends Canvas implements MouseListener, MouseMotionListener {
  188. // Constants for shapes
  189. public
  190. static final int CIRCLE = 1;
  191. public
  192. static final int ROUNDED_RECTANGLE = 2;
  193. public
  194. static final int RECTANGLE_3D = 3;
  195.  
  196. // Coordinates of points to draw
  197. private
  198. int x1, y1, x2, y2;
  199.  
  200. // shape to draw
  201. private
  202. int shape = CIRCLE;
  203. private
  204. final int PAINT = 0; // do we need to draw current component
  205. private
  206. final int NOPAINT = 1; // we dont need to draw current componet
  207. private
  208. int paintOrRepaint = NOPAINT; // at the beginning we dont need to draw
  209. /**
  210. *Method to set the shape
  211. *@param shape
  212. */
  213. public
  214. void setShape(int shape) { this.shape = shape; }
  215.  
  216. // filled color
  217. private
  218. Color filledColor = null;
  219. /**
  220. *Method to set filled color
  221. *@param color
  222. */
  223. public
  224. void setFilledColor(Color color) { filledColor = color; }
  225.  
  226. /**
  227. *Constructor
  228. */
  229. public
  230. DrawCanvas() {
  231. addMouseListener(this);
  232. addMouseMotionListener(this);
  233. } // end of constructor
  234.  
  235. /**
  236. *painting the component
  237. */
  238. public
  239. void paint(Graphics g) {
  240.  
  241. // the drawing area
  242. int x, y, width, height;
  243.  
  244. // determine the upper-left corner of bounding rectangle
  245. x = Math.min(x1, x2);
  246. y = Math.min(y1, y2);
  247.  
  248. // determine the width and height of bounding rectangle
  249. width = Math.abs(x1 - x2);
  250. height = Math.abs(y1 - y2);
  251.  
  252. // current shape : by default it is a line after using a switch we update
  253. // this to
  254. // circle of rectangle
  255. Shape cur_shape = new Line2D.Double(x1, y1, x2, y2);
  256. switch (shape) {
  257. case ROUNDED_RECTANGLE:
  258. cur_shape = new RoundRectangle2D.Double(x, y, width, height, width / 4,
  259. height / 4);
  260. break;
  261. case CIRCLE:
  262. int diameter = Math.max(width, height);
  263. cur_shape = new Ellipse2D.Double(x, y, diameter, diameter);
  264. break;
  265. case RECTANGLE_3D:
  266. cur_shape = new Rectangle2D.Double(x, y, width, height);
  267. break;
  268. }
  269.  
  270. // if we need to paint this component
  271. // first add it to the shapes list in the Draw class
  272. if (paintOrRepaint == PAINT) {
  273. MyShape sh = new MyShape(cur_shape, filledColor, shape);
  274. Draw.shapes.add(sh);
  275. }
  276.  
  277. Graphics2D graphics = (Graphics2D)g;
  278. for (MyShape s : Draw.shapes) {
  279. if (s.fill != null)
  280. g.setColor(s.fill);
  281. else
  282. g.setColor(Color.BLACK);
  283. if (s.fill != null)
  284. graphics.fill(s.shape);
  285. else
  286. graphics.draw(s.shape);
  287. }
  288. updateShapeList(); // update the Jlist in the GUI
  289. setPaintOrRepaint(
  290. NOPAINT); // make the current componet not to draw next time
  291. }
  292.  
  293. /**
  294. *Implementing MouseListener
  295. *@param event
  296. */
  297. public
  298. void mousePressed(MouseEvent event) {
  299. x1 = event.getX();
  300. y1 = event.getY();
  301. }
  302.  
  303. public
  304. void mouseReleased(MouseEvent event) {
  305. x2 = event.getX();
  306. y2 = event.getY();
  307. // we have a new shape(componet) to draw
  308. // so set the paintOrRepaint to PAINT
  309. // so that repaint method can draw this
  310. // shape on the canvas
  311. setPaintOrRepaint(PAINT);
  312. repaint();
  313. }
  314.  
  315. public
  316. void mouseClicked(MouseEvent e) {}
  317. public
  318. void mouseEntered(MouseEvent e) {}
  319. public
  320. void mouseExited(MouseEvent e) {}
  321.  
  322. /**
  323. *Implementing MouseMotionListener
  324. *@param event
  325. */
  326. @Override public void mouseDragged(MouseEvent event) {
  327. x2 = event.getX();
  328. y2 = event.getY();
  329. }
  330.  
  331. public
  332. void mouseMoved(MouseEvent e) {}
  333.  
  334. private
  335. void setPaintOrRepaint(int option) { paintOrRepaint = option; }
  336. }
  337. }
  338.  
  339. // class shape will store the details about the
  340. // shape that we want to draw and do we need to
  341. // fill it or not
  342. class MyShape {
  343. Shape shape;
  344. Color fill;
  345. String shapeName;
  346.  
  347. MyShape(Shape shape, Color filledColor, int shapeName) {
  348. this.shape = shape;
  349. this.fill = filledColor;
  350. this.shapeName = getName(shapeName);
  351. }
  352.  
  353. private
  354. String getName(int shapeName) {
  355. switch (shapeName) {
  356. case 1:
  357. return "Circle";
  358. case 2:
  359. return "Round Rectangle";
  360. case 3:
  361. return "Rectangle 3D";
  362. default:
  363. return "NoshapeName";
  364. }
  365. }
  366. public
  367. String toString() {
  368. return shapeName + "(" + shape.getBounds().x + "," + shape.getBounds().y +
  369. ")";
  370. }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement