comparison game/Engine.java @ 10:4bacc98973f5

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 29 Jan 2011 01:54:28 +0200
parents a7751971c2a3
children b89ecc8d5557
comparison
equal deleted inserted replaced
9:a7751971c2a3 10:4bacc98973f5
78 public static final int boardMiddle = 4; 78 public static final int boardMiddle = 4;
79 Piece[][] board; 79 Piece[][] board;
80 Piece current; 80 Piece current;
81 public boolean flagGameOver; 81 public boolean flagGameOver;
82 82
83
84
83 int moveX, moveY, movePoint; 85 int moveX, moveY, movePoint;
84 86
85 public GameBoard() 87 public GameBoard()
86 { 88 {
87 board = new Piece[boardSize][boardSize]; 89 board = new Piece[boardSize][boardSize];
120 for (int x = 0; x < boardSize; x++) 122 for (int x = 0; x < boardSize; x++)
121 if (board[x][y] != null) 123 if (board[x][y] != null)
122 { 124 {
123 board[x][y].animate(time); 125 board[x][y].animate(time);
124 } 126 }
127
125 } 128 }
126 129
127 private boolean isEmpty(int x, int y) 130 private boolean isEmpty(int x, int y)
128 { 131 {
129 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null); 132 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null);
225 // Graphics2D g = img.getGraphics(); 228 // Graphics2D g = img.getGraphics();
226 // atrans = AffineTransform.getScaleInstance(img.getWidth(), 3); 229 // atrans = AffineTransform.getScaleInstance(img.getWidth(), 3);
227 230
228 lauta = new GameBoard(); 231 lauta = new GameBoard();
229 addKeyListener(this); 232 addKeyListener(this);
233
230 } 234 }
231 235
232 public void startThreads() 236 public void startThreads()
233 { 237 {
234 System.out.print("startThreads()\n"); 238 System.out.print("startThreads()\n");
252 } 256 }
253 257
254 public void paintComponent(Graphics g) 258 public void paintComponent(Graphics g)
255 { 259 {
256 Graphics2D g2 = (Graphics2D) g; 260 Graphics2D g2 = (Graphics2D) g;
257 261
262 // Use antialiasing when rendering the game elements
258 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 263 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
259 RenderingHints.VALUE_ANTIALIAS_ON); 264 RenderingHints.VALUE_ANTIALIAS_ON);
260 265
266 // Rescale background if component size has changed
261 Dimension dim = getSize(); 267 Dimension dim = getSize();
262 if (oldDim == null || !dim.equals(oldDim)) 268 if (oldDim == null || !dim.equals(oldDim))
263 { 269 {
264 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); 270 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
265 Graphics2D gimg = lautaBGScaled.createGraphics(); 271 Graphics2D gimg = lautaBGScaled.createGraphics();
266 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 272 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
267 RenderingHints.VALUE_INTERPOLATION_BICUBIC); 273 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
268 AffineTransform xform = AffineTransform.getScaleInstance(0.5f, 0.5f); 274
269 gimg.drawImage(lautaBG, xform, null); 275 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null);
270 oldDim = dim; 276 oldDim = dim;
271 System.out.print("scale changed\n"); 277 System.out.print("scale changed\n");
272 } 278 }
273 279
280 // Background, pieces
274 g2.drawImage(lautaBGScaled, 0, 0, null); 281 g2.drawImage(lautaBGScaled, 0, 0, null);
275 lauta.paint(g2, 100, 150, 60); 282 lauta.paint(g2, 100, 150, 60);
276 283
284 // Other elements
285
286 // Request focus to keep keyboard input coming
277 if (!hasFocus()) 287 if (!hasFocus())
288 {
289 System.out.print(".\n");
278 requestFocus(); 290 requestFocus();
291 }
279 } 292 }
280 293
281 public void keyTyped(KeyEvent e) 294 public void keyTyped(KeyEvent e)
282 { 295 {
283 } 296 }
315 328
316 // System.out.print("clock=" + clock + "\n"); 329 // System.out.print("clock=" + clock + "\n");
317 330
318 lauta.animate(clock); 331 lauta.animate(clock);
319 332
320 repaint(); 333 if (clock % 2 == 1)
334 repaint();
321 335
322 try { 336 try {
323 Thread.sleep(10); 337 Thread.sleep(10);
324 } 338 }
325 339