import java.awt.image.*; import java.awt.Dimension; import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; import java.net.*; import java.awt.event.*; import java.io.*; import java.awt.*; import java.applet.*; import java.net.URL; /** This class has used some Sun Microsystems code to build * a canvas for use in a ScrollPane * * @author Daryl Moulder 11950080 *
* Tutor: Sita Ramakrishnan * @version 3.0 */ class PicImage extends Canvas implements MouseListener,MouseMotionListener { // attributes to store image info private int displayWidth = 438; private int displayHeight = 307; private int imageHeight = 0; private int imageWidth = 0; private Image imageBuffer = null; private Image dispImage; private int tx = 0; private int ty = 0; private Dimension preferredSize; private Cursor curse; private NavDisplay navdisp; private Navigator navigator; private int[] minX; private int[] maxX; private int[] minY; private int[] maxY; private String[] methodNames; private String dispName; private int methodSelected; private String myType; /** * This constructor receivs the image from the Beaches class and it's * prefered size which is stored in local variables. * @param Image img, Dimension prefSize */ PicImage(Image img, Dimension prefSize, Applet nav, String type) { myType = type; methodSelected = 0; dispImage = img; preferredSize = prefSize; if(type.equals("NavDisplay")) navdisp = (NavDisplay)nav; else navigator = (Navigator)nav; addMouseMotionListener(this); addMouseListener(this); minX = new int[6]; maxX = new int[6]; minY = new int[6]; maxY = new int[6]; methodNames = new String[6]; dispName = ""; //Init co-ords for(int i = 0;i < 6; i++) { maxX[i] = minX[i] = minY[i] = maxY[i] = 1; methodNames[i] = ""; } } /** * Set the display width for a new image * @param int displayW */ public void setDisplayWidth(int displayW) { displayWidth = displayW; } /** * Set the display height for a new image * @param int displayH */ public void setDisplayHeight(int displayH) { displayHeight = displayH; } /** * Set the image for a new display image * @param int displayI */ public void setDisplayImage(Image displayI) { dispImage = displayI; } /** Set the name of the picture to be displayed * @param A String display name */ public void setDisplayName(String displayN) { dispName = displayN; } /** Set the coordinates for each method *@param array of minimum Y values *@param array of minimum X values *@param array of maximum Y values *@param array of maximum X values */ public void setCoords(int newMinY[],int newMaxY[],int newMinX[],int newMaxX[]) { minY = newMinY; minX = newMinX; maxX = newMaxX; maxY = newMaxY; } /** Set the methodNames for each method to be processed * @param An array of method names as a String */ public void setMethodNames(String newMethodNames[]) { methodNames = newMethodNames; } /** Get the minimum size for this image */ public Dimension getMinimumSize() { return new Dimension(10, 10); } /** get the preffered size for this image */ public Dimension getPreferredSize() { return preferredSize; } /** This method overides the update method it updates the screen * when the ImageObserver receives all of the picture * @return a boolean is updated or not * @param an Image img, int infoflags,ints x and y coordinates
, ints image width and height */ public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & ImageObserver.ALLBITS) != 0) { imageWidth = dispImage.getWidth(null); imageHeight = dispImage.getHeight(null); repaint(); return false; } else return true; } /** Override the Paint method to stop flickering * @param a Graphics object g */ public void update(Graphics g) { paint(g); } /** Overrides the paint method, paint the picture to a offscreen imageBuffer before displaying * when the picture has been fully drawn (updated). * @param a Graphics object g */ public void paint(Graphics g) { g.translate(-tx, -ty); //g.drawImage(image, 0, 0, getBackground(), this); if ((imageWidth <= 0) || (imageHeight <= 0)) { // Image has not been fully loaded - don't transfer buffer to screen yet g.drawString("Ready to load image", 0, 10); imageBuffer = createImage(1, 1); Graphics bufferG = imageBuffer.getGraphics(); bufferG.drawImage(dispImage, 0, 0, displayWidth,displayHeight,this); bufferG.dispose(); return; } // If we've got this far, image has loaded to create full buffer... imageBuffer = createImage(displayWidth, displayHeight); Graphics bufferG = imageBuffer.getGraphics(); bufferG.drawImage(dispImage, 0, 0, displayWidth,displayHeight, null); bufferG.dispose(); // ... then transfer buffer contents to screen //g.drawImage(imageBuffer, 0, 0, displayWidth, displayHeight, this); if(methodSelected == 0) { g.drawImage(imageBuffer, 0, 0, displayWidth, displayHeight, this); } if(methodSelected == 1) { g.drawImage(imageBuffer, 0, 0, displayWidth, displayHeight, this); g.setColor(Color.black); g.setXORMode(Color.white); g.fillRect(minX[0], minY[0],(maxX[0]-minX[0]),(maxY[0]-minY[0])); } if(methodSelected == 2) { g.drawImage(imageBuffer, 0, 0, displayWidth, displayHeight, this); g.setColor(Color.black); g.setXORMode(Color.white); g.fillRect(minX[1], minY[1],(maxX[1]-minX[1]),(maxY[1]-minY[1])); } if(methodSelected == 3) { g.drawImage(imageBuffer, 0, 0, displayWidth, displayHeight, this); g.setColor(Color.black); g.setXORMode(Color.white); g.fillRect(minX[2], minY[2],(maxX[2]-minX[2]),(maxY[2]-minY[2])); } if(methodSelected == 4) { g.drawImage(imageBuffer, 0, 0, displayWidth, displayHeight, this); g.setColor(Color.black); g.setXORMode(Color.white); g.fillRect(minX[3], minY[3],(maxX[3]-minX[3]),(maxY[3]-minY[3])); } if(methodSelected == 5) { g.drawImage(imageBuffer, 0, 0, displayWidth, displayHeight, this); g.setColor(Color.black); g.setXORMode(Color.white); g.fillRect(minX[4], minY[4],(maxX[4]-minX[4]),(maxY[4]-minY[4])); } if(methodSelected == 6) { g.drawImage(imageBuffer, 0, 0, displayWidth, displayHeight, this); g.setColor(Color.black); g.setXORMode(Color.white); g.fillRect(minX[5], minY[5],(maxX[5]-minX[5]),(maxY[5]-minY[5])); } } public void mouseClicked(MouseEvent evt) { int x = evt.getX(); int y = evt.getY(); if (x > minX[0] && x< maxX[0] && y > minY[0] && y < maxY[0]) { if(dispName.equals("NavigatorClassDiagram.gif")) { navigator.drawDrawNavEventClassDiag(); } else navdisp.dispInfo(1,dispName); } if (x > minX[1] && x< maxX[1] && y > minY[1] && y < maxY[1]) { if(dispName.equals("NavigatorClassDiagram.gif")) { navigator.drawNavListenersClassDiag(); } else navdisp.dispInfo(2,dispName); } if (x > minX[2] && x< maxX[2] && y > minY[2] && y < maxY[2]) { if(dispName.equals("NavigatorClassDiagram.gif")) { navigator.drawNavListenersClassDiag(); } else navdisp.dispInfo(3,dispName); } if (x > minX[3] && x< maxX[3] && y > minY[3] && y < maxY[3]) { if(dispName.equals("NavigatorClassDiagram.gif")) { navigator.drawNavClassDiag(); } else navdisp.dispInfo(4,dispName); } if (x > minX[4] && x< maxX[4] && y > minY[4] && y < maxY[4]) { navdisp.dispInfo(5,dispName); } if (x > minX[5] && x< maxX[5] && y > minY[5] && y < maxY[5]) { navdisp.dispInfo(6,dispName); } } /** Empty class to impliment a mouseEntered method */ public void mouseEntered(MouseEvent evt) { } /** Empty class to impliment a mouseExited method */ public void mouseExited(MouseEvent evt) { } /** Empty class to impliment a mouseReleaced method */ public void mouseReleased(MouseEvent evt) { } /** Empty class to impliment a mousePressed method */ public void mousePressed(MouseEvent evt) { } /** Empty class to impliment a mouseDragged method */ public void mouseDragged (MouseEvent evt) { } public void mouseMoved(MouseEvent evt) { curse = new Cursor(java.awt.Cursor.HAND_CURSOR); int x = evt.getX(); int y = evt.getY(); //System.out.println("x is " + x + " and y is " + y ); if (x > minX[0] && x< maxX[0] && y > minY[0] && y < maxY[0]) { setCursor(curse); if(myType.equals("NavDisplay")) navdisp.showStatus(methodNames[0] + " Selected"); else navigator.showStatus(methodNames[0] + " Selected"); if(methodSelected != 1) { methodSelected = 1; this.repaint(); } } else if (x > minX[1] && x< maxX[1] && y > minY[1] && y < maxY[1]) { setCursor(curse); setCursor(curse); if(myType.equals("NavDisplay")) navdisp.showStatus(methodNames[1] + " Selected"); else navigator.showStatus(methodNames[1] + " Selected"); if(methodSelected != 2) { methodSelected = 2; this.repaint(); } } else if (x > minX[2] && x< maxX[2] && y > minY[2] && y < maxY[2]) { setCursor(curse); if(myType.equals("NavDisplay")) navdisp.showStatus(methodNames[2] + " Selected"); else navigator.showStatus(methodNames[2] + " Selected"); if(methodSelected != 3) { methodSelected = 3; this.repaint(); } } else if (x > minX[3] && x< maxX[3] && y > minY[3] && y < maxY[3]) { setCursor(curse); if(myType.equals("NavDisplay")) navdisp.showStatus(methodNames[3] + " Selected"); else navigator.showStatus(methodNames[3] + " Selected"); if(methodSelected != 4) { methodSelected = 4; this.repaint(); } } else if (x > minX[4] && x< maxX[4] && y > minY[4] && y < maxY[4]) { setCursor(curse); navdisp.showStatus(methodNames[4] + " Selected"); if(methodSelected != 5) { methodSelected = 5; this.repaint(); } } else if (x > minX[5] && x< maxX[5] && y > minY[5] && y < maxY[5]) { setCursor(curse); navdisp.showStatus(methodNames[5] + " Selected"); if(methodSelected != 6) { methodSelected = 6; this.repaint(); } } else { if(methodSelected != 0) { methodSelected = 0; if(myType.equals("NavDisplay")) navdisp.showStatus(" "); else navigator.showStatus(" "); curse = curse.getDefaultCursor(); setCursor(curse); this.repaint(); } } } }