Hallo,
ich kenne mich noch nicht gut mit Java aus und habe mal versucht ein kleines Spiel zu programmieren. Das hat auch funktioniert, aber als ich es am nächsten Tag nochmal getestet habe, habe ich plötzlich eine ganze Reihe an Fehlermeldungen bekommen und ich hab keine Idee, woran das liegen könnte. Ich benutze Eclipse. Hier ist die erste Klasse, von der aus gestartet wird:
Das ist die zweite Klasse:
Und das sind die Fehlermeldungen:
Ich wär sehr dankbar, wenn sich jemand die Mühe macht und sich das anschaut.
ich kenne mich noch nicht gut mit Java aus und habe mal versucht ein kleines Spiel zu programmieren. Das hat auch funktioniert, aber als ich es am nächsten Tag nochmal getestet habe, habe ich plötzlich eine ganze Reihe an Fehlermeldungen bekommen und ich hab keine Idee, woran das liegen könnte. Ich benutze Eclipse. Hier ist die erste Klasse, von der aus gestartet wird:
Java:
package ordicolor;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class frame extends JFrame implements ActionListener {
//deklaration
private JButton start;
private JButton info;
private JButton exit;
private JLabel lbl1;
public static JFrame fenster;
public static void main(String[] args) {
//Ein frame wird erstellt.
frame frame = new frame("Ordicolor Fahrstuhlsimulator - Men�");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450,320);
frame.setLayout(null);
frame.setVisible(true);
}
public frame(String title){
super(title);
//Die Buttons werden hinzugef�gt.
start = new JButton("Spiel starten");
start.setBounds(50,50,150,50);
start.addActionListener(this);
add(start);
info = new JButton("Credits");
info.setBounds(250,50,150,50);
info.addActionListener(this);
add(info);
exit = new JButton("Exit");
exit.setBounds(175,225,100,50);
exit.addActionListener(this);
add(exit);
lbl1 = new JLabel("Ordicolor Fahrstuhlsimulator");
lbl1.setBounds(150,150,200,50);
add(lbl1);
}
/**
* �ffnet das Spielfenster.
*/
public static void fenster(){
fenster = new JFrame("Ordicolor");
fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenster.setSize(1200,675);
fenster.setResizable(false);
fenster.setVisible(true);
fenster.add(new gui());
}
public void fensterTutorial(){
// fenster.add(new tutorial());
}
//Pr�ft, ob Kn�pfe gedt�ckt werden.
public void actionPerformed(ActionEvent e) {
if (e.getSource() == start){
fenster();
}
if (e.getSource() == info){
Object[] options = { "OK"};
JOptionPane.showOptionDialog(null, "Programmiert von apple_pie1998", "Information",JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null, options, options[0]);
}
if (e.getSource() == exit){
System.exit(0);
}
}
}
Das ist die zweite Klasse:
Java:
package ordicolor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class gui extends JPanel implements ActionListener{
Image bg;
Image vg;
int key;
static int FahrstuhlHoehe;
Timer time;
int Etage;
boolean fahren;
String lblanweisung;
Random rnd;
boolean richtigeEtage;
int rndEtage;
int aktuelleEtage;
int sollEtage;
ImageIcon i1;
ImageIcon i2;
//Konstruktor
public gui(){
fahren = false;
FahrstuhlHoehe = -4725;
Etage = 0;
setFocusable (true);
i1 = new ImageIcon((getClass().getResource("Fahrstuhl.jpg")));
i2 = new ImageIcon((getClass().getResource("Innen.png ")));
bg = i1.getImage();
vg = i2.getImage();
rnd = new Random();
richtigeEtage = true;
lblanweisung = new String("");
addKeyListener(new AL());
time = new Timer(5,this);
time.start();
}
//Hier kommen alle Methoden rein, die vom Timer aufgerufen werden sollen.
public void actionPerformed(ActionEvent e){
repaint();
fahren();
anweisung();
}
//Hier wird gesteuert, wie der Fahrstuhl fahren soll.
public void fahren(){
if(fahren==true){
if(Etage < FahrstuhlHoehe){
FahrstuhlHoehe -=2;
if(Etage >= FahrstuhlHoehe){
fahren = false;
}
}
if(Etage > FahrstuhlHoehe){
FahrstuhlHoehe +=2;
if(Etage <= FahrstuhlHoehe){
fahren = false;
}
}
}
}
//Hier werden die Bilder in das Fenster gezeichnet.
public void paint(Graphics g){
super.paint(g);
Graphics2D f2 = (Graphics2D)g;
f2.drawImage(bg, 0, FahrstuhlHoehe, null);
f2.drawImage(vg, 0, 0, null);
g.setColor( Color.red );
g.drawString( lblanweisung, 525, 50 );
}
//Generiert ein zuf�lliges Stockwerk, in das der Fahrstuhl fahren soll und testet, ob der Fahrstuhl dort ist.
public void anweisung(){
if(rndEtage == 7){
sollEtage = 0;
}
if(rndEtage == 6){
sollEtage = -676;
}
if(rndEtage == 5){
sollEtage = -1350;
}
if(rndEtage == 4){
sollEtage = -2026;
}
if(rndEtage == 3){
sollEtage = -2700;
}
if(rndEtage == 2){
sollEtage = -3376;
}
if(rndEtage == 1){
sollEtage = -4050;
}
if(rndEtage == 0){
sollEtage = -4726;
}
if(sollEtage < FahrstuhlHoehe + 4){
if(sollEtage > FahrstuhlHoehe - 4){
if(fahren == false){
richtigeEtage = true;
}
}
}
if(richtigeEtage == true){
rndEtage = rnd.nextInt(7 - 0)+1;
lblanweisung = String.valueOf(rndEtage);
richtigeEtage = false;
System.out.print("test");
}
}
//Das ist eine private innere Klasse, die die gedr�ckten Tasten abfragt (also die Bewegung steuert).
private class AL extends KeyAdapter{
public AL(){}
public void keyPressed(KeyEvent e){
key = e.getKeyCode();
if(fahren == false){
//Etagen
if(key == KeyEvent.VK_7){
Etage = 0;
fahren = true;
}
if(key == KeyEvent.VK_6){
Etage = -676;
fahren = true;
}
if(key == KeyEvent.VK_5){
Etage = -1350;
fahren = true;
}
if(key == KeyEvent.VK_4){
Etage = -2026;
fahren = true;
}
if(key == KeyEvent.VK_3){
Etage = -2700;
fahren = true;
}
if(key == KeyEvent.VK_2){
Etage = -3376;
fahren = true;
}
if(key == KeyEvent.VK_1){
Etage = -4050;
fahren = true;
}
if(key == KeyEvent.VK_E){
Etage = -4726;
fahren = true;
}
}
}
public void keyReleased(KeyEvent e){
key = e.getKeyCode();
if(key == KeyEvent.VK_W || key == KeyEvent.VK_S || key == KeyEvent.VK_1){
FahrstuhlHoehe += 0;
}
}
}
}
Und das sind die Fehlermeldungen:
Code:
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at ordicolor.gui.<init>(gui.java:41)
at ordicolor.frame.fenster(frame.java:67)
at ordicolor.frame.actionPerformed(frame.java:79)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Ich wär sehr dankbar, wenn sich jemand die Mühe macht und sich das anschaut.