Results 1 to 20 of 20
  1. #1
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Exclamation 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.

    Java 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);
    	}
    	
    }
    I understand that these are the parameters

    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

    Java Code:
    Greencircle gc1 = new Greencircle (5,6,7)
    in the main of another class where 5 is x position, 6 y, and 7 the radius.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    Greencircle gc1 = new Greencircle (5,6,7)
    OK

    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.

  3. #3
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    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);
    	}
    	
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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);

  5. #5
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    Command line error messages.



    Eclipse Code.



    Eclipse Error messages.



    Quote Originally Posted by Junky View Post
    One thing I can see, what is wrong with the below line of code?
    Java Code:
    String s = "hello":
    System.out.println(String s);
    : not ;?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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

  7. #7
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    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
    You just say ...

    Java Code:
    System.out.println(s);
    ...?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,876
    Rep Power
    25

    Default

    radiusH cannot be resolved to a variable
    The IDE or compiler can not find a definition for the variables listed in the error message. You need to define those variables.

    Syntax error on token "int", delete this token
    Here the IDE is telling you what to do. Remove the "int" from this line.

  9. #9
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    Okay, this is the code I have now.

    In the Greencircle 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);
    	}
    }
    And in the Runner class I have this.

    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);
    	}
    A blank white window appears when the program is run, but no circle in it.

    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]

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by Nap View Post
    A blank white window appears when the program is run, but no circle in it.
    Your problem is that you are creating the Circle before you assign values to the variables. Therefore it will use the default value which is zero. You need to create the Circle inside the constructor after all the variables have been assigned.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,876
    Rep Power
    25

    Default

    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.

  12. #12
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    This is my new code.

    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);
    		}
    }
    It gives me this error message.

    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
    I know this is because circle is a local variable and stops existing so I wrote 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;
    		circle = (px, py, radiusH, radiusW);
    	}
    	public void paint(Graphics g){
    		Graphics2D ga = (Graphics2D)g;
    		ga.draw(circle);
    		ga.setPaint(Color.green);
    		ga.fill(circle);
    		}
    }
    And got this error.

    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
    I don't know how to change the variables of circle after its been created.

    @ 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 05:47 AM.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Just like the int variables you do not have to initialise your circle variable when you declare it.

  14. #14
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    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 = 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);
    		}
    }
    Now I get the last error + a "misplaced construst".

    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
    Sorry, but I learn better by seeing a code example. Could you write a simple example program for me to dissect?
    Last edited by Nap; 08-08-2011 at 06:28 AM.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    circle = (px, py, radiusH, radiusW);
    C'mon. You need to think. Does that line of code look correct?

    Java Code:
    class Foo {
        String text;
        Bar b;
    
        Foo(String t) {
            text = t;
            b = new Bar(text);
        }
    }

  16. #16
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    Oh okay! So...

    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);
    		}
    }
    this?

    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]

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Works fine for me. Where is your main?

  18. #18
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    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);
    	}
    }

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    Greencircle gc = new Greencircle (5,6,7);
    Frame frame = new Greencircle(5,6,7);
    As Norm had mentioned above, why are you creating 2 instances?

    Also it appears the values you are using are too small. Try using larger values.

  20. #20
    Nap
    Nap is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    Hmmm, ya that was dumb.

    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);
    	}
    }
    Thanks :) It works now.

Similar Threads

  1. Why is there no circle?
    By JohnPringle83 in forum New To Java
    Replies: 3
    Last Post: 05-23-2011, 09:07 AM
  2. Replies: 18
    Last Post: 05-22-2011, 11:00 PM
  3. Josephu's Circle
    By Pyrexkidd in forum New To Java
    Replies: 8
    Last Post: 08-01-2010, 09:44 AM
  4. Circle arrays
    By n00b in forum New To Java
    Replies: 15
    Last Post: 05-05-2010, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •