Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.*;
- /**
- * A simple grid guessing game
- */
- public class GridGuess extends Applet implements ActionListener
- {
- final static int BOARDSIZE = 5;
- private Button[][] ButtonGrid = new Button[BOARDSIZE][BOARDSIZE];
- private Panel p;
- private Label NameLabel;
- private int RowHideSpot, ColHideSpot;
- public void init() {
- setLayout(new BorderLayout());
- p = new Panel();
- p.setLayout(new GridLayout(BOARDSIZE,BOARDSIZE));
- // Randomly select the solution
- RowHideSpot = (int) ((BOARDSIZE-1) * Math.random() );
- ColHideSpot = (int) ((BOARDSIZE-1) * Math.random() );
- for(int r=0;r<BOARDSIZE;r++)
- for(int c=0;c<BOARDSIZE;c++) {
- Button button = new Button("");
- if (r == RowHideSpot && c == ColHideSpot)
- button.setActionCommand("target");
- ButtonGrid[r][c] = button;
- ButtonGrid[r][c].setBackground(Color.WHITE);
- ButtonGrid[r][c].addActionListener(this);
- p.add(ButtonGrid[r][c]);
- }
- add(p,BorderLayout.CENTER);
- // Create label at the bottom
- NameLabel = new Label();
- add(NameLabel,BorderLayout.SOUTH);
- NameLabel.setText("" + RowHideSpot + " " + ColHideSpot);
- }
- public void actionPerformed(ActionEvent e) {
- Button b = (Button)e.getSource();
- // if it's correct button
- b.setBackground(Color.BLUE);
- if (b.getActionCommand().equals("target")){
- NameLabel.setText("CORRECT!");
- b.setBackground(Color.GREEN);
- }
- else
- NameLabel.setText("Nope.");
- }
- public void ErrorFound(String s) {
- NameLabel.setText("Fatal error, " +
- "game terminated: " + s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment