HI Please Help me with Zoom of a buffered image

Hi All,

Please help,

I want to zoom the buffered image using Affine Transforms, the code shown below can rotate the buffered image now how to zoom the same buffered image using Affine Transforms.

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.BufferedImage;

import javax.swing.*;

publicclass RotationBoundsextends JPanelimplements ActionListener

{

BufferedImage image;

AffineTransform at =new AffineTransform();

Rectangle2D.Double bounds =new Rectangle2D.Double();

double theta = 0;

double thetaInc = Math.PI / 2;

publicvoid actionPerformed(ActionEvent e)

{

theta += thetaInc;

setTransform();

repaint();

}

privatevoid setTransform()

{

int iw = image.getWidth();

int ih = image.getHeight();

double cos = Math.abs(Math.cos(theta));

double sin = Math.abs(Math.sin(theta));

double width = iw * cos + ih * sin;

double height = ih * cos + iw * sin;

double x = (getWidth() - iw) / 2;

double y = (getHeight() - ih) / 2;

at.setToTranslation(x, y);

at.rotate(theta, iw / 2.0, ih / 2.0);

x = (getWidth() - width) / 2;

y = (getHeight() - height) / 2;

// Set bounding rectangle that will frame the image rotated

// with this transform. Use this width and height to make a

// new BuffferedImage that will hold this rotated image.

// AffineTransformOp doesn't have this size information in

// the translation/rotation transform it receives.

bounds.setFrame(x - 1, y - 1, width + 1, height + 1);

}

protectedvoid paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

setBackground(Color.gray);

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

if (image ==null)

initImage();

g2.drawRenderedImage(image, at);

// g2.setPaint(Color.blue);

g2.draw(bounds);

g2.setPaint(Color.green.darker());

g2.fill(new Ellipse2D.Double(getWidth() / 2 - 2,

getHeight() / 2 - 2, 4, 4));

}

privatevoid initImage()

{

int w = 360, h = 300;

image =new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = image.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setPaint(new Color(220, 240, 240));

g2.fillRect(0, 0, w, h);

g2.setPaint(Color.red);

g2.drawString("Text for test", 50, 50);

g2.drawRect(0, 0, w - 1, h - 1);

g2.dispose();

g2.setTransform(at);

//setTransform();

}

private JPanel getLast()

{

JButton rotateButton =new JButton("Rotate");

rotateButton.addActionListener(this);

JButton zoomButton =new JButton("Zoom");

JPanel panel =new JPanel();

panel.add(rotateButton);

panel.add(zoomButton);

return panel;

}

publicstaticvoid main(String[] args)

{

RotationBounds test =new RotationBounds();

JFrame f =new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test);

f.getContentPane().add(test.getLast(),"North");

f.setSize(400, 400);

f.setLocation(0, 0);

f.setVisible(true);

}

}

Message was edited by:

New_to_Java

[5738 byte] By [New_to_Javaa] at [2007-11-27 1:59:04]
# 1

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.BufferedImage;

import java.util.Hashtable;

import javax.swing.*;

import javax.swing.event.*;

public class RotationZoom extends JPanel {

AffineTransform at = new AffineTransform();

Point2D.Double imageLoc = new Point2D.Double(100.0, 50.0);

Rectangle2D.Double bounds = new Rectangle2D.Double();

BufferedImage image;

double theta = 0;

double scale = 1.0;

final int PAD = 20;

public RotationZoom() {

initImage();

}

public void addNotify() {

super.addNotify();

setTransform();

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,

RenderingHints.VALUE_INTERPOLATION_BICUBIC);

g2.drawRenderedImage(image, at);

g2.setPaint(Color.red);

g2.draw(bounds);

}

public Dimension getPreferredSize() {

return new Dimension((int)(imageLoc.x + Math.ceil(bounds.width)) + PAD,

(int)(imageLoc.y + Math.ceil(bounds.height)) + PAD);

}

