import java.applet.Applet; import java.awt.*; import java.awt.event.*; import graphs.JSuperSlider; import java.net.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; public class Plota extends JApplet implements Runnable { private Thread m_Runner; private int m_pszX; private int m_pszY; private boolean m_bInvalid; private javax.swing.JPanel m_Board; private PanelA m_PanelA; private final int NUMPOINTS = 1000; public int m_NumPoints; //public double m_X[]; //public double m_Y[]; public Vector m_Xs; public Vector m_Ys; private final double start_value = 0.001491; // pick something unique that's not likely to be an actual value //private final double tolerance = 0.00001; // used for comparing double values public double m_Xmin_orig=start_value; public double m_Xmax_orig=start_value; public double m_Ymin_orig=start_value; public double m_Ymax_orig=start_value; public double m_Xmin=start_value; public double m_Xmax=start_value; public double m_Ymin=start_value; public double m_Ymax=start_value; public int eqNum; public String yAxisImgName=""; public String xAxisImgName=""; public Image xAxisImg,yAxisImg; public String m_strMessage; protected JSuperSlider[] jSlider = new JSuperSlider[6]; String[] params = new String[6]; Hashtable[] labelTable = new Hashtable[6]; javax.swing.JPanel jPanel, innerPanel; public void init() { eqNum = -1; try { eqNum = Integer.parseInt(getParameter("eqNum")); } catch(Exception e){ e.printStackTrace(); } resize(m_pszX, m_pszY); setBackground(Color.blue); this.getContentPane().setLayout(new BorderLayout()); m_Board = new javax.swing.JPanel(); m_PanelA = new PanelA(this); jPanel = new javax.swing.JPanel(); m_PanelA.setBackground(Color.cyan); m_PanelA.addMouseListener(mListener); // to listen for mouse clicks m_Board.add("Center", m_PanelA); this.getContentPane().add("Center", m_Board); JPanel slidersAndLegend = new JPanel(); /* slidersAndLegend.add(getOptionsPanel(), BorderLayout.WEST); slidersAndLegend.add(new JLabel(" "), BorderLayout.CENTER); javax.swing.JComponent legend = getLegend(); if (legend != null) { slidersAndLegend.add(legend, BorderLayout.EAST); }*/ GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); slidersAndLegend.setLayout(gridbag); c.fill = GridBagConstraints.HORIZONTAL; javax.swing.JPanel optionsPanel = getOptionsPanel(); c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.NORTH; gridbag.setConstraints(optionsPanel, c); slidersAndLegend.add(optionsPanel); JLabel blank = new JLabel(" "); c.gridx = 1; c.gridy = 0; gridbag.setConstraints(blank, c); slidersAndLegend.add(blank); javax.swing.JComponent legend = getLegend(); if (legend != null) { c.gridx = 2; c.gridy = 0; c.anchor = GridBagConstraints.NORTH; gridbag.setConstraints(legend, c); slidersAndLegend.add(legend); } //m_Board.add(getOptionsPanel(), BorderLayout.SOUTH); m_Board.add(slidersAndLegend, BorderLayout.SOUTH); javax.swing.Timer loader = new javax.swing.Timer(1, new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SetParam((double)jSlider[0].getValue()/1000,(double)jSlider[1].getValue()/1000,(double)jSlider[2].getValue()/1000,(double)jSlider[3].getValue()/1000,(double)jSlider[4].getValue()/1000,(double)jSlider[5].getValue()/1000); } }); loader.setRepeats(false); loader.setCoalesce(true); loader.start(); } public void start() { if(m_Runner == null) { m_Runner = new Thread(this); m_Runner.start(); } m_PanelA.start(); } public void stop() { if(m_Runner != null) { m_Runner.stop(); m_Runner = null; } m_PanelA.stop(); } //public void getData(int n, double x[], double y[]) { public void getData(int n, Vector xs, Vector ys) { m_NumPoints = n; if (!fromZoom) { // set the hard min & max if this is the first time through System.out.println("Setting hard min & max on the first time through"); m_Xmin = m_Xmax = ( (double[])xs.elementAt(0) )[0]; m_Ymin = m_Ymax = ( (double[])ys.elementAt(0) )[0]; System.out.println("There are a total of [" + xs.size() + "] graphs to look at."); for (int graphnum = 0; graphnum < xs.size(); graphnum++) { System.out.println("Looking at graphnum #" + graphnum); double x[] = (double[])xs.elementAt(graphnum); double y[] = (double[])ys.elementAt(graphnum); for (int i = 0; i < m_NumPoints; i++) { //m_X[i] = x[i]; //m_Y[i] = y[i]; if (x==null) { System.out.println("X WAS NULL"); }; if(x[i] < m_Xmin) m_Xmin = x[i]; if(x[i] > m_Xmax) m_Xmax = x[i]; if(y[i] < m_Ymin) m_Ymin = y[i]; if(y[i] > m_Ymax) m_Ymax = y[i]; } } } /*else { // otherwise, just update the points and not the min & max System.out.println("Updating points but not min & max"); */ /* for (int i=0; i1)) { b[n++]=a.substring(0,i); a=a.substring(i+1); i=-1; } } } public void updateGraphFromSlider() { SetParam((double)jSlider[0].getValue()/1000,(double)jSlider[1].getValue()/1000,(double)jSlider[2].getValue()/1000,(double)jSlider[3].getValue()/1000,(double)jSlider[4].getValue()/1000,(double)jSlider[5].getValue()/1000); updateValueLabels((double)jSlider[0].getValue()/1000,(double)jSlider[1].getValue()/1000,(double)jSlider[2].getValue()/1000,(double)jSlider[3].getValue()/1000,(double)jSlider[4].getValue()/1000,(double)jSlider[5].getValue()/1000); } public javax.swing.JComponent getLegend() { //JPanel ret = new javax.swing.JPanel(new GridLayout(10,0)); JPanel ret = new javax.swing.JPanel(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); ret.setLayout(gridbag); c.fill = GridBagConstraints.VERTICAL; JLabel leglabel = new JLabel("Legend"); c.gridx = 0; c.gridy = 0; gridbag.setConstraints(leglabel, c); ret.add(leglabel); //ret.add(new JLabel("Legend")); boolean noLegend = false; try{ // we want to create a Vector of JLabels for the Legend // get the regular Equation labels -------------------------------------- // URL addr=new URL(this.getCodeBase().getProtocol(), this.getCodeBase().getHost(), this.getCodeBase().getPort(), this.getCodeBase().getPath()+"Equations.txt"); DataInputStream din = new DataInputStream(addr.openStream()); int i=isNumber(din.readLine()); while ((i!=eqNum)||(i==-1)) { i=isNumber(din.readLine()); } String regline1 = din.readLine(); String regline2 = din.readLine(); Vector equations = new Vector(); while ( ((i!=(eqNum+1))||(i==-1)) && (din.available()>0)) { String toadd = din.readLine(); i = isNumber(toadd); equations.add(toadd); } int last_element_index = equations.size()-1; // If it's at the end of the file, take the last value added to the array if (din.available()>0) { last_element_index--; } Vector LegendLabels = new Vector(); for (int graphnum = 0; graphnum <= last_element_index; graphnum++) { JLabel newLabel = new JLabel("---: "+(String)equations.elementAt(graphnum)); newLabel.setForeground(LineColors.getColor(graphnum)); LegendLabels.add(newLabel); } System.out.println("LegendLabels size is now: " + LegendLabels.size()); // use any custom Legend labels ----------------------------------------- // URL addr2=new URL(this.getCodeBase().getProtocol(), this.getCodeBase().getHost(), this.getCodeBase().getPort(), this.getCodeBase().getPath()+"Params.txt"); DataInputStream din2 = new DataInputStream(addr2.openStream()); int i2=isNumber(din2.readLine()); while ((i2!=eqNum)||(i2==-1)) i2=isNumber(din2.readLine()); for (int b=0;b<6;b++) params[b]=din2.readLine(); xAxisImgName=din2.readLine().substring(13);System.out.println(xAxisImgName); yAxisImgName=din2.readLine().substring(13);System.out.println(yAxisImgName); while ( ((i2!=(eqNum+1))||(i2==-1)) && (din2.available()>0)) { String toadd = din2.readLine(); i2 = isNumber(toadd); System.out.println("To add: [" + toadd + "]"); if (toadd.length() > 1) { // so it's not "3" for instance System.out.println("Comparing [" + toadd.substring(0,8).toLowerCase() + "]"); if (toadd.substring(0,8).toLowerCase().equals("nolegend")) { noLegend = true; } else { String imgname = toadd.substring(10); String whichY = toadd.substring(1,2); int whichYint = Integer.parseInt(whichY); whichYint--; // so y1 is equation 0 JLabel newLabel = new JLabel("---: "); newLabel.setForeground(LineColors.getColor(whichYint)); Image img = this.getImage(getClass().getResource(imgname)); JPanel graphdesc = new JPanel(); //graphdesc.setLayout(new GridLayout(0,2)); graphdesc.add(newLabel); graphdesc.add(new JLabel(new ImageIcon(img))); LegendLabels.setElementAt(graphdesc, whichYint); } } } // create the Labels --------------------------------------------------- // for (int graphnum = 0; graphnum < LegendLabels.size(); graphnum++) { //ret.add((JComponent) (LegendLabels.elementAt(graphnum)) ); JComponent toadd = (JComponent)(LegendLabels.elementAt(graphnum)); c.gridx = 0; c.gridy = graphnum + 1; c.anchor = GridBagConstraints.WEST; gridbag.setConstraints(toadd,c); ret.add(toadd); } } catch (Exception e) {e.printStackTrace();} if (noLegend) { return null; } else { return ret; // the legend } } public javax.swing.JPanel getOptionsPanel() { innerPanel = new javax.swing.JPanel(new GridLayout(6,0)); try{ URL addr=new URL(this.getCodeBase().getProtocol(), this.getCodeBase().getHost(), this.getCodeBase().getPort(), this.getCodeBase().getPath()+"Params.txt"); DataInputStream din = new DataInputStream(addr.openStream()); int i=isNumber(din.readLine()); while ((i!=eqNum)||(i==-1)) i=isNumber(din.readLine()); for (int b=0;b<6;b++) params[b]=din.readLine(); xAxisImgName=din.readLine().substring(13);System.out.println(xAxisImgName); yAxisImgName=din.readLine().substring(13);System.out.println(yAxisImgName); } catch (Exception e) {e.printStackTrace();} jPanel.setBackground(Color.red); innerPanel.setSize(510,320); ChangeListener listener = new ChangeListener(){ public void stateChanged(ChangeEvent event){ System.out.println("Slider state change."); System.out.println("before change XMax is: [" + m_Xmax + "], XMin is: [" + m_Xmin + "]"); updateGraphFromSlider(); System.out.println("after change XMax is: [" + m_Xmax + "], XMin is: [" + m_Xmin + "]"); } }; for (int i=0; i=2)) { if (evt.isControlDown()) {zoomXIn();System.out.println("Zooming x in");} else if (evt.isShiftDown()) {zoomYIn();System.out.println("Zooming y in");} else zoomIn(); } else if (evt.getButton()==3) { System.out.println(evt.getModifiers()); if (evt.isControlDown()) {zoomXOut();System.out.println("ZOOMING X OUT");} else if (evt.isShiftDown()) {zoomYOut();System.out.println("ZOOMING Y OUT");} else zoomOut(); } else if (evt.getButton()==2) {restoreDefaults();repaint();} } }; public boolean fromZoom = false; // this variable isn't used yet. (it's from EqEditor, might be useful later) public void restoreDefaults() { m_Xmin = m_Xmin_orig; m_Xmax = m_Xmax_orig; m_Ymin = m_Ymin_orig; m_Ymax = m_Ymax_orig; updateGraphFromSlider(); repaint(); } public void zoomIn(){ System.out.println("Zoom In"); fromZoom=true; m_Xmax = m_Xmax / 2; m_Xmin = m_Xmin / 2; m_Ymax = m_Ymax / 2; m_Ymin = m_Ymin / 2; updateGraphFromSlider(); repaint(); } public void zoomOut() { System.out.println("Zoom Out"); fromZoom=true; m_Xmax = m_Xmax * 2; m_Xmin = m_Xmin * 2; m_Ymax = m_Ymax * 2; m_Ymin = m_Ymin * 2; updateGraphFromSlider(); repaint(); } public void zoomXIn() { System.out.println("Zoom X In"); fromZoom=true; m_Xmax = m_Xmax / 2; m_Xmin = m_Xmin / 2; updateGraphFromSlider(); repaint(); } public void zoomXOut() { System.out.println("Zoom X Out"); fromZoom=true; m_Xmax = m_Xmax * 2; m_Xmin = m_Xmin * 2; updateGraphFromSlider(); repaint(); } public void zoomYIn() { System.out.println("Zoom Y In"); fromZoom=true; m_Ymax = m_Ymax / 2; m_Ymin = m_Ymin / 2; updateGraphFromSlider(); repaint(); } public void zoomYOut() { System.out.println("Zoom Y Out"); fromZoom=true; m_Ymax = m_Ymax * 2; m_Ymin = m_Ymin * 2; updateGraphFromSlider(); repaint(); } }