Results 1 to 20 of 20
Thread: Simple help with a circle.
- 08-08-2011, 01:17 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
Simple help with a circle.
I'm trying to create a circle class that draws a circle using parameters given to it by another class.
I found this code on the net and it works, but I don't understand how to change it so that another class will tell it what to draw.
I understand that these are the parametersJava Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Greencircle extends Frame{ Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f); public void paint(Graphics g){ Graphics2D ga = (Graphics2D)g; ga.draw(circle); ga.setPaint(Color.green); ga.fill(circle); } public static void main(String args []){ Frame frame = new Greencircle(); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); frame.setSize(300, 250); frame.setVisible(true); } }
Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
but I don't know how to change them to variables so another class will change them.
Reading this over it doesn't sound very clear, but what I'd like is to have is
in the main of another class where 5 is x position, 6 y, and 7 the radius.Java Code:Greencircle gc1 = new Greencircle (5,6,7)
- 08-08-2011, 01:29 AM #2
OKJava Code:Greencircle gc1 = new Greencircle (5,6,7)
That code is calling the constructor of the Greencircle class and passes 3 int parameters. So you first task is to write a constructor and assign those parameters to variables. Then instead of hard coding the values when the Shape is created, you use your variables instead.
- 08-08-2011, 01:59 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
I tried that, but I don't know how. :/
I ended up with this.
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Greencircle extends Frame{ int px, py, radius; Shape circle = new Ellipse2D.Float(int px, int py, int radiusH, int radiusW); public Greencircle (int positionxIn, int positionyIn, int radiusIn){ px = positionxIn; py = positionyIn; radius = radiusIn; radiusH = radius; radiusW = radius; } public void paint(Graphics g){ Graphics2D ga = (Graphics2D)g; ga.draw(circle); ga.setPaint(Color.green); ga.fill(circle); } public static void main(String args []){ Frame frame = new Greencircle (5,6,7); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); frame.setSize(300, 250); frame.setVisible(true); } }
- 08-08-2011, 02:04 AM #4
Remember we do not read minds. "It doesn't work" provides zero information to us. If you get errors then copy and paste the full and exact error message and indicate on which line it occurs.
One thing I can see, what is wrong with the below line of code?
Java Code:String s = "hello": System.out.println(String s);
- 08-08-2011, 02:25 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
- 08-08-2011, 02:47 AM #6
I said copy and paste. I cannot see certain screeshots as they get blocked. Also screenshots can be small and hard to view.
Ooops! That was a typo. I was referring to the error in the print statement
- 08-08-2011, 03:02 AM #7
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
You just say ...Java Code:Description Resource Path Location Type radiusH cannot be resolved to a variable Greencircle.java /temp/src line 15 Java Problem radiusW cannot be resolved to a variable Greencircle.java /temp/src line 16 Java Problem Return type for the method is missing Greencircle.java /temp/src line 9 Java Problem Syntax error on token "int", delete this token Greencircle.java /temp/src line 9 Java Problem Syntax error on token "int", delete this token Greencircle.java /temp/src line 9 Java Problem Syntax error on token "int", delete this token Greencircle.java /temp/src line 9 Java Problem Syntax error on token "int", delete this token Greencircle.java /temp/src line 9 Java Problem This method requires a body instead of a semicolon Greencircle.java /temp/src line 9 Java Problem
...?Java Code:System.out.println(s);
- 08-08-2011, 03:16 AM #8
The IDE or compiler can not find a definition for the variables listed in the error message. You need to define those variables.radiusH cannot be resolved to a variable
Here the IDE is telling you what to do. Remove the "int" from this line.Syntax error on token "int", delete this token
- 08-08-2011, 03:48 AM #9
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
Okay, this is the code I have now.
In the Greencircle class I have this.
And in the Runner class I have this.Java Code:package pack1; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Greencircle extends Frame{ int px, py, radius,radiusH , radiusW; Shape circle = new Ellipse2D.Float(px, py, radiusH, radiusW); public Greencircle (int positionxIn, int positionyIn, int radiusIn){ px = positionxIn; py = positionyIn; radius = radiusIn; radiusH = radius; radiusW = radius; } public void paint(Graphics g){ Graphics2D ga = (Graphics2D)g; ga.draw(circle); ga.setPaint(Color.green); ga.fill(circle); } }
A blank white window appears when the program is run, but no circle in it.Java Code:package pack1; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class runner { public static void main(String[] args){ Greencircle gc = new Greencircle (5,6,7); System.out.println("hello"); Frame frame = new Greencircle(5,6,7); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); frame.setSize(300, 250); frame.setVisible(true); System.out.println(gc); }
There are no error messages, and the console says this:
Java Code:hello pack1.Greencircle[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal]
- 08-08-2011, 03:58 AM #10
- 08-08-2011, 04:18 AM #11
Check the coordinates of the circle. Make it big enough to see initially.
Another thing, you are creating two instances of Greencircle. Its a JFrame.
- 08-08-2011, 04:39 AM #12
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
This is my new code.
It gives me this error message.Java Code:package pack1; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Greencircle extends Frame{ int px, py, radius,radiusH , radiusW; public Greencircle (int positionxIn, int positionyIn, int radiusIn){ px = positionxIn; py = positionyIn; radius = radiusIn; radiusH = radius; radiusW = radius; Shape circle = new Ellipse2D.Float(px, py, radiusH, radiusW); } public void paint(Graphics g){ Graphics2D ga = (Graphics2D)g; ga.draw(circle); ga.setPaint(Color.green); ga.fill(circle); } }
I know this is because circle is a local variable and stops existing so I wrote this:Java Code:Description Resource Path Location Type circle cannot be resolved to a variable Greencircle.java /temp2/src/pack1 line 17 Java Problem circle cannot be resolved to a variable Greencircle.java /temp2/src/pack1 line 19 Java Problem
And got this error.Java Code:package pack1; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Greencircle extends Frame{ int px, py, radius,radiusH , radiusW; Shape circle = new Ellipse2D.Float(px, py, radiusH, radiusW); public Greencircle (int positionxIn, int positionyIn, int radiusIn){ px = positionxIn; py = positionyIn; radius = radiusIn; radiusH = radius; radiusW = radius; circle = (px, py, radiusH, radiusW); } public void paint(Graphics g){ Graphics2D ga = (Graphics2D)g; ga.draw(circle); ga.setPaint(Color.green); ga.fill(circle); } }
I don't know how to change the variables of circle after its been created.Java Code:Description Resource Path Location Type Syntax error on token "=", Name expected after this token Greencircle.java /temp2/src/pack1 line 14 Java Problem
@ Norm
I added System.out.println(gc); to the end of runner to check for that.
"pack1.Greencircle[frame0,0,0,0x0,invalid,hidden,layout=java.awt.Bord erLayout,title=,resizable,normal]"
I don't know much about this but I think that the first four 0's are the x and y position and the width and height (the radius). So my conclusion, just like Junky is that its just drawing circle with no height and no width (so not drawing any thing at all) at the 0x and 0y position.Last edited by Nap; 08-08-2011 at 04:47 AM.
- 08-08-2011, 04:42 AM #13
Just like the int variables you do not have to initialise your circle variable when you declare it.
- 08-08-2011, 05:07 AM #14
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
So this?

