十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
你好,按照你的要求代码如下,修改了三处
坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都发电机租赁小微创业公司专业提供成都定制网页设计营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。
简单说明一下,就是加了一个标识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 做游戏一般都是在手机上的
专门有个微型JAVA 叫 J2ME
里面有游戏API 你学学这个就明白了
...想要图片从上到下.?,是自动移动吗?
是的话,用Marquee这个标签就行了,里面有控制方向的属性-
-,没必要写个方法
我要分,赶快采纳哦......
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();
}
}
}
}
}
以上
改变规制时候的X Y就行了.伪代码如下.
int x =0,y=0,;
x++; y++;
g.drawImage( "图片信息" , x, y,锚点);
大概就这样图片就动了.你想移动到哪加个判断就行了.