public class FishMoving02 extends JFrame{
private Point point=new Point(800,100);
private BufferedImage[] fishImage;
private int index=0;
private Thread t;
private JPanel showPanel;
private JPanel mainPanel;
//private Image offsetImage;
//private Graphics gImage;
private int flag=1;
public FishMoving02() {
// TODO Auto-generated constructor stub
super();
setViews();
setFrames();
setMoving();
t.start();
}
public void setViews() {
mainPanel=new JPanel();
mainPanel.setLayout(new BorderLayout());
setContentPane(mainPanel);
fishImage=new BufferedImage[30];
try {
for (int i = 0; i < 30; i++) {
if (i<=14) {
String filePath="res\\swim\\swim_cycle."+(i+1)+".jpg";
fishImage[i]=ImageIO.read(new File(filePath));
System.out.println(filePath);
}else if (i>=15) {
String filePath="res\\swim\\swim_cycle."+(i+1)+".jpg";
fishImage[i]=ImageIO.read(new File(filePath));
System.out.println(filePath);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
showPanel=new JPanel();
showPanel.setLayout(null);
showPanel.setBackground(Color.black);
mainPanel.add(showPanel);
showPanel.addMouseMotionListener(new MouseInputAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
point.x=e.getX();
point.y=e.getY();
}
@Override
public void mousePressed(MouseEvent e) {
point.x=e.getX();
point.y=e.getY();
}
});
}
public void setFrames() {
setSize(1000, 600);
setTitle("小鱼动画");
Toolkit toolkit=Toolkit.getDefaultToolkit();
Dimension screenSize=toolkit.getScreenSize();
setLocation(screenSize.width/2-getWidth()/2, screenSize.height/2-getHeight()/2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g) {
/*super.paint(g1);*/
/*g1.drawImage(fishImage[index++%15], point.x, point.y+100, null);
g2.drawImage(fishImage[index++%30], point.x, point.y+100, null);*/
//从这里让小鱼转头
/*if (flag==1) {
point.x-=10;
g.drawImage(fishImage[index++%15], point.x, point.y+100, null);
if (point.x<=30) {
flag=0;
}
} else {
point.x+=10;
g.drawImage(fishImage[index++%30], point.x, point.y+100, null);
if (point.x>=800) {
flag=1;
}
}*/
if (flag==1) {
super.paint(g);
g.drawImage(fishImage[index++%15], point.x, point.y+100, null);
}else {
index=16;
super.paint(g);
g.drawImage(fishImage[index++%30], point.x, point.y+100, null);//index++%30出了问题
}
}
public void setMoving() {
t=new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (flag==1) {
point.x-=10;
if (point.x<=30) {
flag=0;
}
} else {
point.x+=10;
if (point.x>=800) {
flag=1;
}
}
try {
t.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
});
}
public static void main(String[] args) {
new FishMoving02();
}
}
1.利用ImageIO来进行图片的读入。同时需要用try-catch抛出输入异常
2.利用bufferedImage类,定义一个图片数组,用来存放从内存中读取到的图片
3.BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区可以对图片进行各种操作
4.利用线程使图片运动起来
第一次写,有很多地方没有考虑到,请多多谅解。有不对的地方还望各位大佬指出
补充:当小鱼向右走的时候,会隔段时间出现反复掉头的画面,一开始将index在
g.drawImage(fishImageToRight[index++%30], point.x, point.y + 100, null);
前定为固定值后,虽然不会出现反复掉头,但是此时小鱼是静止向右走,后来经过反复调试,发现如果将头朝左的一组图片,和头朝右的一组图片分开放在不同的文件夹里面,就可以解决这个问题;如下是修改过的代码:
package com.yf.drawAction;
import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class FishMoving extends JFrame {
/*设置小鱼初始位置*/
private Point point=new Point(800,30);
private BufferedImage[] fishImageToLeft,fishImageToRight;
private int index=0;
private JPanel mainPanel;
private JPanel showPanel;
private Thread t;
private int flag=1;
public FishMoving(){
setViews();
setFrames();
setMove();
t.start();
}
public void setFrames(){
setTitle("小鱼动画");
setSize(1000,600);
Toolkit toolkit=Toolkit.getDefaultToolkit();
Dimension screenSize=toolkit.getScreenSize();
setLocation(screenSize.width/2-getWidth()/2,screenSize.height/2-getHeight()/2);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public void setViews(){
mainPanel=new JPanel();
mainPanel.setLayout(new BorderLayout());
setContentPane(mainPanel);
fishImageToLeft=new BufferedImage[15];
fishImageToRight=new BufferedImage[15];
for (int i=0;i<=14;i++){
try {
String fishPathLeft="res\\swim\\swim_cycle"+(i+1)+".jpg";
String fishPathRight="res\\swim02\\swim_cycle"+(i+1)+".jpg";
fishImageToLeft[i]= ImageIO.read(new File(fishPathLeft));
fishImageToRight[i]=ImageIO.read(new File(fishPathRight));
System.out.println(fishPathLeft);
System.out.println(fishPathRight);
} catch (IOException e) {
e.printStackTrace();
}
}
showPanel=new JPanel();
showPanel.setLayout(null);
showPanel.setBackground(Color.black);
mainPanel.add(showPanel);
showPanel.addMouseMotionListener(new MouseInputAdapter() {
@Override
public void mousePressed(MouseEvent e) {
point.x=e.getX();
point.y=e.getY();
}
@Override
public void mouseDragged(MouseEvent e) {
point.x=e.getX();
point.y=e.getY();
}
});
}
public void setMove(){
t=new Thread(new Runnable() {
@Override
public void run() {
while (true){
if(flag==1){
point.x-=10;
if (point.x<=30){
flag=0;
}
}else{
point.x+=10;
if (point.x>=800){
flag=1;
}
}
try {
t.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
});
}
public void paint(Graphics g){
super.paint(g);
if (flag==1){
g.drawImage(fishImageToLeft[index++%15], point.x, point.y + 100, null);
}else if (flag==0){
g.drawImage(fishImageToRight[index++%15], point.x, point.y + 100, null);
}
/*g.drawImage(fishImageToRight[index++%14], point.x, point.y + 100, null);*/
}
public static void main(String[] args) {
new FishMoving();
}
}
这个是图片的百度云链接,有想自己练习的小伙伴可以拿:https://pan.baidu.com/s/1bKd6Rm_vmmoVMJZDJUg8CA 提取码:mm98
也欢迎各位大佬能指出我的不足,互相学习