//Title: HopDemo //Version: 1.2 2000/1/22 //Copyright: Copyright (c) 2000 //Author: David Imai //Description: Draws a complex image using Barry Martin's // Hopalong algorithm. import java.awt.*; import java.applet.*; //import java.awt.event.*; public class HopDemo extends Applet { boolean isStandalone = false; String s1 = new String(); Panel paramPanel = new Panel(); BorderLayout borderLayout1 = new BorderLayout(); TextField atxt = new TextField(12); TextField btxt = new TextField(12); TextField ctxt = new TextField(12); Button newParamBtn = new Button(); Panel controlPanel = new Panel(); Label albl = new Label(); Label blbl = new Label(); Label clbl = new Label(); Button zoomInBtn = new Button(); Button newImageBtn = new Button(); Button zoomOutBtn = new Button(); Button changeBtn = new Button(); Double tempD = new Double(1.0); HopCanvas picture = new HopCanvas(2.5, 0.5, 3.2); FlowLayout flowLayout1 = new FlowLayout(); FlowLayout flowLayout2 = new FlowLayout(); FlowLayout flowLayout3 = new FlowLayout(); //Initialize the applet public void init() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { this.setLayout(borderLayout1); controlPanel.setLayout(flowLayout2); this.add("North", controlPanel); // this.add(controlPanel, BorderLayout.NORTH);//Java 1.1 controlPanel.add(newImageBtn); controlPanel.add(zoomInBtn); controlPanel.add(zoomOutBtn); controlPanel.add(changeBtn); controlPanel.add(newParamBtn); zoomInBtn.setLabel("Zoom In"); zoomOutBtn.setLabel("Zoom Out"); changeBtn.setLabel("Change Color"); newImageBtn.setLabel("New Image"); newParamBtn.setLabel("Change a, b, c"); this.add("Center", picture); // this.add(picture, BorderLayout.CENTER); //Java 1.1 this.add("South", paramPanel); // this.add(paramPanel, BorderLayout.SOUTH); //Java 1.1 paramPanel.setForeground(Color.black); paramPanel.setBackground(Color.white); paramPanel.setLayout(flowLayout3); paramPanel.add(albl); paramPanel.add(atxt); paramPanel.add(blbl); paramPanel.add(btxt); paramPanel.add(clbl); paramPanel.add(ctxt); albl.setBackground(Color.white); albl.setText(" a ="); atxt.setBackground(Color.white); blbl.setBackground(Color.white); blbl.setText(" b ="); btxt.setBackground(Color.white); clbl.setBackground(Color.white); clbl.setText(" c ="); ctxt.setBackground(Color.white); picture.repaint(); showParam(); } //Get Applet information public String getAppletInfo() {return "Applet Information"; } //Display parameters in text boxes public void showParam() { Double d1; d1 = new Double(picture.geta()); s1 = d1.toString(); atxt.setText(s1); d1 = new Double(picture.getb()); s1 = d1.toString(); btxt.setText(s1); d1 = new Double(picture.getc()); s1 = d1.toString(); ctxt.setText(s1); } //Check button presses public boolean action(Event e, Object o) { if (e.target == newImageBtn) // New random image { picture.setparam(); showParam(); picture.repaint(); return(true); } else if (e.target == zoomInBtn) // Zoom in { picture.setdiam(picture.getdiam()/2.0d); picture.repaint(); return(true); } else if (e.target == zoomOutBtn) // Zoom out { picture.setdiam(picture.getdiam()*2.0d); picture.repaint(); return(true); } else if (e.target == changeBtn) // Change background { if ((picture.getbgcolor()).equals(Color.black)) { picture.setbgcolor(Color.white);} else { picture.setbgcolor(Color.black); } picture.repaint(); return(true); } else if (e.target == newParamBtn) // Change a,b,c // redraw picture using values in text fields { tempD = Double.valueOf(atxt.getText()); picture.seta(tempD.doubleValue()); tempD = Double.valueOf(btxt.getText()); picture.setb(tempD.doubleValue()); tempD = Double.valueOf(ctxt.getText()); picture.setc(tempD.doubleValue()); picture.repaint(); return(true); } else return(false); } //end action } //end class HopDemo //================================== class HopCanvas extends Canvas { int scrWidth = 480; //width of image in pixels double a, b, c; //parameters that determine shape double diam; //area displayed float hue; //color of dot int numDots = 2400; //number of dots of each color Color bgcolor = Color.black; // background color String tempS = new String(); //constructor public HopCanvas(double a0, double b0, double c0) { a = a0; b = b0; c = c0; diam = Math.sqrt(Math.abs(a*b*c))*32; //width of area displayed } public void setnumDots(int nd) { numDots = nd;} public double geta() { return a;} public void seta(double aa) { a = aa;} public double getb() { return b;} public void setb(double bb) { b = bb;} public double getc() { return c;} public void setc(double cc) { c = cc;} public double getdiam() { return diam;} public void setdiam(double d) { diam = d;} public Color getbgcolor() { return bgcolor;} public void setbgcolor(Color bc) { bgcolor = bc;} public void setparam() { //choose values of parameters a,b,c a = 8*(Math.random()-0.5); b = 4*(Math.random()-0.5); c = 8*(Math.random()-0.5); diam = Math.sqrt(Math.abs(a*b*c))*32; //width of area displayed } public void paint(Graphics g) { double x0, y0; //bottom right corner coordinates double x,y,xx; //for plotting points int u, v; //position of dot double pixunit; //pixels per unit length pixunit = ((double)scrWidth)/diam; //pixels per unit length x = 0; y = 0; x0 = -0.5 * diam; y0 = 0.5 * (a + diam); //center of screen is at (0,a/2). u = 0; v = 0; g.setColor(bgcolor); g.fillRect(0,0,scrWidth,scrWidth); for(int colorNum=0; colorNum<32; colorNum++) // draw 32 different colors { hue = ((float)colorNum)/32f; g.setColor(Color.getHSBColor(hue,1f,1f)); //draw numDots of this color. for(int i=1; i= 0.0) return 1.0; else return -1.0; } // end sign } // end class HopCanvas