Results 1 to 13 of 13
  1. #1
    Farscience is offline Farscience
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Missing Entities

    (Old Post) I'm making a program that will simulate viruses attacking cells, then reproducing. The problem is, I made an entity class, then 3 classes that extend it: FoodEntity, CellEntity, and VirusEntity. When I try to use my addEntity(entity) method, it only add the first one, and doesn't show the others. I added some debugging lines, and it tells me that everything is all set up and being sent to the draw method, and that everything occupies different x and y coordinates, but nothing is being draw except the first thing. I added a link to a zip file containing all of the source code, and it will compile.


    Thanks.

    Edit: Working on a smaller program that replicates the issue. Feel free to look at the main one, though.

    Edit2: Here it is:


    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.util.ArrayList;
    import java.util.Random;
    
    import javax.swing.JFrame;
    
    
    public class Main {
    	
    	public static JFrame win=new JFrame();
    	public static BufferStrategy strategy;
    	
    	private static ArrayList entities=new ArrayList();
    	
    	private static Random rnd=new Random();
    	
    	public static void main(String[] args){
    		initWin();
    		initEntities();
    		
    		loop();
    	}
    	
    	public static void loop(){
    		strategy=win.getBufferStrategy();
    		Graphics g=strategy.getDrawGraphics();
    		
    		g.setColor(Color.white);
    		g.fillRect(0, 0, 550, 450);
    		
    		for(int a=0;a<entities.size();a++){
    			Entity e=(Entity)entities.get(a);
    			
    			e.logic();
    			e.move();
    			e.draw(g);
    		}
    		
    		g.dispose();
    		strategy.show();
    		
    		try {Thread.sleep(10);} catch (InterruptedException e) {}
    	}
    	
    	public static void initWin(){
    		win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		win.setSize(550,450);
    		win.setLocationRelativeTo(null);
    		win.setResizable(false);
    		win.setIgnoreRepaint(true);
    		win.setVisible(true);
    		
    		win.createBufferStrategy(2);
    		strategy=win.getBufferStrategy();
    	}
    	
    	public static void initEntities(){
    		for(int a=0;a<5;a++){
    			Entity e=new CircleEntity(rnd.nextInt(500)+20, rnd.nextInt(400)+10);
    			entities.add(e);
    		}
    	}
    
    }
    //Next Class:
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public abstract class Entity {
    	
    	protected double x,y,xm,ym;
    	protected Color color;
    	
    	public Entity(double x, double y){
    		this.x=x;
    		this.y=y;
    	}
    	
    	public void draw(Graphics g){
    		
    	}
    	
    	public void move(){
    		x+=xm;
    		y+=ym;
    	}
    	
    	public void logic(){
    		
    	}
    
    }
    // Last one:
    import java.awt.Graphics;
    import java.awt.Color;
    import java.util.Random;
    
    
    public class CircleEntity extends Entity{
    	
    	private Random rnd=new Random();
    
    	public CircleEntity(double x, double y) {
    		super(x, y);
    	}
    	
    	public void draw(Graphics g){
    		g.setColor(Color.black);
    		g.fillOval((int)x, (int)y, 20, 20);
    		g.dispose();
    	}
    	
    	public void logic(){
    		xm=rnd.nextInt(3)-1;
    		ym=rnd.nextInt(3)-1;
    	}
    
    }

    As you can see, the circles don't move, and even though I added 5, there is only one. Any help would be appreciated.

    P.S. I kept the original post+files just in case somebody preferred that (which I doubt, but you never know).
    P.P.S. The other classes are different files, everything isn't one file(there's 3 individual .java files).


    (Old files) https://dl.dropbox.com/u/68488237/Program.zip
    Last edited by Farscience; 09-24-2012 at 03:02 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Missing Entities

    1. You didn't bother to reply to doWhile's response in your earlier thread: How to animate without flickering? Not the best way to continue to get advice on a forum, that.

    2. Many members won't lick on external links, and of those who are willing to click, many can't access file sharing sites as those are blocked by a corporate firewall. Also, if the code is too much to post here, it's usually too much to expect a volunteer to go through. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Not all your code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Farscience is offline Farscience
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Missing Entities

    There are only 6 files, and they aren't too big. I just thought they would take up too much space here. You can scan it with whatever anti-virus program you want, it's clean. I replied to the other guy, I read his response and forgot to say thanks.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Missing Entities

    Unfortunately, all three of Darryl's point 2 apply to me, so I can't attempt to solve your problem for you.

    And, honestly, the following is not meant as any sort of personal attack.

    The main problem I want solved is the ability to add as many entities as I want. I know there are probably many other errors, but please don't respond just saying something like "you misspelled this variable", unless that is related to the big problem. Once that's taken care of, feel free to offer any advice though
    That's not a realistic way of going about asking strangers for help. It just isn't.

    Forums like this are text mediated places. (@world: A picture of an IDE is *not* worth a thousand words. And - god help us - a youtube link is worth even less.) So we have nothing like "tone of voice" or raised eyebrows and all the rest to convey our intent. It follows that we have to choose our words carefully in order to avoid giving an impression we don't mean to give and which we will find unproductive.

    ---

    Construct a SSCCE and describe its behaviour as well as the behaviour you expect or intend.

    ---

    Feel free to ignore whatever you find ignorable (including this!). But what you're beginning is a discussion, and people say whatever they like in a discussion. You have no more say over the points others might make than Canute had over the tide. (And, to the enduring credit of that famous king, human limitations were the very thing he was trying to demonstrate.)

  5. #5
    Farscience is offline Farscience
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Missing Entities

    Sorry, i just used another forum once and I got about five answer completely irrelevant to my question. I'm already working on a new, small program that replicates my problem, then I'll edit my original message and put it in.

    Edit: Done!
    Last edited by Farscience; 09-24-2012 at 03:02 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Missing Entities

    Great, thanks!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Missing Entities

    Your way of doing graphics and animation are foreign to me, and usually I would do my drawing in a JPanel or JComponent's paintComponent(...) override and my animation with a Swing Timer. Where did you receive instruction to do what you are doing above? Do you have a link that we can review because again it is foreign to me and doesn't seem right.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Missing Entities

    Foreign to me too. But, anyway...

    loop() doesn't actually loop! That is it's called once and that's it.

    Which leaves the question of why there's only one circle painted. In the CircleEntity's draw() you call g.dispose(). So what is going to happen when the next entity tries to draw itself using g? (Note that loop() itself calls dispose() and that's probably enough.)

    -----

    Having corrected those little things, I see 5 circles doing a sort of Brownian dance.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Missing Entities

    Looks a bit like archaic AWT code to me, but then I started learning Java long after Swing superseded AWT so I could be wrong.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Farscience is offline Farscience
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Missing Entities

    Your way of doing graphics and animation are foreign to me, and usually I would do my drawing in a JPanel or JComponent's paintComponent(...) override and my animation with a Swing Timer. Where did you receive instruction to do what you are doing above? Do you have a link that we can review because again it is foreign to me and doesn't seem right.
    I might have learned from an old source, but it makes sense to me. I learned from a lot of places, but the biggest one was:
    Space Invaders 101 - An Accelerated Java 2D Tutorial

    Foreign to me too. But, anyway...

    loop() doesn't actually loop! That is it's called once and that's it.

    Which leaves the question of why there's only one circle painted. In the CircleEntity's draw() you call g.dispose(). So what is going to happen when the next entity tries to draw itself using g? (Note that loop() itself calls dispose() and that's probably enough.)

    -----

    Having corrected those little things, I see 5 circles doing a sort of Brownian dance.
    Thanks a bunch. At the end of the loop() method, I added a: loop(), so that it goes back and loops forever. I got rid of the unnecessary g.dispose() method and now I see 5 circles that have eaten too much sugar

    I'm going to try it on my main program to see if that fixes everything. So far, the g.dispose() seems to be the culprit.

    Edit: It works 100% (probably 90% lol) now! I knew it was some tiny little thing :p. Thanks.

    P.S. I shall return (when the bugs do :D).
    Last edited by Farscience; 09-24-2012 at 11:52 PM.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Missing Entities

    At the end of the loop() method, I added a: loop(), so that it goes back and loops forever
    This is not really the best way of doing things.

    Every time loop() is called the computer sets things up so that you can eventually return from loop() and continue what you were doing. (The computer has no idea that you don't intend returning.) Given enough time I would expect your program to run out of "stack" which is the name given to the memory takn up with remembering that you called loop() from loop() which was called from loop() etc, etc.

    A better way to go is to put a while loop in main():

    Java Code:
    public static void main(String[] args){
        initWin();
        initEntities();
        //System.out.println("Entities size: " + entities.size());
        while(true) {
            loop();
        }
    }

  12. #12
    Farscience is offline Farscience
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Missing Entities

    Quote Originally Posted by pbrockway2 View Post
    This is not really the best way of doing things.

    Every time loop() is called the computer sets things up so that you can eventually return from loop() and continue what you were doing. (The computer has no idea that you don't intend returning.) Given enough time I would expect your program to run out of "stack" which is the name given to the memory takn up with remembering that you called loop() from loop() which was called from loop() etc, etc.

    A better way to go is to put a while loop in main():

    Java Code:
    public static void main(String[] args){
        initWin();
        initEntities();
        //System.out.println("Entities size: " + entities.size());
        while(true) {
            loop();
        }
    }
    Your prediction was 100% true! I was wondering what a "stack overflow" was. Thanks.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. How to resolve entities in a SAX parser - please help
    By DerekRaimann in forum Advanced Java
    Replies: 2
    Last Post: 03-04-2011, 11:38 AM
  2. Replies: 5
    Last Post: 02-25-2010, 02:06 PM
  3. entities are passed by value or passed by reference
    By syntrax in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 07:13 AM
  4. EntityManager.refresh() - Works on Detatched Or Managed Entities ?
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-07-2009, 03:34 AM
  5. EntityManager.refresh() works only on managed entities.
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 12-05-2009, 12:11 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
  •