HELP ME PLEASEEE!!!!!! ITS A LEGITAMENT PROBLEM!!!!
/*
NAME:Christopher Benson
DATE:
*/
import java.awt.*;
publicclass Coordinateextends Point// implements Comparable<Coordinate>
{
public Coordinate()
{
super();
}
public Coordinate(int diffX,int diffY)
{
super(diffX, diffY);
}
public Coordinate(double diffX,double diffY)
{
super.setLocation(diffX, diffY);
}
public Coordinate(Coordinate temp)
{
super(temp);
}
public String toString()
{
return"(" + this.x +", " + this.y +")";
}
}
/*
NAME: Christopher Benson
DATE:November 3 2006
PURPOSE:
*/
import java.util.*;
publicclass Polygonsextends Coordinate
{
privatestaticfinaldouble DEG_TO_RAD = Math.PI/180;
privateint numSides, sides, sideLeng;
privatedouble rad;
private Coordinate cntrCoor;
privatedouble radius;
privatedouble cntrlAngl, angle;
ArrayList <Coordinate> Cords;
public Polygons()
{
int sides = 0;
double sideLength = 0;
double radius = 0;
int centerAngl = 0;
}
public Polygons(int numSides)
{
int sides = numSides;
double radius = 10;
int centerAngl = 360/sides;
}
public Polygons(int numSides,double radius)
{
sides = numSides;
radius = rad;
cntrlAngl = 360/sides;
Cords =new ArrayList <Coordinate> (sides);
setCoordinates(cntrlAngl);
}
public Polygons(int numSides,int sideLength)
{
sides = numSides;
sideLength = sideLeng;
radius = 10;
cntrlAngl = 360/sides;
Cords =new ArrayList <Coordinate> (sides);
setCoordinates(cntrlAngl);
}
privatevoid setCoordinates(double angle)
{
double tempAng = angle;
double xCor, yCor;
for(int I = 1; I == sides; I++)
{
xCor = radius * Math.cos(tempAng * DEG_TO_RAD);
yCor = radius * Math.sin(tempAng * DEG_TO_RAD);
Cords.add(new Coordinate(xCor, yCor));
tempAng += cntrlAngl;
}
}
public ArrayList <Coordinate> getCoordinates()
{
ArrayList <Coordinate> temp =new ArrayList <Coordinate> (sides);
Iterator <Coordinate> iter = temp.iterator();
while(iter.hasNext())
{
temp.add(new Coordinate(iter.next()));
}
return temp;
}
publicvoid radius(double rad)
{
radius = rad;
setCoordinates(angle);
}
publicvoid setCenter(Coordinate center,int x1,int y1)
{
ArrayList <Coordinate> temp =new ArrayList <Coordinate> (sides);
Iterator <Coordinate> iter = temp.iterator();
while(iter.hasNext())
{
iter.next().translate(x1,y1);
}
}
publicvoid rotate(double degree)
{
setCoordinates(degree);
}
publicdouble area(double rad,double cntrlAngl)
{
double area;
double apothem = rad * Math.cos(cntrlAngl/2 * DEG_TO_RAD);
return apothem;
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
publicclass DrawCurvesextends Frame
{
CurvCanvas cBoard =new CurvCanvas();// Create area/Canvas for drawings
private ArrayList<Coordinate> drawPts;
privateboolean closedCurve;
privateint numSides;
private String numSidesInput;
public DrawCurves( )// Default constructor
{
setTitle("Curve Drawing");
setSize(1600,1200);
addWindowListener(new MyLocalWindowAdapter());
cBoard.setSize(400,300);// Note: size SHOULD fit in window
cBoard.setBackground(Color.GREEN);
add("Center",cBoard);
getInput();
setVisible(true);
}// DrawCurves()
privatevoid getInput()
{
numSidesInput = JOptionPane.showInputDialog(null,"Please input the number of sides that you would like to use for your polygon");
numSides = Integer.parseInt(numSidesInput);
Polygons poly =new Polygons();
poly.Polygons(numSides, 20);
}
privateclass MyLocalWindowAdapterextends WindowAdapter
{
publicvoid windowClosing(WindowEvent event)
{
dispose();
System.exit(0);
}
}//inner class MyLocalWindowAdapter
privateclass CurvCanvasextends Canvas
{
// paint is the inherited method we should override in order to draw our own image in an AWT window
publicvoid paint(Graphics g)
{
Point startPt, fromPt, toPt;
Iterator <Coordinate> iter = drawPts.iterator();
while(iter.hasNext())
{
fromPt = iter.next();
toPt = iter.next();
int fX = (int)fromPt.getX();
int fY = (int)fromPt.getY();
int tX = (int)toPt.getX();
int tY = (int)toPt.getY();
g.drawLine(fX, fY, tX, tY);
}
g.setColor(Color.RED);//sets color of line to be drawn
g.drawLine(100, 200, 600, 700);//draws line between two sets of coordinates.
}//paint()
}// class CurvCanvas
publicstaticvoid main(String[] args)
{
new DrawCurves( );
}
}//Drawcurves
I need help getting it to allow me to draw what i have. It doesn't seem to be allowing me to use the polygons method polygons(int, int). As you can see it is already there and i have tried recompileing everything and it still gives me that same error. Maybe one of you out there can help me fix it. Thanks for the help
~Chris~
# 1
OK SO I Realy NEED ur Help Please Try As Fast As U Can Im desperate!!! I'll Give u every duke buck i have to the person WhO Helps ME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!111111
# 2
Your setup is kind of confusing. You should create a class that extends Canvas (not an inner class), then create a Frame for it in your main method.
# 3
well that part was given to me so wile it might be better programming, i don't think its necessary. The other ppl in my class didn't feel the need to change it and i know it can be done without changing it so i think i'll leave it alone so that i don't have the chance of running into further unnecessary problems. I mean that in the nicest way of course. Thanks for the advice tho.
# 4
In your DrawCurves() constructor, try adding this:setContentPane(new CurvCanvas());I know that JFrame has that method, not sure about AWT though.
# 5
That just gives me these errors:
F:\DrawCurves.java:18: cannot find symbol
symbol : method setContentPane(DrawCurves.CurvCanvas)
location: class DrawCurves
setContentPane(new CurvCanvas());
^
F:\DrawCurves.java:35: cannot find symbol
symbol : method Polygons(int,int)
location: class Polygons
poly.Polygons(numSides, 20);
# 6
Then just try this:add(new CurvCanvas());
# 7
I still end up with the following error:F:\DrawCurves.java:35: cannot find symbolsymbol : method Polygons(int,int)location: class Polygonspoly.Polygons(numSides, 20);^1 errorTool completed with exit code 1
# 8
Polygons poly = new Polygons();poly.Polygons(numSides, 20);You cant call a construcor like you can a method. Try this instead:Polygons poly = new Polygons(numSides, 20);
# 9
alright well it compiles fine now but it still doesn't print the polygon
# 11
it gives me this runtime error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DrawCurves$CurvCanvas.paint(DrawCurves.java:55)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
at java.awt.Component.dispatchEventImpl(Component.java:4031)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
# 12
Can you repost your code for your DrawCurves class?
# 13
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class DrawCurves extends Frame
{
CurvCanvas cBoard = new CurvCanvas();// Create area/Canvas for drawings
private ArrayList<Coordinate> drawPts;
private boolean closedCurve;
private int numSides;
private String numSidesInput;
public DrawCurves( ) // Default constructor
{
add(new CurvCanvas());
setTitle("Curve Drawing");
setSize(1600,1200);
addWindowListener(new MyLocalWindowAdapter());
cBoard.setSize(400,300); // Note: size SHOULD fit in window
cBoard.setBackground(Color.GREEN);
add("Center",cBoard);
getInput();
setVisible(true);
} // DrawCurves()
private void getInput()
{
numSidesInput = JOptionPane.showInputDialog(null, "Please input the number of sides that you would like to use for your polygon");
numSides = Integer.parseInt(numSidesInput);
Polygons poly = new Polygons(numSides, 20);
}
private class MyLocalWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
dispose();
System.exit(0);
}
} //inner class MyLocalWindowAdapter
private class CurvCanvas extends Canvas
{
// paint is the inherited method we should override in order to draw our own image in an AWT window
public void paint(Graphics g)
{
Point startPt, fromPt, toPt;
Iterator <Coordinate> iter = drawPts.iterator();
while(iter.hasNext())
{
fromPt = iter.next();
toPt = iter.next();
int fX = (int)fromPt.getX();
int fY = (int)fromPt.getY();
int tX = (int)toPt.getX();
int tY = (int)toPt.getY();
g.drawLine(fX, fY, tX, tY);
}
g.setColor(Color.RED);//sets color of line to be drawn
g.drawLine(100, 200, 600, 700);//draws line between two sets of coordinates.
}//paint()
} // class CurvCanvas
public static void main(String[] args)
{
new DrawCurves( );
}
}//Drawcurves
# 14
Whoops, delete this line:add(new CurvCanvas());
# 15
wel...I still get this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DrawCurves$CurvCanvas.paint(DrawCurves.java:55)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
at java.awt.Component.dispatchEventImpl(Component.java:4031)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
# 16
What is line 55 in your DrawCurves class?
# 17
> wel...I still get this:
>
> Exception in thread "AWT-EventQueue-0"
> java.lang.NullPointerException
> at
> DrawCurves$CurvCanvas.paint(DrawCurves.java:55)
So? Fix it.
It tells you the name of the file (DrawCurves.java) and the line number at which this occurs (line 55).
Turn on line numbers in your editor, go to line 55, and see what could be null and why. When you make it so the reference is not null, this error will go away.
It's not our job to fix your stuff, it's yours.
%
# 18
Iterator <Coordinate> iter = drawPts.iterator();Is that line 55? You havent initialized your ArrayList.
# 19
line 55 is blank but this is what is before it:
private class CurvCanvas extends Canvas
{
// paint is the inherited method we should override in order to draw our own image in an AWT window
public void paint(Graphics g)
{
Point startPt, fromPt, toPt;
Iterator <Coordinate> iter = drawPts.iterator();
# 20
i declaired it in the beginning with thisprivate ArrayList<Coordinate> drawPts;
# 21
> i declaired it in the beginning with thisYup, I know, but you never initialized it.
# 22
OhhhWEll then ill fix that THANK SOOO MUCH FOR THE HELP!!!!!
# 23
Your ArrayList is never set. It's just null.
This compiles and runs without exceptions, but it doesn't draw any lines.
Your code isn't good.
%
package swing;
import javax.swing.JOptionPane;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
class Polygons
{
public Polygons(int x, int y)
{
}
}
public class DrawCurves extends Frame
{
CurvCanvas cBoard = new CurvCanvas();// Create area/Canvas for drawings
private List<Point> drawPts = new ArrayList<Point>();
private int numSides;
private String numSidesInput;
public DrawCurves()// Default constructor
{
add(new CurvCanvas());
setTitle("Curve Drawing");
setSize(1600, 1200);
addWindowListener(new MyLocalWindowAdapter());
cBoard.setSize(400, 300); // Note: size SHOULD fit in window
cBoard.setBackground(Color.GREEN);
add("Center", cBoard);
getInput();
setVisible(true);
}
private void getInput()
{
numSidesInput = JOptionPane.showInputDialog(null, "Please input the number of sides that you would like to use for your polygon");
numSides = Integer.parseInt(numSidesInput);
Polygons poly = new Polygons(numSides, 20);
}
private class MyLocalWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
dispose();
System.exit(0);
}
}
private class CurvCanvas extends Canvas
{
public void paint(Graphics g)
{
int numPoints = drawPts.size();
if (numPoints > 0)
{
for (int i = 0; i < numPoints - 1; ++i)
{
Point fromPt = drawPts.get(i);
Point toPt = drawPts.get(i + 1);
int fX = (int) fromPt.getX();
int fY = (int) fromPt.getY();
int tX = (int) toPt.getX();
int tY = (int) toPt.getY();
g.drawLine(fX, fY, tX, tY);
}
// Close the polygon by drawing the line from the last point to the start
Point from = drawPts.get(numPoints - 1);
Point to = drawPts.get(0);
g.drawLine((int) from.getX(), (int) from.getY(), (int) to.getX(), (int) to.getY());
g.setColor(Color.RED);
g.drawLine(100, 200, 600, 700);
}
}
}
public static void main(String[] args)
{
new DrawCurves();
}
}
# 24
You're mixing Swing and AWT - very bad idea, indeed.
Your design isn't good. You're working on UI and other stuff all at the same time. I'd advise that you get your Polygon and Point stuff worked out before you tackle the UI stuff at all. Make is solid before you worry about Swing.
%
# 25
well it uses Polgons(int, int ) wich according to this:
public Polygons(int numSides, int sideLength)
{
sides = numSides;
sideLength = sideLeng;
radius = 10;
cntrlAngl = 360/sides;
Cords = new ArrayList <Coordinate> (sides);
setCoordinates(cntrlAngl);
}
private void setCoordinates(double angle)
{
double tempAng = angle;
double xCor, yCor;
for(int I = 1; I == sides; I++)
{
xCor = radius * Math.cos(tempAng * DEG_TO_RAD);
yCor = radius * Math.sin(tempAng * DEG_TO_RAD);
Cords.add(new Coordinate(xCor, yCor));
tempAng += cntrlAngl;
}
}
should be initializing it....Right?
# 26
What?How is that Polygon constructor supposed to initialize a class member in DrawCurves?%
# 27
That poly variable is declared, initialized, and then immediately GC'd. You don't appear to need the Polygon class at all as written.%
# 28
As I thought - if I remove that line, I don't use the Polygon class at all. It's doing you no good whatsoever. It's a sign that you don't really understand your problem at all.%
# 29
this is true. Im sooo lost.
# 30
I use this :
private void getInput()
{
numSidesInput = JOptionPane.showInputDialog(null, "Please input the number of sides that you would like to use for your polygon");
numSides = Integer.parseInt(numSidesInput);
Polygons poly = new Polygons(numSides, 20);
}
whish according to what i've been tought should fill up the arraylist with points and that i guess is where my problem is then...?
# 31
...oooooooooook well then maybe asking what ur suggestions for a way to fix the problem/where to start to fix it would be a better wuestion;more prone to getting responded to .... .... ....... ...... ...... ...... ?
# 32
> I use this :
Yes, I see. It's wrong. Do you see why?
If not, think about it for a while. Go read about scope and what it means.
// This is the start of a method:
private void getInput()
{
// All the variables declared between the opening and closing braces are local TO THIS METHOD.
numSidesInput = JOptionPane.showInputDialog(null,
l, "Please input the number of sides that you would
like to use for your polygon");
numSides = Integer.parseInt(numSidesInput);
// This method is declared in this method. poly in this method is related to nothing else/
Polygons poly = new Polygons(numSides, 20);
}
// Now poly is out of scope and eligible for GC
// Your DrawCurves class might have a poly variable, but that's not this one.
> whish
"which"?
> according to what i've been tought
"taught"?
You've been taught wrong or, more likely, you've misunderstood entirely.
> should fill
> up the arraylist with points and that i guess is
> where my problem is then...?
Yes.
%
# 33
Go look at the code I've already posted for you.
I suggest that you start looking at that more carefully.
Here's another suggestion: start with a Polygon class, like this:
package swing;
import java.io.Serializable;
import java.awt.Point;
import java.util.List;
import java.util.ArrayList;
/**
* Created by IntelliJ IDEA.
* User: Michael
* Date: Dec 11, 2006
* Time: 8:38:41 PM
* To change this template use File | Settings | File Templates.
*/
public class Polygon implements Serializable
{
private List<Point> verticies;
public static void main(String[] args)
{
if ((args.length > 0) && ((args.length % 2) == 0))
{
List<Point> points = new ArrayList<Point>(args.length/2);
for (int i = 0; i < args.length;)
{
int x = Integer.parseInt(args[i++]);
int y = Integer.parseInt(args[i++]);
Point p = new Point(x, y);
points.add(p);
}
Polygon poly = new Polygon(points);
System.out.println(poly);
}
}
public Polygon()
{
this(new ArrayList<Point>());
}
public Polygon(List<Point> verticies)
{
this.verticies = new ArrayList<Point>(verticies);
}
public Point getPoint(int index)
{
return this.verticies.get(index);
}
public String toString()
{
return new StringBuilder().append("Polygon{").append("verticies=").append(verticies).append('}').toString();
}
}
%
# 34
ok well how would i go about using my iterator to put coordinates into the arraylist so that i could use it and so that it would work?
PS: Thank You soooo much for your patients and small words i really apreciate it(really this isnt one of those sarcasticly rude comments. I realy mean it!)
# 35
> ok well how would i go about using my iterator to put
> coordinates into the arraylist so that i could use it
> and so that it would work?
I wouldn't use an iterator the way you're doing it.
I'd move that option pane input stuff out of the class altogether.
I'd write a constructor that took a Polygon, like the one I gave you, and then I've have my paint method draw the Polygon. That's what you want, right?
I'd get the input Points from the command line, just like the example I gave you already.
> PS: Thank You soooo much for your patients and small
> words i really apreciate it(really this isnt one of
> those sarcasticly rude comments. I realy mean it!)
OK, I'm glad you find it helpful.
%
# 37
> MIKE COME HERE!!!
If I were duffymo, that right there would be enough to make me lose all interest in helping you.
People who answer here are volunteers. They help because they like to. They answer the threads they feel like, when they feel like. Nobody wants to be screamed at like your personal servant.
If you have a question, just post it. Don't yell. Don't use a bunch of exclamation marks. Don't seek out a specific individual.
Just post your question. If somebody is interested and can help, they will.
# 38
ok well so u know i was talking to my friend
# 39
> ok well so u know i was talking to my friend
Ah, I see.
I thought you were screaming for duffymo, since he's been helping you and his name is Michael. That kind of thing happens a lot.
Please do heed the advice about not shouting (all caps) and not using exclamation points, though. It doesn't convey any useful information and can be annoying.
# 40
> > ok well so u know i was talking to my friend
>
> Ah, I see.
>
> I thought you were screaming for duffymo, since he's
> been helping you and his name is Michael. That kind
> of thing happens a lot.
>
> Please do heed the advice about not shouting (all
> caps) and not using exclamation points, though. It
> doesn't convey any useful information and can be
> annoying.
Lucky for the OP that I had to get to bed. I wasn't awake enough to be summoned.
%
# 41
> > MIKE COME HERE!!!
>
>
> If I were duffymo, that right there would be enough
> to make me lose all interest in helping you.
>
> ...
I already lost it after reading the OP's reply (#2):
"OK SO I Realy NEED ur Help Please Try As Fast As U Can Im desperate!!! I'll Give u every duke buck i have to the person WhO Helps ME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!111111"
# 42
2nd reply? i lost interest when i read the title
# 43
The qustion IS what is drawPts!!! it's always null where do you create this Object?!!! no where as I see