Java Applet Bouncing Circle Animation Code Example

Computer science6.66 KB

import java.awt.*;

import java.applet.*;

public class Ocho extends Applet {

Variable Declaration

int x1, y1, ref1;

int x2, y2, ref2;

public void init() {

Variable Initialization

x1 = 100;

y1 = 100;

ref1 = 1;

x2 = 300;

y2 = 400;

ref2 = 1;

}

public void paint(Graphics g) {

Drawing

g.drawRect(100, 100, 300, 300);

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

Behaviors

if (ref1 == 1) x1++;

if (ref1 == 2) y1++;

if (ref1 == 3) x1--;

if (ref1 == 4) y1--;

Limits

if (x1 == 100)

if (y1 == 100) ref1 = 1;

if (x1 == 350)

if (y1 == 100) ref1 = 2;

if (x1 == 350)

if (y1 == 350) ref1 = 3;

if (x1 == 100)

if (y1 == 350) ref1 = 4;

repaint();

}

}



import java.awt.*;

import java.applet.*;

public class Ocho extends Applet {

Variable Declaration

int x1, y1, ref1;

int x2, y2, ref2;

public void init() {

Variable Initialization

x1 = 100;

y1 = 100;

ref1 = 1;

x2 = 300;

y2 = 400;

ref2 = 1;

}

public void paint(Graphics g) {

Drawing

g.drawRect(100, 100, 300, 300);

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

Behaviors

if (ref1 == 1) x1++;

if (ref1 == 2) y1++;

if (ref1 == 3) x1--;

if (ref1 == 4) y1--;

Limits

if (x1 == 100)

if (y1 == 100) ref1 = 1;

if (x1 == 350)

if (y1 == 100) ref1 = 2;

if (x1 == 350)

if (y1 == 350) ref1 = 3;

if (x1 == 100)

if (y1 == 350) ref1 = 4;

repaint();

}

}



import java.awt.*;

import java.applet.*;

public class Ocho extends Applet {

Variable Declaration

int x1, y1, ref1;

int x2, y2, ref2;

public void init() {

Variable Initialization

x1 = 100;

y1 = 100;

ref1 = 1;

x2 = 300;

y2 = 400;

ref2 = 1;

}

public void paint(Graphics g) {

Drawing

g.drawRect(100, 100, 300, 300);

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

Behaviors

if (ref1 == 1) x1++;

if (ref1 == 2) y1++;

if (ref1 == 3) x1--;

if (ref1 == 4) y1--;

Limits

if (x1 == 100)

if (y1 == 100) ref1 = 1;

if (x1 == 350)

if (y1 == 100) ref1 = 2;

if (x1 == 350)

if (y1 == 350) ref1 = 3;

if (x1 == 100)

if (y1 == 350) ref1 = 4;

repaint();

}

}

Download

PDF Word HTML