이 프로그램에서 중요한것은 자동차의 움직임이 아니고,
배터리의 충전, 방전이다
충전이 진행되다가 배터리 잔량이 100%가 되면
wait()에 의해 charge가 멈춘다.
차량을 움직이다가 배터리 잔량이 0%가 되면
wait()에 의해 discharge가 멈춘다.
쓰레드는 시간을 두고 봐야하는 개념인듯하다.
예전에 비해서 훨씬 잘 만든듯..
import java.awt.Graphics;import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.JApplet;
public class test extends JApplet implements Runnable, MouseListener, MouseMotionListener{
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);
}
}
서치데이 투시스터즈 네이쳐투유 낙천적인 몽상가 엘카더브의 마녀 뚝딱공작소 알지 가덕 공방 좋은 나무 곰이 되고 싶어요 내일 그리고 또 내일
댓글을 달아 주세요