1 /* 2 * File : ShowData.java 3 * Author : Nancy McCracken 4 * Created : Wed Sep 24 17:24:48 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 import java.awt.*; 26 import java.io.*; 27 import java.net.*; 28 29 public class ShowData extends java.applet.Applet 30 { 31 DataPanel dataPanel; // the display panel, from the class below 32 String inname = null; // name of the data file 33 34 Choice colorChoice; // color control 35 CheckboxGroup shape; // shape controls 36 Checkbox circleBox, squareBox, plusBox; 37 38 public void init() 39 { InputStream instream = null; 40 Panel controlPanelNorth = new Panel(); 41 42 setLayout(new BorderLayout()); 43 44 // initialize the input stream and data panel 45 try 46 { inname = getParameter("inputfile"); 47 instream = new URL(getDocumentBase(), inname).openStream(); 48 dataPanel = new DataPanel(instream); 49 } 50 catch (IOException e) 51 { System.err.println 52 ("I/O Exception: probably bad filename or bad connection." 53 + e.getMessage()); 54 } 55 56 // make the control panel 57 controlPanelNorth.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 58 59 colorChoice = new Choice(); 60 colorChoice.addItem("green"); 61 colorChoice.addItem("cyan"); 62 colorChoice.addItem("blue"); 63 colorChoice.addItem("purple"); 64 colorChoice.addItem("magenta"); 65 66 shape = new CheckboxGroup(); 67 circleBox = new Checkbox("circle", shape, true); 68 squareBox = new Checkbox("square", shape, false); 69 plusBox = new Checkbox("plus", shape, false); 70 71 controlPanelNorth.add(colorChoice); 72 controlPanelNorth.add(circleBox); 73 controlPanelNorth.add(squareBox); 74 controlPanelNorth.add(plusBox); 75 76 // add the two components to the applet area 77 add("North", controlPanelNorth); 78 add("Center", dataPanel); 79 } 80 81 public void update(Graphics g) 82 { 83 dataPanel.repaint(); 84 } 85 86 public void paint(Graphics g) 87 { 88 dataPanel.repaint(); 89 } 90 91 public boolean handleEvent(Event e) 92 { 93 /* in response to activation of controls, call methods in the 94 DataPanel class that will change the appearance of the display */ 95 96 if (e.target instanceof Choice) 97 { 98 dataPanel.setColor(colorChoice.getSelectedIndex()); 99 repaint(); 100 return true; 101 } 102 else if (e.target instanceof Checkbox) 103 { 104 dataPanel.setShape(shape.getCurrent().getLabel()); 105 repaint(); 106 return true; 107 } 108 else 109 return false; 110 } 111 } 112 113 class DataPanel extends Panel 114 { 115 // variables to hold the data from the file 116 int numdata; 117 double maxx, maxy; 118 double xdata[], ydata[]; 119 120 // variables to scale the data to pixels 121 int maxxoffset, maxyoffset; 122 int xoffset[], yoffset[]; 123 int xscale,yscale; 124 125 // these two variables control the current appearance of the data 126 Color currentColor; 127 String currentshape; 128 129 // the set of allowed colors 130 Color ctable[] = new Color[5]; 131 132 133 public DataPanel(InputStream instream) 134 { 135 try 136 { 137 StreamTokenizer intokens = new StreamTokenizer(instream); 138 intokens.parseNumbers(); intokens.slashSlashComments(true); 139 140 /* read the number of data points and allocate data and pixel 141 offset arrays */ 142 int tok = intokens.nextToken(); 143 numdata = (int) intokens.nval; 144 xdata = new double[numdata]; ydata = new double[numdata]; 145 xoffset = new int[numdata]; yoffset = new int[numdata]; 146 147 /* read maximum x and y data values and scale data from 0 to maxx 148 to pixels to fit the window, and same for y */ 149 tok = intokens.nextToken(); 150 maxx = intokens.nval; 151 tok = intokens.nextToken(); 152 maxy = intokens.nval; 153 maxxoffset = 600; 154 maxyoffset = 450; 155 xscale = (int) (maxxoffset/maxx); 156 yscale = (int) (maxyoffset/maxy); 157 158 // read x,y data pairs 159 for (int i=0; i<numdata; i++) 160 { tok = intokens.nextToken(); xdata[i] = intokens.nval; 161 tok = intokens.nextToken(); ydata[i] = intokens.nval; 162 } 163 /* for debugging 164 for (int i=0; i<numdata; i++) 165 { System.out.println(xdata[i]); 166 System.out.println(ydata[i]); 167 } 168 */ 169 } 170 catch (IOException e) 171 { System.err.println 172 ("I/O Exception: Reading data from file" 173 + e.getMessage()); 174 } 175 176 // initialize current control variables and color table 177 currentColor = Color.green; 178 currentshape = "circle"; 179 setBackground(Color.black); 180 181 ctable[0] = Color.green; 182 ctable[1] = Color.cyan; 183 ctable[2] = Color.blue; 184 ctable[3] = new Color(127,0,255); 185 ctable[4] = Color.magenta; 186 } 187 188 189 public void setColor(int colorindex) 190 { currentColor = ctable[colorindex]; 191 } 192 193 194 public void setShape(String selectedshape) 195 { currentshape = selectedshape; 196 } 197 198 199 public void paint(Graphics g) 200 { 201 g.setColor (currentColor); 202 if (currentshape.equals("circle")) 203 { // draw a circle for each point 204 for (int i=0; i<numdata; i++) 205 { g.fillOval((int)(xdata[i]*xscale), 206 maxyoffset - (int)(ydata[i]*yscale), 12, 12); 207 } 208 } 209 else if (currentshape.equals("square")) 210 { // draw a square for each point 211 for (int i=0; i<numdata; i++) 212 { g.fillRect((int)(xdata[i]*xscale), 213 maxyoffset - (int)(ydata[i]*yscale), 12, 12); 214 } 215 } 216 else if (currentshape.equals("plus")) 217 { //draw a plus for each point 218 g.setFont(new Font("TimesRoman", Font.BOLD, 14)); 219 for (int i=0; i<numdata; i++) 220 { g.drawString("+", (int)(xdata[i]*xscale), 221 maxyoffset - (int)(ydata[i]*yscale)); 222 } 223 } 224 } 225 }