View Single Post
Join Date: Apr 2007
Posts: 15
Reputation: Ortal is an unknown quantity at this point 
Solved Threads: 0
Ortal Ortal is offline Offline
Newbie Poster

Re: build but not run... need a second set of eyes

 
0
  #5
Nov 1st, 2007
I got it semi-working.

  1. /*
  2.  * NewClass.java
  3.  *
  4.  * Created on November 1, 2007, 3:47 PM
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9. /**
  10.  *
  11.  * @author gefeno
  12.  */
  13.  
  14. import java.awt.BorderLayout;
  15. import java.awt.Color;
  16. import java.awt.Dimension;
  17. import java.awt.Graphics;
  18. import java.awt.Graphics2D;
  19. import java.awt.Rectangle;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.MouseEvent;
  23. import java.awt.event.MouseListener;
  24. import java.awt.geom.Ellipse2D;
  25. import java.awt.geom.Line2D;
  26. import java.util.Vector;
  27. import javax.swing.ButtonGroup;
  28. import javax.swing.JCheckBox;
  29. import javax.swing.JComboBox;
  30. import javax.swing.JFrame;
  31. import javax.swing.JPanel;
  32. import javax.swing.JRadioButton;
  33. import javax.swing.UIManager;
  34.  
  35. public class NewClass
  36.  
  37. {
  38. JFrame painting;
  39. JFrame tools;
  40. int drawType;
  41. boolean fill;
  42. Color color;
  43. public NewClass()
  44. {
  45. JFrame drawFrame = new JFrame("Draw");
  46. drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47. drawFrame.getContentPane().add(new ControlPanel(), BorderLayout.CENTER);
  48. drawFrame.pack();
  49. drawFrame.setVisible(true);
  50. JFrame toolFrame = new JFrame("Draw");
  51. toolFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52. toolFrame.getContentPane().add(new DrawPanel(), BorderLayout.CENTER);
  53. toolFrame.pack();
  54. toolFrame.setVisible(true);
  55. }
  56. public static void main(String[] args) throws Exception
  57. {
  58. // Ask for window decorations provided by the look and feel.
  59. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  60. new NewClass();
  61. }
  62. private class ControlPanel extends JPanel
  63. {
  64. // Get rid of this
  65. private static final long serialVersionUID = 1L;
  66. public ControlPanel()
  67. {
  68. this.setPreferredSize(new Dimension(250, 100));
  69. final JRadioButton ovalButton = new JRadioButton("Oval");
  70. ovalButton.setActionCommand("oval");
  71. ovalButton.setSelected(true);
  72. final JRadioButton rectangleButton = new JRadioButton("Rectangle");
  73. rectangleButton.setActionCommand("rectangle");
  74. final JRadioButton lineButton = new JRadioButton("Line");
  75. lineButton.setActionCommand("line");
  76. // Group the radio buttons.
  77. final ButtonGroup group = new ButtonGroup();
  78. group.add(ovalButton);
  79. group.add(rectangleButton);
  80. group.add(lineButton);
  81. ActionListener action = new ActionListener()
  82. {
  83.  
  84. public void actionPerformed(ActionEvent e)
  85. {
  86. if (e.getActionCommand().equals("oval"))
  87. {
  88. drawType = 0;
  89. }
  90. else if (e.getActionCommand().equals("rectangle"))
  91. {
  92. drawType = 1;
  93. }
  94. else if (e.getActionCommand().equals("line"))
  95. {
  96. drawType = 2;
  97. }
  98. }
  99. };
  100. // Register a listener for the radio buttons.
  101. ovalButton.addActionListener(action);
  102. rectangleButton.addActionListener(action);
  103. lineButton.addActionListener(action);
  104. this.add(ovalButton);
  105. this.add(rectangleButton);
  106. this.add(lineButton);
  107. final JCheckBox check = new JCheckBox("Fill with color");
  108. check.addActionListener(new ActionListener()
  109. {
  110.  
  111. public void actionPerformed(ActionEvent e)
  112. {
  113. fill = check.isSelected();
  114. }
  115. });
  116. this.add(check);
  117. final Color[] colors =
  118. {
  119. Color.black, Color.red, Color.green,
  120. Color.blue, Color.yellow, Color.cyan, Color.magenta };
  121. String[] colorStrings =
  122. {
  123. "Black", "Red", "Green", "Blue",
  124. "Yellow", "Cyan", "Magenta"};
  125. final JComboBox colorList = new JComboBox(colorStrings);
  126. colorList.addActionListener(new ActionListener()
  127. {
  128. public void actionPerformed(ActionEvent e)
  129. {
  130. color = colors[colorList.getSelectedIndex()];
  131. }
  132. });
  133. this.add(colorList);
  134. }
  135. }
  136. private class DrawPanel extends JPanel
  137. {
  138. Vector<MyShape> shapes;
  139. private static final long serialVersionUID = 1L;
  140.  
  141. protected void paintComponent(Graphics g)
  142. {
  143. // TODO Auto-generated method stub
  144. super.paintComponent(g);
  145. }
  146. DrawPanel()
  147. {
  148. this.setPreferredSize(new Dimension(500, 500));
  149. this.setLocation(500, 500);
  150. this.setDoubleBuffered(true);
  151. shapes = new Vector<MyShape>();
  152. this.setBackground(Color.white);
  153. final JPanel me = this;
  154. this.addMouseListener(new MouseListener()
  155. {
  156. int x, y;
  157.  
  158. public void mouseClicked(MouseEvent arg0)
  159. {
  160. }
  161.  
  162. public void mouseEntered(MouseEvent arg0)
  163. {
  164. }
  165.  
  166. public void mouseExited(MouseEvent arg0)
  167. {
  168. }
  169.  
  170. public void mousePressed(MouseEvent arg0)
  171. {
  172. x = arg0.getX();
  173. y = arg0.getY();
  174. }
  175.  
  176. public void mouseReleased(MouseEvent arg0)
  177. {
  178. switch (drawType)
  179. {
  180. case 0: // Oval
  181. MyShape oval = new MyShape();
  182. oval.x = Math.min(x, arg0.getX());
  183. oval.y = Math.min(y, arg0.getY());
  184. oval.w = Math.abs(x - arg0.getX());
  185. oval.h = Math.abs(y - arg0.getY());
  186. oval.color = color;
  187. oval.fill = fill;
  188. oval.type = drawType;
  189. shapes.add(oval);
  190. break;
  191. case 1: // Rectangle
  192. MyShape rect = new MyShape();
  193. rect.x = Math.min(x, arg0.getX());
  194. rect.y = Math.min(y, arg0.getY());
  195. rect.w = Math.abs(x - arg0.getX());
  196. rect.h = Math.abs(y - arg0.getY());
  197. rect.color = color;
  198. rect.fill = fill;
  199. rect.type = drawType;
  200. shapes.add(rect);
  201. break;
  202. case 2: // Line
  203. MyShape line = new MyShape();
  204. line.x = x;
  205. line.x2 = arg0.getX();
  206. line.y = y;
  207. line.y2 = arg0.getY();
  208. line.color = color;
  209. line.type = drawType;
  210. shapes.add(line);
  211. break;
  212. }
  213. me.repaint();
  214. }
  215. });
  216. }
  217.  
  218. public void paint(Graphics g)
  219. {
  220. Graphics2D display = (Graphics2D) g;
  221. for (MyShape shape : shapes)
  222. {
  223. switch (shape.type)
  224. {
  225. case 0: // oval
  226. if (shape.fill)
  227. {
  228. display.setPaint(shape.color);
  229. display.fillOval(shape.x, shape.y, shape.w, shape.h);
  230. }
  231. else
  232. {
  233. display.setColor(shape.color);
  234. display.draw(new Ellipse2D.Double(shape.x, shape.y,
  235. shape.w, shape.h));
  236. }
  237. break;
  238. case 1: // Rectangle
  239. if (shape.fill)
  240. {
  241. display.setPaint(shape.color);
  242. display.fillRect(shape.x, shape.y, shape.w, shape.h);
  243. }
  244. else
  245. {
  246. display.setColor(shape.color);
  247. display.drawRect(shape.x, shape.y, shape.w, shape.h);
  248. }
  249. break;
  250. case 2: // Line
  251. display.setColor(shape.color);
  252. display.draw(new Line2D.Double(shape.x, shape.y, shape.x2,
  253. shape.y2));
  254. break;
  255. }
  256. }
  257. }
  258. private class MyShape
  259. {
  260. int x, y, w, h;
  261. int x2, y2;
  262. Color color;
  263. boolean fill;
  264. int type;
  265. }
  266. }
  267. }
Last edited by Ortal; Nov 1st, 2007 at 4:50 pm.
Reply With Quote