Java and Digital Images
by David Martin and Johnny Martin


Listing One
public class VideoGrabberCanvas extends JComponet implements Observer {
    VideoGrabber vg;
 ...
public VideoGrabberCanvas (VideoGrabber camera) {
    super();
    this.vg = camera;
    camera.addObserver(this);
    initializeImage();
}
 ...
}

Listing Two
Image img;
 ...
// Create an action listener which spawns a modal dialog.
captureButton.addActionListener(new ActionListener() {
  public synchronized void actionPerformed(ActionEvent e) {
    try {
      // Modal dialog blocks this thread until "picture" is taken.
      img = SnapshotDialog.TakePicture(TestDialog.this, vc);
    } catch (ConnectFailedException ex) {
      NotifyDialog.showMessageDialog(TestDialog.this,
                                     "Unable to connect to camera");
      return;
    }
    imageCanvas.setImage(img);
    imageCanvas.repaint();
  }

});

Listing Three
/** If camera is connected, shuts it down; otherwise, connects to device. */
private void toggleConnect () {
  if (connected) {
    vc.shutdown();
    snapshotButton.setEnabled(false);
    connectButton.setIcon(ConnectIcon);
    connectButton.setToolTipText("Connect to camera");
    videoCanvas.repaint();  // clear the canvas
  } else {
    try {
      vc.startup();
    } catch (ConnectFailedException ex) {
      JOptionPane.showMessageDialog(this, "Unable to connect to camera",
                                    "Bummer", JOptionPane.ERROR_MESSAGE);
      return;
    }
    snapshotButton.setEnabled(true);
    connectButton.setIcon(DisconnectIcon);
    connectButton.setToolTipText("Disconnect from camera");
  }
  connected = !connected;
}
class ConnectItemListener implements ItemListener {
  public void itemStateChanged (ItemEvent e) {
    toggleConnect();
  }
}

Listing Four
  ...
  settingsButton = createButton(SettingsText, SettingsUpIcon, 
                                SettingsDownIcon, "Change camera settings");
  settingsButton.addActionListener(settingsListener);
  ...
class SettingsListener implements ActionListener {
  public void actionPerformed (ActionEvent e) {
    if (control == null) {
      control = new ControlPanelFrame(vc);
      control.pack();
    }
    control.setVisible(true);
  }
}

Listing Five
  ...
  snapshotButton = createButton(CameraText, CameraUpIcon, CameraDownIcon,
                                "Take picture");
  snapshotButton.addActionListener(snapshotListener);
  ...
class SnapshotListener implements ActionListener {
  public void actionPerformed (ActionEvent e) {

    image = vc.snapshotImage();
    snapshotCanvas.setImage(image);
    snapshotCanvas.repaint();
  }
}





3


