Finde den Fehler einfach nicht. Habe wein Menü für ein spiel gemacht doch sobald ich die Buttons einfügte bekam ich eine Nullpointer. die Konsole hat mir die Zeilen mit dem fehller ausgegeben indem die Buttons in der for schleife gedrawt && geupdated werden.
CODE:
frame:
menu:
collision:
Button:
CODE:
frame:
Java:
private BufferStrategy strat;
public frame(){
super("SasiJump");
}
public void makestrat(){
createBufferStrategy(2);
strat = getBufferStrategy();
}
public void repaint(){
Graphics g = strat.getDrawGraphics();
draw(g);
g.dispose();
strat.show();
}
public static void draw(Graphics g) {
if(main.stat.equalsIgnoreCase("menu")){
Menu.draw(g);
}
}
public static void update(float tslf){
if(main.stat.equalsIgnoreCase("menu")){
Menu.update(tslf);
}else{
Level.update(tslf);
}
}
}
Code:
package main;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Menu {
private static Button[] buttons;
static BufferedImage background = ImageLoader.loadImage("menu_background");
static BufferedImage title = ImageLoader.loadImage("title");
static BufferedImage button_unpressed = ImageLoader.loadImage("button");
static BufferedImage button_pressed = ImageLoader.loadImage("button_clicked");
static BufferedImage button_mouseover = ImageLoader.loadImage("button");
public Menu(){
buttons = new Button[3];
BufferedImage[] textures = {button_unpressed, button_mouseover, button_pressed};
Font f = new Font("SansSerif", 0 , 30);
buttons[0] = new Button(200, "Play", textures, f);
buttons[1] = new Button(350, "Options", textures, f);
buttons[2] = new Button(500, "Exit", textures, f);
}
public static void draw(Graphics g){
g.drawImage(background, 0, 0, main.WIDTH, main.HEIGHT, null);
g.drawImage(title, 100, 50, 600, 200, null);
for(int i = 0; i < buttons.length ; i++){
buttons[i].draw(g);
}
}
public static void update(float tslf){
for(int i = 0; i < buttons.length; i++){
if(buttons[i].update()){
if(i == 0) main.stat = "levelmenu";
else if (i == 1);
else if (i == 2) System.exit(0);
}
}
}
}
Java:
package main;
public class Collision {
public static boolean rectToRect(float rect1x, float rect1y, int width1, int height1,
float rect2x, float rect2y, int width2, int height2){
if(rect1x <= rect2x + width2 && rect1x + width1 >= rect2x && rect1y <= rect2y + height2 && rect1y + height1 >= rect2y) return true;
return false;
}
}
Java:
package main;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Button {
private int x;
private int y;
private Font font;
private FontMetrics fm;
private String name;
private int isPressed;
private BufferedImage[] look;
public Button(int y, String name, BufferedImage[] look, Font font){
this.y = y;
this.name = name;
this.look = look;
this.font = font;
x = 400 - look[0].getWidth()/2;
}
public void draw(Graphics g){
g.setFont(font);
fm = g.getFontMetrics();
g.drawImage(look[isPressed], x, y, null);
g.setColor(Color.BLACK);
g.drawString(name, x + look[isPressed].getWidth() /2 - fm.stringWidth(name)/2, y + look[isPressed].getHeight()/2 + font.getSize()/2);
}
public boolean update(){
if(Keyboard.getButton() != 1&& isPressed == 2){
isPressed = 2;
return true;
}
isPressed = 0;
int mx = Keyboard.getMouseX();
int my = Keyboard.getMouseY();
if(Collision.rectToRect(mx, my, 0 ,0 ,x ,y, look[isPressed].getWidth(), look[isPressed].getHeight())){
if(Keyboard.getButton() == 1) isPressed = 2;
else isPressed = 1;
}
return false;
}
}