Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to display a photograph and dragging it with the mouse should re

ID: 3659034 • Letter: W

Question

Write a program to display a photograph and dragging it with the mouse should resize the photograph.

Explanation / Answer

GUI PROGRAMMING::: import java.io.FileInputStream; import java.io.InputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.FileNotFoundException; import javax.media.jai.RenderedOp; import javax.media.jai.PlanarImage; import javax.media.jai.JAI; import javax.media.jai.OpImage; import java.awt.image.renderable.ParameterBlock; import javax.media.jai.InterpolationNearest; import com.sun.media.jai.codec.ImageCodec; import com.sun.media.jai.codec.ImageEncoder; import com.sun.media.jai.codec.SeekableStream; import com.sun.media.jai.codec.JPEGEncodeParam; public class Resizer{ private int maxDim = 500; public void resize(String inImageUrl, String outImageUrl){ System.out.println("Resizing: " + inImageUrl); try{ InputStream is = new FileInputStream(inImageUrl); SeekableStream s = SeekableStream.wrapInputStream(is, true); RenderedOp objImage = JAI.create("stream", s); ((OpImage)objImage.getRendering()).setTileCache(null); float xScale = 1; float yScale = 1; float width = (float)objImage.getWidth(); float height = (float)objImage.getHeight(); System.out.println("img width: " + objImage.getWidth()); System.out.println("img height: " + objImage.getHeight()); // find the ratio between the width and the height. double ratio = height/width; System.out.println("Ratio: " + ratio); float scale = 1; if(1 == ratio){ System.out.println("Image is sqare"); //calculate the scale scale = maxDim / height; }else if(ratio > 1){ System.out.println( "Image is taller than it is wide, using height."); //calculate the scale scale = maxDim / height; }else{ System.out.println( "Image is wider than it is tall, using width."); //calculate the scale scale = maxDim / width; } System.out.println("Scale: " + scale); ParameterBlock pb = new ParameterBlock(); pb.addSource(objImage); // The source image pb.add(scale); // The xScale pb.add(scale); // The yScale pb.add(0.0F); // The x translation pb.add(0.0F); // The y translation pb.add(new InterpolationNearest()); // The interpolation objImage = JAI.create("scale", pb, null); FileOutputStream out = null; out = new FileOutputStream(outImageUrl); ImageEncoder imgEnc = ImageCodec.createImageEncoder("JPEG", out, new JPEGEncodeParam()); imgEnc.encode(objImage); out.close(); }catch(Exception e){ e.printStackTrace(); System.exit(1); } } /** * This is the maximum dimension for the resized image. * The resizer measures the image to see if it is wider than it * is tall. If so it scales on the width. Otherwise, the height. * Defaults to 500. * @return maxDim */ public int getMaxDim(){ return this.maxDim; } /** * This is the maximum dimension for the resized image. * The resizer measures the image to see if it is wider than it * is tall. If so it scales on the width. Otherwise, the height. * Defaults to 500. * @param maxDim */ public void setMaxDim(int maxDim){ this.maxDim = maxDim; } /** * For testing and debugging only. * Usage: java us.souther.photo.Resizer */ public static void main(String[] args)throws Exception{ String inUrl = args[0]; String outUrl = args[1]; new Resizer().resize(inUrl, outUrl); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote