//APPLET PROGRAMMET //---------------------------------------------------------------------------- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * NewJApplet.java * * Created on 2009-mar-05, 13:37:09 */ package ovn4losnappl; import java.awt.Color; import java.awt.Graphics; import java.io.DataInputStream; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import javax.swing.JApplet; /** * * @author karlssoj */ public class NewJApplet extends JApplet implements Runnable { int foregx = 10, foregy = 300; VaderInfo v; String fel; /** Initializes the applet NewJApplet */ @Override public void init() { v = new VaderInfo(); new ReadData(this); new Thread(this).start(); } @Override public void paint(Graphics g) { getContentPane().setBackground(Color.white); g.setColor(Color.BLACK); g.drawLine(10, 50, 10, 450); g.drawLine(10, 450, 590, 450); g.drawLine(0, 250, 600, 250); if(v.temperatur != -31) { g.drawString(Integer.toString(v.temperatur), Kurva.x2 - 5, Kurva.y2 - 5); g.drawString(Integer.toString(Kurva.xOkning), 50, 50); g.drawLine(Kurva.x1, Kurva.y1, Kurva.x2, Kurva.y2); foregx += Kurva.xOkning; foregy = v.temperatur; } } public void run() { synchronized(this) { while(true) { try { wait(); //Thread.sleep(1000); } catch (InterruptedException ex) {} int x, y; y = v.temperatur; y *= -1; y *= 10; y += 250; Kurva.x1 = Kurva.x2; Kurva.y1 = Kurva.y2; Kurva.y2 = y; Kurva.x2 += Kurva.xOkning; repaint(); } } } /* @SuppressWarnings("unchecked") // private void initComponents() { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); }// // Variables declaration - do not modify // End of variables declaration */ } class Kurva { static int x1 = 10, x2 = 10, y1 = 300, y2 = 300, xOkning = 30; } class ReadData implements Runnable { NewJApplet grafik; public ReadData(NewJApplet g) { grafik = g; new Thread(this).start(); } public void run() { long senastetid = 0, nutid = 0; int temp = 0; while(true) { URL url; URLConnection urlConn; DataInputStream o; try { url = new URL("http://people.arcada.fi/~karlssoj/procprog/data"); urlConn = url.openConnection(); urlConn.setDoInput(true); urlConn.setUseCaches(false); o = new DataInputStream(urlConn.getInputStream()); temp = o.readInt(); nutid = o.readLong(); o.close(); } catch(IOException i){/*grafik.fel = i.getMessage();*/} if(nutid != senastetid) { grafik.v.temperatur = temp; if(senastetid != 0) Kurva.xOkning = (int) (nutid - senastetid) / 100; senastetid = nutid; synchronized(grafik) { grafik.notifyAll(); } } try { Thread.sleep(100); } catch (InterruptedException ex) { // Logger.getLogger(ReadData.class.getName()).log(Level.SEVERE, null, ex); } } } } class VaderInfo { int temperatur = -31; int vindstyrka = -1; } //DEMONPROGRAMMET //---------------------------------------------------------------------------- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ovn4losn; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; /** * * @author Administrator */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws ClassNotFoundException, IOException { // TODO code application logic here new Demon(); } } class Demon implements Runnable { int temperatur; int vindstyrka; public Demon() throws ClassNotFoundException, IOException { //new Thread(this).start(); setTemp(); } void spara() throws ClassNotFoundException, IOException { System.out.println("\nSparar... "); System.out.println("Temp: " + temperatur); System.out.println("Vindstyrka: " + vindstyrka); FileOutputStream f = new FileOutputStream("Z:\\html\\procprog\\data"); DataOutputStream d = new DataOutputStream(f); d.writeInt(temperatur); d.writeLong(System.currentTimeMillis()); d.close(); f.close(); } void setTemp() throws ClassNotFoundException, IOException { while(true) { Random r = new Random(); temperatur = r.nextInt(40) - 20; spara(); try { Thread.sleep(r.nextInt(3000) + 2000); } catch (InterruptedException ex) {} } } public void run() { while(true) { Random r = new Random(); vindstyrka = r.nextInt(30); try { spara(); } catch (ClassNotFoundException ex) {} catch (IOException ex) {} try { Thread.sleep(r.nextInt(3000) + 2000); } catch (InterruptedException ex) {} } } }