While we are talking about changes to JSVGScrollPane, I made a subclass to
implement some changes that I found necessary. Also, I think it would be
great if there were methods like the JScrollPane's scroll bar policies,
however this doesn't look to feasible via a subclass.
I don't know the best way to do this, so I am just going to paste my working
file into this message. The main things I needed were to support:
* Zooming in, center the part of the canvas I want centered when the
zoom is completed
* Enabling "fit width" or "fit to page". To do this I needed a way to
get the size of the viewable area. (Kinda like the JViewport size). This
is complicated by the fact that the scrollbars cannot currently be set to
always visible
public class DJSVGScrollPane extends JSVGScrollPane {
private Point zoomPoint;
public DJSVGScrollPane(JSVGCanvas canvas) {
super(canvas);
canvas.addGVTTreeRendererListener( new RendererListener() );
addMouseWheelListener( new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
vertical.setValue(vertical.getValue() + (10 * e.getWheelRotation()));
}
});
}
public JScrollBar getHorizontalScrollBar() {
return horizontal;
}
public JScrollBar getVerticalScrollBar() {
return vertical;
}
public Dimension getMaximumViewableArea() {
//We have to use the bounds of the enter pane and subtract the scrollbar
sizes. We can't use the bounds of the canvas since
//the scroll bars may not be visible, yet might need to be when we are
done.
Rectangle rect = getBounds();
Dimension rval = new Dimension(rect.width,rect.height);
rval.width -= vertical.getWidth();
rval.height -= horizontal.getHeight();
return rval;
}
private Dimension getViewportSize() {
Dimension rval = null;
Component[] comps = getComponents();
for (int i = 0; i < comps.length; i++) {
if (comps[i] instanceof JSVGCanvas) {
Rectangle rect = comps[i].getBounds();
rval = new Dimension(rect.width,rect.height);
break;
}
}
return rval;
}
public Rectangle getCurrentSection() {
int x = horizontal.getValue();
int y = vertical.getValue();
Dimension d = getViewportSize();
int width = d.width;
int height = d.height;
return new Rectangle(x,y,width,height);
}
public void setScale(double newScale, Point newCenter) {
AffineTransform currentTransform = canvas.getRenderingTransform();
double currentScale = currentTransform.getScaleX(); //We also scale X and
Y together, so just getting X here is sufficient
double relativeScale = newScale / currentScale;
AffineTransform newTransform =
AffineTransform.getScaleInstance(newScale,newScale);
Rectangle currRect = getCurrentSection();
if (newCenter == null) {
//If the scroll bars are adjusted (non-zero values), then we want to
center the view. If not, then we want them to stay there
int newX;
if (currRect.x == 0)
newX = 0;
else
newX = currRect.x + currRect.width / 2;
int newY;
if (currRect.y == 0)
newY = 0;
else
newY = currRect.y + currRect.height / 2;
//set p to be center of displayed area
newCenter = new Point(newX,newY);
}
else {
newCenter.x += currRect.x;
newCenter.y += currRect.y;
}
int halfOfViewWidth = currRect.width / 2;
int halfOfViewHeight = currRect.height / 2;
int newX = (int)Math.max(0, (newCenter.x * relativeScale -
halfOfViewWidth));
int newY = (int)Math.max(0, (newCenter.y * relativeScale -
halfOfViewHeight));
zoomPoint = new Point(newX, newY);
canvas.setRenderingTransform(newTransform);
reset();
}
private final class RendererListener implements GVTTreeRendererListener {
public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
}
public void gvtRenderingStarted(GVTTreeRendererEvent e) {
resizeScrollBars();
if (zoomPoint != null) {
if (zoomPoint.x > getHorizontalScrollBar().getMaximum())
zoomPoint.x = getHorizontalScrollBar().getMaximum();
if (zoomPoint.y > getVerticalScrollBar().getMaximum())
zoomPoint.y = getVerticalScrollBar().getMaximum();
getHorizontalScrollBar().setValue(zoomPoint.x);
getVerticalScrollBar().setValue(zoomPoint.y);
}
}
public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
zoomPoint = null;
}
public void gvtRenderingCancelled(GVTTreeRendererEvent e) {
}
public void gvtRenderingFailed(GVTTreeRendererEvent e) {
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]