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

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