一起用代码吸猫!本文正在参与【喵星人征文活动】
序言
还记得小时候玩的一个手机游戏Tom猫吗,这只猫一直伴随着我长大,小时候我记得我可爱玩这只猫了,虽然他涨的的不好看,但是也不妨碍我喜欢他,今天刚好趁着掘金的吸猫活动,用Java重温一下这只可爱的猫猫。
教程
1.新建一个Maven项目
2.下载素材包
下载相应的动作,密码为gk98图片Animations文件和按钮Buttons文件,并且放在 项目 --> target --> classes --> 项目名 下。
新建一个TomCatPanel类
我们新建一个TomCatPanel类,这个类用于画出猫和定义猫的一点点击事件,代码也比较简单。
package cn.linstudy;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
* @author XiaoLin
*/
public class TomCatPanel extends JPanel implements Runnable, MouseListener {
BufferedImage back;
BufferedImage eat;
BufferedImage cymbal;
BufferedImage drink;
BufferedImage fart;
BufferedImage pie;
BufferedImage scratch;
String[] paths = new String[100];
int count = 0;
int index = 0;
boolean flag = false;
public TomCatPanel() {
try {
this.back = ImageIO.read(TomCatPanel.class.getResource("Animations/Eat/eat_00.jpg"));
this.eat = ImageIO.read(TomCatPanel.class.getResource("Buttons/eat.png"));
this.cymbal = ImageIO.read(TomCatPanel.class.getResource("Buttons/cymbal.png"));
this.drink = ImageIO.read(TomCatPanel.class.getResource("Buttons/drink.png"));
this.fart = ImageIO.read(TomCatPanel.class.getResource("Buttons/fart.png"));
this.pie = ImageIO.read(TomCatPanel.class.getResource("Buttons/pie.png"));
this.scratch = ImageIO.read(TomCatPanel.class.getResource("Buttons/scratch.png"));
} catch (IOException var2) {
var2.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(this.back, 0, 0, 350, 600, (ImageObserver)null);
g.drawImage(this.eat, 20, 300, 50, 50, (ImageObserver)null);
g.drawImage(this.cymbal, 20, 370, 50, 50, (ImageObserver)null);
g.drawImage(this.drink, 20, 440, 50, 50, (ImageObserver)null);
g.drawImage(this.fart, 265, 300, 50, 50, (ImageObserver)null);
g.drawImage(this.pie, 265, 370, 50, 50, (ImageObserver)null);
g.drawImage(this.scratch, 265, 440, 50, 50, (ImageObserver)null);
}
@Override
public void run() {
while(true) {
if (this.flag) {
++this.index;
if (this.index >= this.count) {
this.index = 0;
this.flag = false;
}
try {
this.back = ImageIO.read(TomCatPanel.class.getResource(this.paths[this.index]));
} catch (IOException var3) {
var3.printStackTrace();
}
}
try {
Thread.sleep(50);
} catch (InterruptedException var2) {
var2.printStackTrace();
}
this.repaint();
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
int mouseX = e.getX();
int mouseY = e.getY();
if (mouseX > 20 && mouseX < 70 && mouseY > 300 && mouseY < 350) {
this.count = 40;
this.updateImage("eat");
this.flag = true;
}
if (mouseX > 20 && mouseX < 70 && mouseY > 370 && mouseY < 420) {
this.count = 13;
this.updateImage("cymbal");
this.flag = true;
}
if (mouseX > 20 && mouseX < 70 && mouseY > 440 && mouseY < 490) {
this.count = 81;
this.updateImage("drink");
this.flag = true;
}
if (mouseX > 265 && mouseX < 315 && mouseY > 300 && mouseY < 350) {
this.count = 28;
this.updateImage("fart");
this.flag = true;
}
if (mouseX > 265 && mouseX < 315 && mouseY > 370 && mouseY < 420) {
this.count = 24;
this.updateImage("pie");
this.flag = true;
}
if (mouseX > 265 && mouseX < 315 && mouseY > 440 && mouseY < 490) {
this.count = 56;
this.updateImage("scratch");
this.flag = true;
}
if (mouseX > 100 && mouseX < 250 && mouseY > 100 && mouseY < 250) {
this.count = 81;
this.updateImage("knockout");
this.flag = true;
}
if (mouseX > 120 && mouseX < 230 && mouseY > 400 && mouseY < 500) {
this.count = 34;
this.updateImage("stomach");
this.flag = true;
}
if (mouseX > 235 && mouseX < 285 && mouseY > 450 && mouseY < 540) {
this.count = 26;
this.updateImage("angry");
this.flag = true;
}
if (mouseX > 120 && mouseX < 170 && mouseY > 520 && mouseY < 560) {
this.count = 30;
this.updateImage("footRight");
this.flag = true;
}
if (mouseX > 175 && mouseX < 225 && mouseY > 520 && mouseY < 560) {
this.count = 30;
this.updateImage("footLeft");
this.flag = true;
}
this.repaint();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void updateImage(String str) {
for(int i = 0; i < this.count; ++i) {
if (i < 10) {
this.paths[i] = "Animations/" + str + "/" + str + "_0" + i + ".jpg";
} else {
this.paths[i] = "Animations/" + str + "/" + str + "_" + i + ".jpg";
}
}
}
}
新建一个TomCat类
这个类是主类用于启动项目。
package cn.linstudy;
import java.awt.Component;
import javax.swing.JFrame;
/**
* @Author ljp
* @Date 2021/11/4 11:09
* @Description
*/
public class TomCat {
public static void main(String[] args) {
JFrame frame = new JFrame("Tom猫");
TomCatPanel panel = new TomCatPanel();
frame.add(panel);
Thread t = new Thread(panel);
t.start();
panel.addMouseListener(panel);
frame.setDefaultCloseOperation(3);
frame.setSize(350, 600);
frame.setLocationRelativeTo((Component)null);
frame.setVisible(true);
}
}