1  /*
  2   * File: ShowData.java
  3   * Author: Nancy McCracken
  4   * Converted to Java 1.1: October 30, 1997
  5   * Copyright: Northeast Parallel Architectures Center
  6   *            at Syracuse University 1997
  7   * 
  8   * This Java applet divides the applet area into two parts:  an area
  9   * with controls such as buttons and checkboxes, called controlPanelNorth,
 10   * and an area called dataPanel which is treated as a Canvas to display
 11   * a set of data points.  The dataPanel is a separate class.
 12   * 
 13   * The data points are read from a file which has the following format:
 14   *    an integer which is the number of data points
 15   *    a maximum x value - the horizontal display will cover 0 through this value
 16   *    a maximum y value - the vertical display will cover 0 through this value
 17   *    data points x y
 18   * 
 19   * This applet is intended to be a template for displaying a set of static
 20   * data from a file.  Each point is displayed in a very simple fashion, and
 21   * the only two controls provided is a choice box with 5 colors and a checkbox
 22   * group of 3 checkboxes controlling the shape of the point icon.
 23   *
 24   */
 25  
 26  import java.awt.*;
 27  import java.awt.event.*;
 28  import java.io.*;
 29  import java.net.*;
 30  
 31  public class ShowData extends java.applet.Applet
 32  			implements ItemListener
 33  {
 34    DataPanel dataPanel;		// the display panel, from the class below
 35    String inname = null;		// name of the data file
 36  
 37    Choice colorChoice;		// color control
 38    CheckboxGroup shape;		// shape controls
 39    Checkbox circleBox, squareBox, plusBox;
 40  
 41    public void init()
 42    { InputStream instream = null;
 43      Panel controlPanelNorth = new Panel();
 44  
 45      setLayout(new BorderLayout());
 46      
 47      // initialize the input stream and data panel
 48      try
 49        { inname = getParameter("inputfile");
 50          instream = new URL(getDocumentBase(), inname).openStream();
 51  	dataPanel = new DataPanel(instream);
 52         }
 53      catch (IOException e)
 54        { System.err.println
 55  	  ("I/O Exception:  probably bad filename or bad connection."
 56  	         + e.getMessage());
 57        }
 58  
 59    // make the control panel
 60    controlPanelNorth.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
 61  
 62    colorChoice = new Choice();
 63    colorChoice.addItem("green");
 64    colorChoice.addItem("cyan");
 65    colorChoice.addItem("blue");
 66    colorChoice.addItem("purple");
 67    colorChoice.addItem("magenta");
 68    colorChoice.addItemListener(this);
 69  
 70    shape = new CheckboxGroup();
 71    circleBox = new Checkbox("circle", shape, true);  
 72    squareBox = new Checkbox("square", shape, false);
 73    plusBox = new Checkbox("plus", shape, false);
 74    circleBox.addItemListener(this);
 75    squareBox.addItemListener(this);
 76    plusBox.addItemListener(this);
 77  
 78    controlPanelNorth.add(colorChoice);
 79    controlPanelNorth.add(circleBox);
 80    controlPanelNorth.add(squareBox);
 81    controlPanelNorth.add(plusBox);
 82  
 83    // add the two components to the applet area
 84    add("North", controlPanelNorth);
 85    add("Center", dataPanel);
 86    }
 87  
 88    public void update(Graphics g)
 89    { 
 90      dataPanel.repaint();
 91    }
 92  
 93    public void paint(Graphics g)
 94    { 
 95      dataPanel.repaint();
 96    }
 97  
 98    public void itemStateChanged(ItemEvent e)
 99    {
100    /* in response to activation of controls, call methods in the 
101  	DataPanel class that will change the appearance of the display */
102  
103      if (e.getSource() == colorChoice)
104        { 
105  	dataPanel.setColor(colorChoice.getSelectedIndex());
106  	repaint();
107        }
108      else // it was a checkbox
109        {
110  	dataPanel.setShape(shape.getSelectedCheckbox().getLabel());
111  	repaint();
112        }
113    }
114  }
115  
116  class DataPanel extends Panel
117  {
118    // variables to hold the data from the file
119    int numdata;
120    double maxx, maxy;
121    double xdata[], ydata[];
122  
123    // variables to scale the data to pixels
124    int maxxoffset, maxyoffset;
125    int xoffset[], yoffset[];
126    int xscale,yscale;
127  
128    InputStreamReader isr;
129    // these two variables control the current appearance of the data
130    Color currentColor;
131    String currentshape;
132  
133    // the set of allowed colors
134    Color ctable[] = new Color[5];
135  
136  
137    public DataPanel(InputStream instream)
138    {
139      try
140      {
141        isr = new InputStreamReader(instream);
142        StreamTokenizer intokens = new StreamTokenizer(isr);
143        intokens.parseNumbers();  intokens.slashSlashComments(true);
144  
145        /* read the number of data points and allocate data and pixel
146  	   offset arrays */    
147        int tok = intokens.nextToken();  
148        numdata = (int) intokens.nval;
149        xdata = new double[numdata]; ydata = new double[numdata];
150        xoffset = new int[numdata]; yoffset = new int[numdata];
151  
152        /* read maximum x and y data values and scale data from 0 to maxx
153   	to pixels to fit the window, and same for y */
154        tok = intokens.nextToken();
155        maxx = intokens.nval;
156        tok = intokens.nextToken();
157        maxy = intokens.nval;
158        maxxoffset = 600;
159        maxyoffset = 450;
160        xscale = (int) (maxxoffset/maxx);
161        yscale = (int) (maxyoffset/maxy);
162  
163        // read x,y data pairs
164        for (int i=0; i<numdata; i++)
165        { tok = intokens.nextToken();  xdata[i] = intokens.nval;
166          tok = intokens.nextToken();  ydata[i] = intokens.nval;
167        }
168  /* for debugging
169        for (int i=0; i<numdata; i++)
170        { System.out.println(xdata[i]);
171          System.out.println(ydata[i]);
172        }
173  */
174      }
175      catch (IOException e)
176        { System.err.println
177  	  ("I/O Exception:  Reading data from file"
178  	         + e.getMessage());
179        }
180    
181      // initialize current control variables and color table
182      currentColor = Color.green;
183      currentshape = "circle";
184      setBackground(Color.black);
185  
186      ctable[0] = Color.green;
187      ctable[1] = Color.cyan;
188      ctable[2] = Color.blue;
189      ctable[3] = new Color(127,0,255);
190      ctable[4] = Color.magenta;
191    }
192  
193  
194    public void setColor(int colorindex)
195    { currentColor = ctable[colorindex];
196    }
197  
198  
199    public void setShape(String selectedshape)
200    { currentshape = selectedshape;
201    }
202  
203  
204    public void paint(Graphics g)
205    {
206      g.setColor (currentColor);
207      if (currentshape.equals("circle"))
208        { // draw a circle for each point
209          for (int i=0; i<numdata; i++)
210          { g.fillOval((int)(xdata[i]*xscale), 
211  		maxyoffset - (int)(ydata[i]*yscale), 12, 12);
212          }
213        }
214      else if (currentshape.equals("square"))
215        { // draw a square for each point
216          for (int i=0; i<numdata; i++)
217          { g.fillRect((int)(xdata[i]*xscale), 
218  		maxyoffset - (int)(ydata[i]*yscale), 12, 12);
219          }
220        }
221      else if (currentshape.equals("plus"))
222        { //draw a plus for each point
223  	g.setFont(new Font("TimesRoman", Font.BOLD, 14));
224          for (int i=0; i<numdata; i++)
225          { g.drawString("+", (int)(xdata[i]*xscale), 
226  		maxyoffset - (int)(ydata[i]*yscale));
227          }
228        }
229    }
230  }