import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class birdThread extends Thread
{
private static JLabel l;
final int y=5;
int x=0;
public birdThread(JLabel l)
{
this.l=l;
}
static int getX()
{
return l.getX();
}
static int getY()
{
return l.getY();
}
public void run()
{
while(true)
{
try
{
if(x==360)
{
x=0;
l.setLocation(x, 0);
}
if(90<birdThread.getX()&&birdThread.getX()<190&&gunThread.getY()==57)
{
x=0;
}
System.out.println(l.getLocation());
sleep(20);
x=x+y;
l.setLocation(x, 0);
}catch(InterruptedException e)
{
return;
}
}
}
}
class gunThread extends Thread
{
static boolean isRun = false;
static int y = 302;
private JPanel p1;
int a=0,b=1;
public gunThread(JPanel p1)
{
this.p1=p1;
}
public static int getY()
{
return y;
}
public void run()
{
isRun=true;
a++;
if(a==b)
{
while(y!=-3)
{
try
{
if(90<birdThread.getX()&&birdThread.getX()<190&&y==57)
{
break;
}
sleep(20);
y=y-5;
p1.setLocation(180, y);
System.out.println(y);
}catch(InterruptedException e)
{
return;
}
}
y=302;
p1.setLocation(180, y);
b++;
isRun=false;
return;
}
}
}
public class Main extends JFrame{
JPanel p=new JPanel();
JPanel p1=new JPanel();
ImageIcon icon=new ImageIcon("src/새.png");
JLabel l=new JLabel(icon);
Main()
{
setTitle("사격 게임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(null);
c.add(p);
p.setBackground(Color.black);
p.setSize(50, 50);
p.setLocation(160,312);
p.setVisible(true);
p.setOpaque(true);
c.add(l);
l.setSize(80, 50);
l.setLocation(0,0);
l.setVisible(true);
l.setOpaque(true);
c.add(p1);
c.addKeyListener(new MyKeyListener());
c.setFocusable(true);
c.requestFocus();
p1.setBackground(Color.red);
p1.setSize(10, 10);
p1.setLocation(180,302);
p1.setVisible(true);
p1.setOpaque(true);
setSize(400,400);
setVisible(true);
birdThread th1=new birdThread(l);
th1.start();
}
class MyKeyListener extends KeyAdapter
{
public void keyTyped(KeyEvent e) {
if(!gunThread.isRun && e.getKeyChar()=='\n')
{
gunThread bar =new gunThread(p1);
bar.start();
}
}
}
public static void main(String[] args) {
new Main();
}
}
/*
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
PointerInfo pt = MouseInfo.getPointerInfo();
Container c=getContentPane();
Main()
{
setTitle("버블 게임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(null);
c.addMouseListener(new MyKeyListener());
setVisible(true);
setSize(600,600);
}
class MyKeyListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e) {
BallonThread b =new BallonThread(e.getX(),e.getY());
b.start();
}
}
public static void main(String[] args) {
new Main();
}
class BallonThread extends Thread
{
ImageIcon icon=new ImageIcon("src/풍선.png");
JLabel l=new JLabel(icon);
private int x;
private int y;
int locate=400;
public BallonThread(int x,int y)
{
this.x=x;
this.y=y;
}
public void run()
{
c.add(l);
l.setLocation(x, y);
l.setSize(100, 100);
while(true)
{
try
{
if(l.getY()<=0)
{
//안보이게하고 쓰레드 종료
l.setVisible(false);
return;
}
sleep(20);
y=y-5;
l.setLocation(x, y);
c.repaint();
c.revalidate();
}catch(InterruptedException e)
{
return;
}
}
}
}
}
*/