Gå til innhold

Anbefalte innlegg

Hei, Jeg har nå prøvd og lære meg Java via online videoer og det har gått bra frem til idag. Jeg prøvde å lage statiske animasjoner, men det virker som at java har skiftet eller noe sånt siden programmet nå ikke fungerer.

 

Her er koden

 

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;

public Animation(){
	scenes = new ArrayList();
	totalTime = 0;
	start();

}
public synchronized void addScene(Image i, long t){
	totalTime += t;
	scenes.add(new OneScene(i, totalTime));
}
public synchronized void start(){
	movieTime= 0 ;
	sceneIndex= 0;
}
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++;
		}
	}
}
public synchronized Image getImage(){
	if(scenes.size()==0){
		return null;
	}else{
	return getScene(sceneIndex).pic;
	}
	}
private OneScene getScene(int x){
	return (OneScene)scenes.get(x);

}
private class OneScene{
	Image pic;
	long endTime;
	public OneScene(Image pic, long endTime){
		this.pic = pic;
		this.endTime = endTime;
	}
}
}

 

 

 

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

public class Bucky{
public static void main(String[] Args){
	DisplayMode displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
	Bucky b = new Bucky();
	b.run(displayMode);
}
private Screen screen;
private Animation a;
private Image bg;
public void loadPics(){
	bg = new ImageIcon("C:\\test\\back").getImage();
	Image face1 = new ImageIcon("C:\\test\\an1").getImage();
	Image face2 = new ImageIcon("C:\\test\\an2").getImage();
	a = new Animation();
	a.addScene(face1, 250);
	a.addScene(face2, 250);
}
public void run(DisplayMode dm){
	screen = new Screen();
	try{
		screen.setFullScreen(dm, new JFrame());
		loadPics();
		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;
		a.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(a.getImage(),0,0,null);


}
}

 

Screen class

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

public class Screen {
private GraphicsDevice vc;
public Screen(){
	GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
	vc = env.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame window) {
	window.setUndecorated(true);
	window.setResizable(false);
	vc.setFullScreenWindow(window);

	if(dm != null && vc.isDisplayChangeSupported()){
		try{
			vc.setDisplayMode(dm);
		}catch(Exception ex){

		}
	}
}
public Window getFullScreenWindow(){
	return vc.getFullScreenWindow();
}
public void restoreScreen(){
	Window w = vc.getFullScreenWindow();
	if(w != null){
		w.dispose();
	}
	vc.setFullScreenWindow(null);
}
}

 

 

 

Hadde vært kjempe hyggelig hvis noen kunne ha hjulpet meg og sagt hva som var feil, og eventuelt hvordan å gjøre det ordentlig.

Endret av ordboka
Lenke til kommentar
Videoannonse
Annonse

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...