import java.awt.*; import java.applet.Applet; public class SnowflakeApplet extends Applet implements Runnable { GraphCanvas3 tafel; ButtonControl3 buttonPanel; Thread runner; public void init() { setBackground(Color.white); GridBagLayout gridbag = new GridBagLayout(); setLayout(gridbag); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.weighty = 0.0; LabelPanel l = new LabelPanel(" Click on the part of the curve you want to zoom in on:"); gridbag.setConstraints(l,c); add(l); c.fill = GridBagConstraints.BOTH; c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.weighty = 20.0; c.anchor = GridBagConstraints.WEST; tafel = new GraphCanvas3(); gridbag.setConstraints(tafel,c); add(tafel); c.gridheight = GridBagConstraints.REMAINDER; c.weightx = 0.0; c.weighty = 0.0; buttonPanel = new ButtonControl3(); gridbag.setConstraints(buttonPanel,c); add(buttonPanel); buttonPanel.setCanvas(tafel); tafel.setButtonPanel(buttonPanel); } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void run() { while (true) { repaint(); try {Thread.sleep(1000);} catch (InterruptedException e) {} } } } class stackFrame3 { double x0, y0, x1, y1, x2, y2, x3, y3, x4, y4, xc, yc, vsc, hsc; int seg10, seg11, seg20, seg21, seg30, seg31, cen, cbranch; boolean tripartite; } class LabelPanel extends Panel { LabelPanel(String s) { setLayout(new BorderLayout()); Label l = new Label(s, Label.LEFT); add("Center", l); } } class GraphCanvas3 extends Canvas { private final int arraysize = 1024; private final int maxStackFrames = 10; private final double vhratio = 1; private double sqrt32 = 0; private double[] x,y; private double vscale, hscale; private int h_prev=0,v_prev=0; private double x_cent, y_cent; private boolean tripartite = false; private ButtonControl3 gbuttonPanel; private stackFrame3[] stackFrames; private int stackFrameIndex; private int center, cen_branch; private int[] segStart, segEnd; GraphCanvas3() { x = new double[arraysize+1]; y = new double[arraysize+1]; segStart = new int[3]; segEnd = new int[3]; stackFrames = new stackFrame3[maxStackFrames]; for (int i=0; i=1; q/=4) { for (int p=1; p=1; q/=4) { for (int p=1; p=64) || tripartite)) j -= 32; else if ((i%64 >48) && ((jarraysize) { segStart[temp] = arraysize-32; segEnd[temp] = arraysize; segStart[(temp+2)%3] = 0; segEnd[(temp+2)%3] = 32; } else { segStart[temp] = j; segEnd[temp] = j+64; } } else { segStart[0] = j; segEnd[0] = j+64; } center = i; cen_branch = temp; pushStackFrame(); for (k=0; k<3; k++) { segStart[k] = -1; segEnd[k] = -1; } center = -1; cen_branch = -1; c[0] = x[i]; c[1] = y[i]; ConvertCoords2(c, temp); x_cent = c[0]; y_cent = c[1]; tripartite = false; if (j<0) { c[0] = x[j+arraysize]; c[1] = y[j+arraysize]; ConvertCoords2(c, (temp+1)%3); } else { c[0] = x[j]; c[1] = y[j]; ConvertCoords2(c, temp); } x0 = c[0]; y0 = c[1]; if (j+64>arraysize) { c[0] = x[j+64-arraysize]; c[1] = y[j+64-arraysize]; ConvertCoords2(c, (temp+2)%3); } else { c[0] = x[j+64]; c[1] = y[j+64]; ConvertCoords2(c, temp); } x4 = c[0]; y4 = c[1]; if ( j%64 == 0) InitArray(x0, y0, x4, y4); else { if (j<0) { c[0] = x[j+16+arraysize]; c[1] = y[j+16+arraysize]; ConvertCoords2(c, (temp+1)%3); } else { c[0] = x[j+16]; c[1] = y[j+16]; ConvertCoords2(c, temp); } x1 = c[0]; y1 = c[1]; c[0] = x[j+32]; c[1] = y[j+32]; ConvertCoords2(c, temp); x2 = c[0]; y2 = c[1]; if (j+64>arraysize) { c[0] = x[j+48-arraysize]; c[1] = y[j+48-arraysize]; ConvertCoords2(c, (temp+2)%3); } else { c[0] = x[j+48]; c[1] = y[j+48]; ConvertCoords2(c, temp); } x3 = c[0]; y3 = c[1]; InitArray(x0, y0, x1, y1, x2, y2, x3, y3, x4, y4); } gbuttonPanel.grayoutZoom(false); repaint(); return true; } public void doZoomOut() { if (!popStackFrame()) return; InitArray(x[0],y[0],x[arraysize/4],y[arraysize/4],x[arraysize/2],y[arraysize/2], x[3*arraysize/4],y[3*arraysize/4],x[arraysize],y[arraysize]); if (stackFrameIndex == -1) gbuttonPanel.grayoutZoom(true); repaint(); } public void paint(Graphics g) { int i, rotation; Point p = new Point(0,0); setScaling(x[64], y[64], x[arraysize-64], y[arraysize-64]); g.setColor(getForeground()); if (tripartite) { for (rotation=3; rotation>=1; rotation--) for (i=0; i<=arraysize; i++) { if (i == segStart[rotation%3]) g.setColor(Color.blue); else if (i == segEnd[rotation%3]) g.setColor(getForeground()); else if ((i == center) && (rotation%3 == cen_branch)) { ConvertCoordinates(x[i], y[i], p, rotation); g.drawOval(p.x-2, p.y-2, 5, 5); } plot(g, x[i], y[i], i==0, rotation); } } else for (i=64; i<=arraysize-64; i++) { if (i == segStart[0]) g.setColor(Color.blue); else if (i == segEnd[0]) g.setColor(getForeground()); else if (i == center) { ConvertCoordinates(x[i], y[i], p, 0); g.drawOval(p.x-2, p.y-2, 5, 5); } plot(g, x[i], y[i], i==64, 0); } } } class ButtonControl3 extends Panel{ private GraphCanvas3 gCanvas; private Button zoomoutButton; private boolean zoomDisabled = true; ButtonControl3() { setLayout(new FlowLayout(FlowLayout.CENTER,25,5)); Button button = new Button("Reset"); add(button); zoomoutButton = new Button("Zoom Out"); add(zoomoutButton); } public void setCanvas(GraphCanvas3 theCanvas) { gCanvas = theCanvas; } public void grayoutZoom(boolean grayout) { zoomDisabled = grayout; zoomoutButton.enable(!grayout); repaint(); } public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { if (((String)arg).equals("Reset")) gCanvas.reinit(); if (((String)arg).equals("Zoom Out") && !zoomDisabled) gCanvas.doZoomOut(); return true; } else return false; } }