文章目录
- 前言
- 一、动画是怎么实现的呢?
- [二、根据这个原理 代码是怎么实现的呢?](#二、根据这个原理 代码是怎么实现的呢?)
- 二、代码部分
- 最终的包结构
- 效果图:
前言
接上篇:如何在IDEA中使用Java GUI制作一个双人对打小游戏,上篇: Java GUI制作双人对打游戏上已经完成了基本的UI界面的绘制以及JPanel的制作。本篇文章对剩余部分包括人物,键盘按键监听,背景音乐等完成
一、动画是怎么实现的呢?
动画实现的基本原理可以概括为多个静态图像按照一定顺序快速播放,以产生连续的动态效果。这个原理也被称为动态视觉暂留效应,即人眼在观察快速连续的图像时,会产生视觉的暂留效应,从而使几个连续的图像形成一个平滑的动画。
在传统手工动画制作中,动画师会分别绘制每一帧图像,然后通过快速放映来创建动画。每帧之间的变化是人工制作的,可以控制变化的细节和每一帧的时间,从而达到较高的动画质量。
而在数字动画制作中,动画师使用计算机软件或其他数字工具,绘制或制作框架图像,然后通过调整关键帧之间的插值来自动创建中间帧,以形成连续的动画。数字动画制作速度较快,制作成本也比传统手工动画制作低。
此外,动画的制作还涉及到剧本创作、人物设定、场景设定等多个环节,这些都为动画的最终呈现提供了重要的支撑。在动画角色动作设计和情节处理上,物理学原理也发挥着重要作用,无论是遵循或打破运动规律,都体现着物理学原理的重要性。
总的来说,动画的基本原理是利用视觉暂留效应,在连续的静态图像之间引入微小的差异,以创造平滑的动态效果。同时,通过精心设计和制作,动画能够呈现出丰富多彩的故事情节和生动的角色形象,给观众带来视觉和心灵的享受。
二、根据这个原理 代码是怎么实现的呢?
1.假设在JPanel中有一个画笔,并设置这个画笔的初始位置,我们让这个画笔在特定的时间间隔内执行一次
2.每执行一次我们就放更改画笔的位置,并绘画一张不同的图片
3.连续的执行,类似于动画的一帧,放置一个人物一点细微的动作变化
4.如图所示:一个人物的连续踢腿动作:
在这连续的帧中放入不同的人物动作图片,就形成了动画
二、代码部分
1.角色1(左侧)
java
package src;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;
public class SuperMan {
int i=1;
int j=1;
int k=1;
boolean toright=true;
public SuperMan(int manx, int many, String imagePath) {
this.manx = manx;
this.many = many;
this.ImagePath = imagePath;
}
public SuperMan(){
}
private int manx=200;
private int many=200;
private String ImagePath="image/toright1.png";
private String pose="static";
public String getPose() {
return pose;
}
public void setPose(String pose) {
this.pose = pose;
}
public int getManx() {
return manx;
}
public void setManx(int manx) {
this.manx = manx;
}
public int getMany() {
return many;
}
public void setMany(int many) {
this.many = many;
}
public String getImagePath() {
return ImagePath;
}
public void setImagePath(String imagePath) {
ImagePath = imagePath;
}
public BufferedImage StreamToImage(String str){
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(str);
BufferedImage bufferedImage=null;
try {
bufferedImage= ImageIO.read(resourceAsStream);
} catch (Exception e) {
e.printStackTrace();
}
return bufferedImage;
}
public void drawMe(Graphics g){
if(pose.equals("right")) {
ImagePath = "image/toright" + i + ".png";
System.out.println(i);
BufferedImage bufferedImage = StreamToImage(ImagePath);
i++;
manx += 10;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (i > 3) {
toright=true;
i = 1;
this.pose="static";
}
}
else if(pose.equals("left")) {
ImagePath = "image/toleft" + i + ".png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
i++;
manx -= 10;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (i > 3) {
toright=false;
i = 1;
this.pose="static";
}
}
else if(pose.equals("foot"))
{
if(toright)
{
ImagePath = "man/foot" + j + ".png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
j++;
manx += 3;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (j > 6) {
j = 1;
this.pose = "static";
}
}
else{
ImagePath = "man/foot" + j + "-modified.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
j++;
manx -= 3;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (j > 6) {
j = 1;
this.pose = "static";
}
}
}
else if(pose.equals("hand"))
{
if(toright)
{
ImagePath = "man/hand" + k + ".png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
k++;
manx += 1;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (k > 5) {
k = 1;
this.pose = "static";
}
}
else{
ImagePath = "man/hand" + k+ "-modified.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
k++;
manx -= 1;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (k > 5) {
k= 1;
this.pose = "static";
}
}
}
else if(pose.equals("defense")) {
if(toright){
ImagePath = "man/defence.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
}
else
{
ImagePath = "man/defence-modified.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
}
}
else{
if(toright)
{
ImagePath = "image/toright" + 1 + ".png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
}
else
{
ImagePath = "image/toleft" + 1 + ".png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
}
}
}
}
2.角色2(右侧)
java
package src;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;
public class Matser {
int i=1;
int j=1;
int k=1;
boolean toright=false;
public Matser(int manx, int many, String imagePath) {
this.manx = manx;
this.many = many;
this.ImagePath = imagePath;
}
public Matser(){
}
private int manx=700;
private int many=200;
private String ImagePath="master/toright1.png";
private String pose="static";
public String getPose() {
return pose;
}
public void setPose(String pose) {
this.pose = pose;
}
public int getManx() {
return manx;
}
public void setManx(int manx) {
this.manx = manx;
}
public int getMany() {
return many;
}
public void setMany(int many) {
this.many = many;
}
public String getImagePath() {
return ImagePath;
}
public void setImagePath(String imagePath) {
ImagePath = imagePath;
}
public BufferedImage StreamToImage(String str){
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(str);
BufferedImage bufferedImage=null;
try {
bufferedImage= ImageIO.read(resourceAsStream);
} catch (Exception e) {
e.printStackTrace();
}
return bufferedImage;
}
public void drawMe(Graphics g){
if(pose.equals("right")) {
ImagePath = "master/toright" + i + ".png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
i++;
manx += 6;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (i > 5) {
toright=true;
i = 1;
this.pose="static";
}
}
else if(pose.equals("left")) {
ImagePath = "master/toright" + i + "-modified.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
i++;
manx -= 10;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (i > 5) {
toright=false;
i = 1;
this.pose="static";
}
}
else if(pose.equals("foot"))
{
if(toright) {
ImagePath = "master/foot" + j + ".png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
j++;
manx += 3;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (j > 6) {
j = 1;
this.pose = "static";
}
}
else
{
ImagePath = "master/foot" + j + "-modified.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
j++;
manx -= 3;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (j > 6) {
j = 1;
this.pose = "static";
}
}
}
else if(pose.equals("hand")) {
if(toright) {
ImagePath = "master/hand" + k + ".png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
k++;
manx += 1;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (k > 6) {
k = 1;
this.pose = "static";
}
}
else
{
ImagePath = "master/hand" + k + "-modified.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
k++;
manx -= 1;
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
if (k > 6) {
k = 1;
this.pose = "static";
}
}
}
else if(pose.equals("defense")) {
if(toright){
ImagePath = "master/Defense.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
}
else
{
ImagePath = "master/Defense-modified.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
}
}
else{
if(toright)
{
ImagePath = "master/toright" + 1 + ".png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
}
else
{
ImagePath = "master/toright" + 1 + "-modified.png";
BufferedImage bufferedImage = StreamToImage(ImagePath);
Image image = new ImageIcon(bufferedImage).getImage();
g.drawImage(image, manx, many, null);
}
}
}
}
补充
两个角色有很多相同的部分,最好提取一个公共类,提示:
java
package src;
import java.util.List;
public abstract class CommonMan {
private int Manx;
private int Many;
private List<String> ManImagePathList;
public int getManx() {
return Manx;
}
public void setManx(int manx) {
Manx = manx;
}
public int getMany() {
return Many;
}
public void setMany(int many) {
Many = many;
}
public List<String> getManImagePathList() {
return ManImagePathList;
}
public void setManImagePathList(List<String> manImagePathList) {
ManImagePathList = manImagePathList;
}
}
提取公共类是一种代码重构技术,其核心思想是将多个类中重复或相似的代码片段提取出来,形成一个独立的公共类。这个公共类包含了这些共享的代码或逻辑,使得原有的类可以通过调用这个公共类的方法来减少重复代码,提高代码的可读性、可维护性和复用性。
在实际应用中,提取公共类通常用于处理项目中多处重复的代码或某一特定场景处理相对分散的情况。例如,在一个信息管理系统中,可能会存在多个Servlet处理不同的功能,但如果这些Servlet中有大量重复的代码,就可以考虑提取这些重复的代码到一个公共类中,从而简化代码结构,提高开发效率。
提取公共类时,需要注意以下几点:
确保提取出来的公共类是独立和完整的,不包含对其他类的过度依赖。
根据使用频率和代码复用性来决定是否有必要提取公共类。
公共类的方法应该是完全独立的,只关注完成特定的任务,而不涉及外部的其他逻辑。
总之,提取公共类是一种有效的代码优化手段,有助于提升代码质量和开发效率。
最终的包结构
效果图: