java让图片运动的代码 java让图片动起来

在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;
【java让图片运动的代码 java让图片动起来】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 staticImage[][] 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; irow; i) {
for (int j = 0; jcol; 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 staticImage[] 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; jcol; 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;
}
publicImage 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);
怎么在GUI中用键盘控制图片运动?java问题?废话不多说java让图片运动的代码,直接上代码
------------------------------------------
import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MoveImage {
JFrame win;
Icon img;
JLabel lb;
public MoveImage() {
win = new JFrame("MoveImage");
// 加载图片
img = new ImageIcon(getClass().getResource("img.gif"));
lb = new JLabel();
// 装载图片
lb.setIcon(img);
win.setBounds(200, 0, 400, 300);
// 响应键盘上的键按下事件
win.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
move(e.getKeyCode());
}
});
win.add(lb);
win.setBackground(Color.WHITE);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
win.validate();
}
/**
* 移动图片java让图片运动的代码, 只处理上下左右4个方向键,其它的不处理
*
* @param keyCode
*按下的键盘上的键的键值
*/
void move(int keyCode) {
// 记下原来的位置
Point pos = lb.getLocation();
switch (keyCode) {
case KeyEvent.VK_UP:// 上方向键
pos.y -= 1;
break;
case KeyEvent.VK_RIGHT:// 右方向键
pos.x= 1;
break;
case KeyEvent.VK_DOWN:// 下方向键
pos.y= 1;
break;
case KeyEvent.VK_LEFT:// 左方向键
pos.x -= 1;
}
// 设置新位置
lb.setLocation(pos);
lb.validate();
}
/**
* @param args
*/
public static void main(String[] args) {
new MoveImage();
}
}
Java如何让多个图片都按照一定轨迹下落图片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.就可以不显示pathjava让图片运动的代码了
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程序实现图片的移动(最好有例子)import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class DrawTest extends JFrame {
private int x = 50;
private int y = 50;
private Image offScreenImage = null;
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, 30, 30);
g.setColor(c);
}
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(500, 500);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, 500, 500);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public static void main(String[] args) {
DrawTest d = new DrawTest();
}
public DrawTest() {
init();
addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
y -= 5;
break;
case KeyEvent.VK_RIGHT:
x= 5;
break;
case KeyEvent.VK_DOWN:
y= 5;
break;
case KeyEvent.VK_LEFT:
x -= 5;
break;
}
}
});
}
public void init() {
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBackground(Color.GREEN);
this.setResizable(false);
this.setBounds(140, 140, 500, 500);
this.setVisible(true);
MyThread mt = new MyThread();
new Thread(mt).start();
}
class MyThread implements Runnable {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
以上
java语言里怎么让图象移动到指定位置啊,最好用一小段代码说明.改变规制时候java让图片运动的代码的X Y就行java让图片运动的代码了.伪代码如下.
int x =0,y=0,;
x; y;
g.drawImage( "图片信息" , x, y,锚点);
大概就这样图片就动java让图片运动的代码了.java让图片运动的代码你想移动到哪加个判断就行java让图片运动的代码了.
Java,能帮我写多个图片随机在窗口运动的方法么!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312" /
title漂浮广告/title
style type="text/css"
body{
margin:0;
margin-top:3px;
padding:0;
font-size:12px;
line-height:20px;
color:#333;
}
.float{
position:absolute;
z-index:1;
}
.main{
width:95%;
margin-left:auto;
margin-right:auto;
}
.left_indent{
padding-left:20px;
}
.red{
color:#F00;
}
/style
/head
body
div id="float" class="float"img src="https://www.04ip.com/post/guimei_float.jpg" id="floatImg" alt="漂浮广告" //div
script type="text/javascript"
//定义全局变量
var moveX = 0;//X轴方向移动的距离
var moveY = 0;//Y轴方向移动的距离
var step = 1;//图片移动的速度
var directionY = 0;//设置图片在Y轴的移动方向
var directionX = 0;//设置图片在X轴的移动方向
function changePos(){
var img = document.getElementById("float");//图片所在层ID
var width = document.documentElement.clientWidth;//浏览器宽度
var height = document.documentElement.clientHeight;//浏览器高度
var imgHeight=document.getElementById("floatImg").height;//漂浮图片高度
var imgWidth=document.getElementById("floatImg").width;//漂浮图片宽度
img.style.left =parseInt(moveXdocument.documentElement.scrollLeft) "px";//漂浮图片距浏览器左侧位置
img.style.top = parseInt(moveYdocument.documentElement.scrollTop) "px";//漂浮图片距浏览器顶端位置
//alert(img.style.left);
if (directionY==0){
moveY = moveYstep;//漂浮图片在Y轴方向上向下移动
}
else{
moveY = moveY - step;//漂浮图片在Y轴方向上向上移动
}
if (moveY0) {//如果漂浮图片漂到浏览器顶端时java让图片运动的代码,设置图片在Y轴方向上向下移动
directionY = 0;
moveY = 0;
}
if (moveY = (height - imgHeight)) {//如果漂浮图片漂到浏览器底端时java让图片运动的代码,设置图片在Y轴方向上向上移动
directionY = 1;
moveY = (height - imgHeight);
}
if (directionX==0){
moveX = moveXstep;//漂浮图片在X轴方向上向右移动
}
else {
moveX = moveX - step;//漂浮图片在X轴方向上向左移动
}
if (moveX0) {//如果漂浮图片漂到浏览器左侧时,设置图片在X轴方向上向右移动
directionX = 0;
moveX = 0;
}
if (moveX = (width - imgWidth)) {//如果漂浮图片漂到浏览器右侧时,设置图片在X轴方向上向左移动
directionX = 1;
moveX = (width - imgWidth);
}
// setTimeout("changePos()",30);
}
setInterval("changePos()",30);
//window.onload=changePos;
/script
div class="main"br /
/div
/body
/html
这里是单个图片的漂浮效果 , 自己修改一下图片的路径和名称就好java让图片运动的代码了,看能不能帮助你
java让图片运动的代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java让图片动起来、java让图片运动的代码的信息别忘了在本站进行查找喔 。

    推荐阅读