import java.awt.*; import java.applet.Applet; import java.util.*; public class FishWorld extends Applet { //declare some panels private Panel controlPanel; private Button bodyColorButton; private Choice bodyColorChoice; private Button tailColorButton; private Choice tailColorChoice; private Button setPositionButton; private TextField xPositionField; private TextField yPositionField; private Button resizeButton; private TextField resizeField; private Button swimButton; private TextField swimAmpField; private Button resetButton; //set up default values for these variables private Color selectedBodyColor = Color.red; private Color selectedTailColor = Color.green; private Canvas fishtank; //keeps track of the fish who will be performing all the commands private Fish currentFish; //the init() method contains only the layout of the applet window; //all sub-panels should be created in other methods in order to //keep this method clean. public void init () { this.setLayout (new BorderLayout()); this.add("West", makeControlPanel()); this.add("Center", makeFishtank()); } // this method happens every time the window becomes active again public void start () { currentFish = makeFish(fishtank.getGraphics()); } //make all the GUI panels public Panel makeControlPanel() { controlPanel = new Panel(); controlPanel.setLayout (new GridLayout(8,1)); controlPanel.add(makeBodyColorButton()); controlPanel.add(makeBodyColorChoice()); controlPanel.add(makeTailColorButton()); controlPanel.add(makeTailColorChoice()); controlPanel.add(makeSetPositionPanel()); controlPanel.add(makeResizePanel()); controlPanel.add(makeSwimPanel()); controlPanel.add(makeResetButton()); return controlPanel; } public Canvas makeFishtank() { fishtank = new Canvas (); return fishtank; } public Fish makeFish(Graphics g) { Fish fred = new Fish(g); return fred; } //set up rest of GUI stuff public Panel makeBodyColorPanel () { Panel bodyColorPanel = new Panel(); bodyColorPanel.add(makeBodyColorButton()); bodyColorPanel.add(makeBodyColorChoice()); return bodyColorPanel; } public Panel makeTailColorPanel () { Panel tailColorPanel = new Panel(); tailColorPanel.add(makeTailColorButton()); tailColorPanel.add(makeTailColorChoice()); return tailColorPanel; } public Panel makeSetPositionPanel () { Panel setPositionPanel = new Panel(); setPositionPanel.add(makeSetPositionButton()); setPositionPanel.add(makeXPositionField()); setPositionPanel.add(makeYPositionField()); return setPositionPanel; } public Panel makeResizePanel () { Panel resizePanel = new Panel(); resizePanel.add(makeResizeButton()); resizePanel.add(makeResizeField()); return resizePanel; } public Panel makeSwimPanel () { Panel swimPanel = new Panel(); swimPanel.add(makeSwimButton()); swimPanel.add(makeSwimAmpField()); return swimPanel; } public Button makeBodyColorButton () { bodyColorButton = new Button ("Set Body Color"); return bodyColorButton; } public Choice makeBodyColorChoice () { bodyColorChoice = new Choice(); bodyColorChoice.setBackground (selectedBodyColor); bodyColorChoice.addItem ("Color.red"); bodyColorChoice.addItem ("Color.orange"); bodyColorChoice.addItem ("Color.yellow"); bodyColorChoice.addItem ("Color.green"); bodyColorChoice.addItem ("Color.blue"); bodyColorChoice.addItem ("Color.cyan"); bodyColorChoice.addItem ("Color.magenta"); return bodyColorChoice; } public Button makeTailColorButton () { tailColorButton = new Button ("Set Tail Color"); return tailColorButton; } public Choice makeTailColorChoice () { tailColorChoice = new Choice(); tailColorChoice.setBackground (selectedTailColor); tailColorChoice.addItem ("Color.red"); tailColorChoice.addItem ("Color.orange"); tailColorChoice.addItem ("Color.yellow"); tailColorChoice.addItem ("Color.green"); tailColorChoice.addItem ("Color.blue"); tailColorChoice.addItem ("Color.cyan"); tailColorChoice.addItem ("Color.magenta"); return tailColorChoice; } public Button makeSetPositionButton () { setPositionButton = new Button ("Set Position"); return setPositionButton; } public TextField makeXPositionField () { xPositionField = new TextField ("1", 3); return xPositionField; } public TextField makeYPositionField () { yPositionField = new TextField ("20", 3); return yPositionField; } public Button makeResizeButton () { resizeButton = new Button ("Resize Fish"); return resizeButton; } public TextField makeResizeField () { resizeField = new TextField ("100", 3); return resizeField; } public Button makeSwimButton () { swimButton = new Button ("Swim Up and Down"); return swimButton; } public TextField makeSwimAmpField () { swimAmpField = new TextField ("20", 3); return swimAmpField; } public Button makeResetButton () { resetButton = new Button ("Reset"); return resetButton; } //the action method responds to mouse-clicks and checks //to see where the mouse-click happened. public boolean action (Event event, Object arg) { if (event.target == bodyColorButton) { currentFish.setBodyColor(selectedBodyColor); } else if (event.target == bodyColorChoice) { bodyColorAction(arg); } else if (event.target == tailColorButton) { currentFish.setTailColor(selectedTailColor); } else if (event.target == tailColorChoice) { tailColorAction(arg); } else if (event.target == setPositionButton) { setPositionAction(); } else if (event.target == resizeButton) { resizeAction(); } else if (event.target == swimButton) { swimAction(); } else if (event.target == resetButton) { resetAction(); } else { return super.action(event, arg); } return true; } //all the specific action responses public void bodyColorAction(Object arg) { if (arg.equals("Color.red")) { selectedBodyColor = Color.red; } else if (arg.equals("Color.orange")) { selectedBodyColor = Color.orange; } else if (arg.equals("Color.yellow")) { selectedBodyColor = Color.yellow; } else if (arg.equals("Color.green")) { selectedBodyColor = Color.green; } else if (arg.equals("Color.blue")) { selectedBodyColor = Color.blue; } else if (arg.equals("Color.cyan")) { selectedBodyColor = Color.cyan; } else if (arg.equals("Color.magenta")) { selectedBodyColor = Color.magenta; } bodyColorChoice.setBackground(selectedBodyColor); } public void tailColorAction(Object arg) { if (arg.equals("Color.red")) { selectedTailColor = Color.red; } else if (arg.equals("Color.orange")) { selectedTailColor = Color.orange; } else if (arg.equals("Color.yellow")) { selectedTailColor = Color.yellow; } else if (arg.equals("Color.green")) { selectedTailColor = Color.green; } else if (arg.equals("Color.blue")) { selectedTailColor = Color.blue; } else if (arg.equals("Color.cyan")) { selectedTailColor = Color.cyan; } else if (arg.equals("Color.magenta")) { selectedTailColor = Color.magenta; } tailColorChoice.setBackground(selectedTailColor); } public void setPositionAction () { int xPos = Integer.parseInt(xPositionField.getText()); int yPos = Integer.parseInt(yPositionField.getText()); currentFish.setPosition(new Point (xPos, yPos)); } public void resizeAction () { int newSize = Integer.parseInt(resizeField.getText()); currentFish.resize(newSize); } public void swimAction () { int amp = Integer.parseInt(swimAmpField.getText()); currentFish.swimUpandDown(amp); } public void resetAction () { Dimension d = fishtank.size (); Graphics fishg = fishtank.getGraphics(); fishg.setColor (Color.white); fishg.fillRect (0,0,d.width, d.height); start(); } }