/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Main extends JFrame{
Main(){
ImageIcon icon = new ImageIcon("ball.png");
Container c = getContentPane();
JLabel la = new JLabel(icon);
c.add(la);
setSize(300, 500);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BallThread extends Thread{
Point point;
Container c;
JLabel la = new JLabel(new ImageIcon("ball.png"));
public BallThread(Point point, Container c) {
this.point=point;
this.c = c;
}
public synchronized void run() {
c.add(la);
la.setLocation(point);
la.setSize(30,30);
while(true) {
if(point.y==0) {
c.remove(la);
c.repaint();
}
la.setLocation(point);
point.y--;
try {
sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Main extends JFrame{
Container cp= getContentPane();
Main(){
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
cp.setLayout(null);
cp.addMouseListener(new Mouse());
}
class Mouse implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
BallThread bth=new BallThread(e.getPoint(),cp);
bth.start();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
public static void main(String[] args) {
new Main();
}
}
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MonsterThread extends Thread{
ImageIcon cat=new ImageIcon("cat.png");
JLabel Monster = new JLabel(cat);
JLabel Player;
JPanel GP;
public MonsterThread(JLabel Player, JPanel GP) {
this.Player=Player;
this.GP=GP;
}
public void run() {
Monster.setLocation(450,0);
Monster.setSize(30,30);
GP.add(Monster);
while(true) {
int Mx=Monster.getX();
int My=Monster.getY();
int Px=Player.getX();
int Py=Player.getY();
if(Px<Mx) {
Monster.setLocation(Mx-1,My);
}
else if(Px>Mx) {
Monster.setLocation(Mx+1,My);
}
else if(Py>Monster.getY()) {
Monster.setLocation(Mx,My+1);
}
else if(Py<Monster.getY()) {
Monster.setLocation(Mx,My-1);
}
try {
sleep(8);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Main extends JFrame{
ImageIcon mouse=new ImageIcon("mouse.png");
JLabel Player = new JLabel(mouse);
JPanel GamePanel=new JPanel();
Main(){
Container cp=getContentPane();
cp.setLayout(new BorderLayout());
JPanel Infor = new JPanel();
cp.add(Infor,BorderLayout.NORTH);
Infor.setBackground(Color.GRAY);
JLabel score=new JLabel("Score:");
Infor.add(score);
JLabel num=new JLabel();
Infor.add(num);
cp.add(GamePanel,BorderLayout.CENTER);
GamePanel.setFocusable(true);
GamePanel.requestFocus();
GamePanel.addKeyListener(new Key());
GamePanel.setLayout(null);
Player.setSize(30,30);
GamePanel.add(Player);
MonsterThread th= new MonsterThread(Player,GamePanel);
th.start();
setSize(500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
class Key implements KeyListener{
@Override
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
Integer Key=e.getKeyCode();
int px=Player.getX();
int py=Player.getY();
if(Key==KeyEvent.VK_Q) {
System.exit(0);
}
else if(Key==KeyEvent.VK_LEFT) {
Player.setLocation(px-10,py);
}
else if(Key==KeyEvent.VK_RIGHT) {
Player.setLocation(px+10,py);
}
else if(Key==KeyEvent.VK_UP) {
Player.setLocation(px,py-10);
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN) {
Player.setLocation(px,py+10);
}
}
public void keyReleased(KeyEvent e) {}
}
}