/* * zazacam * * Copyright (C) 2001 Brian Rudy (brudyNO@SPAMpraecogito.com) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * ** Info ** * Authored in NetBeans 3.2 IDE * * Simple image grabber for Zaza's webcams. Image can be paused by * clicking on the applet. * * ** Revision History ** * 0.02 12-4-2001 * Updated delay to allow fractional seconds * * 0.01 11-10-2001 * First working version. Uses BufferedImage for double buffering. * Fairly decent performance and low resource usage. * ** Known Bugs ** * 'White' image between frames on slow connections related to screen * repaint interval. Occasional null pointer exception due to improper * threading. */ import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; //import java.awt.Toolkit; import java.net.*; import java.io.*; import java.util.Date; /* * Gets new image every n seconds */ public class zazacam extends JApplet implements ActionListener { boolean frozen = false; Timer timer; AnimationPane animationPane; Image fgImage; boolean isApplet = false; boolean gotImage = false; //Invoked only when run as an applet. public void init() { isApplet = true; //Get the image. //fgImage = java.awt.Toolkit.getDefaultToolkit().getImage(getParameter("IMAGE")); buildUI(getContentPane()); } void buildUI(Container container) { int imageDelay = 1; if (isApplet) { imageDelay = Integer.valueOf(getParameter("DELAY")).intValue(); } //How many seconds between frames? //int delay = 1000 * imageDelay; int delay = (imageDelay > 0) ? (1000 / imageDelay) : 100; //Set up a timer that calls this object's action handler. timer = new Timer(delay, this); timer.setInitialDelay(0); timer.setCoalesce(true); animationPane = new AnimationPane(fgImage); container.add(animationPane, BorderLayout.CENTER); animationPane.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (frozen) { frozen = false; startAnimation(); } else { frozen = true; //Give the window a chance to show status animationPane.repaint(); stopAnimation(); } } }); } //Invoked by a browser only. public void start() { startAnimation(); } //Invoked by a browser only. public void stop() { stopAnimation(); } //Can be invoked from any thread. public synchronized void startAnimation() { if (frozen) { //Do nothing. The user has requested that we //stop changing the image. } else { //Start animating! if (!timer.isRunning()) { timer.start(); } } } //Can be invoked from any thread. public synchronized void stopAnimation() { //Stop the animating thread. if (timer.isRunning()) { timer.stop(); } } public void actionPerformed(ActionEvent e) { //Display it. animationPane.repaint(); } class AnimationPane extends JPanel { public Image foreground; private java.awt.image.BufferedImage bimg; public AnimationPane(Image foreground) { this.foreground = foreground; } //Draw the current frame of animation. public void paintComponent(Graphics g) { java.awt.Graphics2D g2 = (Graphics2D) g; super.paintComponent(g); //paint any space not covered //by the background image int compWidth = getWidth(); int compHeight = getHeight(); int imageWidth, imageHeight; BufferedImage bi; Image newImage; Date ts = new Date(); if (bimg == null || bimg.getWidth() != compWidth || bimg.getHeight() != compHeight) { bimg = (java.awt.image.BufferedImage) createImage(compWidth, compHeight); System.out.print("Resizing canvas.\n"); } g2 = bimg.createGraphics(); g2.setBackground(Color.white); System.out.println("Getting new image..."); //Get a new image if (isApplet) { try { URL fgURL = new URL(getParameter("URL") + "?" + ts.getTime()); newImage = getImage(fgURL); g2.clearRect(0, 0, compWidth, compHeight); //g2.drawImage(java.awt.Toolkit.getDefaultToolkit().getImage(getParameter("IMAGE")),0,0,compWidth,compHeight,this); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(newImage, 0); tracker.waitForID(0); } catch (Exception e) {} int width = newImage.getWidth(this); int height = newImage.getHeight(this); bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D biContext = bi.createGraphics(); biContext.drawImage(newImage, 0, 0, compWidth,compHeight, null); System.out.println("Repainting..."); g.drawImage(bi,0,0,compWidth,compHeight,this); //g2.dispose(); //g.drawImage(bimg,0,0,compWidth,compHeight,this); //g2.drawImage(bimg,0,0,compWidth,compHeight,this); gotImage = true; System.out.println("Got it!"); } catch (MalformedURLException b) { } catch (IOException b) {} } else { try { URL fgURL = new URL("http://zazaconsole.exhibits.thetech.org/~brudy/images/newpicture.jpg?" + ts.getTime()); newImage = java.awt.Toolkit.getDefaultToolkit().getImage(fgURL); //g2.clearRect(0, 0, compWidth, compHeight); //g2.drawImage(java.awt.Toolkit.getDefaultToolkit().getImage(fgURL),0,0,compWidth,compHeight,this); //g.drawImage(java.awt.Toolkit.getDefaultToolkit().getImage(fgURL),0,0,compWidth,compHeight,this); //g2.dispose(); //g.drawImage(bimg,0,0,compWidth,compHeight,this); //g2.drawImage(bimg,0,0,compWidth,compHeight,this); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(newImage, 0); tracker.waitForID(0); } catch (Exception e) {} int width = newImage.getWidth(this); int height = newImage.getHeight(this); bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D biContext = bi.createGraphics(); biContext.drawImage(newImage, 0, 0, compWidth,compHeight, null); System.out.println("Repainting..."); g.drawImage(bi,0,0,compWidth,compHeight,this); gotImage = true; System.out.println("Got it!"); } catch (MalformedURLException b) { } catch (IOException b) { } } // System.out.println("compWidth=" + String.valueOf(compWidth) + // "compHeight=" + String.valueOf(compHeight)); //If we have a valid width and height for the //foreground image, draw it. //imageWidth = foreground.getWidth(this); //imageHeight = foreground.getHeight(this); //if ((imageWidth > 0) && (imageHeight > 0)) { //if (bi != null) { // System.out.println("Repainting..."); // g.clearRect(0,0,compWidth,compHeight); // g.drawImage(bi,0,0,compWidth,compHeight,this); //} //Paint frozen message if we need to if (frozen) { g.setColor(Color.blue); g.drawString("Frozen",10,compHeight-10); } } } //Invoked only when run as an application. public static void main(String[] args) { JFrame f = new JFrame("zazacam"); final zazacam controller = new zazacam(); controller.buildUI(f.getContentPane()); f.addWindowListener(new WindowAdapter() { public void windowIconified(WindowEvent e) { controller.stopAnimation(); } public void windowDeiconified(WindowEvent e) { controller.startAnimation(); } public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setSize(new Dimension(340, 260)); f.setVisible(true); controller.startAnimation(); } }