de-bugging a program
Ive written out this program and can't figure out where im going wrong. Its prob something really simple. If anyone could assist it would be much appreciated
thanks
phil
--
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;
publicclass DrawingBoardextends JFrame
{
publicstaticvoid main(String [] args)
{
new DrawingBoard();
}
public DrawingBoard()
{
this.setSize(300, 300);
this.setTitle("The Drawing Board");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new PaintSurface(), BorderLayout.CENTER);
this.setVisible(true);
}
privateclass PaintSurfaceextends JComponent
{
ArrayList shapes =new ArrayList();
Point startDrag, endDrag;
public PaintSurface(){
this.addMouseListener(new MouseAdapter(){
public Void mousePressed(MouseEvent e){
startDrag =new Point(e.getX(), e.getY());
endDrag = startdrag;
repaint();
}
publicvoid mouseReleased(MouseEvent e){
Shape r = makeRectangle(
startDrag.x, startDrag.y,
e.getX(), e.getY());
shapes.add(r);
startDrag =null;
endDrag =null;
repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter(){
publicvoid mouseDragged(MouseEvent e){
endDrag =new Point(e.getX(), e.getY());
repaint();
}
});
}
publicvoid paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
//turn on antialiasing
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//draw background grid
g2.setPaint(Color.LIGHT_GRAY);
for (int i = 0; i < getSize().width; i += 10){
Shape line =new Line2D.Float(
i, 0, i, getSize().height);
g2.draw(line);
}
for (int i = 0; i < getSize().height; i += 10){
Shape line =new Line2D.Float(
0, i, getSize().width, i);
g2.draw(line);
}
//draw the shapes
Color[] colors ={
Color.RED, Color.BLUE,
Color.PINK, Color.YELLOW,
Color.MAGENTA, COLOR.CYAN};
int colorindex = 0;
g2.setStroke(new BasicStroke(2));
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.50f));
for (Shape s : shapes){
g2.setPaint(Color.BLACK);
g2.draw(s);
g2.setPaint(colors[ (colorIndex++) % 6]);
g2.fill(s);
}
// paint the temporary rectangle
if (startDrag !=null && endDrag !=null){
g2.setPaint(Color.LIGHT_GRAY);
Shape r = makeRectangle(
startDrag.x, startDrag.y,
endDrag.x, endDrag.y);
g2.draw(r);
}
}
private Rectangle2D.Float makeRectangle(
int x1,int y1,int x2,int y2){
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
int width = Math.abs(y1 - y2);
returnnew Rectangle2D.Float(
x, y, width, height);
}
}
Message was edited by:
phil171982
my psychic powers are waning. can you use old-fashioned methods to tell me the problem
Personally I just love it when people drop by and just unload their work and thinking for me to do for them.
I've fixed it for you. did you get my telepathic message with the code in?
Sorry,
It's saying it has these problems:
"DrawingBoard.java": ';' expected at line 86, column 24
"DrawingBoard.java": illegal start of expression at line 101, column 9
"DrawingBoard.java": ';' expected at line 100, column 12
"DrawingBoard.java": illegal start of expression at line 101, column 9
"DrawingBoard.java": ')' expected at line 100, column 12
"DrawingBoard.java": illegal start of expression at line 101, column 9
"DrawingBoard.java": ';' expected at line 100, column 12
"DrawingBoard.java": '}' expected at line 111, column 4
> "DrawingBoard.java": ';' expected at line 86, column> 24You know where line 86, column 24 is in the code (DrawingBoard.java) right? But do you think we do?
> Ive written out this program and can't figure out
> where im going wrong. Its prob something really
> simple. If anyone could assist it would be much
> appreciated
I am guessing that you are not able to compile the code.
> public Void mousePressed(MouseEvent e) {
This should be void
>startDrag = new Point(e.getX(), e.getY());
> endDrag = startdrag;
Your variable names do not match.
> Color.MAGENTA, COLOR.CYAN};
COLOR is not the same as Color. Java is case-sensitive.
> Sorry, > It's saying it has these problems:It means you are missing closing brackets }, )
thanks, ive changed them things. the things that its saying are wrong are the following:
for (Shape s : shapes)
really dont know why this is coming up with an error.
the other things are the - } at the end of the document, 4 in all. i cant see how there wrong.
}
}
private Rectangle2D.Float makeRectangle(
int x1, int y1, int x2, int y2) {
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
int width = Math.abs(y1 - y2);
return new Rectangle2D.Float(
x, y, width, height);
}
}
yeah thats what i thought but ive tryed adding them into it and it doesn't seem to work. whenever i add them it makes a new error elsewhere in the document
> for (Shape s : shapes)
What error does the compile show?
> really dont know why this is coming up with an
> error.
> the other things are the - } at the end of the
> document, 4 in all. i cant see how there wrong.
All I can see in your code is that there is no closing brace to your DrawingBoard class declaration.
>int x = Math.min(x1, x2);
>int y = Math.min(y1, y2);
You can't pass an int to Math.min(). You need a double. But in my opinion, you need not use the math class at all.
int x = (x1 < x2)?x1:x2;
> for (Shape s : shapes)
You are getting a compilation error on this line because your shapes ArrayList would return an Object, not a Shape (since you haven't made use of generics). The change you need to make for using generics is while declaring the ArrayList shapes.
ArrayList<Shape> shapes = new ArrayList<Shape>();
> >int x = Math.min(x1, x2);
> >int y = Math.min(y1, y2);
>
> You can't pass an int to Math.min(). You need a
> double. But in my opinion, you need not use the math
> class at all.
Read the Math API again. There are 4 min methods and 4 max methods.
for <(Shape s : shapes)it says that the error is - ';' expected at line 85 (85:20) ive made changes as you said but still getting them } errors. tis a pain!!
> > You can't pass an int to Math.min(). You
> need a
> > double. But in my opinion, you need not use the
> math
> > class at all.
>
> Read the Math API again. There are 4 min methods and
> 4 max methods.
Yes sorry. Thanks for pointing out. :)
I really don't know what I was thinking there.
EDIT: Maybe I do. I had a custom Math class which did not declare a min(int, int) in the same folder.
> ive made changes as you said but still getting them }> errors. tis a pain!!You should try and match them carefully. An advanced editor would be of some help in this situation.
Here's your code in compilable form:
package sample;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class DrawingBoard extends JFrame {
private static final long serialVersionUID = 6879414596505971308;
public static void main(String [] args) {
new DrawingBoard();
}
public DrawingBoard() {
this.setSize(300, 300);
this.setTitle("The Drawing Board");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new PaintSurface(), BorderLayout.CENTER);
this.setVisible(true);
}
private class PaintSurface extends JComponent {
private static final long serialVersionUID = 8235911711167316334;
ArrayList<Shape> shapes = new ArrayList<Shape>();
Point startDrag, endDrag;
public PaintSurface() {
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
endDrag = startDrag;
repaint();
}
public void mouseReleased(MouseEvent e) {
Shape r = makeRectangle(
startDrag.x, startDrag.y,
e.getX(), e.getY());
shapes.add(r);
startDrag = null;
endDrag = null;
repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
endDrag = new Point(e.getX(), e.getY());
repaint();
}
});
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//turn on antialiasing
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//draw background grid
g2.setPaint(Color.LIGHT_GRAY);
for (int i = 0; i < getSize().width; i += 10) {
Shape line = new Line2D.Float(
i, 0, i, getSize().height);
g2.draw(line);
}
for (int i = 0; i < getSize().height; i += 10) {
Shape line = new Line2D.Float(
0, i, getSize().width, i);
g2.draw(line);
}
//draw the shapes
Color[] colors = {
Color.RED, Color.BLUE,
Color.PINK, Color.YELLOW,
Color.MAGENTA, Color.CYAN};
int colorIndex = 0;
g2.setStroke(new BasicStroke(2));
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.50f));
for (Shape s : shapes) {
g2.setPaint(Color.BLACK);
g2.draw(s);
g2.setPaint(colors[ (colorIndex++) % 6]);
g2.fill(s);
}
// paint the temporary rectangle
if (startDrag != null && endDrag != null) {
g2.setPaint(Color.LIGHT_GRAY);
Shape r = makeRectangle(
startDrag.x, startDrag.y,
endDrag.x, endDrag.y);
g2.draw(r);
}
}
private Rectangle2D.Float makeRectangle(
int x1, int y1, int x2, int y2) {
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);
return new Rectangle2D.Float(
x, y, width, height);
}
}
}
ArrayList<Shape> shapes = new ArrayList<Shape>();ive declered this in the arraylist but it aint made any difference,Did you mean for me to put this in and it would get rid of the error previously stated or would i have to alter that too?
> Did you mean for me to put this in and it would get> rid of the error previously stated or would i have to> alter that too?huh? See the code I posted.
thanks for taking the time to do this but its not making any changes from the original errors i was getting and its producing new errors when i include the bit of code you added:-Arraylist<Shape> shapes = new ArrayList<Shape>();
saw your code and it produced the same errors and more.is it working on your applet?
What version of Java are you using? You need at least 1.5 for the ArrayList<Shape>.
Please post your error messages.
> Im using JBuilder9What version of Java is JBuilder 9 using?
> Im using JBuilder9That doesn't answer my question. What version of Java are you using? JBuilder can use different versions.
<identifier> expected at line 40 (40:26)
';' expected at line 106 (106:38)
illegal start of expression at line 121 (121:9)
';' expected at line 120 (120:12)
illegal start of expression at line 121 (121:9)
')' expected at line 120 (120:12)
illegal start of expression at line 121 (121:9)
';' expected at line 120 (120:12)
these are the error messages im getting
sorry for late reply, working on 2 computers as jbuilder isnt on this one and other doesnt have internet!!
cant see where to find out what version of java its using!
We don't know what those lines are.Look in the JBuilder configuration options (look at the various menu items and look for JDK or something like that). It should say what it is using.
its version 1.4.1ahh **** does that eman i cant do what im trying to do?
> its version 1.4.1
>
> ahh **** does that eman i cant do what im trying to do?
No, it just means you need to do it slightly differently. Instead of:
for (Shape s : shapes)
You'll need to use an iterator over the ArrayList:
Iterator shapeIter = shapes.iterator();
while (shapeIter.hasNext())
{
Shape s = (Shape)shapeIter.next();
// do something with Shape "s".
}
1.4.1 isn't even the latest 1.4. The latest is 1.4.2. You could also upgrade to either 1.5 (also known as 5.0) or 6.
its for a uni project and needs to be read on the uni systems which are all the 1.4 version. tis a pain!! cheapskate uni subscriptions!!
> cheapskate uni subscriptions!!Java downloads are for free. But you probably don't have the permission to install the latest version. Maybe you should request your administrator.
> its for a uni project and needs to be read on the uni> systems which are all the 1.4 version. tis a pain!!> cheapskate uni subscriptions!!So, change your code to use 1.4.
ive used this within the code and its now saying:
illegal start of type at line 42
it underlines the while command.
Iterator shapeIter = shapes.iterator();
while (shapeIter.hasNext())
{
Shape s = (Shape)shapeIter.next();
// do something with Shape "s".
}
> ive used this within the code and its now saying:
>
> illegal start of type at line 42
>
> it underlines the while command.
Well, why did you put it at line 42? That's where ArrayList shapes is declared, right? That's outside a method or initializer block--you can't do such code there. You want that iterator and while loop to replace the "for (Shape s : shapes)" loop in "paint".
sorry, im being dosey.
everything is working on it now exept for an error saying:
<identifier> expected
for the following bit of code at line 40
ArrayList<[b]Shape[/b]> shapes = new ArrayList<Shape>();
thanks for your time on this matter
As above..............
sorry, im being dosey.
everything is working on it now exept for an error saying:
<identifier> expected
for the following bit of code at line 40 - shape is underlined
ArrayList<Shape> shapes = new ArrayList<Shape>();
thanks for your time on this matter
> everything is working on it now exept for an error
> saying:
>
> <identifier> expected
>
> for the following bit of code at line 40
That works in Java 1.5; not 1.4. You need to revert back to what it was before my suggestion.
ArrayList shapes = new ArrayList();
ahh man,
done that,
now its producing a new error as follows:
- cannot resolve symbol: variable s in class DrawingBoard.PaintSurface at line 115
- cannot resolve symbol: variable s in class DrawingBoard.PaintSurface at line 117
(underlined where error occurs)
g2.setPaint(Color.BLACK);
g2.draw(s);
g2setPaint(colors[ (colorIndex++) %6]);
g2.fill(s);
Did you copy the code in Reply #29?What is the whole loop near where your error is occurring (line 115-117 and surrounding)?
yeah i entered the code, and yes its line 115 and 117
the code i have now is as follows:
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.util.Iterator;
public class DrawingBoard extends JFrame {
private static final long serialVersionUID = 687971308;
public static void main(String [] args) {
new DrawingBoard();
}
public DrawingBoard() {
this.setSize(300, 300);
this.setTitle("The Drawing Board");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new PaintSurface(), BorderLayout.CENTER);
this.setVisible(true);
}
private class PaintSurface extends JComponent {
private static final long serialVersionUID = 823594;
ArrayList shapes = new ArrayList();
Point startDrag, endDrag;
public PaintSurface() {
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
endDrag = startDrag;
repaint();
}
public void mouseReleased(MouseEvent e) {
Shape r = makeRectangle(
startDrag.x, startDrag.y,
e.getX(), e.getY());
shapes.add(r);
startDrag = null;
endDrag = null;
repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
endDrag = new Point(e.getX(), e.getY());
repaint();
}
});
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//turn on antialiasing
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//draw background grid
g2.setPaint(Color.LIGHT_GRAY);
for (int i = 0; i < getSize().width; i += 10) {
Shape line = new Line2D.Float(
i, 0, i, getSize().height);
g2.draw(line);
}
for (int i = 0; i < getSize().height; i += 10) {
Shape line = new Line2D.Float(
0, i, getSize().width, i);
g2.draw(line);
}
//draw the shapes
Color[] colors = {
Color.RED, Color.BLUE,
Color.PINK, Color.YELLOW,
Color.MAGENTA, Color.CYAN};
int colorIndex = 0;
g2.setStroke(new BasicStroke(2));
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.50f));
Iterator shapeIter = shapes.iterator();
while (shapeIter.hasNext())
{
Shape s = (Shape)shapeIter.next();
// do something with Shape "s".
}
{
g2.setPaint(Color.BLACK);
g2.draw(s);
g2.setPaint(colors[ (colorIndex++) % 6]);
g2.fill(s);
}
// paint the temporary rectangle
if (startDrag != null && endDrag != null) {
g2.setPaint(Color.LIGHT_GRAY);
Shape r = makeRectangle(
startDrag.x, startDrag.y,
endDrag.x, endDrag.y);
g2.draw(r);
}
}
private Rectangle2D.Float makeRectangle(
int x1, int y1, int x2, int y2) {
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);
return new Rectangle2D.Float(
x, y, width, height);
}
}
}
You have not copied it correctly. You were supposed to do those operations within the loop. Change this part:
Iterator shapeIter = shapes.iterator();
while (shapeIter.hasNext())
{
Shape s = (Shape)shapeIter.next();
// do something with Shape "s".
}
{
g2.setPaint(Color.BLACK);
g2.draw(s);
g2.setPaint(colors[ (colorIndex++) % 6]);
g2.fill(s);
}
to:
Iterator shapeIter = shapes.iterator();
while (shapeIter.hasNext())
{
Shape s = (Shape)shapeIter.next();
// do something with Shape "s".
g2.setPaint(Color.BLACK);
g2.draw(s);
g2.setPaint(colors[ (colorIndex++) % 6]);
g2.fill(s);
}
there are no script errors anymore but instead it produces thi error when attempting to run the project:
java.lang.Error: Do not use DrawingBoard.add() use DrawingBoard.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:458)
at javax.swing.JFrame.addImpl(JFrame.java:484)
at java.awt.Container.add(Container.java:518)
at DrawingBoard.<init>(DrawingBoard.java:32)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:306)
at java.lang.Class.newInstance(Class.java:259)
at com.borland.jbuilder.runtime.applet.AppletTestbed.a(Unknown Source)
at com.borland.jbuilder.runtime.applet.AppletTestbed.startApplet(Unknown Source)
at com.borland.jbuilder.runtime.applet.AppletTestbed.main(Unknown Source)
Exception in thread "main"
Message was edited by:
phil171982
> java.lang.Error: Do not use DrawingBoard.add() use> DrawingBoard.getContentPane().add() insteadThen change your code as this line tells you to do.
> this.add(new PaintSurface(),> BorderLayout.CENTER);Change this line to:this.getContentPane.add(new PaintSurface(), BorderLayout.CENTER);
.Message was edited by: phil171982
Did that solve your problem?
ive added that and now theres an error message saying:Cannot resolve symbol: variable getContentPane in class DrawingBoard at line 32
> annot resolve symbol: variable getContentPane in> class DrawingBoard at line 32I made a typo there. It should be getContentPane() instead of getContentPane.
man its sooo close to working, it flashes the applet up when i run it and then displays the following message at the bottom of the code page(no particular line):The class Drawing is not a subclass of java.awt.Applet
typo sorryman its sooo close to working, it flashes the applet up when i run it and then displays the following message at the bottom of the code page(no particular line):The class DrawingBoard is not a subclass of java.awt.Applet
> typo sorry
>
> man its sooo close to working, it flashes the applet
> up when i run it and then displays the following
> message at the bottom of the code page(no particular
> line):
>
> The class DrawingBoard is not a subclass of
> java.awt.Applet
You missed a typo yet. the word so is spelt with one o not three. ;-)
Of course it isn't an Applet--you said "extends JFrame", and JFrame doesn't extend Applet.Run it as an application, not an applet [you do have a main method already]:java -cp . DrawingBoardOr, do that wherever your IDE lets you specify the class with the main
> man its sooo close to working, it flashes the applet
> up when i run it and then displays the following
> message at the bottom of the code page(no particular
> line):
>
> The class DrawingBoard is not a subclass of
> java.awt.Applet
I'm not very sure of this but what happens if you make your class extend JApplet instead of JFrame. JApplet is a top-level container as far as I remember reading.
public class DrawingBoard extends JApplet {
// Rest of the code
}
great it finally works, thanks for all your help on the matter,sorry for being such a pain.wish i had the knowledge of Java that people like yourself have!!catcha later
yeah that change from JFrame to JApplet works!
now i can draw the squares i gotta find out how i can select them once there drawn and assign a material to them using a drop down menu of different materials!! Doing this for a university architecture project where i want to draw materials and be able to change them once there drawn!
don't want to bug you any longer though as feel ive taken up enough of you time already,
thanks again