Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: animation looping using double buffering on: 2013-10-18 09: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();
    }
}
2  Game Development / Newbie & Debugging Questions / Re: animation looping using double buffering on: 2013-10-16 13:47:56
well, actually, i want the image to move like those in conveyor belt, that loops back...
3  Game Development / Newbie & Debugging Questions / animation looping using double buffering on: 2013-10-13 11: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!
4  Game Development / Newbie & Debugging Questions / suggestions about image manipulation in double buffering on: 2013-10-09 12:37:50
I'm making a GUI for my board game, and I want to have a better animation display. My question is how can I change the appearance(height or width) of an image in double buffering as it animate. More specifically, In my program, how can the arrow image loop like a conveyor belt? Like it will gradually decrease its width started from the right side.  

hope you can help me!
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

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

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

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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!