Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2010
    Rep Power

    Default Instantiating Multiple Graphics objects

    Hello guys, I'm trying to make an application that produces multiple moving circles. I have 3 classes, an Orb class that creates and animates the circle, a mainFrame class that works as a container for the circles, and a masterClass class that works as the main class.

    The problem that I've been having is that whenever I try to instantiate more than one "Orb" object, Java only instantiates the last one. Can anyone please help me understand why this is and help me fix this? I have a feeling that it has something to do with the Graphics but I don't know how to tackle the problem.

    Here is the source code:

    Orb class
    Java Code:
    import java.awt.*;
    import java.awt.image.ImageObserver;
    import java.text.AttributedCharacterIterator;
    import java.awt.Graphics;
    import javax.swing.*;
    import java.util.Random;
    public class Orb extends JPanel implements Runnable
      //Create random Integers
      Random xAxis = new Random();
     Random yAxis = new Random();
     //Assign random integers to Variables
     private int x;
     private int y;
     private int numberX;
     private int numberY;
     private String word;
     public Orb(String w)
      x = xAxis.nextInt(500);
      y = yAxis.nextInt(500);
     public void setWord(String w)
      word = w; 
     public String getWord()
       return word;
     ///////DRAWING METHOD AND RUN METHOD/////////////////////////////
     //this paintComponent method creates the circle
     public synchronized void paintComponent(Graphics g){
      g.fillOval( x,  y, 50,50);
      //this method draws a circle by calling the paintComponent method
        public void run() {
          try {
             // change x and y in a Timer or background thread
           while ((x + numberX < getWidth() && y + numberY < getHeight())) {
       ///this tests which object gets instantiated/////
        Random increaseXadd = new Random();
               Random increaseYadd = new Random();
               Random increaseXsubtract = new Random();
               Random increaseYsubtract = new Random();
               int numberXsubtract = increaseXsubtract.nextInt(4);
               int numberYsubtract = increaseYsubtract.nextInt(4);
               numberX = increaseXadd.nextInt(4)-numberXsubtract;
               numberY = increaseYadd.nextInt(4)-numberYsubtract;
                x+= numberX; 
                y+= numberY;
        //////SHOWS THE COORDINATES OF THE OBJECT////////        
                System.out.println("x: "+x+"  y: "+y);
          } catch (Exception e) {

    mainFrame class

    Java Code:
    import javax.swing.JOptionPane;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.FlowLayout;
    public class mainFrame extends JFrame{
      public mainFrame(Orb o1, Orb o2, int x, int y){
      ///////o2 gets instantiated
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    //THis is the main class
    public class MasterClass{
      public static void main(String[] args){
    String word = "poop1";
    String word2 = "poop2";
    Orb o1 = new Orb(word);
    Orb o2 = new Orb(word2);
      Thread t2 = new Thread(o1);
      Thread t1 = new Thread(o2);
        mainFrame newMainFrame = new mainFrame(o1, o2, 500, 500);

    Thanks for the help guys I appreciate it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    JFrame (or actually its contentPane which you are in fact adding your components to) uses BorderLayout by default. And if you add a component to a BorderLayout-using container without also specifying a BorderLayout constant, then that component gets added to the Container BorderLayout.CENTER, replacing anything added previously BorderLayout.CENTER. So, one thing to possibly do is to give the JFrame's contentPane a different layout manager.

    Other issues:
    * I'm cringing when I see synchronized as a modifier of your paintComponent method and I'm absolutely sure that it's not necessary. Get rid of it.
    * If you want moving circles that are all in the same frame, then I wouldn't have your orb class extend JPanel but rather just give orb the logic behind your moving objects (position field, ability to change position randomly) and drawing code (a draw or paint method that accepts a Graphics object and lets the Orb object draw itself).
    * Then create a JPanel that holds an ArrayList<Orb> and add your Orb objects to the JPanel. In the JPanel's paintComponent method, iterate through the array list and tell each Orb object to paint itself.
    * Each class/object only needs one Random object, and you would not re-create this object inside of any loop or run method.
    * I think that your background threading will work but many of us would try to do this with a Swing Timer first.
    Last edited by Fubarable; 11-25-2010 at 01:53 PM.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Ljubljana, Slovenia
    Rep Power


    Quote Originally Posted by Fubarable View Post
    * If you want moving circles that are all in the same frame, then I wouldn't have your orb class extend JPanel but rather just give orb the logic behind your moving objects (position field, ability to change position randomly) and drawing code (a draw or paint method that accepts a Graphics object and lets the Orb object draw itself).
    This is one thing I used to do as well, untill a course I took this year at my uni. There, one of the assistant professors has shown us a different organisational paradigm that is closer to the MVC principle, where objects know their state and know how to respond to input (the model and controller part) and a central rendering class takes care of all the drawing work (the view part). It does make sense in the sense, if you let the object draw itself, it doesn't know how to draw itself in the context of the scene it is part of. Now, as long as projects are small and mainly of a learn/test nature, these things don't matter too much, but I just thought I'd throw it out there. Also, the guy who showed this to us knows what he's talking about, he used to write articles for a gaming magazine and ported XNA to objective c with the use of OpenGL instead of DirectX, making it usefull for making graphic applications for Apple handhelds. So all in all, I trust his judgement in this :D
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Multiple Graphics Objects?
    By MrFish in forum Java 2D
    Replies: 7
    Last Post: 10-29-2010, 08:37 PM
  2. using multiple methods for graphics
    By jforce93 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2010, 07:05 PM
  3. Clear Graphics Objects from Jpanel
    By DavidG24 in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2009, 10:34 PM
  4. how to deserialize multiple objects in a file
    By xcallmejudasx in forum Advanced Java
    Replies: 11
    Last Post: 12-16-2008, 06:29 PM
  5. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 04:57 AM

Posting Permissions

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