GAMES LESSON Eightteen: Menu

Panels and buttons can be added to an applet to provide a basic menu. These items can be placed into the applet in various ways. The following example shows a really simple panel containing three buttons. Try it out.

The panel and button are declared before the init method like this:

Panel menuSpace; Button resetB, pauseB, colorB;

They are initiated in the init method like this:

resetB = new Button("RESET"); resetB.setBackground(Color.white); resetB.addActionListener(this); pauseB = new Button("PAUSE"); pauseB.setBackground(Color.white); pauseB.addActionListener(this); colorB = new Button("BALL COLOR"); colorB.setBackground(Color.white); colorB.addActionListener(this); menuSpace=new Panel(); menuSpace.setBackground(Color.green); menuSpace.add(resetB); menuSpace.add(pauseB); menuSpace.add(colorB); add(menuSpace);

Notice that they are hooked up to an ActionListener. Inspect the code for the entire applet (shown below) to see how the ActionListener is organized.

MENU.java

import java.awt.*; import java.applet.*; import java.util.*; import java.awt.event.*; public class MENU extends Applet implements Runnable, ActionListener{ 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; Button resetB, pauseB, colorB; Color COLORS[] = { Color.green, Color.red, Color.blue, Color.black, Color.magenta, Color.cyan, Color.white }; Color curr_color = Color.green; public void init(){ d = getSize(); floor = d.height-20; resetB = new Button("RESET"); resetB.setBackground(Color.white); resetB.addActionListener(this); pauseB = new Button("PAUSE"); pauseB.setBackground(Color.white); pauseB.addActionListener(this); colorB = new Button("BALL COLOR"); colorB.setBackground(Color.white); colorB.addActionListener(this); menuSpace=new Panel(); menuSpace.setBackground(Color.green); menuSpace.add(resetB); menuSpace.add(pauseB); menuSpace.add(colorB); add(menuSpace); reset(); offI=createImage(d.width,d.height); tt = new Thread(this); tt.start(); } public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if(src == resetB){ reset(); } else if(src == pauseB){ not_pause=!not_pause; } else if(src == colorB){ cc++; cc%=COLORS.length; curr_color=COLORS[cc]; } } 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(curr_color); offG.fillOval(x,y,10,10); offG.drawLine(0,floor,d.width,floor); g.drawImage(offI, 0,0, this); } }


ASSIGNMENT: