Methode für typ unbestimmt

  • Themenstarter Themenstarter _Teddy_
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
T

_Teddy_

Gast
Hallo allerseits,
ich habe ein kleines Problem, ich habe 2Klassen in einem Java Projekt,
die Klassen GamePanel und Sprite, in der Klasse Sprite hab ich eine Methode
loadPics(string,int), nun will ich diese Methode in GamePanel aufrufen aber er
gibt mir folgende Fehlermeldung:

"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method loadPics(String, int) is undefined for the type GamePanel

at GamePanel.doInitializations(GamePanel.java:50)
at GamePanel.<init>(GamePanel.java:41)
at GamePanel.main(GamePanel.java:28)"

irgendwie hab ich im Gefühl,das ich den Fehler längst gesehen haben müsste 😀,
und wenn ihn mir jemand zeigt, dann hau ich warscheinlich den Kopf auf den schreibtisch XD
aber naja, was soll man machen.

Danke schonmal für die hilfe
liebe grüße

Teddy
 
Ich weiß zwar nicht wie dein Quellcode aussieht aber auf eine Methode von deiner anderen Klasse kannst du nur über eine Instanz des Objekt zugreifen bzw. wenn die Methode static ist auch ohne Instanz.

Bsp:

Code:
public class Sprite {

	public void loadPics(String s,int i)
	{
		//do
	}
}

Code:
public class GamePanel {

	//z.B.
	public GamePanel() {
		new Sprite().loadPics("hallo", 5);
	}
}

wenn nicht zeige mal deinen Code
 
Die Klasse GamePanel

Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;



public class GamePanel extends JPanel implements Runnable{

	private static final long serialVersionUID = 1L;
	boolean game_running = true;
	
	long delta = 0;
	long last = 0;
	long fps = 0;
	
	Sprite copter;
	Vector<Sprite> actors;

	

	
	public static void main(String[] args) {
		new GamePanel(800,600);

	}
	
	
	public GamePanel(int w,int h){
		this.setPreferredSize(new Dimension(w,h));
		JFrame frame = new JFrame("GameDemo");
		frame.setLocation(100,100);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(this);
		frame.pack();
		frame.setVisible(true);
		doInitializations();
		
	}
	
	private void doInitializations(){
		
		last = System.nanoTime();
		
		actors = new Vector<Sprite>();
		BufferedImage[] heli = new sprite().loadPics("pics/heli.gif", 4);
		copter = new Sprite(heli,400,300,100,this);
		actors.add(copter);
		
		Thread t = new Thread(this);
		t.start();
		
	}
	
	public void run(){
		
		while(game_running){
			
			computeDelta();
			checkKeys();
			doLogic();
			moveObjects();
			
			repaint();
		
		try{
			Thread.sleep(10);
		} catch(InterruptedException e) {}
		}
	}
	
	private void moveObjects() {
		for(Movable mov:actors){
			mov.move(delta);
		}
	}


	private void doLogic() {
		for(Movable mov:actors){
			mov.doLogic(delta);
		}
		
	}


	private void checkKeys() {
		// TODO Auto-generated method stub
		
	}


	private void computeDelta(){
		
		delta = System.nanoTime() - last;
		last = System.nanoTime();
		
		fps = ((long) 1e9)/delta;
	}

	
	@Override
	public void paintComponent( Graphics g){
		super.paintComponent(g);
		
		g.setColor(Color.red);
		g.drawString("FPS: " + Long.toString(fps), 20, 10);
		
		if(actors!=null){
			for(Drawable draw:actors){
				draw.drawObjects(g);
			}
		}
	}
}


Das interface Movable:
Code:
public interface Movable {
	
	public void doLogic(long delta);
	
	public void move(long delta);
}


das interface drawable:
Code:
import java.awt.Graphics;

public interface Drawable {
	
	public void drawObjects(Graphics g);

}

die klasse Sprite:

Code:
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;




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;
	
	
	
	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;
	}

	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();
		}


		
	}

	private void computeAnimation() {

		currentpic++;
		
		if(currentpic >=pics.length){
			currentpic = 0;
		}
		
	}

	public void move(long delta) {
		
		if(dx!=0){
			x += dx*(delta/1e9);
		}
		
		if(dy!=0){
			y += dy*(delta/1e9);
		}
		
	}
	
	public void setVerticalSpeed(double d) {
		dy = d;
	}
	
	public void setHoorizontalSpeed(double d) {
		dx = d;
	}
	
	public double getVerticalSpeed() {
		return dy;
	}
	
	public double getHoorizontalSpeed() {
		return dx;
	}
	
	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;
	}

}
 
Hallo!

Die Methode Sprite.loadPics() ist private deklariert und damit nur in der Klasse Sprite sichtbar!

Code:
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;
}

MfG Freddy
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben