Verstehe Beispiel-Code nicht

Status
Nicht offen für weitere Antworten.

Reality

Top Contributor
Hi,
schade, dass mein Grips manchmal nicht dazu ausreicht... Seit einigen Tage komme ich einfach nicht weiter, weil ich ein Beispiel-Code nicht vollständig kapiere:

Code:
import java.awt.*;

import javax.swing.*;

public class AnimationTest {
	  public static void main(String[] args) {
	    DisplayMode displayMode;
	    
	    if(args.length == 3){
	      displayMode = new DisplayMode(Integer.parseInt(args[0]),
	                                    Integer.parseInt(args[1]),
	                                    Integer.parseInt(args[2]),
	                                    DisplayMode.REFRESH_RATE_UNKNOWN);
	    }

	    else
	      displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);

	    AnimationTest test = new AnimationTest();
	    test.run(displayMode);
	  }

	  private static final long DEMO_TIME = 5000;

	  private SimpleScreenManager screen;
	  private Image bgImage;
	  private Animation anim;

	  public void run(DisplayMode displayMode){
	    screen = new SimpleScreenManager();

	    try{
	      screen.setFullScreen(displayMode, new JFrame());
	      loadImages();
	      animationLoop();
	    }
	    finally{
	      screen.restoreScreen();
	    }
	  }

	  public void loadImages(){
	    bgImage = loadImage("images/background.jpg");
	    Image player1 = loadImage("images/player1.png");
	    Image player2 = loadImage("images/player.png");
	    Image player3 = loadImage("images/player3.png");
	    
	    //create Animation
	    anim = new Animation();
		anim.addFrames(player1, 250);
	    anim.addFrames(player2, 150);
	    anim.addFrames(player1, 150);
	    anim.addFrames(player2, 150);
	    anim.addFrames(player3, 200);
	    anim.addFrames(player2, 150);
	  }

	  private Image loadImage(String fileName){
	    return new ImageIcon(fileName).getImage();
	  }

	  public void animationLoop(){
	  	long startTime = System.currentTimeMillis();
	  	long currTime = startTime;
	  	
	  	while(currTime - startTime < DEMO_TIME){
	  		long elapsedTime = 
	  			System.currentTimeMillis() - currTime;
	  		currTime += elapsedTime;
	  		
	  		//update animation
	  		anim.update(elapsedTime);
	  		
	  		//draw to screen
	  		Graphics g = screen.getFullScreenWindow().getGraphics();
	  		draw(g);
	  		g.dispose();
	  		
	  		//take a nap
	  		try{
	  			Thread.sleep(20);
	  		}catch(InterruptedException ex){}
	  	}	
	  }
	  
	  public void draw(Graphics g){
	  	//drawbackground
	  	g.drawImage(bgImage, 0, 0, null);
	  	
	  	//draw Image
	  	g.drawImage(anim.getImage(), 0, 0, null);
	  }
}

Code:
import java.awt.*;
import java.util.ArrayList;

public class Animation {
  private ArrayList frames;
  private int currFrameIndex;
  private long animTime;
  private long totalDuration;

  //Creates a new empty Animation
  public Animation(){
    frames = new ArrayList();
    totalDuration = 0;
    start();
  }

  //Adds an image to the animation with the specified duration

  public synchronized void addFrames(Image image, long duration){
    totalDuration += duration;
    frames.add(new AnimFrame(image, totalDuration));
  }

  //Starts this animation over from the beginning

  public synchronized void start(){
    animTime = 0;
    currFrameIndex = 0;
  }

  //Updates this animation´s current image (frame), if necessary
  public synchronized void update(long elapsedTime){
    if(frames.size() > 1){
      animTime += elapsedTime;  //elapsed = abgelaufen

      if(animTime >= totalDuration){
        animTime = animTime % totalDuration;
        currFrameIndex = 0;
      }

      while(animTime > getFrame(currFrameIndex).endTime){
        currFrameIndex++;
      }
    }
  }

  public synchronized Image getImage(){
    if(frames.size() == 0)
      return null;

    else
      return getFrame(currFrameIndex).image;
  }

  private AnimFrame getFrame(int i){
    return (AnimFrame)frames.get(i);
  }

  private class AnimFrame{
    Image image;
    long endTime;

    public AnimFrame(Image image, long endTime){
      this.image = image;
      this.endTime = endTime;
    }
  }
}

Dieser Code-Abschnitt macht mir Probleme:

Code:
public synchronized void update(long elapsedTime){
    if(frames.size() > 1){
      animTime += elapsedTime;  //elapsed = abgelaufen

      if(animTime >= totalDuration){
        animTime = animTime % totalDuration;
        currFrameIndex = 0;
      }
[code]
      while(animTime > getFrame(currFrameIndex).endTime){
        currFrameIndex++;
      }
}
}[/code]

Das Buch schreibt zu
Code:
animTime = animTime % totalDuration;

It´s used here to make sure the animation time starts over when the animation is done so that the animation loops
Ich weiss bis jetzt nicht, was er mit starts over meint und denn Sinn des Codes. Auch das verwirrt mich:

Code:
      while(animTime > getFrame(currFrameIndex).endTime){
        currFrameIndex++;
      }
Er erhöht es solange, bis animTime größer ist. Kann es aber nicht sein, dass currFrame größer wird, als das ArrayList Indexe hat?

Liebe Grüße
Reality
 

Illuvatar

Top Contributor
Zur Erklärung voraus:
Code:
int x = ...; //irgendwas
x %= 376; //entspricht x = x % 376
if (x < 376) //immer true
  ;
else
  ; //unreachable statement

Code:
private long totalDuration;
private long animTime;
public synchronized void addFrames(Image image, long duration){ 
    totalDuration += duration; 
    frames.add(new AnimFrame(image, totalDuration)); 
}
public synchronized void update(long elapsedTime){ 
      animTime += elapsedTime;  //elapsed = abgelaufen 
      animTime = animTime % totalDuration; 
}
--> animTime wird nicht größer als totalDuration, in dem Fall beginnt sie von vorne (starts over) und wird zu einer Schleife :wink:

Die zweite Frage erübrigt sich dann.
 

Reality

Top Contributor
Oh man! Die Methoden und Klassen sind vielleicht interdependent geschrieben!
Danke, aber wieso schreibt man nicht
Code:
 animTime = 0;
statt
Code:
 animTime = animTime % totalDuration;
?

Liebe Grüße
Reality
 

Illuvatar

Top Contributor
% heißt modulo heißt "Rest der Teilung durch".
13 % 10 = 13 % 5 = 3

D.h. wenn animTime == totalDuration ist würde das auch stimmen, wenn animTime größer ist, ist das Ergebnis nachher soviel, wie animTime größer war.
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben