Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  animation looping using double buffering  (Read 681 times)
0 Members and 1 Guest are viewing this topic.
Offline ssilao78

Junior Newbie





« Posted 2013-10-13 13:15:47 »

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!
Offline Grimmov

Senior Newbie





« Reply #1 - Posted 2013-10-13 13:36:56 »

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".
Online Phased
« Reply #2 - Posted 2013-10-13 13:38:40 »

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,

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Phased
« Reply #3 - Posted 2013-10-13 13:42:04 »

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".

a flag is another word for a boolean basically, e.g. some sought of if statement to get back to the original image.
Offline Grimmov

Senior Newbie





« Reply #4 - Posted 2013-10-13 13:48:11 »

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.
Offline ssilao78

Junior Newbie





« Reply #5 - Posted 2013-10-16 15:47:56 »

well, actually, i want the image to move like those in conveyor belt, that loops back...
Offline zngga
« Reply #6 - Posted 2013-10-16 22:05:06 »

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.

My code never has bugs... it just develops unexpected features!
Offline ssilao78

Junior Newbie





« Reply #7 - Posted 2013-10-18 11:40:12 »

here's the code by the way, SLR

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
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();
    }
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (17 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (20 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!