import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Auto
{
private static boolean fahren;
static double dx;
static double dy;
static AffineTransform rotation;
static AffineTransform translation;
private static boolean[] keyPressed = new boolean[4];
private static int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
private static int maxKmh;
private static int saveKmh;
private int kg;
private int beschleunigung;
private static Image car;
private final static String load = "C:\\Dokumente und Einstellungen\\Karl\\Desktop\\Suchen\\NFS2D\\";
private static String name;
private static int x;
private static int y;
private static JPanel map = null;
private static double degree = 0;
public Auto(int max, int kg, int besch, String name, CarPanel karte, int x, int y,boolean fahr)
{
this.maxKmh = max;
this.kg = kg;
this.beschleunigung = besch;
this.name = name;
this.map = karte;
this.x = x;
this.y = y;
this.saveKmh = this.maxKmh;
this.fahren = fahr;
}
public static class Game extends JFrame
{
public Game(String s)
{
super(s);
this.setDefaultCloseOperation(Game.EXIT_ON_CLOSE);
this.setSize(500, 500);
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
update(e, true);
}
public void keyReleased(KeyEvent e)
{
update(e, false);
}
public void moveLocic()
{
if(degree >= 6.26 || degree <= -6.26)
{
degree = 0;
}
double dx;
double dy;
dx = Math.sin(Math.toRadians(-degree));
dy = Math.cos(Math.toRadians(-degree));
rotation = AffineTransform.getRotateInstance(Math.toRadians(degree), x + car.getWidth(map) / 2, y + car.getHeight(map) / 2);
translation = AffineTransform.getTranslateInstance(dx, dy);
System.out.println(degree);
}
public void update(KeyEvent e, boolean pressed)
{
if(fahren)
{
if (e.getKeyCode() == KeyEvent.VK_UP)
{
maxKmh = saveKmh;
keyPressed[UP] = pressed;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
keyPressed[RIGHT] = pressed;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
keyPressed[LEFT] = pressed;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
keyPressed[DOWN] = pressed;
}
if (e.isShiftDown() && e.getKeyCode() == KeyEvent.VK_UP)
{
maxKmh = 300;
keyPressed[UP] = pressed;
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
moveLocic();
}
}
});
new Thread()
{
public void run()
{
while (true)
{
// y -= (keyPressed[UP] ? maxKmh/10 : 0);
if (keyPressed[UP])
{
x -= translation.getTranslateX();
y -= translation.getTranslateY();
}
if (keyPressed[DOWN])
{
x += translation.getTranslateX();
y += translation.getTranslateY();
}
degree += (keyPressed[RIGHT] ? 0.1 : 0);
// x += (keyPressed[RIGHT] ? 5 : 0);
// y += (keyPressed[DOWN] ? 5 : 0);
// x -= (keyPressed[LEFT] ? 5 : 0);
degree -= (keyPressed[LEFT] ? 0.1 : 0);
repaint();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}.start();
}
}
public static class CarPanel extends JPanel
{
public CarPanel()
{
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
map.setBackground(Color.white);
File f = new File(load + name + ".gif");
try
{
car = ImageIO.read(f);
}
catch (IOException e)
{
e.printStackTrace();
}
rot(g.create(),x,y);
}
}
public static void rot(final Graphics g, int xPos, int yPos)
{
Graphics2D g2d = (Graphics2D) g;
int w = car.getWidth(null);
int h = car.getHeight(null);
xPos = x;
yPos = y;
//center of rotation is center of the Image:
int xRot = x + w / 2;
int yRot = y + h / 2;
//rotate:
AffineTransform rotation = g2d.getTransform();
rotation.rotate(degree, xRot, yRot);
//draw Image with rotation:
g2d.setTransform(rotation);
g2d.drawImage(car, xPos, yPos, w, h, (ImageObserver)map);
g2d.dispose();
}
}