资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java让图片运动的代码 java图片滚动

怎么编写java程序实现图片的移动(最好有例子)

import java.awt.Color;

成都创新互联长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为福鼎企业提供专业的网站制作、做网站,福鼎网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。

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,能帮我写多个图片随机在窗口运动的方法么

!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="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(moveX + document.documentElement.scrollLeft)+"px"; //漂浮图片距浏览器左侧位置

img.style.top = parseInt(moveY + document.documentElement.scrollTop)+"px"; //漂浮图片距浏览器顶端位置

//alert(img.style.left);

if (directionY==0){

moveY = moveY + step; //漂浮图片在Y轴方向上向下移动

}

else{

moveY = moveY - step; //漂浮图片在Y轴方向上向上移动

}

if (moveY 0) { //如果漂浮图片漂到浏览器顶端时,设置图片在Y轴方向上向下移动

directionY = 0;

moveY = 0;

}

if (moveY = (height - imgHeight)) { //如果漂浮图片漂到浏览器底端时,设置图片在Y轴方向上向上移动

directionY = 1;

moveY = (height - imgHeight);

}

if (directionX==0){

moveX = moveX + step; //漂浮图片在X轴方向上向右移动

}

else {

moveX = moveX - step; //漂浮图片在X轴方向上向左移动

}

if (moveX 0) { //如果漂浮图片漂到浏览器左侧时,设置图片在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做游戏,怎么让背景图片滚动啊

首先需要一个大图片bg,长度为bgWeight,它作为背景,相当于一个首尾相连的画卷,人物走动的话,它就滚动

int pos=0;//它记录窗口坐标(左边)在bg的偏移;

pos=(pos+moveSpeed)%bgWeight;

然后根据pos的位置绘制背景,有可能你是绘制 bg的尾部+bg的头部 来形成完整的背景

经典代码:

public Ribbon(int movS, BufferedImage im) {

bim = im; //im就是背景图片

xImHead = 0;//当前游戏窗口左边在背景图片中的坐标

pWidth = GamePanel.WIDTH;游戏窗口宽

pHeight = GamePanel.HEIGHT;游戏窗口高

width = bim.getWidth();背景图片宽度

moveSize = movS;移动距离

}

public void draw(Graphics g) /* Consider 5 cases:考虑5种情况

// when xImHead == 0, draw only the im head

//when xImHead 0, draw the im tail and im head, or only the im tail.

//when xImHead 0, draw the im tail, or the im tail and im head

// xImHead can range between -width to width (exclusive)

*/ {

if (xImHead == 0) // draw im head at (0,0)//情况1

{

drawRibbon(g, bim, 0, pWidth, 0, pWidth);

}

else if ((xImHead 0) (xImHead pWidth)) {//情况2

// draw im tail at (0,0) and im head at (xImHead,0)

drawRibbon(g, bim, 0, xImHead, width - xImHead, width); // im tail

drawRibbon(g, bim, xImHead, pWidth, 0, pWidth - xImHead); // im head

}

else if (xImHead = pWidth) // only draw im tail at (0,0),情况3

{

drawRibbon(g, bim, 0, pWidth,

width - xImHead, width - xImHead + pWidth); // im tail

}

else if ((xImHead 0) (xImHead = pWidth - width)) {//情况4

drawRibbon(g, bim, 0, pWidth, -xImHead, pWidth - xImHead); // im body

}

else if (xImHead pWidth - width) { //情况5

// draw im tail at (0,0) and im head at (width+xImHead,0)

drawRibbon(g, bim, 0, width + xImHead, -xImHead, width); // im tail

drawRibbon(g, bim, width + xImHead, pWidth,

0, pWidth - width - xImHead); // im head

}

} // end of display()

累死,看看这里,很详细

看完还不会找我!

怎么在GUI中用键盘控制图片运动?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();

}

/**

* 移动图片, 只处理上下左右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语言里怎么让图象移动到指定位置啊,最好用一小段代码说明.

改变规制时候的X Y就行了.伪代码如下.

int x =0,y=0,;

x++; y++;

g.drawImage( "图片信息" , x, y,锚点);

大概就这样图片就动了.你想移动到哪加个判断就行了.

在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让图片运动的代码 java图片滚动
网站网址:http://www.cdkjz.cn/article/hhoecp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220