Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: java creating smooth multi image animation? Image flashing :/

  1. #1
    dev.ryanJ is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default java creating smooth multi image animation? Image flashing :/

    I've been looking through/for guides all night trying to find how to smooth out my multiple image animation. I tried a few guides, found one i liked but it's quite choppy.

    atm my animation works but flashes. What i mean by flashing, finishes the animation but flashes when restarting. I only use 12 Images so it goes kind of quick. I also need to be able to change how long you see each image.

    my animation clas:

    Java Code:
    package graphics;
    
    import java.awt.Image;
    import java.util.ArrayList;
    
    public class Animation {
    	
    	private ArrayList<OneScene> scenes;
    	private int sceneIndex;
    	private long movieTime;
    	private long totalTime;
    	
    	//Constructor
    	public Animation() {
    		scenes = new ArrayList<OneScene>();
    		totalTime = 0;
    		start();
    	}
    	
    	//Add scene to ArayList & set time for each scene
    	public synchronized void addScene(Image i, long t) {
    		totalTime += t;
    		scenes.add(new OneScene(i, totalTime));
    	}
    	
    	//Start animation
    	public synchronized void start() {
    		movieTime = 0;
    		sceneIndex = 0;
    	}
    	
    	//Change Scenes
    	public synchronized void update(long timePassed) {
    		if (scenes.size() > 1) {
    			movieTime += timePassed;
    			if (movieTime >= totalTime) {
    				movieTime = 0;
    				sceneIndex = 0;
    			}
    			while(movieTime > getScene(sceneIndex).endTime) {
    				sceneIndex++;
    			}
    		}
    	}
    	
    	//Get animation (Image) current scene
    	public synchronized Image getImage() {
    		if (scenes.size() ==0) {
    			return null;
    		}else{
    			return getScene(sceneIndex).pic;
    		}
    	}
    	
    	//Get Scene
    	private OneScene getScene(int x) {
    		return (OneScene)scenes.get(x);
    	}
    	
    	//Private Class
    	public class OneScene {
    		Image pic;
    		long endTime;
    		
    		public OneScene(Image pic, long endTime) {
    			this.pic = pic;
    			this.endTime = endTime;
    		}
    	}
    & a few of my methods:

    Java Code:
    Image load1 = new ImageIcon(""+cachemainDir+"1.png").getImage();
    		Image load2 = new ImageIcon(""+cachemainDir+"2.png").getImage();
    		Image load3 = new ImageIcon(""+cachemainDir+"3.png").getImage();
    		Image load4 = new ImageIcon(""+cachemainDir+"4.png").getImage();
    		Image load5 = new ImageIcon(""+cachemainDir+"5.png").getImage();
    		Image load6 = new ImageIcon(""+cachemainDir+"6.png").getImage();
    		Image load7 = new ImageIcon(""+cachemainDir+"7.png").getImage();
    		Image load8 = new ImageIcon(""+cachemainDir+"8.png").getImage();
    		Image load9 = new ImageIcon(""+cachemainDir+"9").getImage();
    		Image load10 = new ImageIcon(""+cachemainDir+"10.png").getImage();
    		Image load11 = new ImageIcon(""+cachemainDir+"11.png").getImage();
    		Image load12 = new ImageIcon(""+cachemainDir+"12.png").getImage();
    		Anim = new Animation();
    		Anim.addScene(load1, 100);
    		Anim.addScene(load2, 100);
    		Anim.addScene(load3, 100);
    		Anim.addScene(load4, 100);
    		Anim.addScene(load5, 100);
    		Anim.addScene(load6, 100);
    		Anim.addScene(load7, 100);
    		Anim.addScene(load8, 100);
    		Anim.addScene(load9, 100);
    		Anim.addScene(load10, 100);
    		Anim.addScene(load11, 100);
    		Anim.addScene(load12, 100);
    and...

    Java Code:
    while (AppLoading) {
    			long timePassed = System.currentTimeMillis() - cumTime;
    			cumTime += timePassed;
    			Anim.update(timePassed);
    			
    			Graphics g = getGraphics();
    			draw(g);
    			g.dispose();
    			
    			try{
    				Thread.sleep(0);
    			}catch(Exception ex){}
    }

    lastly....

    Java Code:
    public void draw(Graphics g) {
    		new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    		g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
    		g.drawImage(Anim.getImage(), 300, 300, null);
    	}

  2. #2
    dev.ryanJ is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: java creating smooth multi image animation? Image flashing :/

    Anyone? I'm still stuck on this :/

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: java creating smooth multi image animation? Image flashing :/

    Short answer: never use getGraphics() of a Component.

    Longer answer here: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Also, AWT is last century stuff. Use Swing or one of the newer toolkits, like JavaFX.

    db
    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Image Animation flickering
    By totalspelnerd in forum AWT / Swing
    Replies: 6
    Last Post: 04-23-2013, 09:08 PM
  2. 2D game animation not smooth
    By Marcz in forum Java 2D
    Replies: 9
    Last Post: 04-05-2012, 03:51 PM
  3. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  4. creating image using java
    By mmahesh_mca in forum New To Java
    Replies: 1
    Last Post: 04-26-2008, 11:23 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •