Код:
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
//import java.awt.Image;
enum Direction
{
up(0), right(1), down(2), left(3);
private int dir;
Direction(int i)
{
dir = i;
}
int GetDir()
{return dir;}
}
interface Renderable
{
public AffineTransform at = new AffineTransform();
public void Render(Graphics b);
}
abstract class Unit implements Renderable
{
BufferedImage imgu = null;
int w, h;
int x;
int y;
int dx, dy;
int speed;
Direction d;
void Update()
{
dx = dy = 0;
boolean check[] = new boolean[4];
check = Kb.KeyBoard();
do{
if(check[Direction.up.GetDir()])
{
dy = -1 * speed;
d = Direction.up;
break;
}
if(check[Direction.down.GetDir()])
{
dy = speed;
d = Direction.down;
break;
}
if(check[Direction.left.GetDir()])
{
dx = -1 * speed;
d = Direction.left;
break;
}
if(check[Direction.right.GetDir()])
{
dx = speed;
d = Direction.right;
break;
}
}while(false);
x = x + dx;
y = y + dy;
}
Unit()
{
this.speed = 1;
this.x = 33;
this.y = 33;
this.dx = this.dy = 0;
d = Direction.up;
}
Unit(int x , int y)
{
this.x = x;
this.y = y;
this.dx = this.dy = 0;
d = Direction.up;
}
public void Render(Graphics b)
{
imgu = new BufferedImage(67, 67, BufferedImage.TYPE_INT_ARGB);
Graphics2D r = imgu.createGraphics();
try {
imgu = ImageIO.read((new File("S.PNG")));
} catch(IOException e) {System.out.print("Image download ERROR!");
}
w = imgu.getWidth();
h = imgu.getHeight();
at.setToRotation(Math.PI*(d.GetDir() * 0.5), (x + w / 2), (y + h / 2) );
r = (Graphics2D)b;
r.setTransform(at);
b.drawImage(imgu, x, y, null);
at.setToRotation(0);
r.setTransform(at);
}
}
class Player extends Unit
{
Player(){super();}
Player(int x , int y){super(x , y);}
}
public class Demo extends Frame implements Runnable{
Thread t;
Player p;
Panel w;
Image buffer;
static Frame f;
Graphics img;
Graphics g;
int Width, Height;
public void run()
{
while(!Kb.qPressed){
this.paint();
this.p.Update();
try{
Thread.sleep(25);
}catch(InterruptedException e){}
}
}
Demo()
{
Width = 640;
Height = 480;
t = new Thread(this, "Vasya");
f = new Frame();
p = new Player();
Panel w = null;
w = new Panel();
f.add(w);
f.setSize(Width, Height);
f.setVisible(true);
f.addKeyListener(new Kb());
f.addWindowListener(
new WindowAdapter(){
public void windowClosed(WindowEvent e) { System.exit(0); }
public void windowClosing(WindowEvent e) { System.exit(0); }
}
);
g = w.getGraphics();
buffer = w.createImage(Width, Height);
img = buffer.getGraphics();
t.start();
}
public void paint()
{
img.setColor(Color.red);
img.fillRect(0, 0, Width, Height);
p.Render(img);
g.drawImage(buffer, 0, 0, null);
}
public static void main(String arg[])
{
Demo demo = new Demo();
try{
Thread.sleep(11111);
demo.t.join();
}catch(InterruptedException e){}
f.dispose();
}
}
class Kb extends KeyAdapter
{
private static boolean[] keyPress = {false, false, false, false};
public static boolean qPressed = false;
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_Q) {
qPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
keyPress[Direction.left.GetDir()] = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
keyPress[Direction.right.GetDir()] = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
keyPress[Direction.up.GetDir()] = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
keyPress[Direction.down.GetDir()] = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_Q) {
qPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
keyPress[Direction.left.GetDir()] = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
keyPress[Direction.right.GetDir()] = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
keyPress[Direction.up.GetDir()] = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
keyPress[Direction.down.GetDir()] = false;
}
}
public static boolean[] KeyBoard()
{
return keyPress;
}
}
Особенно интересует как можно средствами java упростить класс обработки событий клавиатуры(описан в конце). Ну или как обычно это реализовывается в играх?