# Quaxlis tutorium problem -.-



## oyo (23. Sep 2009)

hi ich hab ein problem mit dem anzeigen der fps, da bei mir immer 0 steht...
darum wird wahrscheinlich auch kein helicopter angezeigt. ich habe mich eigentlich an das tutorium gehalten. hier mal mein code:

```
import java.awt.*;
	import java.awt.event.*;
	import java.awt.image.BufferedImage;
	import java.io.IOException;
	import java.net.URL;
	import java.util.Vector;
	import javax.imageio.ImageIO;
	import javax.swing.*;

	public class gamepanel extends JPanel implements Runnable{
		boolean game_running = true;
		long delta = 0;
		long last = 0;
		long fps = 0;
		long gameover = 0;
		sprite copter;
		Vector<sprite> actors;
		private static final long	serialVersionUID	= 1L;
		
		public static void main(String[] args){
			new gamepanel(800,600);
		}
		public gamepanel(int w, int h){
			this.setPreferredSize(new Dimension(w,h));
			this.setBackground(Color.cyan);
			JFrame frame = new JFrame("GameDemo");
			frame.setLocation(100,100);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			frame.add(this);
			frame.pack();
			frame.setVisible(true);
			doIni();
			}
		private void doIni() {
			BufferedImage[] heli = this.loadPics("pics/heli.gif", 4);
			last = System.nanoTime();
			actors = new Vector<sprite>();
			copter = new sprite(heli, 400, 300 ,100, this);
			actors.add(copter);
			Thread t = new Thread();
			t.start();
			
		}
		public void run() {
			
			 while(game_running){
			
				  
				computeDelta();
				doLogic();
				moveObjects();
				repaint();
				
			  try {
					Thread.sleep(10);
				  } 
			  catch (InterruptedException e) {}	
			 }
				
		}
			

		private void computeDelta(){
			
			delta = System.nanoTime() - last;
			last = System.nanoTime();
			
			fps = ((long) 1e9)/delta;
		}
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			
			
			
			g.setColor(Color.white);
			g.drawString("FPS: " + Long.toString(fps), 30, 20);
			if(actors!=null){
				for(Drawable draw:actors){
					draw.drawObjects(g);
				}
			}
			
					
		}
		private BufferedImage[] loadPics(String path, int pics){
			
			BufferedImage[] anim = new BufferedImage[pics];
			BufferedImage source = null;
			
			URL pic_url = getClass().getClassLoader().getResource(path);

			try {
				source = ImageIO.read(pic_url);
			} catch (IOException e) {}
			
			for(int x=0;x<pics;x++){
				anim[x] = source.getSubimage(x*source.getWidth()/pics, 0, 
						source.getWidth()/pics, source.getHeight());
			}
			
			return anim;
		}
		public void doLogic(){
			for(Movable mov:actors){
				mov.dologic(delta);
			}	
		}
		public void moveObjects(){
			for(Movable mov:actors){
				mov.move(delta);
			}	
		}
		

}
```

und noch die sprite klasse:

```
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public class sprite extends Rectangle2D.Double implements Drawable, Movable{

	long delay;
	long animation = 0;
	gamepanel parent;
	BufferedImage[] pics;
	int currentpic = 0;
	
	protected double dx;
	protected double dy;
	
	int loop_from;
	int loop_to;
	
	boolean remove = false;
  
	public sprite(BufferedImage[] i, double x, double y, long delay, gamepanel p ){
		
		pics = i;
		this.x = x;
		this.y = y;
		this.delay = delay;
		this.width = pics[0].getWidth();
		this.height = pics[0].getHeight();
	  parent = p;
	  loop_from = 0;
	  loop_to = pics.length - 1;
	}
	
	public void drawObjects(Graphics g) {
		g.drawImage(pics[currentpic], (int) x, (int) y, null);
	}

	public void dologic(long delta) {

		animation += (delta/1000000);
		if (animation > delay) {
			animation = 0;
			computeAnimation();
		}

	}

	public void move(long delta) {
		 
    if(dx!=0){
      x += dx*(delta/1e9);
    }
    
    if(dy!=0){
      y += dy*(delta/1e9);
    }

	}

	private void computeAnimation(){
    
		currentpic++;

    if(currentpic>pics.length){
        currentpic = 0;
    }
		
	}
	
	public void setVerticalSpeed(double d) {
		    dy = d;
		  }
	public void setHorizontalSpeed(double d) {
		    dx = d;
		  }
		  
	public double getVerticalSpeed(){
		  	return dy;
		  }
		  
	public double getHorizontalSpeed(){
		  	return dx;
		  }


  
	
}
```

habe ich irgendwas übersehen oder namen falsch deklariert ??
ich kann selber gerade nichts finden ;(


----------



## Antoras (23. Sep 2009)

```
Thread t = new Thread(this);
```
Wenn das 
	
	
	
	





```
this
```
 fehlt, dann wird deine run-Methode überhaupt nicht aufgerufen. Solche Fehler findest du mit Konsolenausgaben und wenn das nicht hilft mit dem Debugger.

Ach ja: Halt dich bitte an die Code Conventions, also z.B. Klassennamen groß schreiben (gamepanel -> GamePanel).


----------



## oyo (23. Sep 2009)

alles klar vielen dank !


----------

