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

This needs to be in java. Knob Resizing If a click­drag is on a knob, we want to

ID: 3573932 • Letter: T

Question

This needs to be in java.

Knob Resizing


If a click­drag is on a knob, we want to resize that shape. Modify the click detection code so that it can notice if a click falls on a knob of a shape. A click on one of the knobs of a shape initiates a resize of that shape. Otherwise, a click within the bounds of the shape should initiate a move of that shape. Any click within the bounds rectangle will count to select that shape ­­ we won't worry about the empty space for a DOval where the drawn oval falls inside the bounds.


Here is an algorithm for knob resize that works well:
• The initial click is on some knob/point ­­ call t hat the "moving" point.
• From the list of knobs, find the point in the opp osite corner from the moving point. Call that the "anchor" point, and remember it for the duration of the mouse drag.
• During the drag, update the moving point while the anchor point does not move. Now consider the rectangle defined by anchor point in its original location and the moving point wherever it is now. That rectangle should be the new bounds rectangle for the shape.

This algorithm works even if we grab, say, the lower right knob and drag it straight up so it appears to flip the oval like this...

The resize logic is tricky, but ultimately it should come down to some setter calls sent to the model to change the bounds rectangle.


Note that the existence of the knobs does not make the bounds rectangle in the model any bigger. The bounds is still just the bounds of the drawn shape. However, the shape now also has different, bigger bounds that includes the shape and also its knobs. You may want to add a getBigBounds()convenience method on shape that returns the bigger bounds for clients who need it.

Explanation / Answer

import java.awt.Graphics2D;

import java.awt.AlphaComposite;

import java.awt.RenderingHints;

import java.io.File;

import java.io.FileOutputStream;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import java.io.IOException;

public class PerfectImageResizerr

{

public static void createThumbnail(String sourceFile, String destFile, int targetWidth, int targetHeight) throws Exception

{

try

{

BufferedImage img = ImageIO.read(new File(sourceFile));

int iw = img.getWidth();

int ih = img.getHeight();

Object hint = RenderingHints.VALUE_INTERPOLATION_BILINEAR;

int type = img.getType() ==0? BufferedImage.TYPE_INT_ARGB : img.getType();

while (iw > targetWidth*2 || ih > targetHeight*2) {

iw = (iw>targetWidth*2) ? iw/2 : iw;

ih = (ih>targetHeight*2) ? ih/2 : ih;

img = scaleImage(img, type, hint, iw, ih);

}

if (iw > targetWidth) {

int iw2 = iw/2;

BufferedImage img2 = scaleImage(img,type,hint,iw2,ih);

if(iw2 < targetWidth) {

img = scaleImage(img, type, hint, targetWidth, ih);

img2 = scaleImage(img2, type, hint, targetWidth, ih);

interp(img2, img, iw-targetWidth, targetWidth-iw2);

}

img = img2;

iw = targetWidth;

if(ih > targetHeight) {

int ih2 =ih/2;

BuffereImage img2 = scaleImage(img, type, hint, iw, ih2);

if(ih2< targetHeight) {

img = scaleimage(img, type, hint, iw, targetHeight);

img2 = scaleImage(img2, type, hint, iw,targetHeight);

interp(imag2, img, ih-targetHeight, targetHeight-ih2);

}

img = img2;

ih=targetHeight;

}

if(iw< targetWidth && ih < targetHeight ) {

img = scaleImage(img, type, hint, targetWidth, targetHeight);

}

ImageIO.write(img, destFile.substring(destFile,lastIndexOf('.')+1), new FileOutputStream(destFile));

} catch(IOException thumbException)

{

thumbException.printStackTrace();

throw new Exception(thumbException);

}

}

private static BufferedImage scaleImage(BufferedImage orig, int type,Object hint, int w, int h)

{

BufferedImage tmp = new BufferedImage(w, h, type);

Graphics2D g2 = tmp.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);

g2.drawImage(orig, 0, 0, w, h, null);

g2.dispose();

return tmp;

}

private static void interp(BufferedImage img1,BufferedImage img2,int weight1,int weight2)

{

float alpha = weight1;

alpha /= (weight1 + weight2);

Graphics2D g2 = img1.createGraphics();

g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));

g2.drawImage(img2, 0, 0, null);

g2.dispose();

}

public static void main(String[] args) throws Exception

{

String sourceFile= "//Image Path ";

BufferedImage img = ImageIO.read(new File(sourceFile));

int iw= img.getWidth();

int ih= img.getHeight();

int targetWidth=74;

double imageHeightPercentage=((double)targetWidth/(double)iw )*100;

imgTotalHeight=ih * (imageHeightPercentage/100);

getHeight=(int) Math.round(imgTotalHeight);

PerfectImageResizer.createThumbnail(sourceFile,"//image path",targetWidth,targetHeight);

}

}

//TO Create Thumbnail method use below code(use this if you need more)

imgWidth = image.getWidth(null);

imgHeight = image.getHeight(null);

int newWidth =100; //if you want the width

double imgHeightPercentage = (newWidth/(double)imgWidth)*100;

double imgTotalHeight=imgHeight*(imgHeightPercentage/100);

int newHeight=(int)Math.round(imgTotalHeight);

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