重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java图片移动代码 java怎么调用图片到代码运行

java 中移动图片,并且在按键盘的“回车”时,切换到另外一张图片!!请在我下面的代码改进!!!谢谢!!

你好,按照你的要求代码如下,修改了三处

创新互联建站,为您提供成都网站建设网站制作、网站营销推广、网站开发设计,对服务成都木制凉亭等多个行业拥有丰富的网站建设及推广经验。创新互联建站网站建设公司成立于2013年,提供专业网站制作报价服务,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏心悦目的作品。 与客户共同发展进步,是我们永远的责任!

简单说明一下,就是加了一个标识boolean,用true/false来表示显示第一张/第二张图片

import java.awt.*;

import java.awt.event.*;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.*;

public class zhandou extends JFrame implements KeyListener {

Image roleImage, Image1;

int x, y;

public zhandou() {

super("MOVE");

Container c = getContentPane();

setSize(320, 240);

setVisible(true);

loadImage();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// setFocusable(true);

addKeyListener(this);

}

boolean key = true;// 加这样一个标识,true是第一张图片,false为第二张图片

public void paint(Graphics g) {

super.paint(g);

// drawRole(g);

if (key) {// 根据标识判断需要显示的图片

g.drawImage(roleImage, x, y, this);

} else {

g.drawImage(Image1, x, y, this);

}

}

public void loadImage() {

ImageIcon icon = new ImageIcon("tupian/草地.jpg");

roleImage = icon.getImage();

ImageIcon ic = new ImageIcon("tupian/right.gif");

Image1 = ic.getImage();

}

class Thread1 extends Thread {

}

/*

* private void drawRole(Graphics g) { g.drawImage(roleImage,100,100,this);

* }

*/

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_UP)

y = y - 5;

else if (e.getKeyCode() == KeyEvent.VK_DOWN)

y = y + 5;

else if (e.getKeyCode() == KeyEvent.VK_RIGHT)

x = x + 5;

else if (e.getKeyCode() == KeyEvent.VK_LEFT)

x = x - 5;

else if (e.getKeyCode() == KeyEvent.VK_ENTER)

key = !key;// 切换标识状态

repaint();

}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

public static void main(String args[]) {

new zhandou();

}

}

java语言里怎么让图象移动到指定位置啊,最好用一小段代码说明.

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

int x =0,y=0,;

x++; y++;

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

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

求Java 实现绘制图形并移动代码

代码如下:import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Graphics;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class Zfx extends MIDlet { private Display display; public Zfx() { display=Display.getDisplay(this); Zfxc qs=new Zfxc(); display.setCurrent(qs); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { }}class Zfxc extends Canvas implements Runnable{ private int x,y,x1,y1,i; private boolean flag; Zfxc(){ init(); } private void init(){ Thread thread=new Thread(this); thread.start(); } protected void paint(Graphics g) { g.setColor(255,255,255); g.fillRect(0, 0, this.getWidth(), this.getHeight()); g.setColor(0); g.fillRect(x, y, 10, 10); } private void logic(){ if(x=this.getWidth()-10){ x1=2; } if(x1==1){ x+=3; }else if(x1==2){ x-=3; } if(y=this.getHeight()-10){ y1=2; } if(y1==1){ y+=3; }else if(y1==2){ y-=3; } } protected void keyPressed(int keyCode) { if(keyCode==-5){ if(i==0){ flag=true; i=1; }else if(i==1){ flag=false; i=0; } } } public void run() { while(true){ if(flag){ logic(); } repaint(); try { Thread.sleep(80); } catch (InterruptedException e) { e.printStackTrace(); } } }}求Java 实现绘制图形并移动代码

怎么编写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怎么调用图片到代码运行
分享链接:http://cqcxhl.com/article/doppicp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP