重写一下update
package cn.dadongzicool.test;
import
public class Test
{
public static void main(String args[]) throws IOException
{
@SuppressWarnings("unused")
DrawImage image = new DrawImage();
}
}
package cn.dadongzicool.test;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import
import
import javax.imageio.ImageIO;
public class DrawImage extends Frame implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
int x=0;
int y=0;
int dbx=0;
int dby=-5;
int bflag=0;
int dx,dy,bx,by;
int imgWidth,imgHeight;
Image img;
Graphics offScreenG;
File file;
BufferedImage bi;
public DrawImage() throws IOException
{
super("DrawImage");
setSize(350,350);
setVisible(true);
setResizable(false);
Toolkit tk=Toolkit.getDefaultToolkit();
img=tk.getImage("E:\\picture\\李静雯.jpg");
file = new File("E:\\picture\\李静雯.jpg");
bi = ImageIO.read(file);
imgWidth = bi.getWidth();
imgHeight = bi.getHeight();
enableEvents(AWTEvent.KEY_EVENT_MASK);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
new Thread(this).start();
}
public void processWindowEvent(WindowEvent e)
{
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
public void processKeyEvent(KeyEvent e)
{
if(e.getID()==KeyEvent.KEY_PRESSED)
{
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
dx=5;
dy=0;
break;
case KeyEvent.VK_LEFT:
dx=-5;
dy=0;
break;
case KeyEvent.VK_UP:
dx=0;
dy=-5;
break;
case KeyEvent.VK_DOWN:
dx=0;
dy=5;
break;
case KeyEvent.VK_SPACE:
dx=0;
dy=0;
bx=x+66;
by=y-5;
bflag=1;
break;
default:
dx=0;
dy=0;
}
x=x+dx;
y=y+dy;
}
}
public void run()
{
while(true)
{
if(by<=0)
{
bflag=0;
}
if(bflag==1)
{
by=by+dby;
}
repaint();
try
{
Thread.sleep(30);
}catch(Exception e){}
}
}
public void paint(Graphics g)
{
Image bufferPage = null;
if(bufferPage==null)
{
bufferPage=createImage(350,350);
}
bufferPage.getGraphics();
g.drawImage(img, x, y, this);
g.fillRect(bx,by,5,7);
g.setColor(Color.white);
g.fillRect(bx, by+5,3,5);
g.dispose();
g.drawImage(bufferPage,getInsets().left,getInsets().top, this);
}
@Override
public void update(Graphics g) {
paint(g);
}
}