Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help With Moving A 2D Array Of Rectangles

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help With Moving A 2D Array Of Rectangles

    Hi, I am working on a basic space invaders app for an into to java class I'm taking. Here is my issue:

    I have a main class called SpaceInvaders. This controls all of the objects in the program(missiles, space invaders, your ship) while the other classes create those objects. Currently I have all of the alien ships stored in a class called Aliens. They are created by using a basic 2D rectangle array. Back in SpaceInvaders I have a method called animateAliens which is supposed to make them move. To create the squadron of aliens I have an instance variable in my SpaceInvaders class:

    private Aliens squadron.

    I cannot, for the life of me, figure out how to get the aliens to move. I cant set the location of the pieces of the array directly from SpaceInvaders because the array is actually made in Aliens. I have posted the code for SpaceInvaders, Aliens, and my Missile class so you can compile it. Please help! I am very stuck!

    /**
     * SpaceInvaders.java 
     * 
     * @author William
     * @class CS
     * @assignment prog12
     * @due 4/24/2012
     * 
     */
     
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //Imports
    import wheelsunh.users.*;
    import java.awt.event.*;
    import java.util.*;
    import java.awt.Color;
     
    public class SpaceInvaders implements Animator, KeyListener
    {  
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Static Class Variables  
     public static final int FRAME_WIDTH=652;
     public static final int FRAME_HEIGHT=650;
     private static int cols=11;
     private static int rows=5;
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Instance Variables
     private AnimationTimer timer;
     private Aliens squadron = null;
     private Vector<Missile> missiles;
     private Rectangle defender;
     private static final int LEFT=37, UP=38, RIGHT=39;
     private int playerScore=0, deltaX=5, alienMoveCount=0, deltaY=0;
     private Color defenderColor=Color.blue;
     private boolean tempvar=true;
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Default Constructor
     public SpaceInvaders() 
     {  
      squadron=new Aliens(); 
      defender=new Rectangle(250,550);     
     
      defender.setColor(defenderColor);
      defender.setSize( 50, 30);
     
      // initialize the Vector of missiles to an empty Vector    
      missiles=new Vector<Missile>();
     
      // create the timer and have it call animate on this animator
      timer=new AnimationTimer(10,this);
     
      // start the animation
      timer.start();
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Animation Method
     // For every missile in the Vector
     // call its "step" method and call the method
     // this.checkMissile( Missile )
     public void animate()
     {  
      while(tempvar==true);
       animateAliens();
      for(int i=0;i<missiles.size();i++)
       animateMissile(missiles.get(i));
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Animates the aliens
     public void animateAliens()
     {
      System.out.println("Animating");
      for(int r=0;r<rows;r++)
      {
        System.out.println("Row Loop");
       for(int c=0;c<cols;c++)
       {
         System.out.println("Column Loop");
        if(alienMoveCount%8==0)
        {
         System.out.println("moving y axis");     
         squadron[r][c].setLocation(squadron[r][c].getXLocation(), squadron[r][c].getYLocation()+deltaY);
         deltaX=-deltaX;
         alienMoveCount=0;
        }
        else
        {
          System.out.println("Moving x axis");
         squadron[r][c].setLocation(squadron[r][c].getXLocation()+deltaX, squadron[r][c].getYLocation());
         alienMoveCount+=1;
        }
       }
      }
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Animates the Missile
     // First call missile.step() to move the missile then 
     // Check to see if the missile hit an enemy ship 
     // if it has then hide, set, and remove it from the vector
     public void animateMissile(  Missile missile )
     {
      missile.step();
      if(squadron.checkMissileStrike(missile.getLocation()))
      {
       playerScore+=10;
       missile.hide();
       missiles.remove(missile);
      }  
     
      else if(missile.getYLocation()<0)
      {
       missile.hide();
       missiles.remove(missile);
      }
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Handles the movement of the defender
     public void keyPressed(KeyEvent e) 
     {
      int code = e.getKeyCode( );
     
      if(code==LEFT)
      {        
       if(defender.getXLocation()<0)
        {}
       else
        defender.setLocation(defender.getXLocation()-10,
                             defender.getYLocation());
      }
     
      else if(code==RIGHT)
      {
       if((defender.getXLocation()+defender.getWidth())>=FRAME_WIDTH)
        {}
       else
        defender.setLocation(defender.getXLocation()+
                             10,defender.getYLocation());
      }
     
      else if(code==UP)
      {
       if(missiles.size()!=0)
       {
        System.out.println("Only 1 missle can be shot at a time!");
       }
       else
       {
        Missile attackmissile = new Missile(defender.getXLocation()+
                   defender.getWidth()/2, defender.getYLocation());
        missiles.add(attackmissile);
       }
      }
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Handles the keys hit
     public void keyTyped(KeyEvent e) 
     {
      // System.out.println( "XXKEY TYPED: " + e.getKeyCode( ) );
      // Not Used But Must Be Kept For KeyListener To Work
     }
     
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Handles the keys release
     public void keyReleased(KeyEvent e) 
     {
      // System.out.println("XXKEY RELEASED: " + e.getKeyCode( ) );
      // Not Used But Must Be Kept For KeyListener To Work
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     //Main
     public static void main( String args[ ] )
     {
      Frame f = new Frame( FRAME_WIDTH, FRAME_HEIGHT );
      SpaceInvaders app = new SpaceInvaders(  ); 
      f.addKeyListener( app );  
     }
    }
    //End Of SpaceInvaders.java

    /**
     * Aliens.java 
     * 
     * @author William
     * @class CS
     * @assignment prog12
     * @due 4/24/2012
     * 
     */
     
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //Imports
    import wheelsunh.users.*;
    import java.awt.Color;
    import java.awt.Point;
     
    public class Aliens 
    {
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Class Variables
     private static int cols=11;
     private static int rows=5;
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Instance Variables
     protected Rectangle[][] squadron; 
     private int alienWidth=36,alienHeight=24,hSpace=6,vSpace=20;
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Default ConstructorCreates The Aliens Based Off Of The 
     // Instance Variables
     public Aliens(  )
     {
      squadron=new Rectangle[rows][cols];     
      for(int r=0;r<rows;r++)
      {
       for(int c=0;c<cols;c++)
       {
        Rectangle ship = new Rectangle();
        ship.setSize(alienWidth,alienHeight);
        ship.setLocation(c*(alienWidth+hSpace),
                         r*(alienHeight+vSpace));
        ship.setColor(Color.RED);
        squadron[r][c]=ship; 
       }
      }
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     //Checks For Missile Strikes With Aliens
     public boolean checkMissileStrike( Point p )
     {
      for(int r=0;r<5;r++)
      {
       for(int c=0;c<11;c++)
       {
        if(squadron[r][c].contains(p))
        {
         System.out.println("Row: "+r+", Column: "+c);
         squadron[r][c].setLocation(-100,-100);
         return true;
        }
       }      
      }
      return false;
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     //Main
     public static void main(String[] args) 
     {
      new Frame();
      new Aliens();
     }  
    }
    //End Of Aliens.java

    /**
     * Missile.java 
     * 
     * @author William 
     * @class CS
     * @assignment prog12
     * @due 4/24/2012
     * 
     */
     
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Imports
    import wheelsunh.users.*;
    import java.awt.Color;
     
    public class Missile extends ShapeGroup
    {
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Static Class Variables  
     public static final int FRAME_WIDTH=652;
     public static final int FRAME_HEIGHT=650;
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Instance Variables
     private RoundedRectangle body;
     private int deltaY=-5, bodyX=4, bodyY=25;
     private Color bodyColor=Color.green, frameColor=Color.orange;
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Default Constructor
     public Missile(int x, int y)
     {
      body=new RoundedRectangle(0, 0);
      body.setFillColor(bodyColor);
      body.setFrameColor(frameColor);
      body.setSize(bodyX,bodyY); 
      add(body);     
      setLocation(x,y);
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Moves The Missile
     public void step()
     {
      setLocation(getXLocation(), getYLocation() + deltaY);
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Hides The Missile
     public void hide( )
     {
      body.hide();
     }
     
     //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     // Main
     public static void main(String[] args)
     {
      Frame f = new Frame(FRAME_WIDTH, FRAME_HEIGHT);
      SpaceInvaders app = new SpaceInvaders(); 
      f.addKeyListener(app);  
     }   
    }
    //End Of Missile.java
    *Edited to remove my name
    Last edited by billg118; April 22nd, 2012 at 12:27 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Moving A 2D Array Of Rectangles

    The usual way for one class to access methods or data in another class is by getting a reference to the class with the data and using that to call accessor methods to get/set the data.

    In the class with method where you want to get to the data, is there a way that the class can have a reference to the class with the data? For example the reference to the class with the data could be passed in the constructor of the class where you need the reference and the constructor could save it in a class variable so all its methods could use it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Question on Rectangles
    By parkBENch in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2012, 11:13 PM
  2. adding rectangles in images
    By mserrao in forum AWT / Java Swing
    Replies: 4
    Last Post: September 1st, 2011, 06:27 AM
  3. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 12:59 PM
  4. How to form a rectangle from the union of two Rectangles.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 3rd, 2010, 07:22 AM
  5. Problem in navigation in a txt file with specific direction
    By wendybird79 in forum Collections and Generics
    Replies: 1
    Last Post: May 10th, 2009, 07:54 AM