📄 display.java
字号:
* scaling factor along the x-dimension, so be careful when
* using this value in rare non-uniform scaling cases.
*/
public double getScale() {
return m_transform.getScaleX();
}
/**
* Returns the x-coordinate of the top-left of the display,
* in absolute (item-space) co-ordinates.
* @return the x co-ord of the top-left corner, in absolute coordinates
*/
public double getDisplayX() {
return -m_transform.getTranslateX();
}
/**
* Returns the y-coordinate of the top-left of the display,
* in absolute (item-space) co-ordinates.
* @return the y co-ord of the top-left corner, in absolute coordinates
*/
public double getDisplayY() {
return -m_transform.getTranslateY();
}
/**
* Pans the view provided by this display in screen coordinates.
* @param dx the amount to pan along the x-dimension, in pixel units
* @param dy the amount to pan along the y-dimension, in pixel units
*/
public synchronized void pan(double dx, double dy) {
m_tmpPoint.setLocation(dx, dy);
m_itransform.transform(m_tmpPoint, m_tmpPoint);
double panx = m_tmpPoint.getX();
double pany = m_tmpPoint.getY();
m_tmpPoint.setLocation(0, 0);
m_itransform.transform(m_tmpPoint, m_tmpPoint);
panx -= m_tmpPoint.getX();
pany -= m_tmpPoint.getY();
panAbs(panx, pany);
}
/**
* Pans the view provided by this display in absolute (i.e. item-space)
* coordinates.
* @param dx the amount to pan along the x-dimension, in absolute co-ords
* @param dy the amount to pan along the y-dimension, in absolute co-ords
*/
public synchronized void panAbs(double dx, double dy) {
damageReport();
m_transform.translate(dx, dy);
try {
m_itransform = m_transform.createInverse();
} catch ( Exception e ) { /*will never happen here*/ }
}
/**
* Pans the display view to center on the provided point in
* screen (pixel) coordinates.
* @param p the point to center on, in screen co-ords
*/
public synchronized void panTo(Point2D p) {
m_itransform.transform(p, m_tmpPoint);
panToAbs(m_tmpPoint);
}
/**
* Pans the display view to center on the provided point in
* absolute (i.e. item-space) coordinates.
* @param p the point to center on, in absolute co-ords
*/
public synchronized void panToAbs(Point2D p) {
double sx = m_transform.getScaleX();
double sy = m_transform.getScaleY();
double x = p.getX(); x = (Double.isNaN(x) ? 0 : x);
double y = p.getY(); y = (Double.isNaN(y) ? 0 : y);
x = getWidth() /(2*sx) - x;
y = getHeight()/(2*sy) - y;
double dx = x-(m_transform.getTranslateX()/sx);
double dy = y-(m_transform.getTranslateY()/sy);
damageReport();
m_transform.translate(dx, dy);
try {
m_itransform = m_transform.createInverse();
} catch ( Exception e ) { /*will never happen here*/ }
}
/**
* Zooms the view provided by this display by the given scale,
* anchoring the zoom at the specified point in screen coordinates.
* @param p the anchor point for the zoom, in screen coordinates
* @param scale the amount to zoom by
*/
public synchronized void zoom(final Point2D p, double scale) {
m_itransform.transform(p, m_tmpPoint);
zoomAbs(m_tmpPoint, scale);
}
/**
* Zooms the view provided by this display by the given scale,
* anchoring the zoom at the specified point in absolute coordinates.
* @param p the anchor point for the zoom, in absolute
* (i.e. item-space) co-ordinates
* @param scale the amount to zoom by
*/
public synchronized void zoomAbs(final Point2D p, double scale) {;
double zx = p.getX(), zy = p.getY();
damageReport();
m_transform.translate(zx, zy);
m_transform.scale(scale,scale);
m_transform.translate(-zx, -zy);
try {
m_itransform = m_transform.createInverse();
} catch ( Exception e ) { /*will never happen here*/ }
}
/**
* Rotates the view provided by this display by the given angle in radians,
* anchoring the rotation at the specified point in screen coordinates.
* @param p the anchor point for the rotation, in screen coordinates
* @param theta the angle to rotate by, in radians
*/
public synchronized void rotate(final Point2D p, double theta) {
m_itransform.transform(p, m_tmpPoint);
rotateAbs(m_tmpPoint, theta);
}
/**
* Rotates the view provided by this display by the given angle in radians,
* anchoring the rotation at the specified point in absolute coordinates.
* @param p the anchor point for the rotation, in absolute
* (i.e. item-space) co-ordinates
* @param theta the angle to rotation by, in radians
*/
public synchronized void rotateAbs(final Point2D p, double theta) {
double zx = p.getX(), zy = p.getY();
damageReport();
m_transform.translate(zx, zy);
m_transform.rotate(theta);
m_transform.translate(-zx, -zy);
try {
m_itransform = m_transform.createInverse();
} catch ( Exception e ) { /*will never happen here*/ }
}
/**
* Animate a pan along the specified distance in screen (pixel)
* co-ordinates using the provided duration.
* @param dx the amount to pan along the x-dimension, in pixel units
* @param dy the amount to pan along the y-dimension, in pixel units
* @param duration the duration of the animation, in milliseconds
*/
public synchronized void animatePan(double dx, double dy, long duration) {
double panx = dx / m_transform.getScaleX();
double pany = dy / m_transform.getScaleY();
animatePanAbs(panx,pany,duration);
}
/**
* Animate a pan along the specified distance in absolute (item-space)
* co-ordinates using the provided duration.
* @param dx the amount to pan along the x-dimension, in absolute co-ords
* @param dy the amount to pan along the y-dimension, in absolute co-ords
* @param duration the duration of the animation, in milliseconds
*/
public synchronized void animatePanAbs(double dx, double dy, long duration) {
m_transact.pan(dx,dy,duration);
}
/**
* Animate a pan to the specified location in screen (pixel)
* co-ordinates using the provided duration.
* @param p the point to pan to in screen (pixel) units
* @param duration the duration of the animation, in milliseconds
*/
public synchronized void animatePanTo(Point2D p, long duration) {
Point2D pp = new Point2D.Double();
m_itransform.transform(p,pp);
animatePanToAbs(pp,duration);
}
/**
* Animate a pan to the specified location in absolute (item-space)
* co-ordinates using the provided duration.
* @param p the point to pan to in absolute (item-space) units
* @param duration the duration of the animation, in milliseconds
*/
public synchronized void animatePanToAbs(Point2D p, long duration) {
m_tmpPoint.setLocation(0,0);
m_itransform.transform(m_tmpPoint,m_tmpPoint);
double x = p.getX(); x = (Double.isNaN(x) ? 0 : x);
double y = p.getY(); y = (Double.isNaN(y) ? 0 : y);
double w = getWidth() /(2*m_transform.getScaleX());
double h = getHeight()/(2*m_transform.getScaleY());
double dx = w-x+m_tmpPoint.getX();
double dy = h-y+m_tmpPoint.getY();
animatePanAbs(dx,dy,duration);
}
/**
* Animate a zoom centered on a given location in screen (pixel)
* co-ordinates by the given scale using the provided duration.
* @param p the point to center on in screen (pixel) units
* @param scale the scale factor to zoom by
* @param duration the duration of the animation, in milliseconds
*/
public synchronized void animateZoom(final Point2D p, double scale, long duration) {
Point2D pp = new Point2D.Double();
m_itransform.transform(p,pp);
animateZoomAbs(pp,scale,duration);
}
/**
* Animate a zoom centered on a given location in absolute (item-space)
* co-ordinates by the given scale using the provided duration.
* @param p the point to center on in absolute (item-space) units
* @param scale the scale factor to zoom by
* @param duration the duration of the animation, in milliseconds
*/
public synchronized void animateZoomAbs(final Point2D p, double scale, long duration) {
m_transact.zoom(p,scale,duration);
}
/**
* Animate a pan to the specified location in screen (pixel)
* co-ordinates and zoom to the given scale using the provided duration.
* @param p the point to center on in screen (pixel) units
* @param scale the scale factor to zoom by
* @param duration the duration of the animation, in milliseconds
*/
public synchronized void animatePanAndZoomTo(final Point2D p, double scale, long duration) {
Point2D pp = new Point2D.Double();
m_itransform.transform(p,pp);
animatePanAndZoomToAbs(pp,scale,duration);
}
/**
* Animate a pan to the specified location in absolute (item-space)
* co-ordinates and zoom to the given scale using the provided duration.
* @param p the point to center on in absolute (item-space) units
* @param scale the scale factor to zoom by
* @param duration the duration of the animation, in milliseconds
*/
public synchronized void animatePanAndZoomToAbs(final Point2D p, double scale, long duration) {
m_transact.panAndZoom(p,scale,duration);
}
/**
* Indicates if a view transformation is currently underway.
* @return true if a transform is in progress, false otherwise
*/
public boolean isTranformInProgress() {
return m_transact.isRunning();
}
/**
* Activity for conducting animated view transformations.
*/
private class TransformActivity extends Activity {
// TODO: clean this up to be more general...
// TODO: change mechanism so that multiple transform
// activities can be running at once?
private double[] src, dst;
private AffineTransform m_at;
public TransformActivity() {
super(2000,20,0);
src = new double[6];
dst = new double[6];
m_at = new AffineTransform();
setPacingFunction(new SlowInSlowOutPacer());
}
private AffineTransform getTransform() {
if ( this.isScheduled() )
m_at.setTransform(dst[0],dst[1],dst[2],dst[3],dst[4],dst[5]);
else
m_at.setTransform(m_transform);
return m_at;
}
public void panAndZoom(final Point2D p, double scale, long duration) {
AffineTransform at = getTransform();
this.cancel();
setDuration(duration);
m_tmpPoint.setLocation(0,0);
m_itransform.transform(m_tmpPoint,m_tmpPoint);
double x = p.getX(); x = (Double.isNaN(x) ? 0 : x);
double y = p.getY(); y = (Double.isNaN(y) ? 0 : y);
double w = getWidth() /(2*m_transform.getScaleX());
double h = getHeight()/(2*m_transform.getScaleY());
double dx = w-x+m_tmpPoint.getX();
double dy = h-y+m_tmpPoint.getY();
at.translate(dx,dy);
at.translate(p.getX(), p.getY());
at.scale(scale,scale);
at.translate(-p.getX(), -p.getY());
at.getMatrix(dst);
m_transform.getMatrix(src);
this.run();
}
public void pan(double dx, double dy, long duration) {
AffineTransform at = getTransform();
this.cancel();
setDuration(duration);
at.translate(dx,dy);
at.getMatrix(dst);
m_transform.getMatrix(src);
this.run();
}
public void zoom(final Point2D p, double scale, long duration) {
AffineTransform at = getTransform();
this.cancel();
setDuration(duration);
double zx = p.getX(), zy = p.getY();
at.translate(zx, zy);
at.scale(scale,scale);
at.translate(-zx, -zy);
at.getMatrix(dst);
m_transform.getMatrix(src);
this.run();
}
protected void run(long elapsedTime) {
double f = getPace(elapsedTime);
damageReport();
m_transform.setTransform(
src[0] + f*(dst[0]-src[0]),
src[1] + f*(dst[1]-src[1]),
src[2] + f*(dst[2]-src[2]),
src[3] + f*(dst[3]-src[3]),
src[4] + f*(dst[4]-src[4]),
src[5] + f*(dst[5]-src[5])
);
try {
m_itransform = m_transform.createInverse();
} catch ( Exception e ) { /* won't happen */ }
repaint();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -