1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| import java.awt.*; import java.awt.event.*; import javax.swing.*;
@SuppressWarnings("serial") public class GameBoard extends JPanel implements Runnable { private final int SCALE = 16; private final long PERIOD = 20 * 1000000; private static int uIndex = 0; private static int vIndex = 0; private static int wIndex = 1; long beforeTime; long afterTime; long difference; long sleepTime; private Image TILE_0; private Image TILE_0_TOP; private Image TILE_A; private Image TILE_A_TOP; private Image TILE_B; private Image TILE_B_TOP; private Graphics bufferGraphics; private Image bufferImage; private Thread game; private volatile boolean running = false; private GameTile tileGrid; public GameBoard() { loadTileImage(); tileGrid = new GameTile(2); setBackground(Color.WHITE); setFocusable(true); requestFocus(); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { default: break; case KeyEvent.VK_UP: if(uIndex < tileGrid.getU() / 2) uIndex++; break; case KeyEvent.VK_DOWN: if(uIndex > -(tileGrid.getU() / 2)) uIndex--; break; case KeyEvent.VK_LEFT: if(vIndex < tileGrid.getV() / 2) vIndex++; break; case KeyEvent.VK_RIGHT: if(vIndex > -(tileGrid.getV() / 2)) vIndex--; break; case KeyEvent.VK_PAGE_UP: if(wIndex < tileGrid.getW() - 2) wIndex++; break; case KeyEvent.VK_PAGE_DOWN: if(wIndex > 2) wIndex--; break; case KeyEvent.VK_SPACE: uIndex = 0; vIndex = 0; break; case KeyEvent.VK_ESCAPE: System.exit(0); break; } } @Override public void keyReleased(KeyEvent e) {} @Override public void keyTyped(KeyEvent e) {} }); } public void addNotify() { super.addNotify(); startGame(); } private void startGame() { if(game == null || !running) { game = new Thread(this); game.start(); running = true; } } public void stopGame() { if(running) { running = false; } } @SuppressWarnings("static-access") public void run() { while(running) { beforeTime = System.nanoTime(); gameUpdate(); gameRender(); paintScreen(); afterTime = System.nanoTime(); difference = afterTime - beforeTime; if(difference < PERIOD) { sleepTime = PERIOD - difference; try { game.sleep(sleepTime / 1000000); } catch (InterruptedException e) {} System.out.printf("%d.%03d ms\n", difference / 1000000, difference % 1000000 / 1000); } else { System.out.printf("LAG! %d.%03d ms\n", difference / 1000000, difference % 1000000 / 1000); } } } private void gameUpdate() { if(running || game != null) { } } private void gameRender() { if(bufferImage == null) { bufferImage = createImage(800, 600); if(bufferImage == null) { System.out.println("ERROR: Buffer image still does not exist!"); return; } else { bufferGraphics = bufferImage.getGraphics(); } } bufferGraphics.setColor(Color.WHITE); bufferGraphics.fillRect(0, 0, 800, 600); draw(bufferGraphics); } public void draw(Graphics g) { drawTile(g); } public void paintScreen() { Graphics g; try { g = this.getGraphics(); if(bufferImage != null && g != null) { g.drawImage(bufferImage, 0, 0, null); } Toolkit.getDefaultToolkit().sync(); g.dispose(); } catch(Exception e) {} } private void loadTileImage() { TILE_0 = new ImageIcon("tile0.png").getImage(); TILE_0_TOP = new ImageIcon("tile0top.png").getImage(); TILE_A = new ImageIcon("tileA.png").getImage(); TILE_A_TOP = new ImageIcon("tileAtop.png").getImage(); TILE_B = new ImageIcon("tileB.png").getImage(); TILE_B_TOP = new ImageIcon("tileBtop.png").getImage(); } private void drawTileImage(int i, int j, int k, Graphics g) { if(tileGrid.getTileID(i, j, k) == 0) g.drawImage(TILE_0, toX(i, j), toY(i, j, k), null); else if(tileGrid.getTileID(i, j, k) == 1) g.drawImage(TILE_A, toX(i, j), toY(i, j, k), null); else if(tileGrid.getTileID(i, j, k) == 2) g.drawImage(TILE_B, toX(i, j), toY(i, j, k), null); } private void drawTileTopImage(int i, int j, int k, Graphics g) { if(tileGrid.getTileID(i, j, k) == 0) g.drawImage(TILE_0_TOP, toX(i, j), toY(i, j, k), null); else if(tileGrid.getTileID(i, j, k) == 1) g.drawImage(TILE_A_TOP, toX(i, j), toY(i, j, k), null); else if(tileGrid.getTileID(i, j, k) == 2) g.drawImage(TILE_B_TOP, toX(i, j), toY(i, j, k), null); } private void drawTile(Graphics g) { for(int i = 0; i < tileGrid.getU(); i++) { for(int j = 0; j < tileGrid.getV(); j++) { drawTileImage(i, j, wIndex - 1, g); drawTileImage(i, j, wIndex, g); drawTileTopImage(i, j, wIndex + 1, g); } } } private int toX(int i, int j) { return((getWidth() / 2 - SCALE) - SCALE * (i + uIndex) + SCALE * (j + vIndex)); } private int toY(int i, int j, int k) { return((getHeight() / 2 - SCALE * tileGrid.getV() / 2) + SCALE * (i + uIndex + j + vIndex) / 2 - SCALE * (k - wIndex)); } } |