8 Replies Latest reply: Oct 2, 2008 4:53 AM by 807589 RSS

    Comparing objects in arraylist

    807589
      Guys!
      I filled an arraylist with several objects that are moving. Now I want to check whether the objects are at the same position using a for loop. How can I do this best?
        • 1. Re: Comparing objects in arraylist
          800322
          How would we know? It's your code. What exactly is your problem? A for loop over an array list? Comparing positions? Determining a moving object?
          • 2. Re: Comparing objects in arraylist
            807589
            Every object in the arraylist has a position, and I want to compare these positions.
            • 3. Re: Comparing objects in arraylist
              807589
              Use a loop
              Save the position(s) (e.g. to an int)
              Compare the positions using ==

              But what is that good for ?
              • 4. Re: Comparing objects in arraylist
                807589
                I've constructed a booelan method where I check whether the coordinates of the objects are the same:
                public boolean isGeraakt()
                     {
                          for(BewegendeDelen obj : bewegendedelenarray)
                          if(raket != null)
                          {     // bewegendedelenarray.get(1).getX()             bewegendedelenarray.get(1).getX()
                               if (raket.getX() >= obj.getX()  && raket.getX() <= (obj.getX()+obj.getWeergave().length()) && raket.getX()==obj.getY())
                               {     //haalt obj uit de lijst
                                    gemist = false;
                                    bewegendedelenarray.remove(raket);
                                    //bewegendedelenarray.remove()
                                    raket = null;     
                                    repaint();
                                    score++;
                               }
                               else
                               {
                                    gemist = true;
                               }
                          }
                          else 
                          {
                               System.out.println("geen raket!");
                          } 
                          return !gemist;
                     }     
                • 5. Re: Comparing objects in arraylist
                  807589
                  It would have been much easier if you had added that "position" is an attribute of the objects you have created !

                  I'm not from the Netherlands .... so is there still any question ?

                  if obj.getX and obj.getY return double values, this will often not become true even if you think the values are equal:
                  raket.getX()==obj.getY()
                  You should consider a delta when comparing doubles.
                  • 6. Re: Comparing objects in arraylist
                    807589
                    the objects are strings with an x and y coordinate;
                    getX returns an integer
                    public int getX()
                    {
                    return x;
                    }
                    
                    public int x;
                    • 7. Re: Comparing objects in arraylist
                      807589
                      Roddy wrote:
                      the objects are strings with an x and y coordinate;
                      I'm sure you mean: the "BewegendeDelen" object has a string attribute and one or two int attributes. Strings don't have x,y-coordinates.

                      Sorry but if you need help you have to ask a precise question! It's not possible to guess what you want to know.
                      • 8. Re: Comparing objects in arraylist
                        807589
                        yep sorry about that
                        Here is my code:
                        import java.applet.*;
                        import java.awt.*;
                        import javax.swing.Timer;
                        import java.awt.Graphics2D;
                        import java.awt.event.ActionEvent;
                        import java.awt.event.ActionListener;
                        import java.lang.Object;
                        import java.util.ArrayList;
                        import java.util.Random;
                        
                        
                        //een simpel schietspel van 15 seconden waarbij het ruimteschip dmv raketten bewegende aliens 
                        //moet zien te raken en dmv bewegen zijn positie kan bepalen
                        public class Spel extends Applet 
                        {     
                             //Timers
                             Timer nieuwePositieTimer;
                             Timer nieuweAlienTimer;
                             Timer stopSpelTimer;
                             //een nieuwe arraylist bewegendedelen
                             ArrayList<BewegendeDelen> bewegendedelenarray = new ArrayList<BewegendeDelen>();
                             Ruimteschip ruimteschip = new Ruimteschip();
                            Raket raket = null;
                             
                             
                             public void init()
                             {
                                  setBackground(Color.white);
                                  
                                  //nieuwepositielistener
                                  ActionListener nieuwePositieListener = new nieuwePositieListener();
                                  //interval van 1/4 seconde
                                  final int nieuwepositieDELAY = 250;
                                  //nieuwe positie timer
                                  Timer nieuwePositieTimer = new Timer(nieuwepositieDELAY, nieuwePositieListener);
                                this.nieuwePositieTimer = nieuwePositieTimer;
                                  //start de nieuwe positietimer
                                  nieuwePositieTimer.start();
                                  
                                  //nieuwe alien listener
                                  ActionListener nieuweAlienListener = new nieuweAlienListener();
                                  //interval van 4 seconden
                                  final int nieuwealienDELAY = 4000;
                                  //nieuwe alientimer
                                  Timer nieuweAlienTimer = new Timer(nieuwealienDELAY, nieuweAlienListener);
                                this.nieuweAlienTimer = nieuweAlienTimer;
                                
                                  //start de nieuwe alientimer
                                  nieuweAlienTimer.start();
                                  
                                  //stop het spel listener
                                  ActionListener stopSpelListener = new stopSpelListener(15); 
                                  //1 seconde interval
                                  final int stopspelDELAY = 1000;
                                  //nieuwe timer stop het spel
                                  stopSpelTimer = new Timer(stopspelDELAY, stopSpelListener);
                                  //start de timer
                                  stopSpelTimer.start();
                                  
                                  //bewegendedelenarray.add(alien);
                                  bewegendedelenarray.add(ruimteschip);
                                  //bewegendedelenarray.add(raket);
                                  
                             }     
                             
                             //tekenen
                             public void paint(Graphics g)
                             {     //print uit bewegendedelenarray obj van het type BewegendeDelen
                                  for(BewegendeDelen obj : bewegendedelenarray)
                                  {     //teken de bewegende delen (ruimteschip, aliens en raketten)
                                       System.out.println(obj);
                                       obj.paint(g);
                                       //als teller op nul staat game over!
                                       if (count <= 0)
                                       g.drawString("Game over!", 250, 250);
                                       //laat de score zien als de teller op nul staat
                                       if (count <= 0) 
                                       g.drawString("Je score is: " + score + "!", 250, 300); 
                                  }     
                             }
                             
                             
                             
                             //pijltjes toetsen indrukken om te bewegen en spatie om te schieten
                             public boolean keyDown(Event e, int key) 
                                  {     //naar links
                                       if(key == Event.LEFT)
                                       {     if (ruimteschip.getX() > 10)
                                            {
                                                 ruimteschip.beweegLinks();
                                                 System.out.println("links");
                                                 repaint();
                                            }
                                       }
                                       else
                                       //naar rechts
                                       if(key == Event.RIGHT) 
                                       {     if (ruimteschip.getX() < 450)
                                            {
                                                 ruimteschip.beweegRechts();
                                                 repaint();
                                            }
                                       }
                                       
                                       else
                                       //schieten
                                       if (key == 32) 
                                       {               
                                            raket = new Raket(ruimteschip.getX(), ruimteschip.getY());
                                        bewegendedelenarray.add(raket);
                                            raket.beweeg();
                                            repaint();
                                                 
                                       }
                                  return true;
                                  }
                             
                             
                             //kijken of de raket een alien raakt
                             public boolean isGeraakt()
                             {
                                  for(BewegendeDelen obj : bewegendedelenarray)
                                  //controleren of er een raket is
                                  while(raket != null)
                                  {     //als objecten zelfde coordinaten hebben, verwijder object, opnieuw tekenen en score ophogen
                                       if (obj.getX() >= obj.getX()  && obj.getX() <= (obj.getX()+obj.getWeergave().length()) && obj.getX()==obj.getY())
                                       {     //roep verwijderen aan
                                            obj.verwijder();
                                            
                                            raket = null;     
                                            repaint();
                                            score++;
                                       }
                             
                                  }
                                  
                                  return !gemist;
                             }     
                             
                             //iedere 1/4 seconden krijgen de aliens een nieuwe positie 
                             class nieuwePositieListener implements ActionListener
                             {
                                  public void actionPerformed(ActionEvent event)
                                  {   System.out.println("nieuwe pos");
                                     Random generator = new Random();
                                      
                                         for(BewegendeDelen obj : bewegendedelenarray)
                                       {     
                                            obj.beweeg();
                                       }          
                                     repaint(); 
                                  }
                                  
                             } 
                             
                             //om de vier seconden een nieuwe alien op het scherm
                             class nieuweAlienListener implements ActionListener
                             {
                                  public void actionPerformed(ActionEvent event)
                                  {     //voeg een nieuw alien object aan de array toe en repaint
                                      System.out.println("nieuwe alien");
                                       bewegendedelenarray.add(new Alien());
                                       //repaint();
                                  }
                                  
                        
                             } 
                             
                             //na 15 seconden stopt het spel: Game Over!
                             class stopSpelListener implements ActionListener
                             {
                                  public stopSpelListener(int initialCount)
                                  {
                                       count = initialCount;
                                  }
                                  
                                  public void actionPerformed(ActionEvent event)
                                {   count--;   
                                    
                                    //als de teller op 0 staat is het spel voorbij
                                       if (count == 0) 
                                       {
                                            nieuwePositieTimer.stop();
                                          nieuweAlienTimer.stop();
                                          stopSpelTimer.stop();
                                       } 
                                       else 
                                       {
                                        System.out.println("time left: " + count);
                                       }                    
                                  }
                                  
                                  //private int count;
                             } 
                             
                             
                             
                             
                             //instantievariabelen
                             private int count;
                             private int score;
                             private boolean gemist;
                        }     
                        superclass
                        import java.awt.*;
                        import java.awt.Graphics2D;
                        
                        //superclass met gezamenlijke eigenschappen van de bewegende delen ruimteschip, alien en raket
                        public class BewegendeDelen
                        {
                             public void paint(Graphics g) 
                            {      //weergave drawstring met x en y positie
                                 Graphics2D g2D = (Graphics2D)g;
                                g.drawString(weergave, x, y); 
                             }
                             
                             //zet de x positie
                             public void setX(int xPos)
                             {
                                  x = xPos;
                             }
                             
                             //en y positie
                             public void setY(int yPos)
                             {
                                  y = yPos;
                             }
                             
                        
                             public void setWeergave(String deWeergave)
                             {
                                  weergave = deWeergave;
                             }
                             
                             public String getWeergave()
                             {
                                  return weergave;
                             }
                             
                             public int getX()
                             {
                                  return x;
                             }
                             
                             public int getY()
                             {
                                  return y;
                             }
                             
                        
                             
                             
                             //beweegmethod rechts
                             public void beweegRechts()
                             {     //verschuif rechts
                                  x = x + 25;
                             }
                            
                            //beweegmethod links
                             public void beweegLinks()
                             {     //verschuif links
                                  x = x - 25;
                             }
                             
                             public void beweeg() 
                             {
                             }
                             
                             
                             public void verwijder()
                             {
                             }
                             
                             public String toString() 
                             {
                                return weergave + ": (" + x + ", " + y +")";
                             }
                             
                             //geef de breedte terug
                             public int getWidth()
                             {
                                  return width;
                             }
                             
                             
                             
                             
                             
                                
                             //instantievariabelen
                             public int x;
                             public int y;
                             public String weergave;
                             public int width;
                        
                        } 
                        and subclass spaceship
                        import java.awt.*; 
                        import java.awt.event.*; 
                        import javax.swing.*;
                        import java.awt.Graphics2D;
                        
                        //subklasse van "bewegendedelen" die eigenschappen ervan erft, en met beweegmethods
                        public class Ruimteschip extends BewegendeDelen
                        {      
                             public Ruimteschip()
                             {   
                                  weergave = "<<=>>";
                                x = 250;
                                 y = 500;
                             }     
                        } 
                        subclass alien
                        import java.awt.*;
                        import java.awt.image.*;
                        import java.applet.Applet;
                        
                        //de alien class
                        public class Alien extends BewegendeDelen
                        {
                             public Alien()
                             {   
                                  weergave = "^^^";
                                  y = 25;
                             }
                             
                             public void beweeg() 
                             {
                                  x= x+10;
                             }
                             
                        
                        } 
                        subclass raket
                        import java.awt.*;
                        import java.awt.image.*;
                        import java.applet.Applet;
                        
                        public class Raket extends BewegendeDelen
                        {
                             public Raket(int x, int y)
                            { 
                                 weergave = "|";
                                 setX(x+17);
                                 setY(y);
                            
                            } 
                        
                             //schietmethod, verticaal omhoog bewegen
                             public void beweeg()
                             {
                                  y = y - 40;
                             }     
                                  
                             //een lege string als weergave
                             public void verwijder()
                             {
                                  weergave = " ";
                             }
                        } 
                        So I want to compare whether raket and alien collide...
                        Thanks!