Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to zoom/scale BufferedImage zoom level > 100% ?

843807Apr 25 2003 — edited Apr 28 2003
I'm using Graphics2D drawing commands in a BufferedImage which will then be placed into an ImageIcon then into a JLabel, and then fnially into a JScrollPane.

I want my scaling to work from 0-1000%, and it currently works great from 0-100% when using BufferedImage's getSubimage(), but it crashes when zoom level is greater than 100%.

This is because getSubimage() cannot return a rectangle that's larger than the dimensions of the BufferedImage.

I want my logical world coordinate system to be same same regardless of the zoom level, for example 100x100 pixels (those are the coordinates that I want to be able use when drawing regardless of the zoom level.)

It seems that when zooming > 100%, that the BufferedImage will physically have to be larger than 100x100 or the drawing will get clipped (it's clipping now on the right and bottom sides).

So, how can I separate the world coordinates from the physical coordinates?

Here's the code: (JDK 1.4.1)

protected void paintComponent(Graphics g) {
super.paintComponent(g);

// an attempt at memory conservation

BufferedImage drawingBufferedImage = null;
ImageIcon drawingImageIcon = null;
drawingLabel.setIcon(null);

imagex = (int) (pageWidthIn * pageDpi);
imagey = (int) (pageHeightIn * pageDpi);

// Create the BufferedImage as an 8 bit, 256 color image to save memory

drawingBufferedImage = new BufferedImage(imagex, imagey, BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g2 = (Graphics2D) drawingBufferedImage.createGraphics();
g2.transform(getAffineTransform(scaleFactor));

// paint the background of the canvas

g2.setBackground(Color.white);
java.awt.geom.Rectangle2D rect = new Rectangle2D.Float(0, 0, imagex, imagey);
g2.setPaint(Color.white);
g2.fill(rect);
g2.setPaint(Color.black);

// { draw a bunch of stuff like text, lines, rectangles, etc. using Graphics2D }

// this call works great from 0%-100% scaling, but crashes when > 100%

drawingImageIcon = new ImageIcon(drawingBufferedImage.getSubimage(0, 0,
(int) (imagex * scaleFactor), (int) (imagey * scaleFactor)));

drawingLabel.setIcon(drawingImageIcon);
drawingLabel.setPreferredSize(new Dimension(drawingImageIcon.getIconWidth(),drawingImageIcon.getIconHeight()));

doLayout();
g2.dispose();

drawingLabel.repaint();
drawingBufferedImage.flush();
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 26 2003
Added on Apr 25 2003
3 comments
548 views