g2d.setTransform(oldTransform); } public int getIconWidth() { return (int) (realIcon.getIconWidth() * scale); } public int getIconHeight() { return (int) (realIcon.getIconHeight() * scale); } public static void main(String[] args) { JLabel label = new JLabel(); label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
ImageIcon imgIcon = new ImageIcon(ZoomIcon.class.getResource("test.jpg")); Icon zoomIcon = new ZoomIcon(imgIcon, 0.9f); label.setIcon(zoomIcon);
JFrame f = new JFrame(); JPanel p = new JPanel(); p.add(label); f.getContentPane().add(p, BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true);
drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
import java.awt.geom.AffineTransform;import javax.swing.*;public class ZoomIcon
implements Icon
{
private Icon realIcon = null;
private float scale = 1;
public ZoomIcon(Icon icon, float scale)
{
realIcon = icon;
this.scale = scale;
} public void paintIcon(Component c, Graphics g, int x, int y)
{
Graphics2D g2d = (Graphics2D) g;
AffineTransform oldTransform = g2d.getTransform();
g2d.translate(x, y);
g2d.scale(scale, scale);
realIcon.paintIcon(c, g2d, 0, 0);
g2d.setTransform(oldTransform);
} public int getIconWidth()
{
return (int) (realIcon.getIconWidth() * scale);
} public int getIconHeight()
{
return (int) (realIcon.getIconHeight() * scale);
} public static void main(String[] args)
{
JLabel label = new JLabel();
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
ImageIcon imgIcon = new ImageIcon(ZoomIcon.class.getResource("test.jpg"));
Icon zoomIcon = new ZoomIcon(imgIcon, 0.9f);
label.setIcon(zoomIcon);
JFrame f = new JFrame();
JPanel p = new JPanel();
p.add(label);
f.getContentPane().add(p, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Gueber() .能不能加你qq!
372106576