User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 391,560 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,742 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 292 | Replies: 4
Reply
Join Date: Apr 2007
Posts: 13
Reputation: Ortal is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Ortal Ortal is offline Offline
Newbie Poster

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

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



Code:
 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.





/*
 * Try1.java
 *
 * Created on October 10, 2007, 11:43 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 *//**
 *
 * @author gefeno
 */import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
public class Main{
 JFrame painting;
 JFrame tools; int drawType;
 boolean fill;
 Color color; public Main() {
  JFrame drawFrame = new JFrame("Draw");
  drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  drawFrame.getContentPane().add(new ControlPanel(), BorderLayout.CENTER);
  drawFrame.pack();
  drawFrame.setVisible(true);  JFrame toolFrame = new JFrame("Draw");
  toolFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  toolFrame.getContentPane().add(new DrawPanel(), BorderLayout.CENTER);
  toolFrame.pack();
  toolFrame.setVisible(true);
 } public static void main(String[] args) throws Exception {
  // Ask for window decorations provided by the look and feel.
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  new Main();
 } private class ControlPanel extends JPanel {
  // Get rid of this
  private static final long serialVersionUID = 1L;  public ControlPanel() {
   this.setPreferredSize(new Dimension(250, 100));   final JRadioButton ovalButton = new JRadioButton("Oval");
   ovalButton.setActionCommand("oval");
   ovalButton.setSelected(true);
   final JRadioButton rectangleButton = new JRadioButton("Rectangle");
   rectangleButton.setActionCommand("rectangle");
   final JRadioButton lineButton = new JRadioButton("Line");
   lineButton.setActionCommand("line");   // Group the radio buttons.
   final ButtonGroup group = new ButtonGroup();
   group.add(ovalButton);
   group.add(rectangleButton);
   group.add(lineButton);   ActionListener action = new ActionListener() {
    
    public void actionPerformed(ActionEvent e) {
     if (e.getActionCommand().equals("oval")) {
      drawType = 0;
     } else if (e.getActionCommand().equals("rectangle")) {
      drawType = 1;
     } else if (e.getActionCommand().equals("line")) {
      drawType = 2;
     }
    }
   };   // Register a listener for the radio buttons.
   ovalButton.addActionListener(action);
   rectangleButton.addActionListener(action);
   lineButton.addActionListener(action);   this.add(ovalButton);
   this.add(rectangleButton);
   this.add(lineButton);   final JCheckBox check = new JCheckBox("Fill with color");
   check.addActionListener(new ActionListener() {
    
    public void actionPerformed(ActionEvent e) {
     fill = check.isSelected();
    }
   });   this.add(check);   final Color[] colors = { Color.black, Color.red, Color.green,
     Color.blue, Color.yellow, Color.cyan, Color.magenta };
   String[] colorStrings = {"Black", "Red", "Green", "Blue", 
     "Yellow", "Cyan", "Magenta"};
   final JComboBox colorList = new JComboBox(colorStrings);
   colorList.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     color = colors[colorList.getSelectedIndex()];
    }
   });
   this.add(colorList);
  }
 } private class DrawPanel extends JPanel {
  Vector<MyShape> shapes;
  private static final long serialVersionUID = 1L;  
  protected void paintComponent(Graphics g) {
   // TODO Auto-generated method stub
   super.paintComponent(g);
  }  DrawPanel() {
   this.setPreferredSize(new Dimension(500, 500));
   this.setLocation(500, 500);
   this.setDoubleBuffered(true);
   shapes = new Vector<MyShape>();
   this.setBackground(Color.white);
   final JPanel me = this;
   this.addMouseListener(new MouseListener() {
    int x, y;    
    public void mouseClicked(MouseEvent arg0) {
    }    
    public void mouseEntered(MouseEvent arg0) {
    }    
    public void mouseExited(MouseEvent arg0) {
    }    
    public void mousePressed(MouseEvent arg0) {
     x = arg0.getX();
     y = arg0.getY();
    }    
    public void mouseReleased(MouseEvent arg0) {
     switch (drawType) {
     case 0: // Oval
      MyShape oval = new MyShape();
      oval.x = Math.min(x, arg0.getX());
      oval.y = Math.min(y, arg0.getY());
      oval.w = Math.abs(x - arg0.getX());
      oval.h = Math.abs(y - arg0.getY());
      oval.color = color;
      oval.fill = fill;
      oval.type = drawType;
      shapes.add(oval);
      break;     case 1: // Rectangle
      MyShape rect = new MyShape();
      rect.x = Math.min(x, arg0.getX());
      rect.y = Math.min(y, arg0.getY());
      rect.w = Math.abs(x - arg0.getX());
      rect.h = Math.abs(y - arg0.getY());
      rect.color = color;
      rect.fill = fill;
      rect.type = drawType;
      shapes.add(rect);
      break;     case 2: // Line
      MyShape line = new MyShape();
      line.x = x;
      line.x2 = arg0.getX();
      line.y = y;
      line.y2 = arg0.getY();
      line.color = color;
      line.type = drawType;
      shapes.add(line);
      break;
     }
     me.repaint();
    }   });
  }  
  public void paint(Graphics g) {
   Graphics2D display = (Graphics2D) g;   for (MyShape shape : shapes) {
    switch (shape.type) {
    case 0: // oval
     if (shape.fill) {
      display.setPaint(shape.color);
      display.fillOval(shape.x, shape.y, shape.w, shape.h);
     } else {
      display.setColor(shape.color);
      display.draw(new Ellipse2D.Double(shape.x, shape.y,
        shape.w, shape.h));
     }
     break;    case 1: // Rectangle
     if (shape.fill) {
      display.setPaint(shape.color);
      display.fillRect(shape.x, shape.y, shape.w, shape.h);
     } else {
      display.setColor(shape.color);
      display.drawRect(shape.x, shape.y, shape.w, shape.h);
     }
     break;    case 2: // Line
     display.setColor(shape.color);
     display.draw(new Line2D.Double(shape.x, shape.y, shape.x2,
       shape.y2));
     break;    }   }
  }  private class MyShape {
   int x, y, w, h;
   int x2, y2;
   Color color;
   boolean fill;
   int type;
  }
 }
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,599
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 11
Solved Threads: 257
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

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

  #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  
