•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,583 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 3,636 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: 352 | Replies: 4
![]() |
| |
•
•
Join Date: Apr 2007
Posts: 13
Reputation:
Rep Power: 2
Solved Threads: 0
This program will build/compile but not run. I get an error message
Code:
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.
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;
}
}
}
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.
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.
•
•
Join Date: Apr 2007
Posts: 13
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
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?
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 19
Solved Threads: 200
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.
•
•
Join Date: Apr 2007
Posts: 13
Reputation:
Rep Power: 2
Solved Threads: 0
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 4:50 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- build but wont run... hmmm please help. (C++)
- my project is IDE for java (Java)
- Inventory Application for DVDs (Java)
- Using Windows XP Visual Styles with Controls on Windows Forms (VB.NET)
- When run the windows application by Compiler all works but after I deployed and run, (VB.NET)
- unresolved external symbol when using a hashtable class (C++)
- Tool Needed (C++)
- www.gthelp.com (Website Reviews)
- java price is right game help (Java)
Other Threads in the Java Forum
- Previous Thread: Swing Components
- Next Thread: Sum of Vectors



Hybrid Mode