viewimageM.php?id=programming&no=29bcc427b78777a16fb3dab004c86b6f0c84ca7046af80d4122901631ae35077770d5c7a3d6dd02f060f120e29c313ae94d46de75632b4ec21fb56ab9189334f045e3e3f

viewimageM.php?id=programming&no=29bcc427b78777a16fb3dab004c86b6f0c84ca7046af80d4122901631ae35077770d5c7a3d6dd02f060f120e29c313ae94d13ab15d32e3bd2dfb45bf8dc03c4b2a

viewimageM.php?id=programming&no=29bcc427b78777a16fb3dab004c86b6f0c84ca7046af80d4122901631ae35077770d5c7a3d6dd02f060f120e29c313ae948239e65666e1b878fb43a78e81785181d5

코드 설명
AWT를 이용하여 프레임을 띄우고
거기다가 타겟이미지랑 총 이미지를 박아둠
쓰레드 돌려서 타겟이 움직이게 해놓고
버튼 하나 만들어서 버튼 눌르면 총알 이미지가 타겟이 있는 방향으로 움직이게 해놨음..

거의 다 됐고 이제 목표물 맞췃을때 라벨이 있는 숫자가 증가해서 화면에 뿌려주면 될것 같은데..
생각하고 있는 알고리즘이
타겟 이미지랑 총알 이미지말 겹쳤을때 카운트가 증가해서 라벨에 표시되게 하는 거거든?
이미지 위치값 받아오는거랑 그 검사하는 소스를 어디다가 넣어야 될지 모르겠네...

package java_1202;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class MyShootingGame extends MyFrame implements Runnable, ActionListener {
 private Image target, gun, ammo;
 private Toolkit kit = Toolkit.getDefaultToolkit();
 private int x1, y1, width1, height1;
 private int x2, y2, width2, height2;
 private int x3, y3, width3, height3;
 private Button b_fire;
 private Thread t_target, t_fire;
 private Label l_cnt;
 private int cnt = 0;
 private Panel p_cnt, p_fire;
 
 public MyShootingGame() {
  super("ShootingGame", 650, 650);
  target = kit.getImage(getClass().getResource("target.jpg"));
  gun = kit.getImage(getClass().getResource("gun.jpg"));
  ammo = kit.getImage(getClass().getResource("ammo.jpg"));
  
  x1 = 5; //적당한 위치
  y1 = 65; //적당한 위치
  width1 = 120; //실제 사진값
  height1 = 120; //실제 사진값
  
  x2 = 450;
  y2 = 310;
  width2 = 175;
  height2 = 28;
  
  x3 = 450;
  y3 = 313;
  width3 = 30;
  height3 = 5;
  
  b_fire = new Button("fire");
  l_cnt = new Label("맞춘횟수 : " + cnt);
  p_fire = new Panel(new FlowLayout());
  p_cnt = new Panel(new FlowLayout());
  p_cnt.add(l_cnt);
  p_fire.add(b_fire);
  setLayout(new BorderLayout());
  add("South", p_fire);
  add("North", p_cnt);
  
  b_fire.addActionListener(this);
  
  t_target = new Thread(this);
  t_target.start();
 }

 @Override
 public void run() {
  int targerSpeed = 50;

  while (true) {
   try {
    Thread.sleep(200);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   y1 += targerSpeed;
   if (y1 >= 460 || y1 <= 65) {
    targerSpeed *= -1;
   }
   update(getGraphics());
  }
 }
 
 @Override
 public void paint(Graphics g) {
  g.drawImage(target, x1, y1, width1, height1, this);
  g.drawImage(ammo, x3, y3, width3, height3, this);
  g.drawImage(gun, x2, y2, width2, height2, this);
//  if (x1 == x3) {
//   cnt++;
//   System.out.println(cnt);
//  }
 }

 @Override
 public void actionPerformed(ActionEvent event) {
  if(event.getSource().equals(b_fire)) {
   int ammoSpeed = 50;
   x3 = 450;

   while (x3 >= -25) {
    try {
     Thread.sleep(100);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    x3 -= ammoSpeed;
    update(getGraphics());
   }
  }
 }
}

public class ShootingGame {
 public static void main(String[] args) {
  try {
   new MyShootingGame().setVisible(true);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}