Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Nedd Java help.Randomising Scene time - Animation

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Nedd Java help.Randomising Scene time - Animation

    I'm learning how to use java and am follow thenewboston's java game development tutorials.

    I've just learn how to add scenes to the animation and switch between them, with each scene lasting a set amount of time. However, i want to make it so each scene lasts for a random amount of time.

    For example...
    The first time you open the program this happens:
    Start program,
    Face with eyes open - 2.7 seconds,
    Face with eyes closed - 0.5 seconds.
    Face with eyes open - 3.3 seconds,
    Face with eyes closed - 1.2 seconds.
    Face with eyes open - 2.7 seconds,
    Program closes.
    And when you open the same program a second time without changing the code this happens:
    Start program,
    Face with eyes open - 3.4 seconds,
    Face with eyes closed - 0.4 seconds.
    Face with eyes open - 2.8 seconds,
    Face with eyes closed - 1.0 seconds.
    Face with eyes open - 3.1 seconds,
    Program closes.
    As you can see the length of time each scene appears for is randomised.

    However, the code the tutorial shows you how to do makes it so each scene time is put into the main method and won't allow me to randomise it.

    Here is the main class:
    import java.awt.*;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
     
    public class Ravi {
     
     
    	public static void main (String args[]){
    		Ravi r = new Ravi();
    		r.run();
     
    	}
     
    	private Animation a;
    	private ScreenManager s;
    	private Image bg;
     
    	private static final DisplayMode modes1[] = {
    		new DisplayMode (800,600,32,0),
    		new DisplayMode (800,600,24,0),
    		new DisplayMode (800,600,16,0),
    		new DisplayMode (640,480,32,0),
    		new DisplayMode (640,480,24,0),
    		new DisplayMode (640,480,16,0),
     
    	};
     
    	//Load images and add scenes
    	public void loadImages(){
    		bg = new ImageIcon("C:\\test\\back.jpg").getImage();
    		Image face1 = new ImageIcon("C:\\test\\Animation1.png") .getImage();
    		Image face2 = new ImageIcon("C:\\test\\Animation2.png") .getImage();
     
    		a = new Animation();
    		a.addScene(face1, 200);
    		a.addScene(face2, 200);
    	}
     
    	//main method called form main
    	public void run(){
    		s = new ScreenManager();
    		try{
    			DisplayMode dm = s.findFirstCompatibleMode(modes1);
    			s.setFullScreen(dm);
    			loadImages();
    			movieLoop();
    		}finally{
    			s.restoreScreen();
    		}
    	}
     
    	//play movie
    	public void movieLoop(){
    		long startingTime = System.currentTimeMillis();
    		long cumTime = startingTime;
    		while(cumTime - startingTime <10000){
    			long timePassed = System.currentTimeMillis() - cumTime;
    			cumTime += timePassed;
    			a.update(timePassed);
     
    			//draw and update screen
    			Graphics2D g = s.getGraphics();
    			draw(g);
    			g.dispose();
    			s.update();
     
    			try{
    				Thread.sleep(20);
    			}catch(Exception ex){}
    		}
    	}
     
    	//draws graphics
    	public void draw(Graphics g){
    		g.drawImage(bg, 0, 0, null);
    		g.drawImage(a.getImage(), 350, 250, null);
    	}
     
    }

    And here is the Animation class.
    import java.awt.Image;
    import java.util.ArrayList;
     
    public class Animation {
     
    	private ArrayList scenes;
    	private int sceneIndex;
    	private long movieTime;
    	private long totalTime;
     
    	//CONSTRUCTOR
    	public Animation (){
    		scenes = new ArrayList();
    		totalTime = 0;
    		start();
    	}
     
    	//add scene to ArrayList and set time for each scene
    	public synchronized void addScene(Image i, long t){
    		totalTime += t;
    		scenes.add(new OneScene(i, totalTime));
    	}
     
    	//start animation from beginning
    	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 animations current scene (aka image)
    	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 INNER CLASS ////////
     
    	private class OneScene{
    		Image pic;
    		long endTime;
    		public OneScene(Image pic, long endTime) {
    			this.pic = pic;
    			this.endTime = endTime;
    		}
    	}
     
    }

    here is the last episode of thenewboston's animation tutorials:


    I'm not sure where to put the random number generator to make it effect the scene time and change it every time it cycles through.

    Any help would be greatly appreciated!

    Vaderico


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,063
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Nedd Java help.Randomising Scene time - Animation

    Face with eyes open - 2.7 seconds,
    Where are these messages printed, I don't see them in the posted code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nedd Java help.Randomising Scene time - Animation

    no that's what i want to physically happen. for example:
    i want the first scene to physically be there for 2.7 seconds
    then the second scene to physically be in view for 1.7 second etc
    because what's happening at the moment is more like this

    Start program,
    Face with eyes open - 2.5 seconds,
    Face with eyes closed - 1.0 seconds.
    Face with eyes open - 2.5 seconds,
    Face with eyes closed - 1.0 seconds.
    Face with eyes open - 2.5 seconds,
    Program closes.

    and i want each scene to be in view for a random amount of time

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,063
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Nedd Java help.Randomising Scene time - Animation

    The posted code does not compile without errors because of missing classes.

    Where would those messages be printed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nedd Java help.Randomising Scene time - Animation

    oh sorry I should have posted every class

    there is one more, it doesn't effect the animation so I didn't think I needed to post it

    import java.lang.reflect.InvocationTargetException;
    import javax.swing.JFrame;
     
    public class ScreenManager {
     
    	private GraphicsDevice vc;
     
    	//give vc access to moniter screen
    	public ScreenManager(){
    		GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		vc = e.getDefaultScreenDevice();
    	}
    	//get all compatible DM
    	public DisplayMode[] getCompatibleDisplayModes(){
    		return vc.getDisplayModes();
    	}
     
    	//compares DM passed in to vc DM and see if they match
    	public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
    		DisplayMode goodModes[] = vc.getDisplayModes();
    		for(int x=0;x<modes.length;x++){
    			for(int y = 0;y<goodModes.length;y++){
    				if(displayModesMatch(modes[x], goodModes[y])){
    					return modes[x];
    				}
    			}
    		}
    		return null;
    	}
     
    	//get current DM
    	public DisplayMode getCurrentDisplayMode(){
    		return vc.getDisplayMode();
    	}
     
    	//check if two modes match each other
    	public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
    		if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
    			return false;
    		}
    		if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
    			return false;
    		}
    		if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
    			return false;
    		}
     
    		return true;
    	}
     
    	//make frame full screen
    	public void setFullScreen(DisplayMode dm){
    		JFrame f = new JFrame();
    		f.setUndecorated(true);
    		f.setIgnoreRepaint(true);
    		f.setResizable(false);
    		vc.setFullScreenWindow(f);
     
    		if(dm != null && vc.isDisplayChangeSupported()){
    			try{
    				vc.setDisplayMode(dm);
    			}catch(Exception ex){}
    		}
    		f.createBufferStrategy(2);
    	}
     
    	//we will set graphics object = to this
    	public Graphics2D getGraphics(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			BufferStrategy s = w.getBufferStrategy();
    			return (Graphics2D)s.getDrawGraphics();
    		}else{
    			return null;
    		}
    	}
     
    	//updates display
    	public void update(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			BufferStrategy s = w.getBufferStrategy();
    			if(!s.contentsLost()){
    				s.show();
    			}
    		}
    	}
     
    	//returns full screen window
    	public Window getFullScreenWindow(){
    		return vc.getFullScreenWindow();
    	}
     
    	//get width of window
    	public int getWidth(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			return w.getWidth();
    		}else{
    			return 0;
    		}
    	}
     
    	//get height of window
    	public int getHeight(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			return w.getHeight();
    		}else{
    			return 0;
    		}
    	}
     
    	//get our of fullscreen
    	public void restoreScreen(){
    		Window w = vc.getFullScreenWindow();
    		if(w != null){
    			w.dispose();
    		}
    		vc.setFullScreenWindow(null);
    	}
     
    	//create image compatible with monitor
    	public BufferedImage createCompatibleImage(int w, int h, int t){
    		Window win = vc.getFullScreenWindow();
    		if(win != null){
    			GraphicsConfiguration gc = win.getGraphicsConfiguration();
    			return gc.createCompatibleImage(w,h,t);
    		}
    		return null;
    	}	
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,063
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Nedd Java help.Randomising Scene time - Animation

    Start debugging the code to see how it works and how changes to the code changes the way it works.
    Try changing the duration of the sleep to see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nedd Java help.Randomising Scene time - Animation

    Okay i played around with it and figured it out but it is VERY sloppy.
    i ended up deleting the 'loadImage' method completely, and putting everything into the 'movieLoop' method.

    I am very new at this and don't know the proper way of achieving the same results

    here is the main class that I have edited:
    import java.awt.*;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import java.util.Random;
     
    public class Ravi {
     
     
    	public static void main (String args[]){
    		Ravi r = new Ravi();
    		r.run();
     
    	}
     
    	private Animation a;
    	private ScreenManager s;
    	private Image bg;
     
    	private static final DisplayMode modes1[] = {
    		new DisplayMode (800,600,32,0),
    		new DisplayMode (800,600,24,0),
    		new DisplayMode (800,600,16,0),
    		new DisplayMode (640,480,32,0),
    		new DisplayMode (640,480,24,0),
    		new DisplayMode (640,480,16,0),
     
    	};
     
    	//Load images and add scenes
     
    	//main method called form main
    	public void run(){
    		s = new ScreenManager();
    		try{
    			DisplayMode dm = s.findFirstCompatibleMode(modes1);
    			s.setFullScreen(dm);
    			movieLoop();
    		}finally{
    			s.restoreScreen();
    		}
    	}
     
    	//play movie
    	public void movieLoop(){
    		Random generator = new Random();
    		int number1 = 800+generator.nextInt(1500);
    		int number2 = 50+generator.nextInt(100);
     
    		bg = new ImageIcon("C:\\test\\back.jpg").getImage();
    		Image face1 = new ImageIcon("C:\\test\\Animation1.png") .getImage();
    		Image face2 = new ImageIcon("C:\\test\\Animation2.png") .getImage();
    		a = new Animation();
     
    		long startingTime = System.currentTimeMillis();
    		long cumTime = startingTime;
    		while(cumTime - startingTime <10000){
    			a.addScene(face1, number1);
    			a.addScene(face2, number2);
    			long timePassed = System.currentTimeMillis() - cumTime;
    			cumTime += timePassed;
    			a.update(timePassed);
     
    			//draw and update screen
    			Graphics2D g = s.getGraphics();
    			draw(g);
    			g.dispose();
    			s.update();
     
    			number1 = 800+generator.nextInt(1500);
    			number2 = 50+generator.nextInt(100);
    			try{
    				Thread.sleep(20);
    			}catch(Exception ex){}
    		}
    	}
     
    	//draws graphics
    	public void draw(Graphics g){
    		g.drawImage(bg, 0, 0, null);
    		g.drawImage(a.getImage(), 350, 250, null);
    	}
     
    }

    how else could I have done it without it looking so sloppy?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,063
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Nedd Java help.Randomising Scene time - Animation

    how else could I have done it
    Figure out how the code worked and made modifications to the code to make it work the way you want.
    There is no magic way. The code has to be analyzed and debugged to see how it works.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java animation code
    By MW130 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: January 17th, 2013, 12:53 PM
  2. Java animation using pictures help
    By NekoSH0GUN in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2011, 11:49 AM
  3. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  4. Replies: 1
    Last Post: April 1st, 2009, 02:47 PM
  5. How to make java based animation?
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: June 7th, 2008, 06:55 AM