/* Date: 10/05/96 File is: AlphaBlend.java Release: 1.0 * Last Modified: 12/19/96 by: abb@nuccard.eushc.org * Copyright (c) Andrew B. Barclay 1996. All rights reserved. */ // I like to know what I'm importing for sanity checks. import java.applet.Applet ; import java.awt.image.FilteredImageSource ; import java.awt.Color ; import java.awt.Dimension ; import java.awt.Event ; import java.awt.Graphics ; import java.awt.Image ; // Classes that I've added: import AlphaFilter ; public class AlphaBlend extends Applet { Image srcim1, srcim2 ; Image alphaim ; int alpha ; int thresh ; int lastx, lasty ; int dx, dy ; Dimension dim1, dim2 ; Image osim ; Graphics osgr ; public String getAppletInfo() { return "Alpha Blending Applet by Andrew Barclay" ; } /* ============================================================ * Initializing stuff: */ public void init() { String param ; param = getParameter("src1") ; srcim1 = getImage( getDocumentBase(), param ) ; param = getParameter("src2") ; srcim2 = getImage( getDocumentBase(), param ) ; // Parse the alpha (converted from percentage). param = getParameter( "alpha" ) ; alpha = (int)( 0.5 + (255.0/100.0)* Double.valueOf( param ).doubleValue() ) ; // Parse the threshold param = getParameter( "thresh" ) ; if( param != null ) { thresh = (int)( 0.5 + (255.0/100.0)* Double.valueOf( param ).doubleValue() ) ; } else { thresh = 0 ; } setBackground( Color.black ) ; } // Public methods for JavaScript control. public synchronized void setAlpha( double pct ) { alpha = (int)( 0.5 + (255.0/100.0)*pct ) ; if( alphaim != null ) { // conserve resources alphaim.flush() ; } alphaim = null ; repaint() ; } public synchronized void setThreshold( double pct ) { thresh = (int)( 0.5 + (255.0/100.0)*pct ) ; if( alphaim != null ) { // conserve resources alphaim.flush() ; } alphaim = null ; repaint() ; } /* ============================================================ * Painting. */ public void update( Graphics gr ) { paint( gr ) ; } public void paint( Graphics gr ) { if( alphaim == null ) { alphaim = filterImage( srcim2, alpha, thresh ) ; } int winwidth = size().width ; int winheight = size().height ; if( osim == null ) { int w1 = srcim1.getWidth( this ) ; int h1 = srcim1.getHeight( this ) ; int w2 = alphaim.getWidth( this ) ; int h2 = alphaim.getHeight( this ) ; /* * Drawing the offscreen image with no zoom and * then zooming it to the screen makes this really, * really slow. Offscreen image must be same size * as onscreen for best speed. */ if( w1 > -1 && h1 > -1 && w2 > -1 && h2 > -1 ) { // Size to largest image dimensions. int maxw = Math.max( w1, w2 ) ; int maxh = Math.max( h1, h2 ) ; dim1 = new Dimension( (maxw * winwidth)/w1, (maxh * winheight)/h1 ) ; dim2 = new Dimension( (maxw * winwidth)/w2, (maxh * winheight)/h2 ) ; osim = createImage( winwidth, winheight ) ; osgr = osim.getGraphics() ; gr.clearRect( 0, 0, 30, 100 ) ; gr.setColor( Color.red ) ; gr.drawString( "Got width and height.", 10, 20 ) ; } else { gr.clearRect( 0, 0, 30, 100 ) ; gr.setColor( Color.red ) ; gr.drawString( "Waiting on width and height...", 10, 20 ) ; } repaint( 100 ) ; } else { osgr.drawImage( srcim1, 0, 0, dim1.width, dim1.height, this ) ; osgr.drawImage( alphaim, dx, dy, dim2.width, dim2.height, this ) ; gr.drawImage( osim, 0, 0, winwidth, winheight, this ) ; gr.setColor( Color.cyan ) ; gr.drawString( "dx="+dx+", dy="+dy, 10, 20 ) ; } } Image filterImage( Image srcim, int alpha, int threshold ) { AlphaFilter filter = new AlphaFilter( alpha, threshold ) ; return createImage( new FilteredImageSource( srcim.getSource(), filter ) ) ; } /* ============================================================ * Event handling. */ public boolean mouseDown( Event evt, int x, int y ) { lastx = x ; lasty = y ; return true ; } public boolean mouseDrag( Event evt, int x, int y ) { dx += x-lastx ; dy += y-lasty ; lastx = x ; lasty = y ; repaint( 100 ) ; return true ; } public boolean keyDown( Event evt, int key ) { boolean ret = false ; switch( key ) { case Event.UP: case 'k': dy-- ; repaint( 100 ) ; return true ; case Event.DOWN: case 'j': dy++ ; repaint( 100 ) ; return true ; case Event.LEFT: case 'h': dx-- ; repaint( 100 ) ; return true ; case Event.RIGHT: case 'l': dx++ ; repaint( 100 ) ; return true ; default: System.out.println( "keyDown: key = "+key ) ; return false ; } } }