Otoz mam stworzyc 3 klasy, 1 z kwadratem ktory sie porusza, 2 ktora stworzy 5 kwadratow na raz poruszajacych sie razem i 3 ktora stworzy kilka takich "zestawow" kwadratow.
pierwsze 2 mam zrobione, ale 3 mi nie smiga w ogole.
Prosze pomozcie bo stracilem nadzieje
Kwadrat (dziala)
Kod:
package assignment1;
/*
* Square class
* Author: Tony Cooke
* Date: 2009
*/
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
Kod:
package assignment1;
import java.awt.*;
public class FleetOfShips
{
private Square[] fleetMembers; // declare fleetMembers as an array of Square
private int total = 5; // Max number of squares in fleet
FleetOfShips() // Constructor
{
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);
}
}
}
Grupa grup (nie dziala):
Kod:
package assignment1;
import java.awt.*;
public class FleetOfFleets
{
private FleetOfShips[] fleetMembers; // declare fleetMembers as an array of FleetOfShips
private int total = 5; // Max number of squares in fleet
FleetOfFleets() // Constructor
{
fleetMembers = new FleetOfShips[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 FleetOfShips(); // create a FleetOfShips 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);
}
}
}
Zakładki