Hi guys I wasted my entire day today trying to figure out and I just can't. I lost all my hope.
Square:
package assignment1; import java.awt.*; public class Square { private double x,y; // Current position of the square. private Color color; // The color of the square. private int side; // The side of the square public Square() // constructor { x = 100; y = 100; side = 5; color = Color.orange; } public void draw(Graphics g) { // Draw the square in the graphics context g. Note: The drawing color // in g is changed to the color of the square. g.setColor(color); g.fillRect( (int)(x), (int)(y), (int)(side), (int)(side) ); } public void setSide(int r) { side = r; } public int getSide() { return side; } public int getX() { return (int) x; } public int getY() { return (int) y; } public void setX(int xIn) { x = xIn; } public void setY(int setY) { y = setY; } } // end class Square
This is the square class, it draws an orange square that moves around in the applet viewer, works fine and as it supposed to do.
Ship:
package assignment1; import java.awt.*; public class Ship { private Square[] fleetMembers; // declare fleetMembers as an array of Square private int total = 5; // Max number of squares in fleet private double x,y; Ship() // Constructor { x = 100; y = 100; fleetMembers = new Square[total]; // create an empty array with 5 "locations" for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle { fleetMembers[i] = new Square(); // create a Square object for location [i] in the array fleetMembers[i].setX (i * 10); // multiply i by 10, and pass this to the setX method fleetMembers[i].setY (i * 10); // multiply i by 10, and pass this to the setY method } } public void draw(Graphics g) { for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle { fleetMembers[i].draw(g); // ask fleetMember[i] to draw itself } } public void goSouth(int howFar) { for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle { // ask fleetMember[i] for its current Y position // then add howFar (howFar to go south) to it // then pass this to the setY method fleetMembers[i].setY (fleetMembers[i].getY() + howFar); } } public int getX() { return (int) x; } public int getY() { return (int) y; } public void setX(int xIn) { x = xIn; } public void setY(int setY) { y = setY; } }
This is my ship class, it is basically a set of 5 squares that move around together, works fine as well.
The problem begins here:
package assignment1; import java.awt.*; public class FleetOfShips { private Ship[] fleetMembers; // declare fleetMembers as an array of Ship private int total = 5; // Max number of Ships in fleet private double x,y; FleetOfShips() // Constructor { x = 100; y = 100; fleetMembers = new Ship[total]; // create an empty array with 5 "locations" for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle { fleetMembers[i] = new Ship(); // create a Ship object for location [i] in the array fleetMembers[i].setX (i * 50); // multiply i by 10, and pass this to the setX method } } public void draw(Graphics g) { for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle { fleetMembers[i].draw(g); // ask fleetMember[i] to draw itself } } public void goSouth(int howFar) { for (int i = 0; i < total; i++) // loop: carry on whilst i is less than 5, add 1 each cycle { // ask fleetMember[i] for its current Y position // then add howFar (howFar to go south) to it // then pass this to the setY method fleetMembers[i].setY (fleetMembers[i].getY() + howFar); } } public int getX() { return (int) x; } public int getY() { return (int) y; } public void setX(int xIn) { x = xIn; } public void setY(int setY) { y = setY; } }
This is my FleetOfShips code. It is supposed to be a set of ships moving around together, but for unknown to me reasons it doesn't work. It does compile without any problems but when I run the applet it doesn't do anything.
Guys please help me, the deadline is for tomorrow and I spent last 7 hours trying to figure this out. I lost all my hope.