I have make a Simple program using Java Swing.
If you start, the missiles across canvas from left to right, when you click canvas the balloon is made.
I make crush event between two labels. But, It doesn't work well. Sometime It's worked well, But some Balloon doesn't cognize missile.
(..sorry i'm little the south korean boy. I don't know english grammar well..)
This is my source file
Game2.java
public class Game2 {
public Game2(){
JFrame jF = new JFrame();
jF.setTitle("게임");
jF.setDefaultCloseOperation(jF.EXIT_ON_CLOSE);
jF.setSize(500, 500);
jF.setVisible(true);
MyPanel myPanel = new MyPanel();
jF.setContentPane(myPanel);
}
public static void main(String[] args) {
new Game2();
}
}
MyPanel.java
public class MyPanel extends JPanel implements Runnable {
private ArrayList<Balloon> ballList = new ArrayList<Balloon>();
private ArrayList<Missile> misList = new ArrayList<Missile>();
public MyPanel() {
setLayout(null);
Thread setMissileThread = new Thread(this);
setMissileThread.start();
}
@Override
public void run() {
// 마우스 클릭 이벤트 처리(풍선)
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
ballList.add(new Balloon(e.getX(), e.getY()));
// System.out.println("풍선 생성");
}
});
// 자동 처리(미사일)
while (true) {
// synchronized(ballList){
Missile mis;
misList.add(mis = new Missile());
mis.start();
// System.out.println("미사일생성");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
return;
}
// 미사일 충돌
if (!misList.isEmpty() && !ballList.isEmpty()) {
for (Missile misArr : misList) {
for (Balloon ballArr : ballList) {
Point ballPos = ballArr.getPosition();
Point misPos = misArr.getPosition();
if(ballArr.visible){System.out.println("살아있더"+ballPos.x+", "+ballPos.y);}
if (ballPos.x - 10 <= misPos.x + 60
&& misPos.x + 60 <= ballPos.x + 40
&& ballPos.y - 20 <= misPos.y + 15
&& misPos.y + 25 <= ballPos.y + 50) {
//visible을 따져서 충돌 파괴모션을 설정한다.
if (ballArr.visible == true) {
ballArr.visible = false;
// 라벨 삭제
remove(misArr.missile);
remove(ballArr.ball);
repaint();
System.out.println("Bomb!");
}
/*
* //ArrayList 인덱스 삭제 misList.remove(misArr);
* ballList.remove(ballArr);
*/
}
}
//}
}
}
}
}
public boolean intersects(JLabel testa, JLabel testb)
{
boolean b3 = false;
if(testa.contains(testb.getX(), testb.getY()))
{
b3 = true;
}
return b3;
}
class Missile extends Thread {
JLabel missile;
int xPos, yPos;
Random r = new Random();
public Missile() {
imgSetting();
setLoc();
repaint();
}
void imgSetting() {
ImageIcon img = new ImageIcon(
"C:/Users/JS_UbSE/Desktop/missile.png");
Image reImg = img.getImage();
Image changedImg = reImg.getScaledInstance(60, 30,
java.awt.Image.SCALE_SMOOTH);
ImageIcon IMG = new ImageIcon(changedImg);
missile = new JLabel(IMG);
missile.setSize(IMG.getIconWidth(), IMG.getIconHeight());
}
void setLoc() {
int xPos = 0;
int yPos = r.nextInt(500);
missile.setLocation(xPos, yPos);
add(missile);
this.xPos = xPos;
this.yPos = yPos;
repaint();
}
public Point getPosition() {
Point p = new Point();
p.x = xPos;
p.y = yPos;
return p;
}
public void run() {
while (xPos < 500) {
int nextXPos = xPos + 5;
missile.setLocation(nextXPos, yPos);
xPos = nextXPos;
repaint();
try {
Thread.sleep(25);
} catch (InterruptedException e) {
return;
}
}
remove(missile);
return;
}
}
class Balloon extends Thread {
JLabel ball;
int xPos, yPos;
private boolean visible;
public Balloon(int x, int y) {
visible = true;
imgSetting();
setLoc(x, y);
repaint();
}
void imgSetting() {
ImageIcon img = new ImageIcon(
"C:/Users/JS_UbSE/Desktop/balloon.png");
Image reImg = img.getImage();
Image changedImg = reImg.getScaledInstance(30, 40,
java.awt.Image.SCALE_SMOOTH);
ImageIcon IMG = new ImageIcon(changedImg);
ball = new JLabel(IMG);
}
void setLoc(int mouseX, int mouseY) {
ball.setSize(30, 40);
ball.setLocation(mouseX, mouseY);
xPos = mouseX;
yPos = mouseY;
add(ball);
}
public Point getPosition() {
Point p = new Point();
p.x = xPos;
p.y = yPos;
//System.out.println(xPos + ", " + yPos);
return p;
}
}
}