can someone give me a simple java code that animates an image that loops back to its original position. As i remember like in html, it uses some kind of flag to get the image back, though i forget it. Anyway, I really needed it for our project…
Appreciate any help!
Your question is very unclear-- can you be more precise about what you want the image to do? Do you want to move it in a circle? I also have no idea what you’re talking about with a “flag” to “get the image back”.
The simplest way to create animation for 2D is to just reset a counter when it is greater then the amount of sprites in the animation.
e.g. for 6 sprites you could use code similar to this, and would have to be the most easiest way to produce animation,
long oldTime = System.currentTimeMillis();
int animationWaitTime = 200; //in milli seconds
int animationIndex = 0;
int amountOfSprites = 6;
public void update(){
if(System.currentTimeMillis() > oldTime + animationWaitTime){
oldTime = System.currentTimeMillis();
sprite[animationIndex].render();
animationIndex++;
if(animationIndex >= amountOfSprites){
animationIndex = 0;
}
}
}
if you want a pong sought of effect, using the exact same code above with a little alter
long oldTime = System.currentTimeMillis();
int animationWaitTime = 200; //in milli seconds
int animationIndex = 0;
int amountOfSprites = 6;
int increacement = 1;
public void update(){
if(System.currentTimeMillis() > oldTime + animationWaitTime){
oldTime = System.currentTimeMillis();
sprite[animationIndex].render();
animationIndex += increacement;
if(animationIndex >= amountOfSprites){
increacement = -1;
}else if(animationIndex <= 0){
increacement = 1;
}
}
}
code is probably not perfect, but it will make you understand what to do, you could add an if statement in there to allow for pong effect to be turned on and off
a flag is another word for a boolean basically, e.g. some sought of if statement to get back to the original image.
I know what a flag and a boolean are… the problem here is the inarticulate nature of the OP’s question. Making assumptions in order to answer him won’t help him learn to communicate better.
well, actually, i want the image to move like those in conveyor belt, that loops back…
Asking to be spoon feed code doesn’t really work well here.
If you want someone else to provide code to you, then there are plenty of freelance sites out there where you can find a programmer.
If that is not the case,
Then show us what you have and where your specific problem is; do you need help with the movement? with the rendering? loading an image? what? Show us that you have put in some effort to solve the problem and give us who are willing to help an entry point into your problem.
here’s the code by the way, SLR
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Convey implements ActionListener {
int frameNumber = -1;
Timer timer;
boolean frozen = false;
JLayeredPane layeredPane;
JLabel bgLabel, fgLabel;
int fgHeight, fgWidth;
int bgHeight, bgWidth;
static String fgFile = "images/1ryt.png";
static String bgFile = "images/convey.jpg";
void buildUI(Container container, Image bgImage, Image fgImage) {
final ImageIcon bgIcon = new ImageIcon(bgImage);
final ImageIcon fgIcon = new ImageIcon(fgImage);
bgWidth = bgIcon.getIconWidth();
bgHeight = bgIcon.getIconHeight();
fgWidth = fgIcon.getIconWidth();
fgHeight = fgIcon.getIconHeight();
//Set up a timer that calls this object's action handler
timer = new Timer(100, this); //delay = 100 ms
timer.setInitialDelay(0);
timer.setCoalesce(true);
//Create a label to display the background image.
bgLabel = new JLabel(bgIcon);
bgLabel.setOpaque(true);
bgLabel.setBounds(0, 0, bgWidth, bgHeight);
bgLabel.setLocation(50,0);
//Create a label to display the foreground image.
fgLabel = new JLabel(fgIcon);
fgLabel.setBounds(-fgWidth, -fgHeight, fgWidth, fgHeight);
//Create the layered pane to hold the labels.
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(
new Dimension(bgWidth, bgHeight));
layeredPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (frozen) {
frozen = false;
startAnimation();
} else {
frozen = true;
stopAnimation();
}
}
});
layeredPane.add(bgLabel, new Integer(0)); //low layer
layeredPane.add(fgLabel, new Integer(1)); //high layer
container.add(layeredPane, BorderLayout.CENTER);
}
public synchronized void startAnimation() {
if (frozen) {
//Do nothing. The user has requested that we
//stop changing the image.
} else {
//Start animating!
if (!timer.isRunning()) {
timer.start();
}
}
}
public synchronized void stopAnimation() {
//Stop the animating thread.
if (timer.isRunning()) {
timer.stop();
}
}
public void actionPerformed(ActionEvent e) {
//Advance animation frame.
frameNumber++;
//Display it.
fgLabel.setLocation(((frameNumber*5) % (fgWidth + bgWidth)) - fgWidth, (bgHeight - fgHeight)/2);
}
public static void main(String[] args) {
Image bgImage = Toolkit.getDefaultToolkit().getImage(Convey.bgFile);
Image fgImage = Toolkit.getDefaultToolkit().getImage(Convey.fgFile);
final Convey convey = new Convey();
JFrame f = new JFrame("Conveyor Belt");
f.addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
convey.stopAnimation();
}
public void windowDeiconified(WindowEvent e) {
convey.startAnimation();
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
convey.buildUI(f.getContentPane(), bgImage, fgImage);
f.setSize(500, 125);
f.setVisible(true);
convey.startAnimation();
}
}