供你参考一下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class lianxi3 extends JFrame implements ActionListener {
JPanel jPanel;
JLabel jLabel;
Image image;
ImageIcon imageIcon;
JButton jButton;
float www = 1;
float hhh = 1;
public lianxi3() {
super("图片");
jButton = new JButton("变大");
jButton.addActionListener(this);
image = Toolkit.getDefaultToolkit().getImage("src/img1.jpg");
jLabel = new JLabel();
jPanel = new JPanel();
imageIcon = new ImageIcon(image);
jLabel.setIcon(imageIcon);
jPanel.add(jLabel);
this.add(jLabel, BorderLayout.NORTH);
this.add(jButton, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(600, 600);
}
public static void main(String[] args) {
new lianxi3();
}
public void big() {
int width = image.getHeight(jLabel);
int height = image.getWidth(jLabel);
double bigger = 1.5;
www = (float) (width * bigger);
hhh = (float) (height * bigger);
BufferedImage bufImg = new BufferedImage(image.getWidth(null), image.getHeight(null),BufferedImage.TYPE_4BYTE_ABGR);
Graphics g = bufImg .createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
imageIcon.setImage(rotateImage(bufImg,30));
//imageIcon.setImage(imageIcon.getImage().getScaledInstance((int)www, (int)hhh, Image.SCALE_DEFAULT));
jLabel.setIcon(imageIcon);
jLabel.setSize((int)www, (int)hhh);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton) {
big();
System.out.println("aaa");
}
}
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int nw = (int) (bufferedimage.getWidth()*1.414);
int nh = (int) (bufferedimage.getHeight()*1.414);
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(nw, nh, type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), nw / 2, nh / 2);
graphics2d.translate(nw/2-w/2, nh/2-h/2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
}