import java.applet.Applet; import java.awt.*; import go.*; import goText.*; class Objects { GoVertex square; GoVertex cross; final static int YELLOW_SQUARE = 0; final static int GREEN_SQUARE = 1; final static int CROSS = 2; Objects() { square = new GoTriangleFan(4); cross = new GoLines(4); square.xyz(0, -1.0, -1.0, 0.0); square.xyz(1, 1.0, -1.0, 0.0); square.xyz(2, 1.0, 1.0, 0.0); square.xyz(3, -1.0, 1.0, 0.0); cross.xyz(0, -1.0, -1.0, 0.0); cross.xyz(1, 1.0, 1.0, 0.0); cross.xyz(2, -1.0, 1.0, 0.0); cross.xyz(3, 1.0, -1.0, 0.0); } void render(Go go) { // // Draw yellow square with black cross. // go.identity(); go.scale(0.4, 0.4, 4.0); go.translate(-1.3, 0.0, 0.0); go.name(YELLOW_SQUARE); go.color(1.0, 1.0, 0.0); go.render(square); go.push(Go.NAME); go.name(CROSS); go.color(0.0, 0.0, 0.0); go.render(cross); go.pop(Go.NAME); // // Draw green square with black cross. // go.identity(); go.scale(0.4, 0.4, 0.4); go.translate(1.3, 0.0, 0.0); go.name(GREEN_SQUARE); go.color(0.0, 1.0, 0.0); go.render(square); go.push(Go.NAME); go.name(CROSS); go.color(0.0, 0.0, 0.0); go.render(cross); go.pop(Go.NAME); } } class Select extends GoInterface { // Selectable objects. Objects objects; // Object selection text feedback. GoText selectionText; Select() { // // Setup the background. // go.background(0, 0, 0); // // Create the objects that can be selected. // objects = new Objects(); // // Init object selection text feedback. // selectionText = new GoText(0.0, 0.7, 0.0, // position 1.0, 0.0, // path 0.15, 0.15, // scale GoText.CENTER_CENTER, // alignment new GoHershey("hersheyFonts/meteorology"), // font "Select something with the mouse"); // text } // // Interface render method. // public void render() { // render objects go.clear(Go.IMAGE); objects.render(go); // render selectionText go.identity(); go.color(1.0, 1.0, 1.0); selectionText.render(go); // display swap(); } // // Interface resized method. // public void resized(int width, int height) { // // Setup projection matrix. // go.matrixMode(Go.PROJECTION); go.identity(); if(width > height) { go.ortho(-1.0 * width / height, 1.0 * width / height, -1.0, 1.0, -1.0, 1.0); } else { go.ortho(-1.0, 1.0, -1.0 * height / width, 1.0 * height / width, -1.0, 1.0); } go.matrixMode(Go.MODELVIEW); } // // Interface mousePressed method. // public void mousePressed(int x, int y) { int width = go.width(); int height = go.height(); // // Save projection matrix. // go.push(Go.PROJECTION); // // Setup projection matrix for select mode. // go.push(Go.MATRIX_MODE); go.matrixMode(Go.PROJECTION); go.identity(); go.select(x, height - y, 5.0, 5.0); if(width > height) { go.ortho(-1.0 * width / height, 1.0 * width / height, -1.0, 1.0, -1.0, 1.0); } else { go.ortho(-1.0, 1.0, -1.0 * height / width, 1.0 * height / width, -1.0, 1.0); } go.pop(Go.MATRIX_MODE); // // Render in select mode. // go.push(Go.RENDER_MODE); go.renderMode(Go.SELECTION); objects.render(go); go.pop(Go.RENDER_MODE); // // Restore projection matrix. // go.pop(Go.PROJECTION); // // Find out what was selected, based on the name stack. // boolean yellowSquareSelected = false; boolean greenSquareSelected = false; boolean crossSelected = false; for(GoSelection selection = go.selection(); selection != null; selection = selection.next()) { int stackSize = selection.stackSize(); for(int i = 0; i < stackSize; i++) { switch(selection.name(i)) { case Objects.YELLOW_SQUARE: yellowSquareSelected = true; break; case Objects.GREEN_SQUARE: greenSquareSelected = true; break; case Objects.CROSS: crossSelected = true; break; } } } // // Setup selectionText based on what was found. // selectionText.text(""); if(yellowSquareSelected) { if(crossSelected) { selectionText.text("Cross in yellow square"); } else { selectionText.text("Yellow square"); } } else if(greenSquareSelected) { if(crossSelected) { selectionText.text("Cross in green square"); } else { selectionText.text("Green square"); } } // // Print the selection. // rerender(); // // Clear for next time. // go.clear(Go.SELECTION); } } public class select extends Applet { public void init() { // // Fonts will be found relative to the html document // that includes this applet. // GoFont.path(getDocumentBase()); // // Create Select. // setLayout(new GridLayout(1, 1)); add(new Select()); } }