Sei sulla pagina 1di 6

import import import import import import import import import

java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; java.awt.event.KeyEvent; java.awt.event.KeyListener; java.awt.event.MouseEvent; java.awt.event.MouseListener; java.awt.event.MouseMotionListener; java.awt.image.BufferedImage;

import javax.swing.JApplet; public class test extends JApplet implements Runnable, MouseListener, MouseMotio nListener{ Thread t = new Thread(this); BufferedImage tempImage; Graphics2D g2d; Charger c; Battery b; Motor m; Car car; static final int FPS = 30; static final int w = 500; static final int h = 500; double cx=0; double cy=0; double angle=0; long lastTime = System.currentTimeMillis(); long curTime = lastTime; static boolean pressed = false;

public void init(){ tempImage = new BufferedImage(500,500,BufferedImage.TYPE_4BYTE_ABGR); g2d = tempImage.createGraphics(); b = new Battery(); c = new Charger(b); m = new Motor(b); car = new Car(200,200); c.start(); m.start(); t.start(); setSize(w,h); setVisible(true); setFocusable(true); addMouseListener(this);

addMouseMotionListener(this); } public void run(){ while(true){ //---------------------------------------------long lastTime = System.currentTimeMillis(); long curTime = lastTime; while( true ) { curTime = System.currentTimeMillis(); if(curTime - lastTime >= (1000.0/FPS)){ lastTime = curTime; if(checkDistance() == false){ pressed = false; } if(pressed == true){ if(b.status > 0){ double tempx = cx-car.x; double tempy = cy-car.y; angle = Math.atan2(tempy, tempx); car.x += Math.cos(angle)*car.speed; car.y += Math.sin(angle)*car.speed; car.angle = angle; } } repaint(); } }

} } public static void main(String[] args){

new test(); } public void paint(Graphics g){ g2d.setBackground(Color.white); g2d.clearRect(0,0,w,h); g2d.setColor(Color.black); g2d.drawRect(10, 10, 10, 100); for(int i=0;i<b.status;i++){ //g2d.fillRect(10,110-10*b.status,10,b.status*10); //drawBlock(g2d, 10, 10*b.status); //drawBlock(g2d, 10, 10+10*b.status); drawBlock(g2d, 10, 100, i);

} System.out.println(b.status); g2d.setColor(Color.red); car.drawCar(g2d, (int)car.x, (int)car.y); g2d.setColor(Color.RED); //for(int i=0;i<10;i++) //g2d.fillRect(110,10+i*10,10,10); //drawBlock(g2d, 10, 10, i); g.drawImage(tempImage,0,0,w,h,this); } public void drawBlock(Graphics2D g2d, int x, int y, int i){ g2d.setColor(Color.BLACK); g2d.fillRect(x, y-i*10, 10, 9); } public void keyReleased(KeyEvent e){ pressed = false; } public void keyTyped(KeyEvent e){ } public void mouseDragged(MouseEvent e){ curTime = System.currentTimeMillis(); if(curTime - lastTime >= (1000.0/FPS)){ lastTime = curTime; cx = e.getX(); cy = e.getY(); if(b.status > 0 && checkDistance()){ pressed = true; moveCar(); repaint(); } } } public void mouseReleased(MouseEvent e){ repaint(); pressed = false; } public void mousePressed(MouseEvent e){ cx = e.getX(); cy = e.getY(); if(b.status > 0){ moveCar(); repaint();

pressed = true; } } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mouseMoved(MouseEvent e){ } public void mouseClicked(MouseEvent e){ } public void moveCar(){ double tempx = cx-car.x; double tempy = cy-car.y; angle = Math.atan2(tempy, tempx); car.x += Math.cos(angle)*car.speed; car.y += Math.sin(angle)*car.speed; car.angle = angle; } public boolean checkDistance(){ double tempx = cx-car.x; double tempy = cy-car.y; double distance = Math.sqrt(Math.pow(tempx, 2) + Math.pow(tempy, 2)); if(distance > car.speed){ return true; } return false; } } class Battery{ int status; public Battery(){ status = 0; } public synchronized void charge(){ // if(status >= 10){ try{ wait(); }catch(Exception e){} } if(status < 10) status++; System.out.println("Charging : "+status+" %"); try{ Thread.sleep(100);

}catch(Exception e){} notifyAll(); } public synchronized void discharge(){ if(status <= 0){ try{ wait(); }catch(Exception e){} } if(test.pressed == true){ if(status > 0) { status--; } System.out.println("\t\t Using : "+status+" %"); } try{ Thread.sleep(500); }catch(Exception e){} notifyAll(); } } class Charger extends Thread{ Battery battery; public Charger(Battery b){ battery = b; } public void run(){ while(true){ if(test.pressed == false){ battery.charge(); } } } } class Motor extends Thread{ Battery battery; public Motor(Battery b){ battery = b; } public void run(){ while(true){ if(test.pressed == true){ battery.discharge(); } } } } class Car { double x; double y; double angle; //

double speed; public Car(int x, int y){ this.x = x; this.y = y; this.angle = 0; this.speed = 15; } public void drawCar(Graphics2D g2d, int x, int y){ g2d.rotate(angle, x, y); g2d.setColor(Color.RED); g2d.fillRect(x-15, y-5, 30, 10); g2d.setColor(Color.BLACK); g2d.drawRect(x-15, y-5, 30, 10); g2d.drawRect(x+5, y-5, 10, 10); g2d.rotate(-angle, x, y); } }

Potrebbero piacerti anche