JAVA实现Tom猫

目录

一、效果

二、教程

三、代码


一、效果

首先我们先来看一下效果:(以下运行成功均动态效果

我们点击左右两边的图标,Tom猫便可做出相应的动作,例如,点击“牛奶”这个图标,Tom猫的反应:

当然,点击Tom猫的小肚子和头也会出现做出其他的小动作。

二、教程

1、使用IDEA搭建一个项目,项目名称:TomCat(可根据自己的喜好)

具体搭建过程可看博文用IDEA构建一个简单的Java程序范例,这里就不详细说了。

2、下载相应的动作图片Animations文件和按钮Buttons文件,并且放在 项目 --> target --> classes --> Tomcat

下载地址:

Animations   提取码:Anim

Buttons  提取码:Butt

3、TomCat.class

(1)导入包

import java.awt.Component;
import javax.swing.JFrame;

(2)主函数

在这里我的panel大小设置的是350*600,是根据我的图片的大小设定的。

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);
    }

4、TomCatPanel.class

(1)导入包

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;

(2)类的继承,接口的实现

  • JPanel类:面板组件,非顶层容器
  • MouseListener:鼠标监听接口
public class TomCatPanel extends JPanel implements Runnable, MouseListener{}

(3)类型的定义

  • back:BufferedImage,为Tom猫的背景
  • eat, cymbal, drink, fart, pie, scratch:BufferedImage,为界面的6个不同的按钮。分别表示吃、敲锣、喝、放屁、扔、抓6个动作
  • path:图片路径
  • count:int
  • index:int
  • flag:boolean
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;

(4)画布函数TomCatPanel()

在这里定义Tom猫的背景图片

  • back:Tom猫的背景图片
  • eat:画布上的吃东西的按钮
  • cymbal:画布上敲锣的按钮
  • drink:画布上喝牛奶的按钮
  • fart:画布上放屁的按钮
  • pie:画布上pizza的按钮
  • scratch:画布上抓的按钮
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();
        }
    }

(5)画笔函数paint()

  • 画笔名称g
  • 背景及按钮的位置,及大小
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);
    }

(6)线程函数run()

由于每个动作的图片的张数不同,因为我们每次更新图片时,需要判断图片的更新图片的次数是否大于图片的总数,如果超过,flag = false,停止更新。

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();
        }
    }

(7)鼠标点击函数mousePressed(MouseEvent e)

mouseX,mouseY:为鼠标的坐标

 根据鼠标的坐标,来判断Tom猫的动作。

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();
    }

(8)图片更新函数updateImage()

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";
            }
        }
    }

三、代码

1、TomCat.class

package TomCat;

import java.awt.Component;
import javax.swing.JFrame;

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);
    }
}

2、TomCatPanel.class

package TomCat;

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;

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();
        }
    }

    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);
    }

    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();
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    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";
            }
        }
    }
}

 

  • 12
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值