I am trying to build an animation. It just has two pictures or scenes. I want it to start at one picture and end at another. When I run my project all I see is a blank screen. I have checked and made sure that my Image paths are correct. The scenes are in an array list. I think my problem is in getting the index for the pictures. Can someone help?

master class

import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class joe2 {

    public static void main(String[] args) {
      DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
      joe2 j = new joe2();
      j.run(dm);
    }
    private Screen screen;
    private Image bg;
    private Animation ani = new Animation();
    
    public void loadPics(){
    bg = new ImageIcon("stewie.jpg").getImage();
    Image scene1 = new ImageIcon("allRight.png").getImage();
    Image scene2 = new ImageIcon("allRight2.png").getImage(); 
    
    ani.addScene(scene1, 250);
    ani.addScene(scene2, 250);
    
}
public void run(DisplayMode dm){
    screen = new Screen();
    try{
       screen.setFullScreen(dm, new JFrame()) ;
       movieLoop();
       
    }finally{
        screen.restoreScreen();   
}
}
public void movieLoop(){
    long startingTime = System.currentTimeMillis();
    long cumTime = startingTime;
    
    while(cumTime - startingTime <5000){
        long timePassed = System.currentTimeMillis() - cumTime;
        cumTime+=timePassed;
        ani.update(timePassed);
        
        Graphics g = screen.getFullScreenWindow().getGraphics();
        draw(g);
        g.dispose();
        try{
            Thread.sleep(20);
        }
        catch(Exception ex){}
    }
}
public void draw(Graphics g){
    g.drawImage(bg, 0, 0, null);
    g.drawImage(ani.getImage(),0,0,null);
}
}

Class for animation

import java.awt.Image;
import java.util.ArrayList;

public class Animation {
    
    private ArrayList scenes;
    private int sceneIndex;
    private long movieTime;
    private long totalTime;
    
    //constroctor
    public Animation(){
        scenes = new ArrayList();
        sceneIndex = 0;
        movieTime = 0;
        totalTime = 0;
        start();
    }
    //adds scene to array list and sets time for each scene
    public synchronized void addScene(Image img, long t){// can only run this method by itself
        totalTime +=t;//everytime i add a scenes it adds the time of the scene
        scenes.add(new OneScene(img, totalTime));    
    }
    //starts animation from begining
    private 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 curent scene
    public synchronized Image getImage(){
        if(scenes.isEmpty()){
            return null;
        }
        else{
            return getScene(sceneIndex).pic;//get curent scene(picture) that your on and return the picture for that scene           
        }           
    }
    private OneScene getScene(int x){//gets scene acording to index
        return (OneScene)scenes.get(x);
    }
    
    //private inner class
    private class OneScene{
        Image pic;
        long endTime;
        
        public OneScene(Image pic, long endTime){
            this.pic = pic;
            this.endTime = endTime;
        }
    }
    }

This class just sets the screen

import java.awt.*;
import javax.swing.JFrame;

public class Screen {

    private GraphicsDevice vc;//controlls video card. . . screen
    
    public Screen(){
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();// all graphic device objects
        vc = env.getDefaultScreenDevice();//accsess computer screen
    }
    
    public void setFullScreen(DisplayMode dm, JFrame window){//Display mode is the monotor settings
        window.setUndecorated(true);
        window.setResizable(false);
        vc.setFullScreenWindow(window);//takes whats in paramater and sets it to full screen
        
        if(dm != null && vc.isDisplayChangeSupported()){//as long as you gave it setting and if is able to be displayed
            try{
               vc.setDisplayMode(dm);//sets resolution and refresh rate 
            }
            catch(Exception e){}
        }
    }
       public Window getFullScreenWindow(){
            return vc.getFullScreenWindow();
        }
       public void restoreScreen(){//closes window
           Window w = vc.getFullScreenWindow();
           if(w != null){
               w.dispose();
           }
           vc.setFullScreenWindow(null);//sets it out of full screen
       }

}

Also I am doing this though a tutorial from TheNewBoston.

Recommended Answers

All 2 Replies

For debugging I'd start with an ordinary screen vs the FULL screen.
Add some printlns so you can see where the execution flow goes and what the values of variables are as they are changed.

commented: Suggestions worked +2

Yes!!! I add print statements in all my methods and found out that my loadPics() method was never called. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.