Join Date: Apr 2007
Posts: 13
Reputation: Ortal is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Ortal Ortal is offline Offline
Newbie Poster

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

  #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  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

  #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.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Join Date: Apr 2007
Posts: 13
Reputation: Ortal is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Ortal Ortal is offline Offline
Newbie Poster

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

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

/*
 * NewClass.java
 *
 * Created on November 1, 2007, 3:47 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
/**
 *
 * @author gefeno
 */
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;

public class NewClass 
 
{
 JFrame painting;
 JFrame tools;
 int drawType;
 boolean fill;
 Color color;
 public NewClass() 
 {
  JFrame drawFrame = new JFrame("Draw");
  drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  drawFrame.getContentPane().add(new ControlPanel(), BorderLayout.CENTER);
  drawFrame.pack();
  drawFrame.setVisible(true);
  JFrame toolFrame = new JFrame("Draw");
  toolFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  toolFrame.getContentPane().add(new DrawPanel(), BorderLayout.CENTER);
  toolFrame.pack();
  toolFrame.setVisible(true);
 }
 public static void main(String[] args) throws Exception 
 {
  // Ask for window decorations provided by the look and feel.
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  new NewClass();
 }
 private class ControlPanel extends JPanel 
 {
  // Get rid of this
  private static final long serialVersionUID = 1L;
  public ControlPanel() 
  {
   this.setPreferredSize(new Dimension(250, 100));
   final JRadioButton ovalButton = new JRadioButton("Oval");
   ovalButton.setActionCommand("oval");
   ovalButton.setSelected(true);
   final JRadioButton rectangleButton = new JRadioButton("Rectangle");
   rectangleButton.setActionCommand("rectangle");
   final JRadioButton lineButton = new JRadioButton("Line");
   lineButton.setActionCommand("line");
   // Group the radio buttons.
   final ButtonGroup group = new ButtonGroup();
   group.add(ovalButton);
   group.add(rectangleButton);
   group.add(lineButton);
   ActionListener action = new ActionListener() 
   {
    
    public void actionPerformed(ActionEvent e) 
    {
     if (e.getActionCommand().equals("oval")) 
     {
      drawType = 0;
     } 
     else if (e.getActionCommand().equals("rectangle")) 
     {
      drawType = 1;
     } 
     else if (e.getActionCommand().equals("line")) 
     {
      drawType = 2;
     }
    }
   };
   // Register a listener for the radio buttons.
   ovalButton.addActionListener(action);
   rectangleButton.addActionListener(action);
   lineButton.addActionListener(action);
   this.add(ovalButton);
   this.add(rectangleButton);
   this.add(lineButton);
   final JCheckBox check = new JCheckBox("Fill with color");
   check.addActionListener(new ActionListener() 
   {
    
    public void actionPerformed(ActionEvent e) 
    {
     fill = check.isSelected();
    }
   });
   this.add(check);
   final Color[] colors = 
       {
        Color.black, Color.red, Color.green,
        Color.blue, Color.yellow, Color.cyan, Color.magenta };
   String[] colorStrings = 
    {
     "Black", "Red", "Green", "Blue", 
     "Yellow", "Cyan", "Magenta"};
   final JComboBox colorList = new JComboBox(colorStrings);
   colorList.addActionListener(new ActionListener() 
   {
    public void actionPerformed(ActionEvent e) 
    {
     color = colors[colorList.getSelectedIndex()];
    }
   });
   this.add(colorList);
  }
 }
 private class DrawPanel extends JPanel 
 {
  Vector<MyShape> shapes;
  private static final long serialVersionUID = 1L;
  
  protected void paintComponent(Graphics g) 
  {
   // TODO Auto-generated method stub
   super.paintComponent(g);
  }
  DrawPanel() 
  {
   this.setPreferredSize(new Dimension(500, 500));
   this.setLocation(500, 500);
   this.setDoubleBuffered(true);
   shapes = new Vector<MyShape>();
    this.setBackground(Color.white);
   final JPanel me = this;
   this.addMouseListener(new MouseListener() 
   {
    int x, y;
    
    public void mouseClicked(MouseEvent arg0) 
    {
    }
    
    public void mouseEntered(MouseEvent arg0) 
    {
    }
    
    public void mouseExited(MouseEvent arg0) 
    {
    }
    
    public void mousePressed(MouseEvent arg0) 
    {
     x = arg0.getX();
     y = arg0.getY();
    }
    
    public void mouseReleased(MouseEvent arg0) 
    {
     switch (drawType) 
     {
      case 0: // Oval
       MyShape oval = new MyShape();
       oval.x = Math.min(x, arg0.getX());
       oval.y = Math.min(y, arg0.getY());
       oval.w = Math.abs(x - arg0.getX());
       oval.h = Math.abs(y - arg0.getY());
       oval.color = color;
       oval.fill = fill;
       oval.type = drawType;
       shapes.add(oval);
       break;
      case 1: // Rectangle
       MyShape rect = new MyShape();
       rect.x = Math.min(x, arg0.getX());
       rect.y = Math.min(y, arg0.getY());
       rect.w = Math.abs(x - arg0.getX());
       rect.h = Math.abs(y - arg0.getY());
       rect.color = color;
       rect.fill = fill;
       rect.type = drawType;
       shapes.add(rect);
       break;
      case 2: // Line
       MyShape line = new MyShape();
       line.x = x;
       line.x2 = arg0.getX();
       line.y = y;
       line.y2 = arg0.getY();
       line.color = color;
       line.type = drawType;
       shapes.add(line);
       break;
     }
     me.repaint();
    }
   });
  }
  
  public void paint(Graphics g) 
  {
   Graphics2D display = (Graphics2D) g;
   for (MyShape shape : shapes) 
   {
    switch (shape.type) 
    {
     case 0: // oval
      if (shape.fill) 
      {
       display.setPaint(shape.color);
       display.fillOval(shape.x, shape.y, shape.w, shape.h);
      } 
      else 
      {
       display.setColor(shape.color);
       display.draw(new Ellipse2D.Double(shape.x, shape.y,
        shape.w, shape.h));
      }
      break;
     case 1: // Rectangle
      if (shape.fill) 
      {
       display.setPaint(shape.color);
       display.fillRect(shape.x, shape.y, shape.w, shape.h);
      } 
      else 
      {
       display.setColor(shape.color);
       display.drawRect(shape.x, shape.y, shape.w, shape.h);
      }
      break;
     case 2: // Line
      display.setColor(shape.color);
      display.draw(new Line2D.Double(shape.x, shape.y, shape.x2,
       shape.y2));
      break;
    }
   }
  }
  private class MyShape 
  {
   int x, y, w, h;
   int x2, y2;
   Color color;
   boolean fill;
   int type;
  }
 }
}

 
Last edited by Ortal : Nov 1st, 2007 at 3:50 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:00 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC