home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-21 | 2.4 KB | 88 lines |
- // HouseView.java - Shows floor plan and allows selecting cameras
- //
- // Copyright (C) 1996 by Dale Gass
- // Non-exclusive license granted to MKS, Inc.
- //
-
- import java.lang.*;
- import java.util.*;
- import java.awt.*;
- import java.net.*;
- import java.applet.*;
- import java.awt.image.*;
-
-
- // HouseView - pop up view within a house
-
- public class HouseView extends Canvas {
- MediaTracker mt; // Tracks image loading
- Image view; // The image itself
- Applet app; // Parent applet
- House house; // Our house
- Camera cams[]; // Camera views
- int selcamera = -1; // Currently selected camera
- int floor; // Which floor
-
- // Constructor - load the image
-
- public HouseView(Applet a, House h, int n) {
- app = a;
- house = h;
- floor = n;
-
- mt = new MediaTracker(app);
-
- mt.addImage(view = app.getImage(app.getCodeBase(),
- h.getCode() + floor + ".gif"), 0);
- try { mt.waitForAll(); } catch(Exception e) { }
- }
-
- // Tell layout manager what size we want
-
- public Dimension minimumSize() { return new Dimension(600, 350); }
- public Dimension preferredSize() { return new Dimension(600, 350); }
-
- // Draw the cameras over the floor plan
-
- public void drawCameras(Graphics g) {
- cams = HouseServer.getCameras(house, floor);
- for (int i=0; i<cams.length; i++)
- cams[i].draw(g, selcamera == i ? Color.white : Color.blue);
- }
-
- // paint the object; draw floorplan, then cameras
-
- public void paint(Graphics g) {
- g.drawImage(view, 0, 0, app);
- drawCameras(g);
- }
-
- // Watch the mouse, highlight the closest camera; upon a click, launch
- // a view... Wait for mouse up, since mouse down events get doubled up
- // upon application focus change. Arrrgh.
-
- public boolean handleEvent(Event evt) {
- if (evt.id == Event.MOUSE_MOVE) {
- int mindist = 9999;
- int newselected = -1;
- if (cams != null) for (int i=0; i<cams.length; i++) {
- int dist = ((evt.x - cams[i].x) * (evt.x - cams[i].x) +
- (evt.y - cams[i].y) * (evt.y - cams[i].y));
- if (dist < mindist) {
- newselected = i;
- mindist = dist;
- }
- }
- if (newselected != selcamera) {
- selcamera = newselected;
- drawCameras(getGraphics());
- }
- } else if (evt.id == Event.MOUSE_UP && selcamera != -1) {
- CameraView cv = new CameraView(app, house, floor, selcamera,
- cams[selcamera]);
- } else
- return (false);
- return (true);
- }
- }
-