Hello, I’m trying to do some zooming on some components in Swing and images. This is what I have done so far and it does not work…
I have created a subclass of JLabel and overridden the paintComponent method to display an image. I then have a mouse listener so whenever the user clicks, it’ll zoom in.
This does not work at all. I’ve been trying many examples from these and the Sun forums. Nothing works.
You can change the tank.gif picture to anything that suits your desires.
Here is my code: the subclass and main driver class…a look at it would be most helpful!
package drivers;
import interfaces.ZoomLabel;
import javax.swing.JFrame;
import main.ExitListener;
public class ZoomDriver
{
/**
* @param args
*/
public static void main ( String[] args )
{
JFrame frame = new JFrame("Zoom Test");
frame.add(new ZoomLabel());
frame.setSize(400, 400);
frame.setLocation(0, 0);
frame.addWindowListener(new ExitListener());
frame.setVisible(true);
}
}
package interfaces;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import javax.swing.JLabel;
import main.ImageUtilities;
public class ZoomLabel extends JLabel implements MouseListener
{
private AffineTransform tx=new AffineTransform();
private AffineTransformOp op=new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
private BufferedImage image;
public ZoomLabel()
{
image = ImageUtilities.getBufferedImage("tank.gif", this);
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(image,op,100,100);
}
public void mouseClicked ( MouseEvent e )
{
}
public void mouseEntered ( MouseEvent arg0 )
{
}
public void mouseExited ( MouseEvent arg0 )
{
}
public void mousePressed ( MouseEvent e )
{
System.out.println("clicking");
tx = AffineTransform.getTranslateInstance(e.getX(),e.getY());
tx.scale(.5, .5);
tx.translate(-e.getX(),-e.getY());
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
}
public void mouseReleased ( MouseEvent arg0 )
{
}
}
Thank you very much!
Edit:
Whoops, and the ImageUtilities class…
package main;
import java.awt.*;
import java.awt.image.*;
/** A class that simplifies a few common image operations, in
* particular creating a BufferedImage from an image file, and
* using MediaTracker to wait until an image or several images are
* done loading. 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class ImageUtilities {
/** Create Image from a file, then turn that into a BufferedImage.
*/
public static BufferedImage getBufferedImage(String imageFile,
Component c) {
Image image = c.getToolkit().getImage(imageFile);
waitForImage(image, c);
BufferedImage bufferedImage =
new BufferedImage(image.getWidth(c), image.getHeight(c),
BufferedImage.TYPE_4BYTE_ABGR );
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(image, 0, 0, c);
return(bufferedImage);
}
/** Take an Image associated with a file, and wait until it is
* done loading. Just a simple application of MediaTracker.
* If you are loading multiple images, don't use this
* consecutive times; instead use the version that takes
* an array of images.
*/
public static boolean waitForImage(Image image, Component c) {
MediaTracker tracker = new MediaTracker(c);
tracker.addImage(image, 0);
try {
tracker.waitForAll();
} catch(InterruptedException ie) {}
return(!tracker.isErrorAny());
}
/** Take some Images associated with files, and wait until they
* are done loading. Just a simple application of MediaTracker.
*/
public static boolean waitForImages(Image[] images, Component c) {
MediaTracker tracker = new MediaTracker(c);
for(int i=0; i<images.length; i++)
tracker.addImage(images[i], 0);
try {
tracker.waitForAll();
} catch(InterruptedException ie) {}
return(!tracker.isErrorAny());
}
}