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

    Default Another simple circle problem

    How do I add multiple circle objects to the window?

    Greencircle class

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

    Java Code:
    package pack1;
    import java.awt.*;
    import java.awt.geom.*;
    
    public class Blackcircle extends Frame{
    	
    	int  px, py, radius, radiusH, radiusW;
    	
    	Shape dot;
    	
    	public Blackcircle(int positionxIn, int positionyIn, int radiusIn){
    		px = positionxIn;
    		py = positionyIn;
    		radius = radiusIn;
    		radiusH = radius;
    		radiusW = radius;
    		dot = new Ellipse2D.Float(px, py, radiusH, radiusW);
    	}
    	
    	public void paint(Graphics g){
    		Graphics2D ga = (Graphics2D)g;
    		ga.draw(dot);
    		ga.setPaint(Color.black);
    		ga.fill(dot);
    	}
    }
    Runner with main

    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);
    		Blackcircle rb1 = new Blackcircle(100, 200, 5);
    		System.out.println("hello");
    		Frame f1 = new Frame();
    		f1.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent we){
    				System.exit(0);
    			}
    		});
    		f1.setSize(300, 250);
    		f1.setVisible(true);
    		[COLOR="red"]// f1.add(gc); //this is the part I don't know[/COLOR]
    		System.out.println(gc);
    		System.out.println(rb1);
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    Um, create multiple instances and store them in a List?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Or don't extend jframe, just make circle a class which knows how to draw itself. Create a few and then draw them all on a JPanel and add the panel to a jframe.

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

    Default

    I need different circle types, a total of 3 different classes.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    My advice still holds. A circle is not a jframe, so it shouldn't inherit from it.

    Create three classes that look like this:

    Java Code:
    public class Circle{
      public void drawCircle(Graphics g){
        //draw the circle here
      }
    }

Similar Threads

  1. Simple help with a circle.
    By Nap in forum New To Java
    Replies: 19
    Last Post: 08-08-2011, 04:00 PM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  3. Replies: 18
    Last Post: 05-22-2011, 10:00 PM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  5. problem with adding circle and rectangle
    By nicnicnic in forum Java 2D
    Replies: 3
    Last Post: 11-16-2009, 06:13 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
  •