//GDj NCAR/MMM 9/6/01 /*********************************************************************** * This is a java applet whose purpose is to load radar images upon user * request. It is to be used on the ABFM web page and will eliminate * the need to constantly be clicking back and clicking on the next * image on ones web browser. The AWT was used due to the fact that * users must download a plugin to interact with Swing applets. ***********************************************************************/ import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.applet.*; import java.io.*; import java.net.*; import java.lang.Number.*; import java.util.StringTokenizer; public class LoadPics extends Applet implements ItemListener, ActionListener, Runnable { static Image img; //ImageController remoteControl; static URL theURL; static URL myURL; static BufferedReader data; static List img_lst; static String curr_date; static String[] filenames; static Panel controls; static Panel imagePane; static Panel imageNamesPane; static Button play; static Button stop; static Button faster; static Button slower; static Button backOne; static Button forwardOne; static Button reset; static String sepChar; public void init() { //curr_date = getParameter("dateToDisplay"); String urlArgs = getDocumentBase().toString(); int begIndex = urlArgs.indexOf("="); begIndex++; curr_date = urlArgs.substring(begIndex); // get file separator to descend into subdirectories //sepChar = System.getProperty("file.separator"); sepChar = "/"; setBackground(Color.white); // get filename containing radar image dates try { this.theURL = new URL(getCodeBase(), "tmp"); } catch (MalformedURLException e) { System.out.println("Bad URL: " + theURL); } int select = -1; // create a list and read in the dates to the list img_lst = new List(20, true); try { InputStream in = theURL.openStream(); data = new BufferedReader(new InputStreamReader(in)); String line; img_lst.add("Choose The Desired Date"); int i = 1; while ((line = data.readLine()) != null) { img_lst.add(line); if (line.equals(curr_date)) select = i; i++; } } catch (IOException e) {} setLayout(new BorderLayout()); img_lst.addItemListener(this); add(img_lst, "East"); controls = new Panel(new FlowLayout()); play = new Button("Play"); play.setBackground(Color.white); stop = new Button("Stop"); stop.setBackground(Color.white); faster = new Button("Faster"); faster.setBackground(Color.white); slower = new Button("Slower"); slower.setBackground(Color.white); forwardOne = new Button("Forward One"); forwardOne.setBackground(Color.white); backOne = new Button("Back One"); backOne.setBackground(Color.white); reset = new Button("Reset Speed"); reset.setBackground(Color.white); play.addActionListener(this); stop.addActionListener(this); faster.addActionListener(this); slower.addActionListener(this); forwardOne.addActionListener(this); backOne.addActionListener(this); reset.addActionListener(this); controls.add(play); controls.add(stop); controls.add(faster); controls.add(slower); controls.add(backOne); controls.add(forwardOne); controls.add(reset); controls.setBackground(Color.blue); add(controls, "North"); //imagePane = new Panel(new FlowLayout()); //add(imagePane, "Center"); if (select > -1) { BufferedReader data1; String urlstr = curr_date; try { this.myURL = new URL(getCodeBase(), urlstr); } catch (MalformedURLException event) { System.out.println("Bad URL: " + myURL); } img_lst.removeAll(); try { InputStream in = myURL.openStream(); data1 = new BufferedReader(new InputStreamReader(in)); img_lst.add("Choose Another Date"); //img_lst.add("4km Layer Only"); //img_lst.add("7km Layer Only"); //img_lst.add("10km Layer Only"); String line; while ((line = data1.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, "<"); int fake_picture = 0; while (st.hasMoreTokens()) { String currToken = st.nextToken(); int end_index; if (((end_index = currToken.indexOf(".jpg")) != -1) || ((end_index = currToken.indexOf(".jpeg")) != -1) || ((end_index = currToken.indexOf(".gif")) != -1)) { if (fake_picture != 0) { img_lst.add(currToken.substring(8,end_index+4)); } else fake_picture++; } } } } catch (IOException exc) {} } } // make the controls for image animation public void makeControls() { /* controls = new Panel(new FlowLayout()); play = new Button("Play"); play.addActionListener(this); stop = new Button("Stop"); stop.addActionListener(this); controls.add(play); controls.add(stop); add(controls, "South"); */ } public void actionPerformed (ActionEvent acevt) { String comm = acevt.getActionCommand(); // start animation if play is pushed if (comm.equalsIgnoreCase("play")) { int[] i = img_lst.getSelectedIndexes(); if (i.length == 0) filenames = img_lst.getItems(); else filenames = img_lst.getSelectedItems(); //filenames = img_lst.getItems(); startLoop(); // remoteControl = new ImageController (filenames, curr_date); } // speed up animation if faster is pushed else if (comm.equalsIgnoreCase("faster")) { if (speed > 50) speed -= 50; } // slow down animation if slower is pushed else if (comm.equalsIgnoreCase("slower")) { if (speed < 1500) speed += 50; } // forward one image else if (comm.equalsIgnoreCase("forward one")) { if (imgIndex >= (layerImages.length - 1)) { imgIndex = 0; img = layerImages[imgIndex]; } else { imgIndex++; img = layerImages[imgIndex]; } repaint(); } // back one image else if (comm.equalsIgnoreCase("back one")) { if (imgIndex == 0) { imgIndex = layerImages.length - 1; img = layerImages[imgIndex]; } else { imgIndex--; img = layerImages[imgIndex]; } repaint(); } // reset the speed of the animation else if (comm.equalsIgnoreCase("reset speed")) speed = 500; else stop(); } // draw the image on the window public void paint(Graphics g) { // public void paint(imagePane.getGraphics(this)) { int iwidth = img.getWidth(this); int iheight = img.getHeight(this); Double d = new Double(iwidth*.8); int crop_width = d.intValue(); d = new Double(iheight*.8); int crop_height = d.intValue(); //g = imagePane.getGraphics(); g.drawImage(img, 0, 30, iwidth, iheight, Color.white, this); } // over-riding the update method prevents flickering public void update(Graphics g) { paint(g); } // when selection is made do appropriate thing public void itemStateChanged(ItemEvent e) { String index = (e.getItem()).toString(); String selection = img_lst.getItem(Integer.parseInt(index)); if (selection.equalsIgnoreCase("all layers")) selection = curr_date; // if selection is an image, load the image if ((selection.indexOf(".jpg") != -1) || (selection.indexOf(".jpeg") != -1) || (selection.indexOf(".gif") != -1)) { loadImage(); } // if selection is to choose another date, load in the dates file else if (selection.equalsIgnoreCase("choose another date")) { img_lst.removeAll(); BufferedReader data2; try { InputStream in = theURL.openStream(); data2 = new BufferedReader(new InputStreamReader(in)); String line; while ((line = data2.readLine()) != null) img_lst.add(line); } catch (IOException eve) {} } // ignore if selection = "Choose The Desired Date" else if (selection.equalsIgnoreCase("choose the desired date")) { } // if selection equals specific layer, only show that layer else if (selection.equalsIgnoreCase("4km layer only") || selection.equalsIgnoreCase("7km layer only") || selection.equalsIgnoreCase("10km layer only")) { String layer; char layer_char = selection.charAt(0); if (layer_char == '1') layer = "_10."; else if (layer_char == '4') layer = "_04."; else layer = "_07."; String[] allImages = img_lst.getItems(); img_lst.removeAll(); //img_lst.add("Choose Another Date"); img_lst.add("All Layers"); // check the list for the specified layer and display appropriately for (int currImage = 0; currImage < allImages.length; currImage++) { if (allImages[currImage].indexOf(layer) != -1) img_lst.add(allImages[currImage]); } makeControls(); } // else, load image names to list else { curr_date = selection; BufferedReader data1; String urlstr = selection; try { this.myURL = new URL(getCodeBase(), urlstr); } catch (MalformedURLException event) { System.out.println("Bad URL: " + myURL); } img_lst.removeAll(); try { InputStream in = myURL.openStream(); data1 = new BufferedReader(new InputStreamReader(in)); // add line that allows you to change date img_lst.add("Choose Another Date"); //img_lst.add("4km Layer Only"); //img_lst.add("7km Layer Only"); //img_lst.add("10km Layer Only"); String line; while ((line = data1.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, "<"); int fake_picture = 0; while (st.hasMoreTokens()) { String currToken = st.nextToken(); int end_index; if (((end_index = currToken.indexOf(".jpg")) != -1) || ((end_index = currToken.indexOf(".jpeg")) != -1) || ((end_index = currToken.indexOf(".gif")) != -1)) { if (fake_picture != 0) { img_lst.add(currToken.substring(8,end_index+4)); // img_lst.add(line); } else fake_picture++; } } } } catch (IOException exc) {} } repaint(); } // load and display the appropriate image public void loadImage() { String img_loc = curr_date + sepChar + img_lst.getSelectedItem(); img = getImage(getCodeBase(), img_loc); //CropImageFilter crop_filter = new CropImageFilter(40,130,510,800); //img = createImage(new FilteredImageSource(img.getSource(), crop_filter)); } /**The class ImageController creates the buttons used to start * and stop image animation. class ImageController implements Runnable { */ Thread radarSim; String[] imageFiles; String date; Image[] layerImages; Image currentImg; int speed; int imgIndex; /* ImageController (String[] filenames, String d) { imageFiles = filenames; date = d; layerImages = new Image[imageFiles.length]; } */ public void startLoop() { speed = 400; imageFiles = filenames; date = curr_date; if (radarSim == null) { radarSim = new Thread(this); radarSim.start(); } } public void stop() { if (radarSim != null) { radarSim.stop(); radarSim = null; } } public void run() { layerImages = new Image[imageFiles.length]; while (true) { for (int i = 0; i < imageFiles.length; i++) { String img_path = date + sepChar + imageFiles[i]; layerImages[i] = getImage(getCodeBase(), img_path); //CropImageFilter crop_filter = new CropImageFilter(40,130,500,800); //layerImages[i] = createImage(new FilteredImageSource(layerImages[i].getSource(), crop_filter)); } for (imgIndex = 0; imgIndex < imageFiles.length; imgIndex++) { img = layerImages[imgIndex]; repaint(); pause(speed); } } } public void pause (int time) { try { Thread.sleep(time); } catch (InterruptedException e) {} } // } }