Advertisement
ahmad_zizo

Untitled

Dec 8th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.87 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package paint;
  7.  
  8. import javax.swing.*;
  9.  
  10. import java.awt.event.*;
  11. import java.awt.*;
  12. import java.awt.geom.*;
  13. import java.util.*;
  14.  
  15. @SuppressWarnings("serial")
  16. /**
  17. *
  18. * @author ramytamer
  19. */
  20. public final class Paint extends JFrame {
  21.  
  22. JButton lineBtn, circleBtn, ellipseBtn, rectangleBtn, squareBtn, triangleBtn, colorBtn;
  23.  
  24. public JButton getSquareBtn() {
  25. return squareBtn;
  26. }
  27.  
  28. public void setSquareBtn(JButton squareBtn) {
  29. this.squareBtn = squareBtn;
  30. }
  31.  
  32. public JButton getLineBtn() {
  33. return lineBtn;
  34. }
  35.  
  36. public void setLineBtn(JButton lineBtn) {
  37. this.lineBtn = lineBtn;
  38. }
  39.  
  40. public JButton getCircleBtn() {
  41. return circleBtn;
  42. }
  43.  
  44. public void setCircleBtn(JButton circleBtn) {
  45. this.circleBtn = circleBtn;
  46. }
  47.  
  48. public JButton getEllipseBtn() {
  49. return ellipseBtn;
  50. }
  51.  
  52. public void setEllipseBtn(JButton ellipseBtn) {
  53. this.ellipseBtn = ellipseBtn;
  54. }
  55.  
  56. public JButton getRectangleBtn() {
  57. return rectangleBtn;
  58. }
  59.  
  60. public void setRectangleBtn(JButton rectangleBtn) {
  61. this.rectangleBtn = rectangleBtn;
  62. }
  63.  
  64. public JButton getTriangleBtn() {
  65. return triangleBtn;
  66. }
  67.  
  68. public void setTriangleBtn(JButton triangleBtn) {
  69. this.triangleBtn = triangleBtn;
  70. }
  71.  
  72. public JButton getColorBtn() {
  73. return colorBtn;
  74. }
  75.  
  76. public void setColorBtn(JButton colorBtn) {
  77. this.colorBtn = colorBtn;
  78. }
  79.  
  80. public Graphics2D getGraphSettings() {
  81. return graphSettings;
  82. }
  83.  
  84. public void setGraphSettings(Graphics2D graphSettings) {
  85. this.graphSettings = graphSettings;
  86. }
  87.  
  88. public int getDrawAction() {
  89. return drawAction;
  90. }
  91.  
  92. public void setDrawAction(int drawAction) {
  93. this.drawAction = drawAction;
  94. }
  95.  
  96. public Color getColour() {
  97. return colour;
  98. }
  99.  
  100. public void setColour(Color colour) {
  101. this.colour = colour;
  102. }
  103.  
  104. Graphics2D graphSettings;
  105.  
  106. int drawAction = 1;
  107.  
  108. Color colour = Color.BLACK;
  109.  
  110. /**
  111. * @param args the command line arguments
  112. */
  113. public static void main(String[] args) {
  114. Paint paint = new Paint();
  115. }
  116.  
  117. public Paint() {
  118.  
  119. this.setSize(800, 670);
  120. this.setResizable(false);
  121. this.setTitle("The awesome paint program");
  122. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  123.  
  124. JPanel btnPanel = new JPanel();
  125. JPanel colorBtnPanel = new JPanel();
  126.  
  127. Box btnsBox = Box.createVerticalBox();
  128. Box colorBtnBox = Box.createVerticalBox();
  129.  
  130. setLineBtn(addBtn("./src/line.png", 1, false));
  131. setCircleBtn(addBtn("./src/circle.png", 2, false));
  132. setEllipseBtn(addBtn("./src/ellipse.png", 3, false));
  133. setRectangleBtn(addBtn("./src/rectangle.png", 4, false));
  134. setSquareBtn(addBtn("./src/square.png", 5, false));
  135. setTriangleBtn(addBtn("./src/triangle.png", 6, false));
  136.  
  137. setColorBtn(addBtn("./src/color.png", 7, true));
  138.  
  139. btnsBox.add(getLineBtn());
  140. btnsBox.add(getCircleBtn());
  141. btnsBox.add(getEllipseBtn());
  142. btnsBox.add(getRectangleBtn());
  143. btnsBox.add(getSquareBtn());
  144. btnsBox.add(getTriangleBtn());
  145. colorBtnBox.add(getColorBtn());
  146.  
  147. btnPanel.add(btnsBox);
  148. colorBtnPanel.add(colorBtnBox);
  149.  
  150. this.add(btnPanel, BorderLayout.WEST);
  151. this.add(colorBtnPanel, BorderLayout.EAST);
  152. this.add(new DrawingBoard(), BorderLayout.CENTER);
  153.  
  154. this.setVisible(true);
  155. }
  156.  
  157. public JButton addBtn(String path, int dAction, boolean isColor) {
  158. JButton btn = new JButton();
  159. Icon btnIcon = new ImageIcon(path);
  160. btn.setIcon(btnIcon);
  161.  
  162. btn.addActionListener(new ActionListener() {
  163.  
  164. @Override
  165. public void actionPerformed(ActionEvent e) {
  166. if (isColor) {
  167. setColour(JColorChooser.showDialog(null, "Pick a Color", Color.BLACK));
  168. } else {
  169. setDrawAction(dAction);
  170. System.out.println(getDrawAction());
  171. }
  172.  
  173. }
  174. });
  175.  
  176. return btn;
  177. }
  178.  
  179. private class DrawingBoard extends JComponent {
  180.  
  181. ArrayList<Shape> shapes = new ArrayList<>();
  182. ArrayList<Color> shapeColor = new ArrayList<>();
  183.  
  184. Point startPoint, endPoint;
  185.  
  186. public Point getStartPoint() {
  187. return startPoint;
  188. }
  189.  
  190. public void setStartPoint(Point startPoint) {
  191. this.startPoint = startPoint;
  192. }
  193.  
  194. public Point getEndPoint() {
  195. return endPoint;
  196. }
  197.  
  198. public void setEndPoint(Point endPoint) {
  199. this.endPoint = endPoint;
  200. }
  201.  
  202. public DrawingBoard() {
  203. this.addMouseListener(new MouseAdapter() {
  204.  
  205. @Override
  206. public void mousePressed(MouseEvent e) {
  207. // When the mouse is pressed get x & y position
  208.  
  209. setStartPoint(new Point(e.getX(), e.getY()));
  210. System.out.println("Setting start & end point: " + e.getX() + "," + e.getY());
  211. setEndPoint(getStartPoint());
  212. repaint();
  213. }
  214.  
  215. @Override
  216. public void mouseReleased(MouseEvent e) {
  217.  
  218. // Create a shape using the starting x & y
  219. // and finishing x & y positions
  220. Shape theShape = null;
  221.  
  222. if (getDrawAction() == 1) {
  223. // Line
  224. System.out.println("(x1,y1)->(" + getStartPoint().x + "," + getStartPoint().y + ")");
  225. System.out.println("(x2,y2)->(" + e.getX() + "," + e.getY() + ")");
  226. theShape = drawLine(getStartPoint().x, getStartPoint().y, e.getX(), e.getY());
  227. } else if (getDrawAction() == 2) {
  228. // Circle
  229. // theShape = drawCircle(getStartPoint().x, getEndPoint().y, e.getX(), e.getY());
  230. } else if (getDrawAction() == 3) {
  231. // Ellipse
  232. // theShape = drawEllipse(getStartPoint().x, getEndPoint().y, e.getX(), e.getY());
  233. } else if (getDrawAction() == 4) {
  234. // Rectangle
  235. theShape = drawRectangle(getStartPoint().x, getStartPoint().y, e.getX(), e.getY());
  236. } else if (getDrawAction() == 5) {
  237. // Square
  238. // theShape = drawSquare(getStartPoint().x, getEndPoint().y, e.getX(), e.getY());
  239. } else if (getDrawAction() == 6) {
  240. // Triangle
  241. // theShape = drawTriangle(getStartPoint().x, getEndPoint().y, e.getX(), e.getY());
  242. }
  243.  
  244. // Add shapes, fills and colors to there ArrayLists
  245. shapes.add(theShape);
  246. System.out.println("Adding Shape");
  247. System.out.println(theShape);
  248. shapeColor.add(getColour());
  249.  
  250. setStartPoint(null);
  251. setEndPoint(null);
  252.  
  253. repaint();
  254.  
  255. }
  256. });
  257.  
  258. this.addMouseMotionListener(new MouseMotionAdapter() {
  259.  
  260. @Override
  261. public void mouseDragged(MouseEvent e) {
  262. // Get the final x & y position after the mouse is dragged
  263. setEndPoint(new Point(e.getX(), e.getY()));
  264. System.out.println("Setting end point: " + e.getX() + "," + e.getY());
  265. repaint();
  266. }
  267. });
  268. }
  269.  
  270. private Line2D.Float drawLine(int x1, int y1, int x2, int y2) {
  271. return new Line2D.Float(x1, y1, x2, y2);
  272. }
  273.  
  274. private Ellipse2D.Float drawEllipse(
  275. int x1, int y1, int x2, int y2) {
  276. int x = Math.min(x1, x2);
  277. int y = Math.min(y1, y2);
  278. int width = Math.abs(x1 - x2);
  279. int height = Math.abs(y1 - y2);
  280.  
  281. return new Ellipse2D.Float(
  282. x, y, width, height);
  283. }
  284.  
  285. private Ellipse2D.Float drawCircle(
  286. int x1, int y1, int x2, int y2) {
  287. int x = Math.min(x1, x2);
  288. int y = Math.min(y1, y2);
  289. int width = Math.abs(x1 - x2);
  290. int height = width;
  291.  
  292. return new Ellipse2D.Float(
  293. x, y, width, height);
  294. }
  295.  
  296.  
  297.  
  298. private Rectangle2D.Float drawRectangle(int x1, int y1, int x2, int y2) {
  299.  
  300. int x = Math.min(x1, x2);
  301. int y = Math.min(y1, y2);
  302.  
  303. int width = Math.abs(x1 - x2);
  304. int height = Math.abs(y1 - y2);
  305.  
  306. return new Rectangle2D.Float(x, y, width, height);
  307. }
  308.  
  309. private Rectangle2D.Float drawSquare(int x1, int y1, int x2, int y2) {
  310.  
  311. int x = Math.min(x1, x2);
  312. int y = Math.min(y1, y2);
  313.  
  314. int width = Math.abs(x1 - x2);
  315. int height = width;
  316.  
  317. return new Rectangle2D.Float(x, y, width, height);
  318. }
  319.  
  320.  
  321.  
  322. @Override
  323. public void paint(Graphics graphics) {
  324. setGraphSettings((Graphics2D) graphics);
  325.  
  326. getGraphSettings().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  327. getGraphSettings().setStroke(new BasicStroke(4));
  328.  
  329. Iterator<Color> colourCounter = shapeColor.iterator();
  330.  
  331. for (Shape shape : shapes) {
  332. // actually i don't know what is 0.40f :D
  333. getGraphSettings().setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
  334.  
  335. getGraphSettings().draw(shape);
  336. getGraphSettings().setPaint(colourCounter.next());
  337. getGraphSettings().fill(shape);
  338.  
  339. }
  340.  
  341. Shape theShape = null;
  342.  
  343. if (getStartPoint() != null && getEndPoint() != null) {
  344. // Color.LIGHT_GRAY
  345. // getGraphSettings().setPaint(Color.BLUE);
  346.  
  347. if (getDrawAction() == 1) {
  348. // Line
  349. theShape = drawLine(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
  350. } else if (getDrawAction() == 2) {
  351. // Circle
  352.  
  353. theShape = drawCircle(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
  354. } else if (getDrawAction() == 3) {
  355. // Ellipse
  356. theShape = drawEllipse(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
  357. } else if (getDrawAction() == 4) {
  358. // Rectangle
  359. theShape = drawRectangle(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
  360. } else if (getDrawAction() == 5) {
  361. // Square
  362. theShape = drawSquare(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
  363. } else if (getDrawAction() == 6) {
  364. // Triangle
  365. // theShape = new Triangle().draw(startpoint.x,startpoint.y,e.getX(),e.getY());
  366. }
  367. }
  368.  
  369. if (theShape != null) {
  370. graphSettings.draw(theShape);
  371. }
  372.  
  373. }
  374. }
  375.  
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement