Wir machen in der Schule Java und ich wollte an unserem Auto-Projekt weiterarbeiten und eine Steuerung hinzufügen doch ich habe jetzt schon Probleme und zwar ich fang eine beliebige Taste ab um das Auto ein bisschen nach Rechts zu bewegen doch es funktioniert nicht es passiert einfach nichts. Das Auto soll sich bewegen bei einer beliebigen Tasten erstmal, danach würde ich alles andere machen. Hier ist mein Quellcode:
Tut mir leid für die Schreibweise wie ich programmiere aber ich bin nicht gerade der größte Profi in Java.
Haupt.java (Main Methode)
Auto.java
und die Steuerung Klasse die Probleme macht:
AutoSteuerung.java
Tut mir leid für die Schreibweise wie ich programmiere aber ich bin nicht gerade der größte Profi in Java.
Haupt.java (Main Methode)
Java:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
public class Haupt extends Frame implements WindowListener
{
//Frame erstellen mit dem Titel Auto
private static Frame autoFrame = new Frame("Auto");
//Größe des Fensters
static final int H_SIZE = 800;
static final int W_SIZE = 600;
//Das/Die Auto(s)
private static Auto car = new Auto();
static ImageObserver myImageObserver;
public Haupt()
{
autoFrame = this;
autoFrame.setSize(H_SIZE, W_SIZE);
autoFrame.addWindowListener(this);
autoFrame.setLocation(50,50);
//Fenster anzeigen:
autoFrame.setVisible(true);
}
public void paint(Graphics g)
{
car.setg(g);
car.setRichtung(Auto.RECHTS);
car.setx(150);
car.sety(150);
car.setMyImageObserver(this);
car.zeichnen();
}
public static void main(String[] args)
{
new Haupt();
//AutoSteuerung starten um die Benutzer eingaben zu kontrollieren um das Auto zu steuern.
new AutoSteuerung(autoFrame, car);
}
public void windowActivated(WindowEvent e){/*System.out.println("windowActivated");*/}
public void windowIconified(WindowEvent e){/*System.out.println("windowIconified");*/}
public void windowDeiconified(WindowEvent e){/*System.out.println("windowDeiconified");*/}
public void windowDeactivated(WindowEvent e){/*System.out.println("windowDeactivated");*/}
public void windowOpened(WindowEvent e){/*System.out.println("windowOpened");*/}
public void windowClosing(WindowEvent e){/*System.out.println("windowClosing");*/ System.exit(0);}
public void windowClosed(WindowEvent e){/*System.out.println("windowClosed");*/}
}
Auto.java
Java:
import java.awt.*;
import java.awt.image.*;
public class Auto
{
public int x;
public int y;
private int richtung;
private Graphics g;
public static final int STOP = 0;
public static final int RECHTS = 1;
public static final int LINKS = 2;
public static final int OBEN = 3;
public static final int UNTEN = 4;
Image wolke_links = Toolkit.getDefaultToolkit().getImage("C:\\rauchwolke_links.png");
Image wolke_rechts = Toolkit.getDefaultToolkit().getImage("C:\\rauchwolke_rechts.png");
ImageObserver myImageObserver;
// Konstrukter Methoden
public Auto()
{
}
public Auto(int r)
{
this.setRichtung(r);
}
public Auto(int x, int y)
{
this.setx(x);
this.sety(y);
}
public Auto(int x, int y, int r)
{
this.setx(x);
this.sety(y);
this.setRichtung(r);
}
// Setter-Getter x,y
public boolean setx(int wert)
{
if (wert < 0) {return false;}
else {x = wert; return true;}
}
public boolean sety(int wert)
{
if (wert < 0) {return false;}
else {y = wert; return true;}
}
public boolean setxy(int x, int y)
{
if (!this.setx(x)) return false;
if (!this.sety(y)) return false;
zeichnen(); return true;
}
public int getx()
{
return x;
}
public int gety()
{
return y;
}
//Set Graphics - Zugriff auf Frame von Auto aus
public void setg(Graphics g)
{
this.g = g;
}
//Setter-Getter Auto Richtung
public boolean setRichtung(int richtung)
{
switch(richtung)
{
case 0: //STOP
case 1: //RECHTS
case 2: //LINKS
case 3: //OBEN
case 4: this.richtung = richtung; return true; //UNTEN
default: return false; //Alles andere gibt es nicht
}
}
public int getRichtung()
{
return richtung;
}
//Setter-Getter ImageObserver
public void setMyImageObserver(ImageObserver io)
{
this.myImageObserver = io;
}
public ImageObserver getMyImageObserver()
{
return myImageObserver;
}
//Zeichnen vom Auto
//Richtung
public void zeichnen() {
switch (richtung)
{
case LINKS:
this.zeichneLinks();
break;
case RECHTS:
this.zeichneRechts();
break;
case OBEN:
//this.zeichneoben();
break;
case UNTEN:
//this.zeichneoben();
break;
}
}
//Zeichnen
public void zeichneLinks()
{
g.setColor(Color.BLACK);
g.fillRect(x, y + 35, 120, 40);// Auto
g.fillRect(x + 10, y + 20, 100, 20);// Auto
g.setColor(Color.BLACK);
g.fillOval(x + 10, y + 50, 30, 30);// Räder
g.fillOval(x + 80, y + 50, 30, 30);// Räder
g.setColor(Color.WHITE);
g.fillOval(x + 13, y + 53, 24, 24);// Felge
g.fillOval(x + 83, y + 53, 24, 24);// Felge
g.setColor(Color.WHITE);
g.fillRect(x + 12, y + 24, 54, 10);// Fenster
g.fillRect(x + 68, y + 24, 36, 10);// Fenster
g.setColor(Color.RED);
g.fillRect(x + 110, y + 45, 10, 15);// Rückleuchte
g.setColor(Color.YELLOW);
g.fillRect(x + 1, y + 50, 10, 10);// Scheinwerfer
this.g.drawImage(wolke_links, x + 117, y + 33, this.getMyImageObserver());
}
public void zeichneRechts()
{
g.setColor(Color.BLACK);
g.fillRect(x, y + 35, 120, 40);// Auto
g.fillRect(x + 10, y + 20, 100, 20);// Auto
g.setColor(Color.BLACK);
g.fillOval(x + 10, y + 50, 30, 30);// Räder
g.fillOval(x + 80, y + 50, 30, 30);// Räder
g.setColor(Color.WHITE);
g.fillOval(x + 13, y + 53, 24, 24);// Felge
g.fillOval(x + 83, y + 53, 24, 24);// Felge
g.setColor(Color.WHITE);
g.fillRect(x + 50, y + 24, 54, 10);// Fenster
g.fillRect(x + 12, y + 24, 36, 10);// Fenster
g.setColor(Color.RED);
g.fillRect(x + 1, y + 45, 10, 15);// Rückleuchte
g.setColor(Color.YELLOW);
g.fillRect(x + 110, y + 50, 10, 10);// Scheinwerfer
this.g.drawImage(wolke_rechts, x - 50, y + 33, this.getMyImageObserver());
}
}
und die Steuerung Klasse die Probleme macht:
AutoSteuerung.java
Java:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import javax.swing.JOptionPane;
public class AutoSteuerung implements KeyListener
{
Frame f;
Auto car;
public AutoSteuerung(Frame f, Auto car)
{
this.f = f;
f.addKeyListener(this);
this.car = car;
}
public void keyPressed(KeyEvent e) {
String s = "Taste gedrückt: Key = " + e.getKeyChar()
+ " Code = " + e.getKeyCode();
car.setRichtung(Auto.LINKS);
car.setx(150);
car.sety(150);
car.setMyImageObserver(car.getMyImageObserver());
car.zeichnen();
f.repaint();
//JOptionPane.showMessageDialog(null, s, "Test", JOptionPane.OK_CANCEL_OPTION);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}