Results 1 to 6 of 6
  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default How to paint multiple instances of class?

    I want to create a class that I can instantiate to paint a fill.oval multiple times on the screen at different locations. I'm having trouble understanding how to do this. The code I am working on:

    Java Code:
    package Main;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class DrawArena{
    	public static void main(String[] args) {
    		new DrawArena();
    	}
    
    	public DrawArena(){
    		JFrame frame = new JFrame("Drawing with Alpha");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(new ArenaComponent());
    		frame.setSize(800,600);
    		frame.setVisible(true);
    		
    	}
    
    	public class ArenaComponent extends JComponent{
    		public void paintComponent(Graphics g){
    
    			int w = getWidth();
    			int h = getHeight();
    
    			g.setColor(Color.black);
    			g.fillOval(w / 2 - ((w/2 + 20) / 2), 10, w / 2 + 20, h - 30);
    			
    			g.setColor(Color.white);
    			g.fillOval(w / 2 - w / 4, 20, w / 2, h - 50); 
    			//System.out.println("W: " + w + "\tH: " + h);
    			
    			new drawCircle();
    		}
    	}
    	
    	public class drawCircle{
    		public void draw(Graphics g){
    			g.setColor(Color.red);
    			g.fillOval(400, 100, 10, 10);
    		}
    	}
    }
    Also, after I have it working to draw one circle, I want to give drawCircle class int parametres where I'll tell it the x, y co-ords I want to draw the oval at. When I try to give drawCircle parametres like this: public class drawCircle(int x, y){, it gives me an error that class is the wrong "type".

    What am I doing wrong?

    Thanks.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,268
    Rep Power
    25

    Default Re: How to paint multiple instances of class?

    it gives me an error
    Please post the full text of the error message. Your edited version leaves out useful information.

    I want to give drawCircle class int parametres
    Give the class a constructor that has int parameters. You won't want to create a new instance of the DrawCircle class in the paintComponent method. It can be called many times.
    Last edited by Norm; 05-28-2012 at 11:50 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to paint multiple instances of class?

    Quote Originally Posted by Norm View Post
    Please post the full text of the error message. Your edited version leaves out useful information.
    It puts a red line under class and says "Syntax error on token "class", invalid Type", when I try to run it, it gives the error:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at Main.DrawArena.main(DrawArena.java:7)
    And highlights line 7: "public static void main(String[] args) {"

    Give the class a constructor that has int parameters. You won't want to create a new instance of the DrawCircle class in the paintComponent method. It can be called many times.
    Where do I want to create a new instance of DrawCircle? I want it to paint ontop of what Arena Component paints.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,268
    Rep Power
    25

    Default Re: How to paint multiple instances of class?

    It puts a red line
    Sorry, I can not see the red lines from here. Can you compile the program with the javac compiler and copy and paste the full text of the error messages here?

    Where do I want to create a new instance of DrawCircle?
    You can create the instances somewhere outside of the paintComponent() method and call each of the instances's draw() method from inside the paintComponent() method.
    Last edited by Norm; 05-29-2012 at 02:08 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to paint multiple instances of class?

    Thanks Norm, I made DrawCircle into its own class file and I instantiated it in the main method of DrawArena, gave the draw method in DrawCircle int parameters and called the DrawCircle.draw(g, x, y) in ArenaComponent.

    Java Code:
    package Main;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Random;
    
    public class DrawCircle{
    	static Random generator = new Random();
    	static int roll = generator.nextInt(100) + 1;
    	public DrawCircle(){
    		int x = 400;
    		int y = 100;
    	}
    	static void draw(Graphics g, int x, int y){
    		g.setColor(Color.red);
    		g.fillOval(400, roll, 10, 10);
    	}
    }
    Java Code:
    package Main;
    
    import javax.swing.*;
    import java.awt.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class DrawArena{
    	public static void main(String[] args) {
    		new DrawArena();
    		new DrawCircle();		
    	}
    
    	public DrawArena(){
    		JFrame frame = new JFrame("Drawing with Alpha");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(new ArenaComponent());
    		frame.setSize(800,600);
    		frame.setVisible(true);
    	}
    
    	public class ArenaComponent extends JComponent{
    		public void paintComponent(Graphics g){
    			
    			int w = getWidth();
    			int h = getHeight();
    
    			g.setColor(Color.black);
    			g.fillOval(w / 2 - ((w/2 + 20) / 2), 10, w / 2 + 20, h - 30);
    			
    			g.setColor(Color.white);
    			g.fillOval(w / 2 - w / 4, 20, w / 2, h - 50); 
    			//System.out.println("W: " + w + "\tH: " + h);
    			
    			DrawCircle.draw(g, 300, 100);
    		}
    	}
    }
    Now I'm trying to figure out how to paint 2 circles at the same time. I can't just call DrawCircle.draw multiple times with different parameters because I guess it just moves the first circle instead of creating 2 different ones. I guess I need to create multiple instances of DrawCircle, maybe with an ArrayList, will keep trying.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,268
    Rep Power
    25

    Default Re: How to paint multiple instances of class?

    create multiple instances of DrawCircle, maybe with an ArrayList,
    That is the way to do it. Remove the static definitions so each instance of the DrawCircle class has its own method and variables
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 02-17-2012, 01:49 PM
  2. Multiplayer game - multiple instances
    By robs in forum Java Gaming
    Replies: 0
    Last Post: 04-18-2011, 09:33 AM
  3. Multiple GUI Interface Instances
    By mutagen in forum AWT / Swing
    Replies: 2
    Last Post: 03-19-2011, 03:06 PM
  4. Replies: 16
    Last Post: 05-22-2010, 03:51 AM
  5. Problem calling multiple instances of a class
    By virex in forum New To Java
    Replies: 1
    Last Post: 03-02-2010, 03:03 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
  •