前两天,有一个小姑娘让帮忙给其写一个小玩意,就是在Panel上绘制一个图片,然后通过键盘的方向键进行控制移动。虽然比较简单,这里还是希望和大家分享一下,里面有两点特别需要注意的知识点,这里一并加以说明。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.wst;
/**
*
* @author Administrator
*/
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.p_w_picpath.BufferedImage;
import javax.swing.*;
public class ImageMove extends JFrame {
private Container content;
private JPanel firstPicturePane;
private JPanel secondPicturePane;
private final static int WIDTH = 600;
private final static int HEIGHT = 600;
protected Image bfImage;
protected Graphics g;
protected int ImageX = 0;
protected int ImageY = 0;
protected int ImageW = 200;
protected int ImageH = 300;
private JPanel mainPane;
public void setImageX(int ImageX) {
this.ImageX = ImageX;
}
public int getImageX() {
return ImageX;
}
public void setImageY(int ImageY) {
this.ImageY = ImageY;
}
public int getImageY() {
return ImageY;
}
public Graphics getGra() {
return g;
}
public ImageMove() throws Exception {
super();
this.setTitle("键盘操作图片移动演示");
content = this.getContentPane();
firstPicturePane = new JPanel();
secondPicturePane = new JPanel();
content.setLayout(new BorderLayout());
content.add(firstPicturePane, BorderLayout.CENTER);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int h = screenSize.height;
int w = screenSize.width;
int x = (w - WIDTH) / 2;
int y = (h - HEIGHT) / 2;
this.setLocation(x, y);
this.setSize(WIDTH, HEIGHT);
bfImage = Toolkit.getDefaultToolkit().getImage("http_imgloadCACVTPTR.jpg");
this.setVisible(true);
g = firstPicturePane.getGraphics();
if (g == null) {
System.out.println("gg is null");
} else {
System.out.println(bfImage.toString());
MediaTracker t = new MediaTracker(this);
t.addImage(bfImage, 0);
t.waitForAll();
g.drawImage(bfImage, ImageX, ImageY, ImageW, ImageH, this);
}
addKeyListener(new MyKeyListener());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class MyKeyListener implements KeyListener {
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
g.drawImage(bfImage, ImageX, ImageY, ImageW, ImageH, null);
g.clearRect(ImageX, ImageY, ImageW, ImageH);
// repaint(ImageX, ImageY, ImageW, ImageH);
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
ImageX -= ((ImageX - 15) >= 0 ? 15 : ImageX);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
ImageX += ((ImageX + ImageW + 15) <= WIDTH ? 15 : (WIDTH - ImageX - ImageW));
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
ImageY -= ((ImageY - 15) >= 0 ? 15 : ImageY);
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
ImageY += ((ImageY + ImageH + 15) <= HEIGHT ? 15 : (HEIGHT - ImageY - ImageH));
}
g.drawImage(bfImage, ImageX, ImageY, ImageW, ImageH, null);
}
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
System.out.println(c);
}
}
public static void main(String[] args) throws Exception {
new ImageMove();
}
}
这里有两点需要特别强调:
1、对于键盘事件进行监听时,对于键盘上的字母键和数字键,可以通过getChar获得,但对于一些功能键和方向键,则必须在pressed和releasd里进行通过getHasCode进行监听和操作,这里可以简单地理解为前者执行高层操作,后者执行较底层操作。
2、在面板上绘制图片的时候,可能会出现代码没问题但图片不能显示的问题,那是因为图片可能未加载完成,这里可以通过如下代码加以控制:
MediaTracker t = new MediaTracker(this);
t.addImage(bfImage, 0);
t.waitForAll();
转载于:https://blog.51cto.com/wwssttt/381506