I am did not use any external libraries and am trying to solve the following error:
Game (4) [Java Application]
com.pipelinegaming.pixelproblem.Game at localhost:50673
Thread [AWT-EventQueue-0] (Running)
Thread [Display] (Suspended (exception NullPointerException))
Sprite.load() line: 25
Sprite.(int, int, int, SpriteSheet) line: 18
Sprite.() line: 9
Screen.render() line: 40
Game.render() line: 137
Game.run() line: 97
Thread.run() line: 745
Thread [DestroyJavaVM] (Running)
/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java
(May 10, 2015, 3:11:03 PM)
Game Class:
package com.pipelinegaming.pixelproblem;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import com.pipelinegaming.pixelproblem.graphics.Screen;
import com.pipelinegaming.pixelproblem.input.Keyboard;
@SuppressWarnings("unused")
public class Game extends Canvas implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width /16 * 9;
public static int scale = 3;
public static String title = "Pixel Problem";
private Keyboard key;
private Screen screen;
private Thread thread;
private boolean running = false;
private JFrame frame;
private BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
private int[] pixels =
((DataBufferInt)image.getRaster().getDataBuffer()).getData();
public Game() {
screen = new Screen(width, height);
frame = new JFrame();
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
key = new Keyboard();
frame.addKeyListener(key);
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop(){
running = false;
try{
thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
}
public void run(){
long lastTime = System.nanoTime();
long sec = System.currentTimeMillis();
final double ns = 1000000000.0 / 60.0;
double delta = 0;
int frames = 0;
int ticks = 0;
while (running) {
long now = System.nanoTime();
delta = delta + (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
ticks++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - sec > 1000) {
sec += 1000;
frame.setTitle(title + " | " + "Tps: " + ticks + " Fps: " + frames);
ticks = 0;
frames = 0;
}
}
stop();
}
public void tick() {
key.update();
}
public void render() {
BufferStrategy buffer = getBufferStrategy();
if(buffer == null){
createBufferStrategy(3);
return;
}
for(int i = 0; i < pixels.length; i++){
pixels[i] = screen.pixels[i];
}
Graphics g = buffer.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
screen.clear();
screen.render();
g.dispose();
buffer.show();
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle(Game.title);
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
Screen Class:
package com.pipelinegaming.pixelproblem.graphics;
import java.util.Random;
public class Screen {
private int height, width;
public int [] pixels;
public final int MAP_SIZE = 8;
public final int MAP_SIZE_MASK = MAP_SIZE - 1;
int [] tiles = new int[MAP_SIZE * MAP_SIZE];
public Screen(int width, int height) {
this.width = width;
this.height = height;
pixels = new int [width * height];
Random random = new Random();
for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++){
tiles [i] = random.nextInt(0xffffff);
}
}
public void clear() {
for(int i = 1; i < 64 * 64; i++){
pixels[i] = 0;
}
}
public void render() {
for (int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
pixels[x+y*width] = Sprite.main_page_background.pixels[(x & 15) +
(y & 15) * Sprite.main_page_background.SIZE];
}
}
}
}
Keyboard Class (Not Used):
package com.pipelinegaming.pixelproblem.input;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Keyboard implements KeyListener{
private boolean [] keys = new boolean[120];
public boolean up, down, left, right;
public void update() {
left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];
down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];
for (int i = 0; i < keys.length; i++){
if(keys[i]) {
System.out.println("Key: " + i);
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
}
SpriteSheet Class:
package com.pipelinegaming.pixelproblem.graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheet {
public String path;
public final int SIZE;
public int[] pixels;
public static SpriteSheet all = new
SpriteSheet("/textures/AllSpriteSheet.png", 256);
public SpriteSheet(String path, int size){
this.path = path;
SIZE = size;
pixels = new int[SIZE * SIZE];
load();
}
private void load() {
try {
BufferedImage image =
ImageIO.read(SpriteSheet.class.getResource(path));
int w = image.getWidth();
int h = image.getHeight();
image.getRGB(0, 0, w, h, pixels, 0, w);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sprite Class:
package com.pipelinegaming.pixelproblem.graphics;
public class Sprite {
public final int SIZE;
public int x, y;
public int [] pixels;
public SpriteSheet sheet;
public static Sprite main_page_background = new Sprite(16, 0, 1,
SpriteSheet.all);
public Sprite (int size, int x, int y, SpriteSheet sheet){
SIZE = size;
pixels = new int [SIZE * SIZE];
this.x = x * size;
this.y = y * size;
load();
}
private void load(){
for (int x = 0; x < SIZE; x++){
pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) *
sheet.SIZE];
}
}
}
I am a begginer to Java progarming but know some other languages, and I would really apreiciate it if someone could please help me out.
Aucun commentaire:
Enregistrer un commentaire