你好,按照你的要求代碼如下,修改了三處
創(chuàng)新互聯(lián)建站,為您提供成都網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站營銷推廣、網(wǎng)站開發(fā)設(shè)計(jì),對(duì)服務(wù)成都木制涼亭等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗(yàn)。創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)公司成立于2013年,提供專業(yè)網(wǎng)站制作報(bào)價(jià)服務(wù),我們深知市場的競爭激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!
簡單說明一下,就是加了一個(gè)標(biāo)識(shí)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;// 加這樣一個(gè)標(biāo)識(shí),true是第一張圖片,false為第二張圖片
public void paint(Graphics g) {
super.paint(g);
// drawRole(g);
if (key) {// 根據(jù)標(biāo)識(shí)判斷需要顯示的圖片
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;// 切換標(biāo)識(shí)狀態(tài)
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String args[]) {
new zhandou();
}
}
改變規(guī)制時(shí)候的X Y就行了.偽代碼如下.
int x =0,y=0,;
x++; y++;
g.drawImage( "圖片信息" , x, y,錨點(diǎn));
大概就這樣圖片就動(dòng)了.你想移動(dòng)到哪加個(gè)判斷就行了.
代碼如下: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 實(shí)現(xiàn)繪制圖形并移動(dòng)代碼
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圖片移動(dòng)代碼 java怎么調(diào)用圖片到代碼運(yùn)行
鏈接URL:http://www.rwnh.cn/article28/doppicp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、Google、網(wǎng)頁設(shè)計(jì)公司、做網(wǎng)站、網(wǎng)站策劃、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)