import java.applet.Applet; import java.awt.*; public class MoveBoxWithCanvas extends Applet { private Button up = new Button("Up"); private Button down = new Button("Down"); private Button left = new Button("Left"); private Button right = new Button("Right"); private Button increase = new Button("[+]"); private Button decrease = new Button("[-]"); private Choice objects = new Choice(); private CheckboxGroup colors = new CheckboxGroup(); private Checkbox red = new Checkbox("red", colors, true), black = new Checkbox("black", colors, false), blue = new Checkbox("blue", colors, false), yellow = new Checkbox("yellow", colors, false), green = new Checkbox("green", colors, false), pink = new Checkbox("magenta", colors, false); private DrawingCanvas drawing = new DrawingCanvas(); // init method public void init() { objects.addItem("Rectangle"); objects.addItem("Circle"); objects.addItem("Oval"); Panel head = new Panel(); head.setLayout(new FlowLayout()); head.add(new Label("Choose an object: ")); head.add(objects); Panel colorCol = new Panel(); colorCol.setLayout(new GridLayout(6,1)); colorCol.add(black); colorCol.add(blue); colorCol.add(green); colorCol.add(pink); colorCol.add(red); colorCol.add(yellow); Panel buttons = new Panel(); buttons.setLayout(new FlowLayout()); buttons.add(up); buttons.add(down); buttons.add(left); buttons.add(right); buttons.add(increase); buttons.add(decrease); setBackground(Color.blue); setLayout(new BorderLayout()); add("South", buttons); add("North", head); add("West", colorCol); add("Center", drawing); } // public methods public boolean action(Event e, Object o) { if (e.target == up) return drawing.handleUp(); else if (e.target == down) return drawing.handleDown(); else if (e.target == left) return drawing.handleLeft(); else if (e.target == right) return drawing.handleRight(); else if (e.target == increase) return drawing.handleIncrease(); else if (e.target == decrease) return drawing.handleDecrease(); else if (e.target == black) return drawing.handleColorChange(Color.black); else if (e.target == black) return drawing.handleColorChange(Color.black); else if (e.target == blue) return drawing.handleColorChange(Color.blue); else if (e.target == green) return drawing.handleColorChange(Color.green); else if (e.target == pink) return drawing.handleColorChange(Color.pink); else if (e.target == red) return drawing.handleColorChange(Color.red); else if (e.target == yellow) return drawing.handleColorChange(Color.yellow); else if (e.target instanceof Choice) return drawing.handleChangeObject(objects.getSelectedItem()); else return super.action(e, o); } }