0% found this document useful (0 votes)
59 views5 pages

Applet Animations

This document contains code for two Java applets. The first applet displays moving text by continuously redrawing the text string with its first character moved to the end, creating an effect of the text moving across the screen from right to left. The second applet animates a bouncing ball by updating its x and y coordinates and reversing the direction when it hits the edge of the applet window. Both applets use threads to continuously redraw the graphics at set time intervals to create animation effects.

Uploaded by

neeraj petel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views5 pages

Applet Animations

This document contains code for two Java applets. The first applet displays moving text by continuously redrawing the text string with its first character moved to the end, creating an effect of the text moving across the screen from right to left. The second applet animates a bouncing ball by updating its x and y coordinates and reversing the direction when it hits the edge of the applet window. Both applets use threads to continuously redraw the graphics at set time intervals to create animation effects.

Uploaded by

neeraj petel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Applet program to display moving text in java

// This java program will move the

// text using applet

package Applet;

import java.awt.*;

import java.applet.*;

public class MovingContent extends Applet implements Runnable {

// enter message

String msg = "Welcome to Includehelp.";

Thread t = null;

// initialize here.

int state;

boolean stopFlag;

// Set colors and initialize text..

public void init() {

setBackground(Color.cyan);

setForeground(Color.red);

// Start the text....

public void start() {

t = new Thread(this);

stopFlag = false;

t.start();

}
// Entry point which runs the text.

public void run() {

char ch;

// Display text reapeated times.

for (;;) {

try {

repaint();

Thread.sleep(250);

ch = msg.charAt(0);

msg = msg.substring(1, msg.length());

msg += ch;

if (stopFlag)

break;

} catch (InterruptedException e) {

System.out.println(e);

// Pause the text.

public void stop() {

stopFlag = true;

t = null;

// Display the text.

public void paint(Graphics g) {

g.drawString(msg, 50, 30);

}
Applet program to show animation of bouncing ball

// This java applet program will show

// the bouncing balls

package Applet;

import java.applet.*;

import java.awt.*;

public class BouncingBall extends Applet implements Runnable {

// x,y coordinates and radius of the circle.

int x = 150, y = 50, r = 20;

int dx = 11, dy = 7;

// create thread.

Thread t;

boolean stopFlag;

// Function to start thread.

public void start() {

t = new Thread(this);

stopFlag = false;

t.start();

// Draw cicle from its present position.

public void paint(Graphics g) {


g.setColor(Color.red);

g.fillOval(x - r, y - r, r * 2, r * 2);

// function to move the image.

public void run() {

while (true) {

if (stopFlag)

break;

// Bounce if we've hit an edge.

if ((x - r + dx < 0) || (x + r + dx > bounds().width)) dx = -dx;

if ((y - r + dy < 0) || (y + r + dy > bounds().height)) dy = -dy;

// Move the circle.

x += dx;

y += dy;

try {

Thread.sleep(100);

} catch (Exception e) {

System.out.println(e);

};

// print circle again n again.

repaint();

// function to stop printing.

public void stop() {

stopFlag = true;

t = null;

}
}

You might also like