GAMES LESSON Nineteen: Hiding and Unhiding Menus

The code for this sample applet is a little long because of all the components we crown into it, but the basic ideas are quite simple. Take some time to play with each of the buttons to make sure that you are quite familiar with how it works.

There are panels used in this applet. The first panel is called menuSpace and it contains three buttons: pauseB, resetB, and config B. The second panel is called configSpace and it contains a label, a panel, and a button: dirT, midSpace, and okayB. The panel called midSpace contains two labels and two textfields: pauseT, resetT, pauseF, resetF. Here is the applet up to the point where all these components are declared and initialized.

public class SHOW extends Applet implements Runnable, ActionListener, KeyListener{ Dimension d; Image offI; Thread tt; int y,x,cc; double speed, xspeed; final static double dec=1.2; final static double acc=.9; final static double xdec=.001; boolean up, not_pause, left; int floor; Panel menuSpace, configSpace, midSpace; Button resetB, pauseB, configB; Label dirT, pauseT, resetT; TextField pauseF, resetF; Button okayB; String pauseL = "p"; String resetL = "r"; public void init(){ d = getSize(); floor = d.height-20; addKeyListener(this); resetB = new Button("RESET"); resetB.setBackground(Color.white); resetB.addActionListener(this); pauseB = new Button("PAUSE"); pauseB.setBackground(Color.white); pauseB.addActionListener(this); configB = new Button("CONFIGURE"); configB.setBackground(Color.white); configB.addActionListener(this); menuSpace=new Panel(); menuSpace.setBackground(Color.green); menuSpace.add(resetB); menuSpace.add(pauseB); menuSpace.add(configB); add(menuSpace); dirT = new Label("Enter single letter shortcut."); pauseT = new Label("pause"); resetT = new Label("reset"); pauseF = new TextField(8); pauseF.setBackground(Color.white); resetF = new TextField(8); resetF.setBackground(Color.white); okayB = new Button("OK"); okayB.setBackground(Color.white); okayB.addActionListener(this); configSpace = new Panel(); configSpace.setBackground(Color.green); configSpace.setLayout(new BorderLayout()); midSpace = new Panel(); midSpace.setBackground(Color.green); midSpace.setLayout(new GridLayout(2,2)); midSpace.add(pauseT); midSpace.add(pauseF); midSpace.add(resetT); midSpace.add(resetF); configSpace.add(dirT, BorderLayout.NORTH); configSpace.add(midSpace, BorderLayout.CENTER); configSpace.add(okayB, BorderLayout.SOUTH); add(configSpace); reset(); offI=createImage(d.width,d.height); tt = new Thread(this); tt.start(); } ...

You probably noticed that configSpace was set to BorderLayout and that midSpace was set to GridLayout. You will have to look up the particulars on these two layouts if you don't already know them!

You should also have noticed that this applet implements both the actionListener and keyListener interfaces. Here are the methods which must be implemented as a result of this:

public void keyPressed(KeyEvent k) { if(k.getKeyChar() == pauseL.charAt(0)){ System.out.println("PAUSE LETTER: " + pauseL.charAt(0)); not_pause=!not_pause; } else if(k.getKeyChar() == resetL.charAt(0)){ System.out.println("RESET LETTER: " + resetL.charAt(0)); reset(); } System.out.println("KEYEVENT TRIGGERED"); } public void keyReleased(KeyEvent k){ System.out.println("Key Released"); } public void keyTyped(KeyEvent k){ System.out.println("Key Typed"); } public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if(src == resetB){ reset(); } else if(src == pauseB){ not_pause=!not_pause; } else if(src == configB){ configSpace.setVisible(true); } else if(src == okayB){ pauseL = pauseF.getText(); resetL = resetF.getText(); if(pauseL.equals("") || pauseL.charAt(0) == ' ') pauseL="p"; if(resetL.equals("") || resetL.charAt(0) == ' ') resetL="r"; System.out.println("PAUSE: " + pauseL + ", RESET: " + resetL); configSpace.setVisible(false); } requestFocus(); //very important!!! }

Notice that at the end of the actionPerformed method a call to requestFocus us made. This is pretty important. Without it, the user's keystrokes will be ignored!

SHOW.java

import java.awt.*; import java.applet.*; import java.util.*; import java.awt.event.*; public class SHOW extends Applet implements Runnable, ActionListener, KeyListener{ Dimension d; Image offI; Thread tt; int y,x,cc; double speed, xspeed; final static double dec=1.2; final static double acc=.9; final static double xdec=.001; boolean up, not_pause, left; int floor; Panel menuSpace, configSpace, midSpace; Button resetB, pauseB, configB; Label dirT, pauseT, resetT; TextField pauseF, resetF; Button okayB; String pauseL = "p"; String resetL = "r"; public void init(){ d = getSize(); floor = d.height-20; addKeyListener(this); resetB = new Button("RESET"); resetB.setBackground(Color.white); resetB.addActionListener(this); pauseB = new Button("PAUSE"); pauseB.setBackground(Color.white); pauseB.addActionListener(this); configB = new Button("CONFIGURE"); configB.setBackground(Color.white); configB.addActionListener(this); menuSpace=new Panel(); menuSpace.setBackground(Color.green); menuSpace.add(resetB); menuSpace.add(pauseB); menuSpace.add(configB); add(menuSpace); dirT = new Label("Enter single letter shortcut."); pauseT = new Label("pause"); resetT = new Label("reset"); pauseF = new TextField(8); pauseF.setBackground(Color.white); resetF = new TextField(8); resetF.setBackground(Color.white); okayB = new Button("OK"); okayB.setBackground(Color.white); okayB.addActionListener(this); configSpace = new Panel(); configSpace.setBackground(Color.green); configSpace.setLayout(new BorderLayout()); midSpace = new Panel(); midSpace.setBackground(Color.green); midSpace.setLayout(new GridLayout(2,2)); midSpace.add(pauseT); midSpace.add(pauseF); midSpace.add(resetT); midSpace.add(resetF); configSpace.add(dirT, BorderLayout.NORTH); configSpace.add(midSpace, BorderLayout.CENTER); configSpace.add(okayB, BorderLayout.SOUTH); add(configSpace); reset(); offI=createImage(d.width,d.height); tt = new Thread(this); tt.start(); } public void keyPressed(KeyEvent k) { if(k.getKeyChar() == pauseL.charAt(0)){ System.out.println("PAUSE LETTER: " + pauseL.charAt(0)); not_pause=!not_pause; } else if(k.getKeyChar() == resetL.charAt(0)){ System.out.println("RESET LETTER: " + resetL.charAt(0)); reset(); } System.out.println("KEYEVENT TRIGGERED"); } public void keyReleased(KeyEvent k){ System.out.println("Key Released"); } public void keyTyped(KeyEvent k){ System.out.println("Key Typed"); } public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if(src == resetB){ reset(); } else if(src == pauseB){ not_pause=!not_pause; } else if(src == configB){ configSpace.setVisible(true); } else if(src == okayB){ pauseL = pauseF.getText(); resetL = resetF.getText(); if(pauseL.equals("") || pauseL.charAt(0) == ' ') pauseL="p"; if(resetL.equals("") || resetL.charAt(0) == ' ') resetL="r"; System.out.println("PAUSE: " + pauseL + ", RESET: " + resetL); configSpace.setVisible(false); } requestFocus(); } public void reset(){ not_pause = true; y = 0; x = 0; speed=1; xspeed=4; } public void run(){ while(true){ if(not_pause){ if(up){ y-=(int)speed; speed-=dec; } else{ y+=(int)speed; speed+=acc; } if(left){ x-=(int)xspeed; } else{ x+=(int)xspeed; } if(xspeed>0) xspeed -= xdec; if(!up && y>floor-5) up=true; else if(up && y<0) up=false; else if(speed<=0) up=false; if(!left && x>d.width) left=true; else if(left && x<0) left=false; repaint(); } try{ Thread.sleep(30); }catch(InterruptedException ie){ } } } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ Graphics offG = offI.getGraphics(); offG.setColor(Color.yellow); offG.fillRect(0,0,d.width,d.height); offG.setColor(Color.red); offG.fillOval(x,y,10,10); offG.drawLine(0,floor,d.width,floor); g.drawImage(offI, 0,0, this); } }


ASSIGNMENT: