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

Reply

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

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

 
0
  #1
Nov 1st, 2007
This program will build/compile but not run. I get an error message



Code:
  1. java.lang.NoClassDefFoundError: midterm1/Main Exception in thread "main" Java Result: 1

Now I am very new to java and all of the logical errors and messages that come with them. Please bare with me. Can anyone take a look? Id appreciate it so much.





  1.  
  2. /*
  3.  * Try1.java
  4.  *
  5.  * Created on October 10, 2007, 11:43 PM
  6.  *
  7.  * To change this template, choose Tools | Template Manager
  8.  * and open the template in the editor.
  9.  *//**
  10.  *
  11.  * @author gefeno
  12.  */import java.awt.BorderLayout;
  13. import java.awt.Color;
  14. import java.awt.Dimension;
  15. import java.awt.Graphics;
  16. import java.awt.Graphics2D;
  17. import java.awt.Rectangle;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.MouseEvent;
  21. import java.awt.event.MouseListener;
  22. import java.awt.geom.Ellipse2D;
  23. import java.awt.geom.Line2D;
  24. import java.util.Vector;
  25. import javax.swing.ButtonGroup;
  26. import javax.swing.JCheckBox;
  27. import javax.swing.JComboBox;
  28. import javax.swing.JFrame;
  29. import javax.swing.JPanel;
  30. import javax.swing.JRadioButton;
  31. import javax.swing.UIManager;
  32. public class Main{
  33. JFrame painting;
  34. JFrame tools; int drawType;
  35. boolean fill;
  36. Color color; public Main() {
  37. JFrame drawFrame = new JFrame("Draw");
  38. drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. drawFrame.getContentPane().add(new ControlPanel(), BorderLayout.CENTER);
  40. drawFrame.pack();
  41. drawFrame.setVisible(true); JFrame toolFrame = new JFrame("Draw");
  42. toolFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43. toolFrame.getContentPane().add(new DrawPanel(), BorderLayout.CENTER);
  44. toolFrame.pack();
  45. toolFrame.setVisible(true);
  46. } public static void main(String[] args) throws Exception {
  47. // Ask for window decorations provided by the look and feel.
  48. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); new Main();
  49. } private class ControlPanel extends JPanel {
  50. // Get rid of this
  51. private static final long serialVersionUID = 1L; public ControlPanel() {
  52. this.setPreferredSize(new Dimension(250, 100)); final JRadioButton ovalButton = new JRadioButton("Oval");
  53. ovalButton.setActionCommand("oval");
  54. ovalButton.setSelected(true);
  55. final JRadioButton rectangleButton = new JRadioButton("Rectangle");
  56. rectangleButton.setActionCommand("rectangle");
  57. final JRadioButton lineButton = new JRadioButton("Line");
  58. lineButton.setActionCommand("line"); // Group the radio buttons.
  59. final ButtonGroup group = new ButtonGroup();
  60. group.add(ovalButton);
  61. group.add(rectangleButton);
  62. group.add(lineButton); ActionListener action = new ActionListener() {
  63.  
  64. public void actionPerformed(ActionEvent e) {
  65. if (e.getActionCommand().equals("oval")) {
  66. drawType = 0;
  67. } else if (e.getActionCommand().equals("rectangle")) {
  68. drawType = 1;
  69. } else if (e.getActionCommand().equals("line")) {
  70. drawType = 2;
  71. }
  72. }
  73. }; // Register a listener for the radio buttons.
  74. ovalButton.addActionListener(action);
  75. rectangleButton.addActionListener(action);
  76. lineButton.addActionListener(action); this.add(ovalButton);
  77. this.add(rectangleButton);
  78. this.add(lineButton); final JCheckBox check = new JCheckBox("Fill with color");
  79. check.addActionListener(new ActionListener() {
  80.  
  81. public void actionPerformed(ActionEvent e) {
  82. fill = check.isSelected();
  83. }
  84. }); this.add(check); final Color[] colors = { Color.black, Color.red, Color.green,
  85. Color.blue, Color.yellow, Color.cyan, Color.magenta };
  86. String[] colorStrings = {"Black", "Red", "Green", "Blue",
  87. "Yellow", "Cyan", "Magenta"};
  88. final JComboBox colorList = new JComboBox(colorStrings);
  89. colorList.addActionListener(new ActionListener() {
  90. public void actionPerformed(ActionEvent e) {
  91. color = colors[colorList.getSelectedIndex()];
  92. }
  93. });
  94. this.add(colorList);
  95. }
  96. } private class DrawPanel extends JPanel {
  97. Vector<MyShape> shapes;
  98. private static final long serialVersionUID = 1L;
  99. protected void paintComponent(Graphics g) {
  100. // TODO Auto-generated method stub
  101. super.paintComponent(g);
  102. } DrawPanel() {
  103. this.setPreferredSize(new Dimension(500, 500));
  104. this.setLocation(500, 500);
  105. this.setDoubleBuffered(true);
  106. shapes = new Vector<MyShape>();
  107. this.setBackground(Color.white);
  108. final JPanel me = this;
  109. this.addMouseListener(new MouseListener() {
  110. int x, y;
  111. public void mouseClicked(MouseEvent arg0) {
  112. }
  113. public void mouseEntered(MouseEvent arg0) {
  114. }
  115. public void mouseExited(MouseEvent arg0) {
  116. }
  117. public void mousePressed(MouseEvent arg0) {
  118. x = arg0.getX();
  119. y = arg0.getY();
  120. }
  121. public void mouseReleased(MouseEvent arg0) {
  122. switch (drawType) {
  123. case 0: // Oval
  124. MyShape oval = new MyShape();
  125. oval.x = Math.min(x, arg0.getX());
  126. oval.y = Math.min(y, arg0.getY());
  127. oval.w = Math.abs(x - arg0.getX());
  128. oval.h = Math.abs(y - arg0.getY());
  129. oval.color = color;
  130. oval.fill = fill;
  131. oval.type = drawType;
  132. shapes.add(oval);
  133. break; case 1: // Rectangle
  134. MyShape rect = new MyShape();
  135. rect.x = Math.min(x, arg0.getX());
  136. rect.y = Math.min(y, arg0.getY());
  137. rect.w = Math.abs(x - arg0.getX());
  138. rect.h = Math.abs(y - arg0.getY());
  139. rect.color = color;
  140. rect.fill = fill;
  141. rect.type = drawType;
  142. shapes.add(rect);
  143. break; case 2: // Line
  144. MyShape line = new MyShape();
  145. line.x = x;
  146. line.x2 = arg0.getX();
  147. line.y = y;
  148. line.y2 = arg0.getY();
  149. line.color = color;
  150. line.type = drawType;
  151. shapes.add(line);
  152. break;
  153. }
  154. me.repaint();
  155. } });
  156. }
  157. public void paint(Graphics g) {
  158. Graphics2D display = (Graphics2D) g; for (MyShape shape : shapes) {
  159. switch (shape.type) {
  160. case 0: // oval
  161. if (shape.fill) {
  162. display.setPaint(shape.color);
  163. display.fillOval(shape.x, shape.y, shape.w, shape.h);
  164. } else {
  165. display.setColor(shape.color);
  166. display.draw(new Ellipse2D.Double(shape.x, shape.y,
  167. shape.w, shape.h));
  168. }
  169. break; case 1: // Rectangle
  170. if (shape.fill) {
  171. display.setPaint(shape.color);
  172. display.fillRect(shape.x, shape.y, shape.w, shape.h);
  173. } else {
  174. display.setColor(shape.color);
  175. display.drawRect(shape.x, shape.y, shape.w, shape.h);
  176. }
  177. break; case 2: // Line
  178. display.setColor(shape.color);
  179. display.draw(new Line2D.Double(shape.x, shape.y, shape.x2,
  180. shape.y2));
  181. break; } }
  182. } private class MyShape {
  183. int x, y, w, h;
  184. int x2, y2;
  185. Color color;
  186. boolean fill;
  187. int type;
  188. }
  189. }
  190. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,427
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #2
Nov 1st, 2007
Your class name needs to match the file name. You have named the class Main put placed it in midterm1.java from the looks of it.

Side note: You really could use some more white space to separate your methods and logical sections within the methods. It will make it much easier for you and others to follow the code.
Reply With Quote Quick reply to this message  
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
  #3
Nov 1st, 2007
Originally Posted by Ezzaral View Post
Your class name needs to match the file name. You have named the class Main put placed it in midterm1.java from the looks of it.

Side note: You really could use some more white space to separate your methods and logical sections within the methods. It will make it much easier for you and others to follow the code.
So I should just rename the class as midterm1?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

 
0
  #4
Nov 1st, 2007
and place it in the proper package, and refactor the code to be more readable, properly indented, and using some more whitespace, and you should probably refactor out a lot of the code into separate classes.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC