网站建设资讯

NEWS

网站建设资讯

java动画移动代码,js移动动画

在Java游戏中让一个人物走动的代码是什么?

代码主要为以下:

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了保靖免费建站欢迎大家使用!

package com.lovo.game.frame;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.MediaTracker;

import javax.swing.JFrame;

import com.lovo.game.role.Fire;

import com.lovo.game.role.GameMap;

import com.lovo.game.role.ZhaoYun;

import com.lovo.game.util.TrackerInit;

public class GameStartFrame extends JFrame implements Runnable{

private Image memoryImage;

private Graphics memoryGraphics;

public static boolean isRun = true;

private static GameMap gameMap = new GameMap();

private ZhaoYun zy = new ZhaoYun();

private Fire fire = new Fire();

public GameStartFrame(){

this.setSize(900,700);

this.setVisible(true);

this.setDefaultCloseOperation(3);

//设置居中

this.setLocationRelativeTo(null);

//初始化双缓冲画布、画笔

this.memoryImage = this.createImage(900,700);

this.memoryGraphics = this.memoryImage.getGraphics();

//媒体追踪器

MediaTracker tracker = new MediaTracker(this);

TrackerInit.initImage(tracker);

//启动线程

Thread th = new Thread(this);

th.start();

}

public static void main(String[] args) {

GameStartFrame game = new GameStartFrame();

}

public void run() {

while(isRun){

this.flushFrame();

try {

Thread.sleep(20);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

private void flushFrame(){

gameMap.drawMap(memoryGraphics);

zy.drawImage(memoryGraphics);

fire.drawImage(memoryGraphics);

//将双缓冲画布中的图片显示在窗体

this.getGraphics().drawImage(this.memoryImage,0,0,this);

}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.role;

import java.awt.Graphics;

import java.awt.Image;

public class GameMap {

public static Image mapImg;

public static int mapx;

public void drawMap(Graphics memoryGraphics){

mapx -=5;

if(mapx =-17100){

mapx = -17100;

}

memoryGraphics.drawImage(mapImg,mapx,0,null);

}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.role;

import java.awt.Graphics;

import java.awt.Image;

import com.lovo.game.util.MoveImageChange;

public class Fire {

public static Image[]fireImage;

private int x =100;

private int y =300;

private int width = 200;

private int height = 130;

private MoveImageChange moveChange = new MoveImageChange(3);

public void drawImage(Graphics memoryGraphics){

Image img = moveChange.imageChange(fireImage);

memoryGraphics.drawImage(img,this.x,this.y,this.width,this.height,null);

}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.util;

import java.awt.MediaTracker;

import com.lovo.game.role.Fire;

import com.lovo.game.role.GameMap;

import com.lovo.game.role.ZhaoYun;

public class TrackerInit {

public static void initImage(MediaTracker tracker){

//初始化地图图片

GameMap.mapImg = CutImage.getSingleImage("image/map.jpg", tracker);

//赵云

ZhaoYun.zyImage = CutImage.cutOneImage("image/boss/playSpear.png",18, 236, 134,tracker);

//火

Fire.fireImage = CutImage.cutOneImage("image/fireImg.png", 6, 283, 161,tracker);

try {

//0组的图片全部等待加载完毕后,在显示

tracker.waitForID(0);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.util;

import java.awt.Image;

import java.awt.MediaTracker;

import java.awt.image.CropImageFilter;

import java.awt.image.FilteredImageSource;

import java.awt.image.ImageProducer;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

public class CutImage {

public static Image[][] cutManyImage(String filePath, int row, int col,

int imageWidth, int imageHight,MediaTracker tracker) {

Image[][] img = new Image[row][col];

ImageIcon imIcon = new ImageIcon(filePath);// 创建图像数组对象

Image imgTemp = imIcon.getImage();// 创建源图像

// 为源 图象获取ImageProducer源

ImageProducer sourceProducer = imgTemp.getSource();

for (int i = 0; i row; i++) {

for (int j = 0; j col; j++) {

// 创建图片分割图像对象,第一、二个参数为分割图像起始坐标。后两个参数为图像大小

CropImageFilter cropImg = new CropImageFilter(j * imageWidth, i * imageHight,imageWidth, imageHight);

ImageProducer imgProducer = new FilteredImageSource(sourceProducer, cropImg);

img[i][j] = new JFrame().createImage(imgProducer);

tracker.addImage(img[i][j], 0);

}

}

return img;

}

public static Image[] cutOneImage(String filePath,int col,

int imageWidth, int imageHight,MediaTracker tracker) {

Image[] img = new Image[col];

ImageIcon imIcon = new ImageIcon(filePath);// 创建图像数组对象

Image imgTemp = imIcon.getImage();// 创建源图像

// 为源 图象获取ImageProducer源

ImageProducer sourceProducer = imgTemp.getSource();

for (int j = 0; j col; j++) {

// 创建图片分割图像对象,第一、二个参数为分割图像起始坐标。后两个参数为图像大小

CropImageFilter cropImg = new CropImageFilter(j * imageWidth, 0,imageWidth, imageHight);

ImageProducer imgProducer = new FilteredImageSource(sourceProducer, cropImg);

img[j] = new JFrame().createImage(imgProducer);

tracker.addImage(img[j], 0);

}

return img;

}

public static Image getSingleImage(String imgPath,MediaTracker tracker){

Image img = new ImageIcon(imgPath).getImage();

tracker.addImage(img, 0);

return img;

}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.util;

import java.awt.Image;

public class MoveImageChange {

private int count;

private Image img;

private int frequency;

private int loopCount;

public MoveImageChange(int frequency){

this.frequency=frequency;

}

public MoveImageChange(int frequency,int count){

this.frequency=frequency;

this.count = count;

}

public Image imageChange(Image[] images){

if(img==null){//初始图片为第一张图片

img=images[0];

}

count++;

if(countfrequency){//当记数器大于频率时

count=0;

loopCount++;

if(loopCount = images.length){//图片下标越界时

loopCount=0;

}

img=images[loopCount];

}

return img;

}

public void setLoopCount(int loopCount){

this.loopCount = loopCount;

}

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.lovo.game.role;

import java.awt.Graphics;

import java.awt.Image;

import com.lovo.game.util.MoveImageChange;

public class ZhaoYun {

public static Image[] zyImage;

private int x =600;

private int y =300;

private int width =200;

private int height =130;

private MoveImageChange moveChange = new MoveImageChange(3);

public void drawImage(Graphics memoryGraphics){

Image img = moveChange.imageChange(zyImage);

memoryGraphics.drawImage(img,this.x,this.y,this.width,this.height,null);

java 简单动画 就是将一个圆点从一个坐标移动到另一个坐标即可

一个简单的范例,不明白追问吧

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class MoveAnimationDemo extends JFrame {

public MoveAnimationDemo() {

this.setContentPane(new AnimationPanel());

this.setSize(500, 500);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

/**

* 开始动画

*/

public void startAnimation() {

// 设定初始条件

x = START_X;

y = START_Y;

// 创建计时器

timer = new Timer(DELAY_TIME, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// 每一次延迟过后,调用一次

x += 1;

y += 1;

repaint();

// 满足结束条件就停止

if (x = END_X || y = END_Y) {

timer.stop();

}

}

});

// 开启计时器

timer.start();

}

public static void main(String[] args) {

MoveAnimationDemo demo = new MoveAnimationDemo();

demo.setVisible(true);

demo.startAnimation();

}

private class AnimationPanel extends JPanel {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(CIRCLE_COLOR);

g.fillOval(x, y, CIRCLE_SIZE, CIRCLE_SIZE);

}

}

// 圆点颜色

private static final Color CIRCLE_COLOR = Color.RED;

// 圆点大小

private static final int CIRCLE_SIZE = 10;

// 起始位置

private static final int START_X = 50;

private static final int START_Y = 50;

// 终止位置

private static final int END_X = 400;

private static final int END_Y = 400;

// 动画帧之间的延时,每秒60帧

private static final int DELAY_TIME = 1000 / 60;

// 当前位置

private int x;

private int y;

private Timer timer;

}

求java做动画代码

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.RenderingHints;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class TestImage extends Frame

{

private static final long serialVersionUID = 1L;

private static boolean PRESSED = false;

private static int pointX = 0;

private static int pointy = 200;

private static int RIGHT_GO = 0;

private static int LEFT_GO = 0;

private static int DIR = 0;

private static int ANGLE = 0;

private static int W = 50;

private static int H = 60;

private _Canvas canvas = null;

public TestImage ()

{

add (canvas = new _Canvas ());

setIgnoreRepaint (true);

requestFocus ();

}

public class _Canvas extends Canvas implements Runnable

{

private static final long serialVersionUID = 1L;

private BufferedImage bi = null;

private Image bufferedImage = null;

private Thread thread = null;

private long sleepTime = 10;

public _Canvas ()

{

try

{

bi = ImageIO.read (new File ("go.png"));

}

catch (IOException e)

{}

setBackground (Color.BLACK);

requestFocus ();

addKeyListener (new KeyListener ()

{

@Override

public void keyTyped ( KeyEvent e )

{}

@Override

public void keyReleased ( KeyEvent e )

{

RIGHT_GO = 0;

PRESSED = false;

}

@Override

public void keyPressed ( KeyEvent e )

{

// 38 40 37 39上下左右

DIR = e.getKeyCode ();

PRESSED = true;

}

});

}

@Override

public void paint ( Graphics g )

{

Graphics2D g2d = (Graphics2D) g;

g2d.setRenderingHint (RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

g2d.drawImage (rotateImage (bi.getSubimage (RIGHT_GO, LEFT_GO, W, H), ANGLE, true), pointX, pointy, W, H,

this);

g2d.dispose ();

}

@Override

public void update ( Graphics g )

{

if (null == bufferedImage)

{

bufferedImage = createImage (getWidth (), getHeight ());

}

Graphics bufferedG = bufferedImage.getGraphics ();

bufferedG.clearRect (0, 0, getWidth (), getHeight ());

paint (bufferedG);

bufferedG.dispose ();

g.drawImage (bufferedImage, 0, 0, this);

g.dispose ();

}

public void start ()

{

thread = new Thread (this);

thread.setName ("TestImage");

thread.setPriority (Thread.MIN_PRIORITY);

thread.start ();

}

public synchronized void stop ()

{

thread = null;

notify ();

}

@Override

public void run ()

{

Thread me = Thread.currentThread ();

while (thread == me  !isShowing () || getSize ().width == 0)

{

try

{

Thread.sleep (555);

}

catch (InterruptedException e)

{

return;

}

}

while (thread == me  isShowing ())

{

if (PRESSED)

{

try

{

if (DIR == 39)

{

RIGHT_GO = RIGHT_GO + 50;

LEFT_GO = 0;

pointX = pointX + 1;

if (pointX  420)

{

ANGLE = 90;

pointX--;

pointy--;

W = 60;

H = 50;

}

if (RIGHT_GO  50)

{

RIGHT_GO = 0;

}

}

else if (DIR == 37)

{

pointX = pointX - 1;

RIGHT_GO = RIGHT_GO + 50;

LEFT_GO = 60;

if (pointX  0)

{

ANGLE = -90;

pointX++;

pointy--;

W = 60;

H = 50;

}

if (RIGHT_GO  50)

{

RIGHT_GO = 0;

}

}

else if (DIR == 38)

{

W = 50;

H = 60;

pointy = 150;

ANGLE = 0;

RIGHT_GO = 100;

}

else if (DIR == 40)

{

W = 50;

H = 60;

ANGLE = 0;

pointy = 200;

RIGHT_GO = 0;

}

Thread.sleep (sleepTime);

repaint ();

}

catch (InterruptedException e)

{

break;

}

}

else

{

RIGHT_GO = RIGHT_GO + 50;

LEFT_GO = 0;

pointX = pointX + 1;

if (RIGHT_GO  50)

{

RIGHT_GO = 0;

}

if (pointX  500)

{

pointX = 0;

}

try

{

Thread.sleep (sleepTime);

repaint ();

}

catch (InterruptedException e)

{

break;

}

}

}

thread = null;

}

}

/**

 * 旋转图像为指定角度

 * 

 * @param degree

 * @return

 */

public static BufferedImage rotateImage ( final BufferedImage image, final int angdeg, final boolean d )

{

int w = image.getWidth ();

int h = image.getHeight ();

int type = image.getColorModel ().getTransparency ();

BufferedImage img;

Graphics2D graphics2d;

( graphics2d = ( img = new BufferedImage (w, h, type) ).createGraphics () ).setRenderingHint (

RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

graphics2d.rotate (d ? -Math.toRadians (angdeg) : Math.toRadians (angdeg), w / 2, h / 2);

graphics2d.drawImage (image, 0, 0, null);

graphics2d.dispose ();

return img;

}

public static void main ( String[] args )

{

EventQueue.invokeLater (new Runnable ()

{

@Override

public void run ()

{

final TestImage ti = new TestImage ();

ti.setSize (new Dimension (500, 300));

ti.setLocationRelativeTo (null);

ti.addWindowListener (new WindowAdapter ()

{

@Override

public void windowClosing ( WindowEvent e )

{

System.exit (0);

}

@Override

public void windowDeiconified ( WindowEvent e )

{

ti.canvas.start ();

}

@Override

public void windowIconified ( WindowEvent e )

{

ti.canvas.stop ();

}

});

ti.setResizable (false);

ti.canvas.start ();

ti.setVisible (true);

}

});

}

}

JAVA SWING 多个延时问题SLEEP

估计你想要的是动画效果,应该这样:

new Thread(){

public void run(){

// 动画代码:移动、渐变、缩放等

}

}.start();// 另启线程,防止阻塞当初进程

用JAVA做个小动画

在你的代码上修改了一下,运行试试吧

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class F extends JFrame {

public F() {

pan p = new pan();

this.add(p);

}

public static void main(String[] args) {

F fl = new F();

fl.setSize(400, 400);

fl.setVisible(true);

fl.run();

fl.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

int x, y;

public void run() {

int r = 100;

int angle = 0;

while (true) {

x = (int) ((Math.cos(angle * 2) * Math.PI) * r);

y = (int) ((Math.sin(angle * 2) * Math.PI) * r);

angle++;

repaint();

}

}

class pan extends JPanel {

public void paint(Graphics g) {

g.drawLine(200, 200, 200, 250);

g.drawLine(200, 225, x, y);

g.drawLine(200, 225, x, y);

g.drawLine(200, 250, 180, 300);

g.drawLine(200, 250, 220, 300);

g.drawOval(175, 150, 50, 50);

}

}

}

Java如何让多个图片都按照一定轨迹下落

图片的位移(下落),可以通过修改图片的x,y坐标来实现, 在Swing/Html中,我们可以使用Timer定时(比如每隔100毫秒)去修改图片的x,y坐标即可实现,

多个图片都按照一定的轨迹移动,那都按照自己的轨迹的算法,去定时修改x,y坐标即可.

JavaFX是java先进的图形界面框架, 里面有3D和各种动画, 所以按照轨迹移动,都能轻松实现

JavaFX参考代码如下

import javafx.animation.Animation;

import javafx.animation.Interpolator;

import javafx.animation.PathTransition;

import javafx.animation.RotateTransition;

import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.image.ImageView;

import javafx.scene.layout.HBox;

import javafx.scene.shape.MoveTo;

import javafx.scene.shape.Path;

import javafx.scene.shape.QuadCurveTo;

import javafx.stage.Stage;

import javafx.util.Duration;

public class PathAnimateDemo extends Application {

public static void main(String[] args) {

launch(args);

}

@Override

public void start(Stage primaryStage) throws Exception {

ImageView imv=new ImageView(getClass().getResource("ball.png").toExternalForm());

Path path = new Path();// 路径;运动轨迹

MoveTo mt = new MoveTo(20, 50);

QuadCurveTo quadTo2 = new QuadCurveTo(175, 190, 350, 30);

path.getElements().addAll(mt, quadTo2);

HBox hbox = new HBox(10);

Button btnStart = new Button("开始");

Button btnPause = new Button("暂停");

Button btnResume = new Button("继续");

Button btnStop = new Button("结束");

hbox.getChildren().addAll(btnStart, btnPause, btnResume, btnStop);

hbox.setPadding(new Insets(20));

hbox.setLayoutX(80);

hbox.setLayoutY(230);

Group root = new Group();

root.getChildren().addAll(imv, path, hbox); // 不添加path.就可以不显示path了

Scene scene = new Scene(root, 430, 300);

primaryStage.setTitle("JavaFX");

primaryStage.setScene(scene);

primaryStage.show();

//旋转动画设置

RotateTransition rt=new RotateTransition(Duration.millis(1000),imv);

rt.setInterpolator(Interpolator.LINEAR);

rt.setFromAngle(0);

rt.setToAngle(360);

rt.setCycleCount(Animation.INDEFINITE);

rt.play();

//路径动画设置

PathTransition pt = new PathTransition(Duration.millis(800), path, imv);// 路径动画

pt.setCycleCount(Animation.INDEFINITE);

pt.setAutoReverse(true);

btnStart.setOnAction(e - {

pt.playFromStart();// 从头开始播放

});

//----按钮的响应设置---

btnPause.setOnAction(e - {

pt.pause();

});

btnResume.setOnAction(e - {

pt.play(); // 播放

});

btnStop.setOnAction(e - {

pt.jumpTo(new Duration(0));// 跳到第0秒处

pt.stop();

});

}

}


网站题目:java动画移动代码,js移动动画
网页网址:http://cdweb.net/article/dscedeg.html