package idv.cqd.test; import java.awt.Canvas; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferStrategy; @SuppressWarnings("serial") public class GeneralTest extends Canvas { Frame mf = new Frame(); protected static final int WIDTH = 200; protected static final int HEIGHT = 200; protected static final int PADDING = 10; protected static final int LINES = 1000; protected static final int BOXSIZE = 10; protected static final float BOXSPEED = 132.2f; //pixel per sec protected static final int WORLDUPDATETIME = 17; //millisec protected float boxX = 0; protected float boxY = 0; protected boolean goingUp = false; protected boolean goingDown = false; protected boolean goingLeft = false; protected boolean goingRight = false; protected boolean gameRunning = true; public void start() { setSize(WIDTH, HEIGHT); mf.setSize(WIDTH, WIDTH); mf.setResizable(false); mf.add(this); mf.pack(); mf.setVisible(true); mf.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { gameRunning = false; System.exit(0); } }); KeyListener kl = new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: goingUp = true; break; case KeyEvent.VK_DOWN: goingDown = true; break; case KeyEvent.VK_LEFT: goingLeft = true; break; case KeyEvent.VK_RIGHT: goingRight = true; break; } } @Override public void keyReleased(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: goingUp = false; break; case KeyEvent.VK_DOWN: goingDown = false; break; case KeyEvent.VK_LEFT: goingLeft = false; break; case KeyEvent.VK_RIGHT: goingRight = false; break; } } }; mf.addKeyListener(kl); this.addKeyListener(kl); createBufferStrategy(3); BufferStrategy startegy = getBufferStrategy(); Graphics g = startegy.getDrawGraphics(); WorldUpdater wu = new WorldUpdater(); wu.init(this); wu.start(); while (gameRunning) { long startTime = System.nanoTime(); g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); for (int i = 0; i < LINES; i++) { g.setColor(Color.WHITE); g.drawLine(randX(), randY(), randX(), randY()); } g.setColor(Color.RED); g.fillRect((int)boxX-BOXSIZE/2, (int)boxY-BOXSIZE/2, BOXSIZE, BOXSIZE); g.drawString("X:"+boxX+" Y:"+boxY, 10, 10); long endTime = System.nanoTime(); g.setColor(Color.RED); g.drawString("fps:"+(1000000000 / (endTime - startTime)), 10, HEIGHT-10); startegy.show(); } } int randX() { return (int) (Math.random() * (WIDTH - 2 * PADDING)) + PADDING; } int randY() { return (int) (Math.random() * (HEIGHT - 2 * PADDING)) + PADDING; } public static void main(String[] args) throws Exception { GeneralTest gt = new GeneralTest(); gt.start(); } } class WorldUpdater extends Thread { GeneralTest world; public void init(GeneralTest obj) { world = obj; } @Override public void run() { while(true){ if (world.goingUp) { world.boxY -= GeneralTest.BOXSPEED / 1000 * GeneralTest.WORLDUPDATETIME; } if (world.goingDown) { world.boxY += GeneralTest.BOXSPEED / 1000 * GeneralTest.WORLDUPDATETIME; } if (world.goingLeft) { world.boxX -= GeneralTest.BOXSPEED / 1000 * GeneralTest.WORLDUPDATETIME; } if (world.goingRight) { world.boxX += GeneralTest.BOXSPEED / 1000 * GeneralTest.WORLDUPDATETIME; } if(world.boxX>GeneralTest.WIDTH) world.boxX=GeneralTest.WIDTH; if(world.boxX<0) world.boxX=0; if(world.boxY>GeneralTest.HEIGHT) world.boxY=GeneralTest.HEIGHT; if(world.boxY<0) world.boxY=0; try { Thread.sleep(GeneralTest.WORLDUPDATETIME); } catch (InterruptedException e) { } } } }