Results 1 to 13 of 13
Thread: Missing Entities
- 09-24-2012, 01:26 AM #1
Farscience
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
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.zipLast edited by Farscience; 09-24-2012 at 03:02 AM.
- 09-24-2012, 01:45 AM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-24-2012, 02:15 AM #3
Farscience
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
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.
- 09-24-2012, 02:17 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
That's not a realistic way of going about asking strangers for help. It just isn't.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
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.)
- 09-24-2012, 02:31 AM #5
Farscience
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
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.
- 09-24-2012, 03:10 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Missing Entities
Great, thanks!
-
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.
- 09-24-2012, 03:32 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 09-24-2012, 04:47 AM #9
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-24-2012, 11:44 PM #10
Farscience
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
Re: Missing Entities
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: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.
Space Invaders 101 - An Accelerated Java 2D Tutorial
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 sugarForeign 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.
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.
- 09-25-2012, 03:58 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Missing Entities
This is not really the best way of doing things.At the end of the loop() method, I added a: loop(), so that it goes back and loops forever
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(); } }
- 09-27-2012, 02:22 AM #12
Farscience
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
- 09-27-2012, 07:33 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
How to resolve entities in a SAX parser - please help
By DerekRaimann in forum Advanced JavaReplies: 2Last Post: 03-04-2011, 11:38 AM -
How to delete entities from many-to-many and and many-to-one relationships
By berlindutza in forum Web FrameworksReplies: 5Last Post: 02-25-2010, 02:06 PM -
entities are passed by value or passed by reference
By syntrax in forum New To JavaReplies: 1Last Post: 12-17-2009, 07:13 AM -
EntityManager.refresh() - Works on Detatched Or Managed Entities ?
By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 12-07-2009, 03:34 AM -
EntityManager.refresh() works only on managed entities.
By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 12-05-2009, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks