Crenox

SketchEvent

Jun 13th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. // By: Sammy Samkough
  2.  
  3. package com.samkough.sketch.main;
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8.  
  9. public class SketchEvent implements MouseMotionListener, ActionListener
  10. {
  11. private static final int WIDTH = 600;
  12. private static final int HEIGHT = 600;
  13.  
  14. JFrame frame;
  15. JPanel pane, top, canvas, bottom, right;
  16. JButton red, blue, green, pink, eraser, clear, line, oval;
  17. Color c;
  18. Font f;
  19.  
  20. private int oldX, oldY, x, y;
  21.  
  22. public SketchEvent()
  23. {
  24. // defining the objects
  25. frame = new JFrame("Sketch");
  26. pane = new JPanel();
  27. top = new JPanel();
  28. canvas = new JPanel();
  29. bottom = new JPanel();
  30. right = new JPanel();
  31. c = Color.RED;
  32. f = new Font("Arial", Font.BOLD, 10);
  33.  
  34. // setting the layout of the JPanels
  35. frame.setContentPane(pane);
  36. pane.setLayout(new BorderLayout());
  37. top.setLayout(new FlowLayout());
  38. bottom.setLayout(new FlowLayout());
  39. right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
  40.  
  41. // setting the background for the drawing JPanel
  42. canvas.setBackground(Color.WHITE);
  43.  
  44. // setting the buttons
  45. red = new JButton("R");
  46. blue = new JButton("B");
  47. green = new JButton("G");
  48. pink = new JButton("P");
  49. eraser = new JButton("E");
  50. clear = new JButton("C");
  51. line = new JButton("L");
  52. oval = new JButton("O");
  53.  
  54. // setting the size of the buttons
  55. red.setPreferredSize(new Dimension(50, 30));
  56. blue.setPreferredSize(new Dimension(50, 30));
  57. green.setPreferredSize(new Dimension(50, 30));
  58. pink.setPreferredSize(new Dimension(50, 30));
  59. eraser.setPreferredSize(new Dimension(50, 30));
  60. clear.setPreferredSize(new Dimension(50, 30));
  61. line.setPreferredSize(new Dimension(50, 50));
  62. oval.setPreferredSize(new Dimension(50, 50));
  63.  
  64. // setting the font of the buttons
  65. red.setFont(f);
  66. blue.setFont(f);
  67. green.setFont(f);
  68. pink.setFont(f);
  69. eraser.setFont(f);
  70. clear.setFont(f);
  71. line.setFont(f);
  72. oval.setFont(f);
  73.  
  74. // setting the region of the JPanels
  75. pane.add(top, BorderLayout.NORTH);
  76. pane.add(canvas, BorderLayout.CENTER);
  77. pane.add(bottom, BorderLayout.SOUTH);
  78. pane.add(right, BorderLayout.EAST);
  79.  
  80. // adding all of the components
  81. top.add(red);
  82. top.add(blue);
  83. top.add(green);
  84. top.add(pink);
  85. bottom.add(eraser);
  86. bottom.add(clear);
  87. right.add(line);
  88. right.add(oval);
  89.  
  90. // setting the addActionListeners
  91. red.addActionListener(this);
  92. blue.addActionListener(this);
  93. green.addActionListener(this);
  94. pink.addActionListener(this);
  95. eraser.addActionListener(this);
  96. clear.addActionListener(this);
  97.  
  98. // setting the addMouseMotionlisteners
  99. canvas.addMouseMotionListener(this);
  100. // line.addMouseMotionListener(this);
  101. // oval.addMouseMotionListener(this);
  102.  
  103. // setting up the frame
  104. frame.setSize(WIDTH, HEIGHT);
  105. frame.setLocationRelativeTo(null);
  106. frame.setResizable(false);
  107. // frame.setTitle("Sammy");
  108. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  109. frame.setVisible(true);
  110. }
  111.  
  112. public int getX()
  113. {
  114. return x;
  115. }
  116.  
  117. public int getY()
  118. {
  119. return y;
  120. }
  121.  
  122. public void mouseMoved(MouseEvent e)
  123. {
  124. oldX = e.getX();
  125. oldY = e.getY();
  126. }
  127.  
  128. public void mouseDragged(MouseEvent e)
  129. {
  130. Graphics g = canvas.getGraphics();
  131.  
  132. g.setColor(c);
  133. g.drawLine(oldX, oldY, e.getX(), e.getY());
  134.  
  135. /*
  136. if (e.getSource() == line)
  137. {
  138. g.drawLine(oldX, oldY, e.getX(), e.getY());
  139. }
  140.  
  141.  
  142. if (e.getSource() == oval)
  143. {
  144. g.drawOval(oldX, oldY, e.getX(), e.getY());
  145. }
  146. */
  147. }
  148.  
  149. public void actionPerformed(ActionEvent e)
  150. {
  151. Graphics g = canvas.getGraphics();
  152.  
  153. if (e.getSource() == red)
  154. {
  155. c = Color.RED;
  156. }
  157.  
  158. if (e.getSource() == blue)
  159. {
  160. c = Color.BLUE;
  161. }
  162.  
  163. if (e.getSource() == green)
  164. {
  165. c = Color.GREEN;
  166. }
  167.  
  168. if (e.getSource() == pink)
  169. {
  170. c = Color.PINK;
  171. }
  172.  
  173. if (e.getSource() == eraser)
  174. {
  175. c = Color.WHITE;
  176. }
  177.  
  178. if (e.getSource() == clear)
  179. {
  180. canvas.repaint();
  181. }
  182.  
  183.  
  184. }
  185. }
  186. -------------------------------------------------------------------------------------------------------------------------------------
  187. // By: Sammy Samkough
  188.  
  189. import javax.swing.*;
  190. import java.awt.*;
  191.  
  192. public class SketchEventTester
  193. {
  194. public static void main(String args[])
  195. {
  196. SketchEvent s1 = new SketchEvent();
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment