Total members 11895 |It is currently Thu Dec 26, 2024 5:07 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Sequence of Images , draw several images in a row
java code
import java.awt.*;
import java.applet.Applet;

/*
* This applet displays several images in a row. It prevents
* flashing by double buffering. However, it doesn't wait until
* the images are fully loaded before drawing them, which causes
* the weird effect of the animation appearing from the top down.
*/

public class ImageSequence extends Applet implements Runnable {
int numberFrame = -1;
int delay;
Thread myThread;
boolean stopSeq = false;

Dimension dimImage;
Image imageObj;
Graphics graphObj;

Image[] images;

public void init() {
String str;
int fps = 10;

//How many milliseconds between frames?
str = getParameter("fps");
try {
if (str != null) {
fps = Integer.parseInt(str);
}
} catch (Exception e) {}
delay = (fps > 0) ? (1000 / fps) : 100;

//Load all the images.
images = new Image[10];
for (int i = 1; i <= 10; i++) {
images[i-1] = getImage(getCodeBase(),
"../../../images/"+i+".gif");
}
}

public void start() {
if (stopSeq) {
//Do nothing. The user has requested that we
//stop changing the image.
} else {
//Start animating!
if (myThread == null) {
myThread = new Thread(this);
}
myThread.start();
}
}

public void stop() {
//Stop the animating thread.
myThread = null;

//Get rid of the objects necessary for double buffering.
graphObj = null;
imageObj = null;
}

public boolean mouseDown(Event e, int x, int y) {
if (stopSeq) {
stopSeq = false;
start();
} else {
stopSeq = true;

//Instead of calling stop(), which destroys the
//backbuffer, just stop the animating thread.
myThread = 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() == myThread) {
//Advance the animation frame.
numberFrame++;

//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 ( (graphObj == null)
|| (d.width != dimImage.width)
|| (d.height != dimImage.height) ) {
dimImage = d;
imageObj = createImage(d.width, d.height);
graphObj = imageObj.getGraphics();
}

//Erase the previous image.
graphObj.setColor(getBackground());
graphObj.fillRect(0, 0, d.width, d.height);
graphObj.setColor(Color.black);

//Paint the frame into the image.
graphObj.drawImage(images[numberFrame % 10], 0, 0, this);

//Paint the image onto the screen.
g.drawImage(imageObj, 0, 0, this);
}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Sequence of Images , draw several images in a row
 bmiles v1.0 - images are not showing     -  
 Displaying Images using servlets     -  
 aligning the scanned images     -  
 Displaying Images in a Gallery     -  
 negative margins with images     -  
 Display images on jsp from servlet     -  
 User rank images     -  
 SWAP images using JQuery just by using the src attribute img     -  
 Exporting Images in Excel in Android     -  
 html slide show images     -  



Topic Tags

Java Graphics
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com