Pages

Subscribe:

Labels

Friday, July 20, 2012

Development : Zoom

Zooming is done by transforming the drawing of pieces.
protected void draw (Graphics g){
Image img = getImage();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
AffineTransform at;
at = AffineTransform.getScaleInstance(scale,scale);
at.translate(getPuzzleX(),getPuzzleY());
g2.drawImage(img, at,null);
}

The result:
initially
zoom out once








zoom out twice

Though it may look correct, I could not manageto set the boundary box and the updated relative position of the piecescorrectly. Therefore, when it is zoomed, the pieces cannot be moved properly.






When moved
After repaint













Thefunction that is used to scale the piece :
public void scale (double scale, int width, int height)
{
this.scale = scale;
double halfWidth = 0.5 * scale *MenuListener.getFrameWidth();
double halfHeight = 0.5 * scale *MenuListener.getFrameHeight();
double x,y;
x = halfWidth - scale * this.curWidth;
y = halfHeight - scale * this.curHeight
this.curWidth = (int)(scale*this.curWidth);
this.curHeight= (int)(scale*this.curHeight);
//debug string
System.out.println ("half width :" +halfWidth);
System.out.println ("current width: "+ this.curWidth);
System.out.println ("Puzzle X :" +getPuzzleX());
System.out.println ("X : " + x);

//this.setPuzzlePosition((int)x,(int)y);
recomputeImageData();
}

Part of the result of the debug string:


The “Puzzle X “should have determined the topleft corner of the piece. However, it showed unexpected result. Therefore, whenthe setPuzzlePosition is uncommented, all the pieces’ positions are wrong.


Currently, I am still finding way to get the re-positioningcorrect.




0 comments:

Post a Comment