Java Applet Animation: Moving a Red Ball Between Rectangles with Graphics

Classified in Computers

Written on in English with a size of 1.34 KB

Java Applet Animation: Moving a Red Ball Between Rectangles

import java.awt.*;

import java.applet.*;

public class universo05 extends Applet {

int x1, y1, ref1;

int x2, y2, ref2;

public void init() {

setSize(1500, 800);

x1 = 110;

y1 = 100;

ref1 = 1;

x2 = 100;

y2 = 150;

ref2 = 1;

}

public void paint(Graphics g) {

g.setColor(Color.cyan);

g.fillRect(100, 100, 400, 50);

g.fillRect(100, 450, 400, 50);

g.setColor(Color.blue);

g.drawLine(100, 500, 500, 100);

g.setColor(Color.red);

g.fillOval(x1, y1, 50, 50);

if (ref1 == 1) x1++;

if (ref1 == 2) {

x1--;

y1++;

}

if (ref1 == 3) x1++;

if (ref1 == 4) x1--;

if (ref1 == 5) {

x1++;

y1--;

}

if (ref1 == 6) x1--;

if (x1 == 450)

if (y1 == 100)

if (ref1 == 1)

ref1 = 2;

if (x1 == 100)

if (y1 == 450)

if (ref1 == 2)

ref1 = 3;

if (x1 == 450)

if (y1 == 450)

if (ref1 == 3)

ref1 = 4;

if (x1 == 100)

if (y1 == 450)

if (ref1 == 4)

ref1 = 5;

if (x1 == 450)

if (y1 == 100)

if (ref1 == 5)

ref1 = 6;

if (x1 == 100)

if (y1 == 100)

if (ref1 == 6)

ref1 = 1;

repaint();

}

}

Related entries: