Results 1 to 6 of 6
- 05-28-2012, 05:23 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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:
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".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); } } }
What am I doing wrong?
Thanks.
- 05-28-2012, 11:43 PM #2
Re: How to paint multiple instances of class?
Please post the full text of the error message. Your edited version leaves out useful information.it gives me an error
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.I want to give drawCircle class int parametresLast edited by Norm; 05-28-2012 at 11:50 PM.
If you don't understand my response, don't ignore it, ask a question.
- 05-29-2012, 09:14 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Re: How to paint multiple instances of class?
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:
And highlights line 7: "public static void main(String[] args) {"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at Main.DrawArena.main(DrawArena.java:7)
Where do I want to create a new instance of DrawCircle? I want it to paint ontop of what Arena Component paints.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.
- 05-29-2012, 02:06 PM #4
Re: How to paint multiple instances of class?
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?It puts a red line
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.Where do I want to create a new instance of DrawCircle?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.
- 05-29-2012, 03:46 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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); } }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.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); } } }
- 05-29-2012, 04:32 PM #6
Re: How to paint multiple instances of class?
That is the way to do it. Remove the static definitions so each instance of the DrawCircle class has its own method and variablescreate multiple instances of DrawCircle, maybe with an ArrayList,If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
How To Paint Multiple Objects Of Same Class Onto One JPanel
By Wangagat in forum AWT / SwingReplies: 2Last Post: 02-17-2012, 01:49 PM -
Multiplayer game - multiple instances
By robs in forum Java GamingReplies: 0Last Post: 04-18-2011, 09:33 AM -
Multiple GUI Interface Instances
By mutagen in forum AWT / SwingReplies: 2Last Post: 03-19-2011, 03:06 PM -
Drawing multiple instances of one class with an array
By Grimmjow in forum New To JavaReplies: 16Last Post: 05-22-2010, 03:51 AM -
Problem calling multiple instances of a class
By virex in forum New To JavaReplies: 1Last Post: 03-02-2010, 03:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks