IN 104 Lecture 4 1999.9.14 Today’s lecture • Discussion of some Java features – – – – event-driven programming (Zoom2 applet) parameter input to method load data from URL about applet • The StockPlotter/WeatherPlotter project • Slides will be available at IN104 home page Event-driven programming • Implement “listener” - define program response in the abstract method(s) • Attach GUI component(s) with listener(s) • Example class MyListener implements ActionListener { public void actionPerformed (ActionEvent e) { /* … */ } } // ... Button knappen=new Button(“Press”); knappen.addActionListener (mylistener); The Zoom2 applet • Scrollbar • AdjustmentListener public class Zoom2 implements AdjustmentListener Zoom2.java (part I) import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Zoom2 extends Applet implements AdjustmentListener { private final int SIZE = 300; private Scrollbar bar = new Scrollbar(Scrollbar.HORIZONTAL, 10, 64, 10, SIZE); private int current_size = 10; private Image picture; public void init() { setLayout (new BorderLayout()); setBackground (Color.white); bar.addAdjustmentListener (this); picture = getImage (getDocumentBase(), "owl.gif"); add (bar, "South"); setSize (SIZE, SIZE); setVisible (true); } // method init Zoom2.java (part II) public void zoom_image (int size) { current_size = size; repaint(); } // method zoom_image public void paint (Graphics page) { page.drawImage (picture, SIZE/2-current_size/2, SIZE/2-current_size/2, current_size, current_size, this); } // method draw public void adjustmentValueChanged (AdjustmentEvent event) { zoom_image (event.getValue()); } // method adjustmentValueChanged } // class Zoo Parameter input to method • Primitive data by copy • Object data by copy of reference (alias) • Example class Num { public int number; } // … void work (Num num, int i) { num.number++; // has effect outside i = 2; // no effect outside num = new Num; // no effect outside } Load data from URL import java.net.URL; import java.io.BufferedReader; import java.io.InputStreamReader; try { URL url = new URL(“http://www.stud.ifi.uio.no/~in104/Quotes/tickers.in104”); BufferedReader reader=new BufferedReader(new InputStreamReader(url.openStream())); String inputLine; while ((inputLine=reader.readLine()) != null) { // … } reader.close(); } catch (Exception e) { /* … */ } Applet life cycle • Web browser starts JRE • JRE loads all java binaries (*class *jar *zip) • JRE launches the applet <applet code=“example.class”> • Applet’s init method - applet parameters input, GUI + other one-time initializations • Applet’s start method - right after “init”, also called e.g. when browser returns to the web page Applet life cycle • Applet’s stop method is called when e.g. browser leaves the web page - should kill computing intensive threads • Applet’s destroy method is called before applet is terminated • Applet’s paint method is responsible for updating the drawing done directly on the Graphics object The Response applet • http://www.ifi.uio.no/~in104/forelesninger/L ectures/Lecture4/Response.html • An example showing when methods init, start, paint, stop and destroy are invoked • Each method increases its own counter every time it’s invoked Applet security • Applet can only load data from the server it was loaded from • Applet normally not allowed to access the local file system • Exception: applets loaded from your local system (file:/…) can read/write to the local file system • An applet cannot run a local executable • Limited access to operating system info HTML tags for applet <applet code=“Test.class” width=300 height=200 archive=“http://www.stud.ifi.uio.no/~in104/ jars/in104.jar”> <param name=“color1” value=“blue”> <param name=“color2” value=“white”> </applet> Receiving parameters • The Applet class has a method named getParameter that can be used to obtain the values assigned by the HTML <param> tag • Example class AppletWithParameters extends Applet { private String foreground, background; public void init () { foreground = getParameter(“color1”); background = getParameter(“color2”); } // … } Project general info • A sophisticated applet capable of – – – – loading data (stock/weather) from URL simple graphical representation of curves interactive GUI events simple mathematical analysis • A web page containing the applet • Deadline for submission: 19. Nov. kl.12:00 (email to the group teacher) • JDK 1.1 • IN104 java library in104.jar also allowed Project rule & exam • Each student should work independently • Don’t open file protection of *.java files at any time! • unix>zip -ee oppgave.zip *.java • Exam in week 48 – – – – – check “godkjent” list meet at the usual group hour form of “declaration” majority examined by the group teacher some randomly chosen students will be examined by the lecturers Requirements • Applet should work as described – user-friendly GUI • Structured and easily understandable source codes (*.java) – clear data structure – meaningful variable names – use of comments Project data • Choice 1: Historical prices - different stocks • For every chosen stock – day close price history (day1, price1, day2, price2...) – day sales history (day1, volumn1, day2, volumn2…) • Choice 2: Meteorological measurements different locations • For every chosen location – temperature history (day1, temp1, day2, temp2…) – precipitation history (day1, rain1, day2, rain2…) Historical stock price data • Ticker list file http://www.stud.ifi.uio.no/~in104/Quotes/tickers.in104 TOTX Totalindeksen AKE Aker RGI A KVI Kværner A NTC NetCom … • One file per stock, e.g. TOTX is as follows http://www.stud.ifi.uio.no/~in104/Quotes/Oslo/TOTX 02.01.97 967.63 960.72 967.63 963.71 582716 03.01.97 978.55 963.78 963.78 978.55 823148 06.01.97 992.54 978.55 978.55 992.42 1107668 07.01.97 996.20 987.54 992.42 992.65 1009872 ... Meteorological measurement • Location list file http://www.stud.ifi.uio.no/~in104/Met_data/locations.in104 01001 JAN MAYEN 01007 NY-ALESUND 01008 SVALBARD LUFTHAVN 01010 ANDOYA … • One file location, e.g. 01001 is as follows http://www.stud.ifi.uio.no/~in104/Met_data/Norway/01001 16.06.99 4.2 5 5.4 3.9 4.1 17.06.99 5.5 3.6 2.7 2.8 9.25 18.06.99 4 3.3 3.5 1.5 3.8 19.06.99 1.8 2.6 2.6 1.7 0.25 ... GUI organization example User response Smoothing filters IN104 java library - in104.jar • Use the on-line documentation! http://www.stud.ifi.uio.no/~in104/in104.jar.doc/packages.html • Learn from the weekly exercises! ~in104/bin/unpack_skeleton.sh • Include the following path into the $CLASSPATH environment variable /hom/in104/www_docs/jars/in104.jar • Use the HTML ARCHIVE tag archive=“http://www.stud.ifi.uio.no/~in104/jars/in104.jar” Handling dates • From String (“14.09.99”) to Date import java.text.SimpleDateFormat; import java.util.Date; SimpleDateFormat parser = new SimpleDateFormat(“dd.MM.yy”); Date date = parser.parse(“14.09.99”); • From Date to int (using 01.01.97 as ref.) Date ref_date = parser.parse(“01.01.97”); final int millisec_in_a_day = 86400000; int day_ident = (date.getTime() -ref_date.getTime())/millisec_in_a_day; 01.01.97 -> 0 14.09.99 -> 985 Coordinate transform • Transform standard coordinates (x,y) to (u,v), those accepted by class Graphics f max y cmin y f min y cmax y f min x f max x cmax x cmin x u s x x t x s x f max x f min x v s y y t y s cmax y cmin y f max y f min y y cmin x cmax x t x cmin f min x x t y cmax y f min y cmax x cmin x f max x f min x cmax y cmin y f max y f min y Smoothing filters • Piecewise linear least square approximation • Gliding average (xj,yj), j=1,…,m • (ai,bi), i=1,…,m-n+1 a i xi n 1 bi i n 1 j i yj n • Cubic spline interpolation/approximation