import java.applet.Applet; import java.awt.*; import java.lang.*; import java.net.*; import java.io.*; import java.util.*; public class cam2 extends Applet implements Runnable { // Defines, in seconds, how long the Image takes to refresh private final int refresh = 60; // Defines, in seconds, how much longer to wait of the image wasn't updated private final int refresh_noupdate = refresh/2; // Defines the refresh rate, in seconds, for when the game is down private final int refresh_camdown = refresh*3; // Defines the location of the cam image private final String camFile = "gfx/cam.jpg"; // Defines the background color for the apllet private final Color bgColor = new Color (0, 0, 0); // Defines, in minutes, the time it takes for "cam is down" image to be // Displayed private final int camDownTime = 1*60; // Defines the location of the "cam is down" image private final String camDownFile = "gfx/nocam.jpg"; // Number of frames to save private final static int buffer = 30; // Do not edit below this line, everything else is just coding. Thread runner; int count = 0; int seconds = refresh; int maxSeconds = seconds; Image[] camImage = new Image[buffer]; Image camDown; private URL dataFile; private Date myDate; private Date oldDate; private Date localDate = new Date(); private int bufferSize = 0; private boolean play = false; private int frame = 0; int minutes = 0; public void init() { setLayout(null); this.requestFocus(); this.setBackground(bgColor); addImage(getImage(getCodeBase(), camFile)); camDown = getImage(getCodeBase(), camDownFile); getImageDate(); } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void run() { while(true) { repaint(); sleep(500); } } public boolean mouseDown(Event evt, int x, int y) { if (y > 239 && y < 251) { play = !play; if (play) frame = bufferSize-1; return true; } return false; } public synchronized void paint(Graphics g) { minutes = (int) ((localDate.getTime() - myDate.getTime())/60000); count += 1; if (count%2 == 0) seconds -= 1; if (seconds <= 0) { oldDate = new Date(myDate.getTime() - 1); getImageDate(); if (myDate.getTime() > oldDate.getTime()) { addImage(getImage(getCodeBase(), camFile)); } else { seconds += refresh_noupdate; } if (minutes <= camDownTime) seconds += refresh; else seconds = refresh_camdown; maxSeconds = seconds; } if (count >= refresh*2) count = 0; if (minutes <= camDownTime) g.drawImage(camImage[frame], 0, 0, this); else g.drawImage(camDown, 0, 0, this); g.setColor(bgColor); g.fillRect(0,240,320,60); g.setColor(Color.white); g.drawRect(0,240,10,10); g.drawRect(11,240,310,10); g.setColor(Color.black); g.drawRect(1,241,9,9); g.drawRect(12,241,307,9); g.setColor(Color.lightGray); g.fillRect(1,241,9,9); g.fillRect(12,241,307,9); g.setColor(Color.white); g.drawRect(14,243,300,4); g.setColor(Color.black); g.drawRect(13,242,300,4); g.setColor(Color.blue); g.fillRect(15,244,(bufferSize*300/buffer),2); g.setColor(Color.black); g.drawRect(13+((bufferSize-frame)*300/buffer),241,3,7); g.setColor(Color.red); g.fillRect(14+((bufferSize-frame)*300/buffer), 242,2,6); g.setColor(Color.black); if (play) { g.drawLine(2,242,2,248); g.drawLine(7,242,7,248); frame -= 1; if (frame < 0) { play = false; frame = 0; } } else { g.drawLine(2,242,2,247); g.drawLine(2,242,7,245); g.drawLine(2,247,7,245); } g.setColor(Color.white); g.drawRect(0,251,102,12); g.setColor(Color.gray); g.fillRect(1,252,102,12); g.setColor(Color.white); g.drawRect(0,251,101,11); g.setColor(Color.black); g.drawRect(2,253,101,11); g.setColor(Color.blue); g.fillRect(3,254,seconds*100/maxSeconds,8); if (minutes <= 5) { g.setColor(Color.green); g.drawString("@ " + myDate.toLocaleString() + " (Active)", 110, 262); } else if (minutes <= 20) { g.setColor(Color.yellow); g.drawString("@ " + myDate.toLocaleString() + " (Idle)", 110, 262); } else { g.setColor(Color.red); g.drawString("@ " + myDate.toLocaleString() + " (Inactive)", 110, 262); } } // Override of update to prevent clearing of screen public void update(Graphics g) { paint(g); } private void sleep(int i) { try { runner.sleep(i); } catch (InterruptedException e) { } } private void getImageDate() { String url = getCodeBase().toString() + camFile; try { this.dataFile = new URL(url); } catch ( MalformedURLException e) { System.out.println("Bad URL: " + url); } String line; // Line Buffer int c = -1; try { URLConnection conn = this.dataFile.openConnection(); conn.setUseCaches(false); conn.connect(); myDate = new Date(conn.getLastModified()); localDate = new Date(conn.getDate()); } catch (IOException e) { System.out.println("IO Error:" + e.getMessage()); } } private void addImage(Image img) { for (int i = bufferSize; i > 0; i--) { camImage[i] = createImage(320, 240); Graphics g = camImage[i].getGraphics(); g.drawImage(camImage[i-1], 0, 0, this); } if (bufferSize > 0) { camImage[0].flush(); System.gc(); } camImage[0] = img; if (bufferSize < buffer-1) bufferSize += 1; } }