/** * @author Kaushik Raj * @see http://www.cis.syr.edu/~kraj/java/ * @email kraj@syr.edu * * (c) copyright-98 Kaushik Raj * All Rights Reserved. * * This source code is free for all non-commercial purposes, as long as the name * and the url address given above is written with the modified program. The author does not gives * the permission to publish the source code in any book or any other publishing media. * * * The author doesnot takes any responsibility for the software/hardware damages * that may occur due to this program. */ /** * File : ControlPanel.java * Last update : 10 July,1998 */ import java.awt.*; import java.applet.*; /** * ControlPanel class controls the panel where buttons are * given for new game and to change the size of maze. */ public class ControlPanel extends Panel { /* displays the label */ private Label senseLabel; private Label levelLabel; private Button newGameButton; private Button bRatSensePlus,bRatSenseMinus; private Button bMazeLevelPlus,bMazeLevelMinus; private TextField ratSenseText,gameLevelText; private MazeGame mg; final static private int MIN_SENSE_LEVEL=1,MAX_SENSE_LEVEL=4; final static private int MIN_GAME_LEVEL=6,MAX_GAME_LEVEL=20; /* default values of the game */ private int noOfRows=10; private int noOfSmellBlocks=2; /* make the panel board */ public ControlPanel(MazeGame mg) { setLayout(null); setForeground(Color.white); setBackground(Color.black); setFont(new Font("Times",Font.BOLD,12)); senseLabel=new Label("Sense Power"); levelLabel=new Label("Game Level"); bRatSensePlus=new Button("+"); bRatSenseMinus=new Button("-"); bMazeLevelPlus=new Button("+"); bMazeLevelMinus=new Button("-"); ratSenseText=new TextField(""); gameLevelText=new TextField(""); newGameButton=new Button("NEW GAME"); levelLabel.reshape(24,12,81,21); senseLabel.reshape(24,84,96,25); bMazeLevelMinus.reshape(24,48,26,24); bMazeLevelPlus.reshape(96,48,26,24); bRatSenseMinus.reshape(24,120,26,24); bRatSensePlus.reshape(96,120,26,24); ratSenseText.reshape(60,48,26,24); gameLevelText.reshape(60,120,26,24); newGameButton.reshape(24,168,98,28); add(senseLabel); add(levelLabel); add(bRatSensePlus); add(bRatSenseMinus); add(bMazeLevelPlus); add(bMazeLevelMinus); add(ratSenseText); add(gameLevelText); add(newGameButton); validate(); this.mg=mg; updateLevels(); } public boolean handleEvent(Event event) { return super.handleEvent(event); } /** returns the level of the game at present */ public int getGameLevel(){ return noOfRows; } /** returns the smell power of the rat */ public int getSensingPower(){ return noOfSmellBlocks; } /** if the new game button is clicked new game starts * otherwise if other buttons are clicked the game level * is increased or decreased as asked by the user */ public boolean action(Event e,Object o) { if (e.target==newGameButton) { //System.out.println("Button Clicked"); mg.makePlayAgain() ; }else if (e.target==bMazeLevelMinus) setGameLevel(false); else if (e.target==bMazeLevelPlus) setGameLevel(true); else if (e.target==bRatSenseMinus) setSenseLevel(false); else if (e.target==bRatSensePlus) setSenseLevel(true); return true ; } /** private call to set the game level */ private void setGameLevel(boolean increase){ if (increase){ if (noOfRows>=MAX_GAME_LEVEL); else noOfRows++; }else{ if (noOfRows<=MIN_GAME_LEVEL); else noOfRows--; } updateLevels(); } /** private call to swet the sense level of the rat */ private void setSenseLevel(boolean increase){ if (increase){ if (noOfSmellBlocks>=MAX_SENSE_LEVEL); else noOfSmellBlocks++; }else{ if (noOfSmellBlocks<=MIN_SENSE_LEVEL); else noOfSmellBlocks--; } updateLevels(); } /** updates the display at the control panel */ private void updateLevels(){ String sGame=""+getGameLevel(); String sSense=""+getSensingPower(); ratSenseText.setText(sGame); gameLevelText.setText(sSense); } }