Ok its a really simple assignment. ive had to make an image comprising of three shapes, Circle,Square,Triangle. i decided to make a boat which floats across the page left to right and then from right to left.
the boat is comprised of 4 separate Squares which are all different objects, and a triangle.
when i run the code the objects take it in turn to move, however i want them all to move together. so all of the shapes that make the boat move in sync.
Hope thats a bit more clear?
I don’t understand. "Move" an object? Can you explain more please. The for construct simply indicates the the code inside its braces with repeat x amount of times. e.g.
for(int x = 0; x < 10; x++){ System.out.println("*");}
x controls how many times the code inside the braces will run.
I saw your last question. You’ve still failed to go to into detail. What is the nature of the assignment? Post some more information, instead of asking vague inquiries. Good luck.
See Rectangle2D for geometric objects. Also, the Graphics2D for the draw(Shape s) method. You’ll also need to know how to use Threads and some simple swing concepts. It would take a miracle to teach you what’s needed online. I suggest you check out Sun’s tutorials for Java programming. They’ve got everything you need, plus example using Swing and Threads.
important methods:
Rectangle2D.setFrame(double d1, double d2, …, ….)
Graphics2D.setColor(Color c);
Graphics2D.draw(Shape s);
Simple example:
class Example {
public static void main(String… args) {
Rectangle2D[] shapes = new Rectangle2D[5];
Canvas c = new Canvas(shapes);
JFrame f = new JFrame().add(c);
//set size and set visible here
new drawThread(c);
}
}
class Canvas extends JPanel {
Canvas(Rectangle2D[] shapes){super();
//set Frame for all shapes
}
//override paintComponent(); required of all swing apps
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.RED);
for(Rectangle2D r : shapes) {g2d.draw(r);}
}
}
class drawThread extends Thread {
drawThread(JPanel canvas){ super(); this.start(); }
public void run() {
while(true) {
//change the frame of the geometric objects, move it
try{Thread.sleep(5000);}catch(Exception){}
canvas.repaint(); //calls paintComponent() on JPanel
}
}
}
That’s about half your project. Get crackin’ baby….