你好,按照你的要求代碼如下,修改了三處
堅(jiān)守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都發(fā)電機(jī)租賃小微創(chuàng)業(yè)公司專業(yè)提供成都定制網(wǎng)頁設(shè)計(jì)營銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。
簡(jiǎn)單說明一下,就是加了一個(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();
}
}
java 做游戲一般都是在手機(jī)上的
專門有個(gè)微型JAVA 叫 J2ME
里面有游戲API 你學(xué)學(xué)這個(gè)就明白了
...想要圖片從上到下.?,是自動(dòng)移動(dòng)嗎?
是的話,用Marquee這個(gè)標(biāo)簽就行了,里面有控制方向的屬性-
-,沒必要寫個(gè)方法
我要分,趕快采納哦......
public void run() {
/* Begin section C. */
// Increment c from 1 to 100.
for (int c = 1; c = 1000; c++) {
// Pause this frame for 250 milliseconds before drawing the next
// frame.
try {
Thread.sleep(250);
} catch (InterruptedException i) {
System.exit(1);
}
// 我以下添加
carbodylocation.changeLocation(new Location(carbodylocation.getVertical() - c, carbodylocation.getHorizontal() + c));
windowLocation.changeLocation(new Location(windowLocation.getVertical() - c, windowLocation.getHorizontal() + c));
window2Location.changeLocation(new Location(window2Location.getVertical() - c, window2Location.getHorizontal() + c));
wheellocation.changeLocation(new Location(wheellocation.getVertical() - c, wheellocation.getHorizontal() + c));
tireLocation.changeLocation(new Location(tireLocation.getVertical() - c, tireLocation.getHorizontal() + c));
tire2Location.changeLocation(new Location(tire2Location.getVertical() - c, tire2Location.getHorizontal() + c));
wheel2Location.changeLocation(new Location(wheel2Location.getVertical() - c, wheel2Location.getHorizontal() + c));
// 以上我添加
// Draw the frame.
repaint();
}
/* End section C. */
}
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();
}
}
}
}
}
以上
改變規(guī)制時(shí)候的X Y就行了.偽代碼如下.
int x =0,y=0,;
x++; y++;
g.drawImage( "圖片信息" , x, y,錨點(diǎn));
大概就這樣圖片就動(dòng)了.你想移動(dòng)到哪加個(gè)判斷就行了.