Sei sulla pagina 1di 6

var 6.

1. Image loader, applet ce afişează un şir de imagini, în timp ce se poate de scris text în TextArea;
Programul:

package lab_pc_img;

import java.awt.*;

import java.applet.Applet;

import java.awt.event.ActionListener;

import javax.swing.JTextArea;

public class LAB_pc_img extends Applet implements Runnable {

TextArea jtextArea;

int frameNumber = -1;

int delay;

Thread animatorThread;

boolean frozen = false;

Dimension offDimension;

Image offImage;

Graphics offGraphics;

Image[] images;

public void init() {

jtextArea = new TextArea(10,100);

add(jtextArea);

setBackground(Color.blue);
String str;

int fps = 2;

//How many milliseconds between frames?

str = getParameter("fps");

try {

if (str != null) {

fps = Integer.parseInt(str);

} catch (Exception e) {}

delay = (fps > 0) ? (10000 / fps) : 10;

//Load all the images.

images = new Image[9];

for (int i = 1; i <= 9; i++) {

images[i-1] = getImage(getCodeBase(),"images/loading"+i+".gif");

public void start() {

if (frozen) {

//Do nothing. The user has requested that we

//stop changing the image.

} else {

//Start animating!

if (animatorThread == null) {

animatorThread = new Thread(this);

animatorThread.start();
}

public void stop() {

//Stop the animating thread.

animatorThread = null;

//Get rid of the objects necessary for double buffering.

offGraphics = null;

offImage = null;

public boolean mouseDown(Event e, int x, int y) {

if (frozen) {

frozen = false;

start();

} else {

frozen = true;

//Instead of calling stop(), which destroys the

//backbuffer, just stop the animating thread.

animatorThread = null;

return true;

public void run() {

//Just to be nice, lower this thread's priority

//so it can't interfere with other processing going on.


Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

//Remember the starting time.

long startTime = System.currentTimeMillis();

//This is the animation loop.

while (Thread.currentThread() == animatorThread) {

//Advance the animation frame.

frameNumber++;

//Display it.

repaint();

//Delay depending on how far we are behind.

try {

startTime += delay;

Thread.sleep(Math.max(0,

startTime-System.currentTimeMillis()));

} catch (InterruptedException e) {

break;

public void paint(Graphics g) {

update(g);

public void update(Graphics g) {


Dimension d = size();

//Create the offscreen graphics context, if no good one exists.

if ( (offGraphics == null)

|| (d.width != offDimension.width)

|| (d.height != offDimension.height) ) {

offDimension = d;

offImage = createImage(d.width, d.height);

offGraphics = offImage.getGraphics();

//Erase the previous image.

offGraphics.setColor(getBackground());

offGraphics.fillRect(0, 0, d.width, d.height);

offGraphics.setColor(Color.black);

//Paint the frame into the image.

offGraphics.drawImage(images[frameNumber % 10], 370, 200, this);

//Paint the image onto the screen.

g.drawImage(offImage, 0, 0, this);

}
Rezultatul:

Potrebbero piacerti anche