Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java and AWT CPS 470 Spring 1998 Laura Campbell 1 Java 1.0 • ADM group thinks they can do it using 1.0 instead of 1.1 • Better chance of running under Netscape than 1.1 • /usr/java/bin/javac • /usr/java/bin/appletviewer 2 Setup for Java on SGIs • Add to .personal file: if (`uname` == "IRIX") then set path=($path /usr/java/bin) endif • A path to the correct javac (/opt/bin/javac) should already be set up on the Suns. 3 Java.awt-components • MenuComponent – MenuBar – MenuItem • Menu, CheckboxMenuItem • Component – Button, Canvas, Checkbox, Choice, Label, List, Scrollbar – TextComponent • TextArea, TextField – Container • Panel, Window 4 References • URLs on class web page(s) • Java books on reserve in Engineering Library • SunSoft Press Java Series • Java API (James Gosling, et al) 5 Lab Assignment • Files – MsuMap.java – MyCanvas.java – ?.java (you’ll subclass Choice) • Task – Keep the map display and the choice items synchronized • How – Subclass Choice so you can override the event handling 6 // MsuMap.java import java.awt.*; import java.applet.*; import java.net.*; public class MsuMap extends Canvas { private Image im; private Image msumap; private Image selectedMsumap; public MsuMap(Image i1, Image i2) { msumap = i1; selectedMsumap = i2; im = msumap; } public void paint( Graphics g ) { g.drawImage(im,0,0,this); } public boolean handleEvent( Event event ) { Object target = event.target; int id = event.id; if ((target == this) && (id == Event.MOUSE_DOWN)) { if (im == msumap) { im = selectedMsumap; } else { im = msumap; } repaint(); return true; } return false; } } 7 // MyCanvas.java (original) import java.applet.*; import java.awt.*; import java.net.*; public class MyCanvas extends Applet { java.awt.Component canvas; java.awt.Choice choiceBox; // Initialize this object. public void init() { super.init(); URL codebase = getCodeBase(); Image msumap = getImage(codebase, "msumap.gif"); Image selected = getImage(codebase, "selected.gif"); // Manual layout may be used for appletviewer // but should NOT be used for Netscape (it gets confused). // Set the layout to null to override it with manual commands. // // setLayout(null); // may uncomment this line for appletviewer // Set the background color to off-white. setBackground(new Color(0xfaf0e6)); resize(800, 500); add(canvas = new MsuMap(msumap,selected)); canvas.move(43, 20); canvas.resize(600, 500); canvas.show(); add(choiceBox = new java.awt.Choice()); choiceBox.addItem("Route"); choiceBox.addItem("Facility"); choiceBox.setFont(new Font("Dialog", 0, 12)); // may uncomment the next line if manual layout used for appletviewer // choiceBox.move(650, 49); choiceBox.show(); } } 8 // MyCanvas.java (revised) import java.applet.*; import java.awt.*; import java.net.*; public class MyCanvas extends Applet { java.awt.Component canvas; RorF_Choice choiceBox; // Using our own subclassed Choice public void init() { super.init(); URL codebase = getCodeBase(); Image msumap = getImage(codebase, "msumap.gif"); Image selected = getImage(codebase, "selected.gif"); // Manual layout may be used for appletviewer // but should NOT be used for Netscape (it gets confused). // Set the layout to null to override it with manual commands. // // setLayout(null); // may uncomment this line for appletviewer // Set the background color to off-white. setBackground(new Color(0xfaf0e6)); resize(800, 500); add(canvas = new MsuMap(msumap,selected)); canvas.move(43, 20); canvas.resize(600, 500); canvas.show(); add(choiceBox = new RorF_Choice()); // Use our own // removed lines of code were placed in the RorF_Choice constructor // may uncomment the next line if manual layout used for appletviewer // choiceBox.move(650, 49); choiceBox.show(); } } 9 // RorF_Choice.java (subclasses Choice) import java.applet.*; import java.awt.*; import java.net.*; public class RorF_Choice extends Choice { public RorF_Choice() { // call parent’s constructor first super(); // place the 3 lines of code from MyCanvas.java here // (use the implicit object “this” instead of “choiceBox”) } public boolean action( Event event, Object obj) { // cast the object argument to a String // print out the event and string now showing on menu System.out.println(“\nPut some message here.”); // return true if event handled successfully // return false if event should be passed to parent return super.action(event,obj); } } 10