private void update() {

setTransform();

revalidate();

repaint();

}

private void setTransform() {

int iw = image.getWidth();

int ih = image.getHeight();

double cos = Math.abs(Math.cos(theta));

double sin = Math.abs(Math.sin(theta));

double width = iw*cos + ih*sin;

double height = ih*cos + iw*sin;

at.setToTranslation(imageLoc.x, imageLoc.y);

at.rotate(theta, scale*iw/2.0, scale*ih/2.0);

at.scale(scale, scale);

double x = imageLoc.x - scale*(width - iw)/2.0;

double y = imageLoc.y - scale*(height - ih)/2.0;

bounds.setFrame(x, y, scale*width, scale*height);

}

private void initImage() {

int w = 240, h = 180;

int type = BufferedImage.TYPE_INT_RGB;

image = new BufferedImage(w,h,type);

Graphics2D g2 = image.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setBackground(new Color(220,220,240));

g2.clearRect(0,0,w,h);

g2.setPaint(Color.red);

g2.draw(new CubicCurve2D.Double(0, h, w*4/3.0, h/4.0,

-w/3.0, h/4.0, w, h));

g2.setPaint(Color.green.darker());

g2.draw(new Rectangle2D.Double(w/3.0, h/3.0, w/3.0, h/3.0));

g2.dispose();

}

private JPanel getControls() {

JSlider rotateSlider = new JSlider(-180, 180, 0);

rotateSlider.setMajorTickSpacing(30);

rotateSlider.setMinorTickSpacing(10);

rotateSlider.setPaintTicks(true);

rotateSlider.setPaintLabels(true);

rotateSlider.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e) {

int value = ((JSlider)e.getSource()).getValue();

theta = Math.toRadians(value);

update();

}

});

rotateSlider.setBorder(BorderFactory.createTitledBorder("theta"));

int min = 50, max = 200, inc = 25;

JSlider zoomSlider = new JSlider(min, max, 100);

zoomSlider.setMajorTickSpacing(inc);

zoomSlider.setMinorTickSpacing(5);

zoomSlider.setPaintTicks(true);

zoomSlider.setLabelTable(getLabelTable(min, max, inc));

zoomSlider.setPaintLabels(true);

zoomSlider.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e) {

int value = ((JSlider)e.getSource()).getValue();

scale = value/100.0;

update();

}

});

zoomSlider.setBorder(BorderFactory.createTitledBorder("scale"));

JPanel panel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.weightx = 1.0;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.gridwidth = GridBagConstraints.REMAINDER;

panel.add(rotateSlider, gbc);

panel.add(zoomSlider, gbc);

return panel;

}

private Hashtable getLabelTable(int min, int max, int inc) {

Hashtable<Integer,JComponent> table = new Hashtable<Integer,JComponent>();

for(int j = min; j <= max; j += inc) {

JLabel label = new JLabel(String.format("%.2f", j/100.0));

table.put(new Integer(j), label);

}

return table;

}

public static void main(String[] args) {

RotationZoom test = new RotationZoom();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new JScrollPane(test));

f.getContentPane().add(test.getControls(), "Last");

f.setSize(500,500);

f.setLocation(200,100);

f.setVisible(true);

}

}

crwooda at 2007-7-12 1:36:27 > top of Java-index,Security,Cryptography...
# 2

Hi,

The code what you have sent works fine initially but when i zoom it to the maximum it loses its clarity, I Want to zoom in percentage from a JComboBox and it should not lose clarity, i have been trying but i haven't been able to come up with any thing, which would help, Lets see, thanks for the help. Do try if you can come up with something it will be great.

New_to_Javaa at 2007-7-12 1:36:27 > top of Java-index,Security,Cryptography...
# 3
If image is loosing clarity, turn anti-aliasing off.
Kilwcha at 2007-7-12 1:36:27 > top of Java-index,Security,Cryptography...