/* Applet imager places multiple images on main image 3/31/97*/ import java.awt.*; import java.awt.image.*; public class imager extends java.applet.Applet { Image image[] = new Image[20] ; // up to 20 images Dimension imdim[] = new Dimension[20] ; public int images; // number of images to show. public int currentImage; public String imagefile[] = new String[20] ; Image imb, imo; int w, h, wback, hback, wo, ho; int red, green, blue ; int xov, yov; // position of overlay image on main image. int xm, ym; // position of previous mouse position. String cols, hexcols ; Graphics offGr; Image offIm; int [] thispixel = new int[1] ; Label l1 = new Label(" Overlay Position: ") ; Label l2 = new Label(" Mouse Position: ") ; Label l3, l4; Font f = new Font("TimesRoman", Font.PLAIN, 12); FontMetrics fm=getFontMetrics(f) ; MediaTracker tracker ; Color bgcolor = Color.white; public void init () { for (images=0; images<20; images++) { // get files from Parameters imagefile[images]= getParameter("im"+images) ; //System.out.println("Valid image: images") ; if(imagefile[images]==null) break; image[images]=getImage(getCodeBase(), imagefile[images]) ; } images-- ; if (images==0) { System.out.println("**** no files found **** ") ; System.exit(1) ; } //System.out.println("Image Count: "+images) ; setFont(f) ; l1.setForeground(Color.yellow) ; l1.setBackground(new Color(0,0,150)) ; l2.setForeground(Color.yellow) ; l2.setBackground(new Color(0,0,150)) ; w = this.size().width; //applet width h = this.size().height; //applet height // imb = getImage(getCodeBase(), "back.gif") ; tracker = new MediaTracker(this) ; //tracker.addImage(imb, 0) ; for (int j =0; j<=images; j++) tracker.addImage(image[j], 0) ; setLayout(new FlowLayout()) ; try{ tracker.waitForID(0) ; } catch(InterruptedException e) { return; } for (int j=0; j<=images; j++) { imdim[j] = new Dimension(image[j].getWidth(this), image[j].getHeight(this)) ; } currentImage=0 ; imo=image[0]; //initial overlay image. wo = imdim[0].width ; // overlay image dimensions. ho = imdim[0].height ; // wback= imb.getWidth(this) ; // background image dimensions. // hback = imb.getHeight(this) ; wback=w; hback=h/2; offIm = createImage(wback, hback ); // create buffer for backgnd image. offGr = offIm.getGraphics(); xov = (wback-wo)/2 ; // upper left corner of initial centered overlay. yov = (hback - ho)/2 ; add(l1) ; add(l2) ; // l3=new Label("Background Image Size: "+wback +" X " + hback +" ") ; l4=new Label("Overlay Image Size: "+wo +" X " + ho +" ") ; // add(l3) ; add(l4) ; } public Insets insets() { return new Insets(h-6*fm.getHeight(),1,1,1) ; } public void update(Graphics g) { paint(g) ; } public void paint (Graphics g) { // offGr.drawImage(imb,0,0,this) ; // overwrite complete screen. offGr.fillRect(0,0,wback,hback) ; offGr.drawImage(imo,xov,yov,this) ; g.drawImage (offIm, 0,0, this); l1.setText(" Overlay Position: " + xov + ", " +yov) ; l2.setText(" Mouse Position: " + xm + ", " +ym) ; } public boolean mouseDown(Event evt, int x, int y) { xm=x; ym=y; repaint() ; return true ; } public boolean mouseDrag(Event evt, int x, int y) { if ( (xm>=xov)&&(xm<=(xov+wo))&&(ym>=yov)&&(ym<=(yov+ho)) ) { //mouse on image? xov += (x-xm) ; // do a relative displacement of image. yov += (y-ym) ; xm=x ; ym=y ; } else { xm=x; ym=y; } repaint() ; return true ; } public boolean keyDown(Event ev, int key) { if ( (char)key == 'i' || (char)key =='I') { xov = (wback-wo)/2 ; // reset to center. yov = (hback - ho)/2 ; repaint() ; } else if ( (char)key == '+' ) { // scroll images forward. if (currentImage!=images) changeImage(++currentImage) ; } else if ( (char)key == '-' ) { // scroll images backward. if (currentImage!=0) changeImage(--currentImage) ; } return true; } public void changeImage(int i) { currentImage=i; // if called from outside applet. if(i<0 || i>images) return ; imo = image[i] ; wo = imdim[i].width ; // overlay image dimensions. ho = imdim[i].height ; xov = (wback-wo)/2 ; // reset to center. yov = (hback - ho)/2 ; l4.setText("Overlay Image Size: "+wo +" X " + ho +" ") ; repaint() ; } public String getAppletInfo() { return "Copyright 1997 Michel Gallant"; } }