Now I get the last error + a "misplaced construst".Java Code:package pack1; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Greencircle extends Frame{ int px, py, radius,radiusH , radiusW; Shape circle = new Ellipse2D.Float; public Greencircle (int positionxIn, int positionyIn, int radiusIn){ px = positionxIn; py = positionyIn; radius = radiusIn; radiusH = radius; radiusW = radius; circle = (px, py, radiusH, radiusW); } public void paint(Graphics g){ Graphics2D ga = (Graphics2D)g; ga.draw(circle); ga.setPaint(Color.green); ga.fill(circle); } }
Sorry, but I learn better by seeing a code example. Could you write a simple example program for me to dissect?Java Code:Description Resource Path Location Type Syntax error on token "=", Name expected after this token Greencircle.java /temp2/src/pack1 line 14 Java Problem Syntax error on token(s), misplaced construct(s) Greencircle.java /temp2/src/pack1 line 7 Java Problem
Last edited by Nap; 08-08-2011 at 05:28 AM.
- 08-08-2011, 05:15 AM #15
C'mon. You need to think. Does that line of code look correct?Java Code:circle = (px, py, radiusH, radiusW);
Java Code:class Foo { String text; Bar b; Foo(String t) { text = t; b = new Bar(text); } }
- 08-08-2011, 05:26 AM #16
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
Oh okay! So...
this?Java Code:package pack1; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Greencircle extends Frame{ int px, py, radius,radiusH , radiusW; Shape circle; public Greencircle (int positionxIn, int positionyIn, int radiusIn){ px = positionxIn; py = positionyIn; radius = radiusIn; radiusH = radius; radiusW = radius; circle = new Ellipse2D.Float(px, py, radiusH, radiusW); } public void paint(Graphics g){ Graphics2D ga = (Graphics2D)g; ga.draw(circle); ga.setPaint(Color.green); ga.fill(circle); } }
It's still not working. Same as before, no errors, white window with no circle, and console prints out
Java Code:hello pack1.Greencircle[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal]
- 08-08-2011, 05:39 AM #17
Works fine for me. Where is your main?
- 08-08-2011, 06:32 AM #18
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
Java Code:package pack1; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class runner { public static void main(String[] args){ Greencircle gc = new Greencircle (5,6,7); System.out.println("hello"); Frame frame = new Greencircle(5,6,7); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); frame.setSize(300, 250); frame.setVisible(true); System.out.println(gc); } }
- 08-08-2011, 06:51 AM #19
As Norm had mentioned above, why are you creating 2 instances?Java Code:Greencircle gc = new Greencircle (5,6,7); Frame frame = new Greencircle(5,6,7);
Also it appears the values you are using are too small. Try using larger values.
- 08-08-2011, 04:00 PM #20
Member
- Join Date
- Aug 2011
- Posts
- 12
- Rep Power
- 0
Hmmm, ya that was dumb.
Thanks :) It works now.Java Code:package pack1; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class runner { public static void main(String[] args){ Greencircle gc = new Greencircle (50,60,70); System.out.println("hello"); gc.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); gc.setSize(300, 250); gc.setVisible(true); System.out.println(gc); } }
Similar Threads
-
Why is there no circle?
By JohnPringle83 in forum New To JavaReplies: 3Last Post: 05-23-2011, 08:07 AM -
Odd results from simple circle area/circumference calculation
By cottoneyemoe in forum New To JavaReplies: 18Last Post: 05-22-2011, 10:00 PM -
Josephu's Circle
By Pyrexkidd in forum New To JavaReplies: 8Last Post: 08-01-2010, 08:44 AM -
Circle arrays
By n00b in forum New To JavaReplies: 15Last Post: 05-05-2010, 05:04 PM


LinkBack URL
About LinkBacks
Reply With Quote




Bookmarks