Integrating Compiler & IDE with your Website and LMS, Join the Free Webinar |
22nd February at 2:30 pm (AEDT) Book Your Seat
Online Java Compiler IDE Font Size: 12px
https://www.jdoodle.com/online-java-compiler
1 import java.awt.*;
2 import java.awt.event.*;
3 import java.util.ArrayList;
4 import java.util.Random;
5 import javax.swing.*;
6
7 public class Board extends JPanel implements ActionListener, KeyListener {
8
9 // controls the delay between each tick in ms
10 private final int DELAY = 25;
11 // controls the size of the board
12 public static final int TILE_SIZE = 50;
13 public static final int ROWS = 12;
14 public static final int COLUMNS = 18;
15 // controls how many coins appear on the board
16 public static final int NUM_COINS = 5;
17 // suppress serialization warning
18 private static final long serialVersionUID = 490905409104883233L;
19
20 // keep a reference to the timer object that triggers actionPerformed() in
21 // case we need access to it in another method
22 private Timer timer;
23 // objects that appear on the game board
24 private Player player;
25 private ArrayList<Coin> coins;
26
27 public Board() {
28 // set the game board size
29 setPreferredSize(new Dimension(TILE_SIZE * COLUMNS, TILE_SIZE * ROWS));
30 // set the game board background color
31 setBackground(new Color(232, 232, 232));
32
33 // initialize the game state
34 player = new Player();
35 coins = populateCoins();
36
37 // this timer will call the actionPerformed() method every DELAY ms
38 timer = new Timer(DELAY, this);
39 timer.start();
40
41 // add key listener to the board
Share this awesome tool with your peers