import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.applet.*;
import java.net.URL;
public class RaceGame extends JComponent
{
static int x=560;
static int y=410;
static int currentAngle=0;
static double hspeed,vspeed;
static int carspeed=0;
double hangle,vangle;
Image car;
Image finish;
Image getImage(String filename)
{
URL url = getClass().getResource(filename);
ImageIcon icon = new ImageIcon(url);
return icon.getImage();
}
public RaceGame()
{
car=getImage("car1.jpeg");
//finish=getImage("finish.jpg");
}
public void CreateBase()
{
JFrame frame=new JFrame("Dare2Race");
frame.setBounds(70,30,650,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=frame.getContentPane();
c.add(new RaceGame());
frame.addKeyListener(new adapter());
c.setBackground(Color.BLACK);
}
public void rotate()
{
currentAngle+=1;
if(currentAngle>=360)
{
currentAngle=0;
}
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
AffineTransform origXform=g2d.getTransform();
AffineTransform newXform=(AffineTransform)(origXform.clone());
newXform.rotate(Math.toRadians(currentAngle),x,y);
g2d.setTransform(newXform);
//g2d.drawImage(finish,12,10,this);
g2d.drawImage(car,x,y,this);
g2d.setTransform(origXform);
g.setColor(Color.WHITE);
g.drawLine(640,380,420,380);
g.drawLine(640,460,320,460);
g.drawLine(420,380,420,300);
g.drawLine(320,460,320,380);
g.drawLine(420,300,125,300);
g.drawLine(320,380,230,380);
g.drawLine(230,380,230,460);
g.drawLine(230,460,2,460);
g.drawLine(125,300,125,370);
g.drawLine(125,370,105,370);
g.drawLine(2,460,2,180);
g.drawLine(105,370,105,300);
g.drawLine(2,180,450,180);
g.drawLine(105,300,105,250);
g.drawLine(105,250,550,250);
g.drawLine(550,250,550,20);
g.drawLine(550,20,275,20);
g.drawLine(450,180,450,100);
g.drawLine(450,100,360,100);
g.drawLine(360,100,360,160);
g.drawLine(360,160,10,160);
g.drawLine(10,160,10,30);
g.drawLine(275,20,275,90);
g.drawLine(275,90,110,90);
g.drawLine(110,90,110,30);
repaint();
}
class adapter extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
case KeyEvent.VK_LEFT:
currentAngle--;
repaint();
break;
case KeyEvent.VK_RIGHT:
currentAngle++;
repaint();
break;
case KeyEvent.VK_UP:
carspeed++;
hangle=Math.toRadians(currentAngle);
vangle=Math.toRadians(currentAngle);
hspeed=((double)carspeed)*Math.cos(hangle);
vspeed=((double)carspeed)*Math.sin(vangle);
x = x - (int) hspeed;
y = y - (int) vspeed;
repaint();
break;
case KeyEvent.VK_DOWN:
carspeed--;
hangle=Math.toRadians(currentAngle);
vangle=Math.toRadians(currentAngle);
hspeed=((double)carspeed)*Math.cos(hangle);
vspeed=((double)carspeed)*Math.sin(vangle);
x = x + (int)hspeed;
y = y + (int)vspeed;
repaint();
break;
}
}
}
public static void main(String[]args)
{
RaceGame race=new RaceGame();
race.CreateBase();
//race.setDoubleBuffered(true);
}
}