1 Replies - 139 Views - Last Post: 27 October 2014 - 06:47 AM Rate Topic: -----

#1 jbasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-October 14

Trying to make the image move

Posted 27 October 2014 - 05:17 AM

I am having trouble making my image move to a certain position in a certain amount of time which is set inside a text file. I was wondering what I'm doing wrong and what I'm missing. Any tips and tricks will help, thanks.

 import java.io.*; 
import java.util.*;
import java.io.FileReader;

public class Animator {
	
	EZImage HP;
	int x, y, destx, desty, startx, starty;
	long duration;
	long starttime;
	boolean interpolation;
	String Moveto;
	
	public Animator(String filename, String filename1)throws java.io.IOException{
	    HP = EZ.addImage("HP2.png", 400, 300);

		
		Scanner fileScanner = new Scanner(new FileReader("Test.txt"));
		
	}
	
	public void readLine()throws java.io.IOException{
		Scanner fileScanner = new Scanner(new FileReader("Test.txt"));
		String line;
		while (fileScanner.hasNextLine()){
			Moveto = fileScanner.next();
			x = fileScanner.nextInt();
			y = fileScanner.nextInt();
			duration = fileScanner.nextLong();
			interpolation = false;
		}
		fileScanner.close();
	}
	
	public void setDestination(int posx, int posy, long dur){
		destx = posx; desty = posy; duration = dur*1000;
		starttime = System.currentTimeMillis();
		startx=x; starty=y;
		interpolation = true;
	}
	
	public void go(){
		
		if (interpolation == true) {
			float normTime = (float) (System.currentTimeMillis() - starttime)/ (float) duration;
			
			x = (int) (startx + ((float) (destx - startx) *  normTime));
			y = (int) (starty + ((float) (desty - starty) *  normTime));
			
			if ((System.currentTimeMillis() - starttime) >= duration) {
				interpolation = false;
				x = destx; y = desty;
			}
			HP.translateTo(x,y);	
		}
	}
	
	
} //(1) 



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to make the image move

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,153
  • Joined: 27-December 08

Re: Trying to make the image move

Posted 27 October 2014 - 06:47 AM

Is there some GUI associated with this that you aren't repainting?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1