CS 596 Java Programming Fall Semester, 1998 Scaling an Image |
||
---|---|---|
San Diego State University -- This page last updated 03-Jan-99 |
The scrollbar value ranges from from 10 to 200. These values represent the percentage used to scale the image. A value of 200 displays an image twice the size of the orginal image.
<DIV ALIGN="center"> <APPLET ARCHIVE="AppletClasses.jar" CODE="ImageScaleApplet.class" WIDTH=350 HEIGHT=330> Your browser does not support Java applets, so you can not see this applet operate. </APPLET> </DIV>
import java.awt.*; import java.awt.image.ImageObserver; public class ImageCanvas extends Canvas { Image displayImage; int imageWidth; int imageHeight; int displayWidth; int displayHeight; public ImageCanvas( Image toDisplay ) { displayImage = toDisplay; imageWidth = displayImage.getWidth( this ); imageHeight = displayImage.getHeight( this ); displayWidth = imageWidth; displayHeight = imageHeight; } public void paint( Graphics display) { //center image int startX = (imageWidth - displayWidth) / 2; int startY = (imageHeight - displayHeight) / 2; //drawImage scales image to fit displayWidth, height display.drawImage( displayImage, startX, startX, displayWidth, displayHeight, this ); } // Override the default action, which would clear the display region // before drawing the image again. The default causes some flicker. // Here we just redraw the image. It covers the entire area. public void update(Graphics display) { paint( display); } // Scale display image public void scaleImage( int percentScale) { displayWidth = (imageWidth * percentScale) / 100; displayHeight = (imageHeight * percentScale) / 100; repaint(); } }
import java.awt.*; import java.applet.Applet; import java.awt.Image; import java.awt.image.ImageObserver; import java.awt.event.AdjustmentListener; import java.awt.event.AdjustmentEvent; public class ImageScaleApplet extends Applet { private Scrollbar imageChanger = new Scrollbar( Scrollbar.HORIZONTAL, 100, //Initial value 1, //Not used, area visiable 10, // min value, seems to be block change rate 201 ); //max value, 1 compensates for visisble area // subtracted from max value. private ImageCanvas tigerCanvas; public void init() { Image tiger = getImage( "../images/tiger.gif" ); tigerCanvas = new ImageCanvas( tiger); imageChanger.addAdjustmentListener( new ImageAdjustment() ); setLayout( new BorderLayout() ); add( BorderLayout.NORTH, imageChanger ); add( BorderLayout.CENTER, tigerCanvas ); } private Image getImage( String imageName) { Image loadedImage = null; try { loadedImage = super.getImage( getCodeBase(), imageName ); //Insure image is downloaded before showing it MediaTracker tracker = new MediaTracker( this ); tracker.addImage( loadedImage, 0 ); tracker.waitForID( 0 ); } catch (InterruptedException netProblem ) { System.out.println( "error"); } return loadedImage; } class ImageAdjustment implements AdjustmentListener { public void adjustmentValueChanged( AdjustmentEvent event ) { // Use the value of the scrollbar as the scale rate of image tigerCanvas.scaleImage( event.getValue()); } } }