
正文
java代码实现人物动画 java画动态图
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java编程实现一个能循环地自左向右移动的动画程序。
效果图
参考代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MoveTextFrame extends JFrame {
JLabel jl;//文字标签
int speed=2;//移动速度
public MoveTextFrame() {
jl = new JLabel("文字动画");
jl.setForeground(Color.RED);
add(jl);
setSize(380, 100);//窗口大小
setLocationRelativeTo(null);//窗口居中
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//设置定时器, 每隔25毫秒,改变一次文字标签的位置
Timer t = new Timer(25, new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x = jl.getX()+speed;//计算移动后的位置
if(x=390){//如果超过就指定像素,就重新从左边开水移动
x=-30;
}
jl.setLocation(x, jl.getY());//更新位置
//repaint();
}
});
t.start();
}
public static void main(String[] args) {
new MoveTextFrame();
}
}
相关问答
Q1: 求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);
}
});
}
}
Q2: 请教用java制作动画的教程
//才几分啊,还有那么多要求?不干java代码实现人物动画了!
//RobotTest.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class RobotTest extends JPanel implements Runnable{
JLabel head,neck,body;
boolean sw;
RobotTest(){
super(null);
head=new JLabel("HEAD"){
protected void paintBorder(Graphics g) {
g.setColor(Color.black);
g.drawOval(0, 0, getSize().width-1,
getSize().height-1);
}
};
neck=new JLabel("htmlNbrEbrCbrK");
body=new JLabel("BODY");
head.setHorizontalAlignment(JLabel.CENTER);
neck.setHorizontalAlignment(JLabel.CENTER);
body.setHorizontalAlignment(JLabel.CENTER);
head.setSize(60,60);
neck.setSize(20,20);
body.setSize(100,128);
neck.setBorder(BorderFactory.createLineBorder(Color.black));
body.setBorder(neck.getBorder());
body.setLocation(0,158);
neck.setLocation(40,0);
head.setLocation(20,0);
add(head);
add(neck);
add(body);
}
public void run(){
int i=0;
while(i1000){
setLocation(i+=5,0);
neck.setSize(20,(sw=!sw)?60:80);
neck.setLocation(neck.getX(),body.getY()-neck.getHeight());
head.setLocation(head.getX(),neck.getY()-head.getHeight());
try {
Thread.sleep(333);
} catch (Exception e) {}
}
}
public static void main(String[] args) {
RobotTest rt = new RobotTest();
rt.setSize(100,300);
new Thread(rt).start();
JFrame f= new JFrame();
f.getContentPane().setLayout(null);
f.getContentPane().add(rt);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600,330);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
Q3: 如果要求这个Java程序中的图片每2秒钟动一次,让人看起来形成动画效果,应该怎么修改
服/务部java代码实现人物动画:051-1288-265-28 (通.话.免.费) 感谢你只持!
余额宝专出至银行java代码实现人物动画的金额java代码实现人物动画,会有以下3种到账时间:
1、单笔 5Wjava代码实现人物动画,提交后的下一个工作日内24点前到账java代码实现人物动画,工作日是不包括国定节假日、双休日,时间顺延。
2、及时到账:可以中信、光大、平安、招行卡通。 4、2小时到账:仅可以无线,日累计5W以内(含),且在该服.务时间内。 95188 希望采纳
支.付.宝到.银行.卡到帐时间分为:次日到帐、2小时到帐
如果绑定了快捷支.付的话,是可以即时到
Q4: 怎么用java做动画
重写paint方法,来实现将自己定义的图片绘制到组件中,然后启动一个线程来控制paint方法。 示例: ××××××××××××××××××××××××××× import javax.swing.*; import java.awt.*;class MyPanel extends JPanel implements Runnable {private Image img;private int i=0;private int j=0;public MyPanel(){img=new ImageIcon("1.png").getImage();}public void paint(Graphics g){g.drawImage(img,0,0,60,104,i*60,j*104,i*60+60,j*104+104,this);}public void run(){while(true){while(j {while(i {try{Thread.sleep(300);}catch(Exception e){}this.repaint();i++;}j++;i=0;}i=0;j=0;}} }public class test extends JFrame {private MyPanel p;public test(){p=new MyPanel();this.add(p,BorderLayout.CENTER);this.setBounds(300,200,300,300);this.setTitle("人物行走图");new Thread(p).start();this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String args[]){new test();} }××××××××××××××××××××××× 将以上源码保存为:test.java,编译,然后下载下面的图片 将下载的图片改名为1.png" target="_blank"
,然后将其和编译后生成的class文件放在同一文件夹下,然后运行就可以了·~~
Q5: 2.Java有哪几种常见的实现动画的方法?
一: 用多线程播放一组图片, 实现动画片java代码实现人物动画的效果; 类似于逐帧动画,每个图片是动画java代码实现人物动画的一帧
二: 在awt/swing界面里, 可以使用paint方法,去绘制图形,然后用swing提供的Timer或者多线程技术,去刷新绘制的图形
三:在JavaFX里, 本身就支持动画,并且封装java代码实现人物动画了很多动画效果可以直接使用,比如逐帧动画.缩放动画,渐变动画,旋转动画,位置动画等.
强烈推荐使用javaFX来实现动画, 因为javaFX是现代化的图形界面工具,具有简单,强大,组件丰富,跨平台,支持Html5, 支持表格, 支持动画等多种优势
下面是一个javaFX绘制的动态表格
javaFX动态表格
java代码实现人物动画的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java画动态图、java代码实现人物动画的信息别忘了在本站进行查找喔。






