import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ScoreThread extends Thread{
}
class CheeseThread extends Thread{
ImageIcon cheeseI = new ImageIcon("cheese.png");
JLabel Player;
JPanel GamePanel;
public CheeseThread(JLabel Player,JPanel GamePanel) {
this.GamePanel=GamePanel;
this.Player=Player;
}
public void run() {
while(true) {
JLabel cheese = new JLabel(cheeseI);
int ranx=(int)(Math.random()*450);
int rany=(int)(Math.random()*400);
int Px = Player.getX();
int Py = Player.getY();
cheese.setSize(30,30);
cheese.setLocation(ranx,rany);
GamePanel.add(cheese);
cheese.repaint();
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class CatThread extends Thread {
ImageIcon cat = new ImageIcon("cat.png");
JLabel Cat = new JLabel(cat);
JLabel Player;
JPanel GP;
public CatThread(JLabel Player, JPanel GP) {
this.Player = Player;
}
public void run() {
Cat.setLocation(450, 0);
Cat.setSize(30, 30);
GP.add(Cat);
while (true) {
int Mx = Cat.getX();
int My = Cat.getY();
int Px = Player.getX();
int Py = Player.getY();
if (Px < Mx) {
Cat.setLocation(Mx - 1, My);
}
else if (Px > Mx) {
Cat.setLocation(Mx + 1, My);
}
else if (Py > Cat.getY()) {
Cat.setLocation(Mx, My + 1);
}
else if (Py < Cat.getY()) {
Cat.setLocation(Mx, My - 1);
}
try {
sleep(8);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Main extends JFrame {
ImageIcon Dmouse = new ImageIcon("Dmouse.png");
ImageIcon Umouse = new ImageIcon("Umouse.png");
ImageIcon Lmouse = new ImageIcon("Lmouse.png");
ImageIcon Rmouse = new ImageIcon("Rmouse.png");
JLabel Mouse = new JLabel(Dmouse);
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:");
JLabel num = new JLabel();
Infor.add(num);
Infor.add(score);
cp.add(GamePanel, BorderLayout.CENTER);
GamePanel.setFocusable(true);
GamePanel.requestFocus();
GamePanel.addKeyListener(new Key());
GamePanel.setLayout(null);
Mouse.setSize(30, 30);
GamePanel.add(Mouse);
CatThread ch = new CatThread(Mouse, GamePanel);
ch.start();
CheeseThread ct = new CheeseThread(Mouse,GamePanel);
ct.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 = Mouse.getX();
int py = Mouse.getY();
if (Key == KeyEvent.VK_Q) {
System.exit(0);
}
else if (Key == KeyEvent.VK_LEFT) {
if (px > 0) {
Mouse.setLocation(px - 10, py);
Mouse.setIcon(Lmouse);
}
}
else if (Key == KeyEvent.VK_RIGHT) {
if (px < 450) {
Mouse.setLocation(px + 10, py);
Mouse.setIcon(Rmouse);
}
}
else if (Key == KeyEvent.VK_UP) {
if (py > 0) {
Mouse.setLocation(px, py - 10);
Mouse.setIcon(Umouse);
}
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (py < 400) {
Mouse.setLocation(px, py + 10);
Mouse.setIcon(Dmouse);
}
}
}
public void keyReleased(KeyEvent e) {
}
}
}