# SpriteSheetloader



## HasiHasi98 (9. Aug 2012)

Ich weiß nicht was daran falsch ist bitte helft mir.

Game.java:

```
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import com.RPG.Patrick.Sprites.SpriteSheetloader;



public class Game extends Canvas implements Runnable
{
	private static final long serialVersionUID = 1L;
	
	public static final int HEIGHT = 360;
	public static final int WIDTH = 480;
	public static final int SCALE = 2;
	public static Dimension Game_GIM = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
	public static final String NAME = "RPG";
	
	private BufferedImage image = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);
	private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
	
	public SpriteSheetloader loader;
	private Screen screen;
	private boolean running = false;
	Random random = new Random();
	
	public void start()
	{
		running = true;
		new Thread(this).start();
	}
	
	public Game()
	{
		
	}
	
	public void init()
	{
		BufferedImage sheet = null;
		try
		{
		    sheet =ImageIO.read(Game.class.getResourceAsStream("/tiles.png"));
		} catch(IOException e)
		{
			e.printStackTrace();
		}
		
		loader = new SpriteSheetloader(sheet);	}
	    *screen* = new Screen(WIDTH, HEIGHT, loader);   *Fehler1: Syntax error on token "screen", VariableDeclaratorId expected after this token
	    
	public void run()
	{
		init();
		while(running)
		{
			tick();
			render();
		}
	}
	

	public void tick()
	{
		screen.render(0, 0, 0, 16, 16);
	}
	
	public void render()
	{
		BufferStrategy bs = getBufferStrategy();
		if(bs == null)
		{
			createBufferStrategy(3);
			requestFocus();
			return;
		}
		
		Graphics g = bs.getDrawGraphics();
		g.drawImage(image, 0, 0, null);
		g.dispose();
		bs.show();
	}
	
	**public static void main(String[] args)**
	{
		Game game = new Game();
		game.setPreferredSize(new Dimension(Game_GIM));
		game.setMaximumSize(new Dimension(Game_GIM));
		game.setMinimumSize(new Dimension(Game_GIM));
		
		JFrame frame = new JFrame(NAME);
		frame.setLayout(new BorderLayout());
		frame.add(game, BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		frame.setResizable(true);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		game.start();
	}

}
```
**Fehler2(Wird nicht von eclipse unterstrichen in der Console wird aber der Fehler) :Exception in thread "main" java.lang.Error: Unresolved compilation problem:  (angezeigt)


andere Klassen
Screen:

```
import com...Sprites.SpriteSheetloader;



public class Screen 
{
	private SpriteSheetloader loader;
	private int w,h;
	int xOffset = 0, yOffset = 0;
	public int[] pixels;
	public Screen(int w, int h,SpriteSheetloader newloader)
	{
		this.loader = newloader;
		this.w = w;
		this.h = h;
		
		pixels = new int[w*h];
	}
	
	public void render (int xPos, int yPos, int tile, int width, int height)
	{
		loader.grabTile(tile, width, height);
		
		xPos -= xOffset;
		yPos -= yOffset;
		
		for(int y = 0;y < height;y++)
		{if(yPos + y < 0 || yPos + y >= h)continue;
			for(int x = 0; x < width;x++)
			{if(xPos + x < 0 || xPos + x >= w)continue;
				
				int col = loader.pixels[x + (y * height)];
				if(col != -65281) pixels[(x + xPos) + (y + yPos) * w] = col;
			}
		}
	}

}
```
SpriteSheetloader:

```
import java.awt.image.*;

public class SpriteSheetloader 
{
	
	public int[] sheetPixels;
	public int [] pixels;
	int x,y,sheetWidth;
	
	public SpriteSheetloader(BufferedImage sheet)
	{
		BufferedImage image = new BufferedImage(sheet.getWidth(), sheet.getHeight(), BufferedImage.TYPE_INT_ARGB);
		image.getGraphics().drawImage(sheet, 0, 0, null);
		
		sheetPixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
		
		sheetWidth = sheet.getWidth();
	}
	
	public void grabTile(int tile, int width, int height)
	{
		sheetPixels = new int [width * height];
		
		int xTile = tile % 16;
		int yTile = tile / 16;
		
		for(int y = 0; y < height; y++)
		{
			for(int x = 0; x < width; x++)
			{
				pixels[(x + (y + width))] = sheetPixels [((x + (xTile * width)) + (y+(yTile * height))) + sheetWidth];
			}
		}
	}

}
[/Java]
```


----------



## nillehammer (9. Aug 2012)

```
**public static void main(String[] args)**
```
Das sieht komisch aus. Das wird so nicht kompilieren. Ansonsten enthalten Java-Fehlermeldungen sehr gute Kontextinformationen (bspw. Zeilennummer etc.) die kann man meist sehr gut selbst auswerten. Zumindest aber hier zur Frage mit dazu posten.


----------



## SlaterB (9. Aug 2012)

in Zeile 53 ist durch } die Methode zu Ende, Zeile 54 steht außerhalb der Methode,

solche Grundfehler können jederzeit auftreten, unbedingt selber überdenken


----------



## HasiHasi98 (9. Aug 2012)

die * sind nur damit man weiß wo der fehler ist 
1Fehler: zeile 54     
	
	
	
	





```
screen = new Screen(WIDTH, HEIGHT, loader);
```
2Fehler: zeile 88     
	
	
	
	





```
public static void main(String[] args)
```


----------



## HasiHasi98 (9. Aug 2012)

Danke hab 2 stunden nichts gefunden.


----------

