我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java中启动动画的代码 代码实现动画

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

代码主要为以下:

我们提供的服务有:做网站、成都做网站、微信公众号开发、网站优化、网站认证、红河ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的红河网站制作公司

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 Web 启动时自动执行代码的几种方式

Web容器启动后执行代码的几种方式

其执行顺序为:

4===5===1===2===3

即指定init-method的Bean开始执行

接着实现spring的Bean后置处理器开始执行

然后是Servlet的监听器执行

再接下来是Servlet的过滤器执行

最后才是Servlet执行

1、实现Servlet监听器接口ServletContextListener

[java] view plain copy

public class InitListener implements ServletContextListener {

@Override

public void contextDestroyed(ServletContextEvent context) {

}

@Override

public void contextInitialized(ServletContextEvent context) {

// 上下文初始化执行

System.out.println("================[ServletContextListener]自动加载启动开始.");

SpringUtil.getInstance().setContext(

span style="white-space:pre" /spanWebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext())

span style="white-space:pre" /span);

}

}

然后在web.xml文件配置该监听器

[html] view plain copy

listener

listener-classcom.test.init.InitListener/listener-class

/listener

2、实现Servlet的过滤器Filter

[html] view plain copy

public class InitFilter implements Filter {

@Override

public void destroy() {

}

@Override

public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException,

ServletException {

}

@Override

public void init(FilterConfig config) throws ServletException {

System.out.println("================[Filter]自动加载启动开始.");

// 读取Spring容器中的Bean[此时Bean已加载,可以使用]

//写启动需要执行的代码

System.out.println("================[Filter]自动加载启动结束.");

}

}

然后在web.xml文件配置过滤器即可

[html] view plain copy

filter

filter-nameInitFilter/filter-name

filter-classcom.test.init.InitFilter/filter-class

/filter

filter-mapping

filter-nameInitFilter/filter-name

url-pattern//url-pattern

/filter-mapping

3、编写一个Servlet,在web.xml里面配置容器启动后执行即可

[html] view plain copy

public class InitServlet extends HttpServlet {

/**

*/

private static final long serialVersionUID = 1L;

@Override

public void init(ServletConfig config) {

try {

super.init();

} catch (ServletException e) {

e.printStackTrace();

}

System.out.println("================[Servlet]自动加载启动开始.");

// 读取Spring容器中的Bean[此时Bean已加载,可以使用]

//执行想要的代码

System.out.println("================[Servlet]自动加载启动结束.");

}

}

然后在web.xml文件配置该Servlet的启动方式为:容器启动后执行

servlet

servlet-nameInitServlet/servlet-name

servlet-classcom.test.init.InitServlet/servlet-class

init-param

param-nameusername/param-name

param-valuetest/param-value

/init-param

!-- 此处指定加载顺序为2,表明还有优先级更高的Servlet要先执行 --

load-on-startup2/load-on-startup

/servlet

servlet-mapping

servlet-nameInitServlet/servlet-name

url-pattern//url-pattern

/servlet-mapping

关于启动后执行,由load-on-startup指定:

(1)当值为0或者大于0时,表示容器在应用启动时就加载这个servlet。值越小,启动优先级越高;

(2)当是一个负数时或者没有指定时,表示该servlet被调用时才加载。

4、如果你使用Spring IOC作为Bean管理容器,那么可以指定init-method其中init-method表示bean加载成功后,立即执行某个方法。配置如下:start为要执行的方法名称

[html] view plain copy

!-- service --

bean id="shopService" class="com.test.teach.service.ShopService" span style="color:#33ffff;"init-method="start"/span

property name="shopDao" ref="shopDao" /

/bean

AnimationDrawable.start()应该在哪里调用啊?

[mw_shl_code=java,true]final ImageView tweenImage = (ImageView) findViewById(R.id.imageView1);tweenImage.setBackgroundResource(R.anim.cubicfacetween); tweenImage.post(new Runnable() { @Override public void run() { AnimationDrawable frameAnimation = (AnimationDrawable) tweenImage.getBackground(); frameAnimation.start(); }}[/mw_shl_code]完美运行,但如果是定制ProgressDialog,然后把动画放到这个自定义的布局里的话,还是没用

怎么用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文件放在同一文件夹下,然后运行就可以了·~~

如何创建启动界面Splash Screen

 启动界面Splash Screen在应用程序是很常用的,往往在启动界面中显示产品Logo、公司Logo或者开发者信息,如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥的时间。

Android 应用程序创建一个启动界面Splash Screen非常简单。比如创建一个工程MySample,主Acitity就叫MySample,创建另一个Activity叫 SplashScreen,用于显示启动界面,资源文件为splash.xml。至于如何制作SplashSceen界面,这不是本文章要讨论的东西,就此略过。

SplashScreen的代码如下:

package com.ctoof.android;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.MotionEvent;

public class SplashScreen extends Activity {

protected boolean _active = true;

protected int _splashTime = 5000;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.splash);

Thread splashTread = new Thread() {

@Override

public void run() {

try {

int waited = 0;

while(_active (waited _splashTime)) {

sleep(100);

if(_active) {

waited += 100;

}

}

} catch(InterruptedException e) {

// do nothing

} finally {

finish();

// 启动主应用

startActivity(new Intent("com.ctoof.android.MySample.MyApp"));

stop();

}

}

};

splashTread.start();

}

@Override

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

_active = false;

}

return true;

}

}

然后在AndroidMainfest.xml中修改代码如下:

?xml version="1.0" encoding="utf-8"?

manifest xmlns:android=""

package="com.ctoof.android"

android:versionCode="1"

android:versionName="1.0"

application android:icon="@drawable/icon" android:label="@string/app_name"

activity android:name=".SplashScreen"

android:label="@string/app_name"

intent-filter

action android:name="android.intent.action.MAIN" /

category android:name="android.intent.category.LAUNCHER" /

/intent-filter

/activity

activity android:name=".MyApp"

intent-filter

action android:name=" com.ctoof.android. MySample.MyApp " /

category android:name="android.intent.category.DEFAULT" /

/intent-filter

/activity

/application

uses-sdk android:minSdkVersion="4" /

/manifest

在这里负责注册两个活动。把负责管理启动界面Splash Screen的活动Activity作为应用程序的主活动,然后在SplashScreen中负责启动MyApp。

另:

很多应用都会有一个启动界面。欢迎画面慢慢隐现,然后慢慢消隐。实现这种效果的方法有两种(暂时只发现两种)

1、使用两个Activity,程序启动时候load第一张Activity,然后由tick触发N秒钟后startActivity另外一张Activity。

2、使用一个Activity,可以用到View.gone() 这个方法。把Acitivity的某些元素移除。

Java代码

public class Splash extends Activity {

private final int SPLASH_DISPLAY_LENGHT = 3000; //延迟三秒

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.splash);

new Handler().postDelayed(new Runnable(){

@Override

public void run() {

Intent mainIntent = new Intent(Splash.this,Main.class);

Splash.this.startActivity(mainIntent);

Splash.this.finish();

}

}, SPLASH_DISPLAY_LENGHT);

}

}

当然可以再Splash中加入动画效果。

求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中启动动画的代码 代码实现动画
网站地址:http://shouzuofang.com/article/hpsceo.html

其他资讯