Home » Category » Swing / AWT / SWT / JFace

Swing / AWT / SWT / JFace: Zooming Swing components in JScrollPane

300| Wed, 19 Sep 2007 23:44:00 GMT| schlenzkaa| Comments (3)

In my application i have a JPanel filled with Swing components. I have to zoom this JPanel within a JScrollPane.

I already wrote a JZoomPane, which has a paint method like

public void paint(Graphics g) {

if (zoomFactor != 1) {

Graphics2D g2 = (Graphics2D)g;

g2.scale(zoomFactor, zoomFactor);

}

super.paint(g);

}

There are two problems i don't know how to solve:

1. To manage the repainting, i know i have to write my own RepaintManager, but i don't know which part i have to customize.

2. I have to catch the MouseEvents and transform them against the zoom factor. But i can't use the GlassPane because i have to zoom only the part in the JScrollPane and not the rest of the application. I experimented a little bit with JLayeredPanes but couldn't get them to work.

Could anybody help me or, even better, send me some sample code.

Thanks,

Oliver

Keywords & Tags: zooming, swing, components, jscrollpane, awt, swt, jface

URL: http://java.itags.org/java-swing/50410/
 
«« Prev - Next »» 3 helpful answers below.

Something like this:

public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D)g;

if (scale != 1.0) {

AffineTransform af = new AffineTransform();

af.scale(scale, scale);

oldTransform = g2d.getTransform();

g2d.transform(af);

}

super.paint(g)

if (scale != 1) {

g2d.setTransform(oldTransform);

}

}

and you have to change size of your panel according to new size.

mouse motion event returns x and y coordinates.

e.getX();

e.getY();

newX=scale*e.getX();

newY=scale*e.getY();

best regards

Stas

stanislavla | Sat, 07 Jul 2007 16:25:00 GMT |

1. The problem is not the scaling itself. I get the panel painted in the correct scaled size when it appears for the first time. But when i scroll the panel, the contained components repaint themselves at the original unscaled position in normal size. I think therefore i have to subclass the RepaintManager (somewhere i read something about it).

2. Scaling the MouseEvents isn't the problem also. The problem is to catch the MouseEvents without using the glasspane, because i only want to scale parts of my application (i.e. the content of the JScrollPane).

schlenzkaa | Sat, 07 Jul 2007 16:25:00 GMT |

You will need to change the size and locations of your child components when you adjust the scale on your main panel. I have been trying to do this, but currently it has been unsuccessful.

jon_ixa | Sat, 07 Jul 2007 16:25:00 GMT |

Swing / AWT / SWT / JFace Hot Answers

Swing / AWT / SWT / JFace New questions

Swing / AWT / SWT / JFace Related Categories