Java,如何修改下面的程序,才能让图片可以选择

希望是新增加一个转左按钮,当点击按钮,图片左转30度import javax.swing.*;import java.awt.*;import java.awt.event.*; public class lianxi3 extends JFrame {JPanel jPanel,jPanel1;JLabel jLabel;Image image;ImageIcon imageIcon;JButton jButton,jButton1;float www=1;float hhh=1;public lianxi3() { super("图片"); jPanel1=new JPanel(); jButton = new JButton("变大"); jButton1 = new JButton("变小"); image = Toolkit.getDefaultToolkit().getImage("src/img1.jpg"); jLabel = new JLabel(); jPanel1.add(jButton); jPanel1.add(jButton1); imageIcon = new ImageIcon(image); jLabel.setIcon(imageIcon); this.add(jLabel, BorderLayout.NORTH); this.add(jPanel1, BorderLayout.SOUTH); this.setVisible(true); this.setSize(600, 600); jButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if(e.getSource()==jButton){ big(); } } }); jButton1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if(e.getSource()==jButton1){ small(); } } });} public static void main(String[] args) { new lianxi3(); } public void big(){ int width=imageIcon.getIconWidth(); int height=imageIcon.getIconWidth(); double bigger=1.2; www=(float)(width*bigger); hhh=(float)(height*bigger); System.out.println(www+""+hhh); Math.pow(www,hhh); image = image.getScaledInstance((int)www, (int)hhh, Image.SCALE_DEFAULT); imageIcon = new ImageIcon(image); jLabel.setIcon(imageIcon); } public void small(){ int width=imageIcon.getIconWidth(); int height=imageIcon.getIconWidth(); double bigger=0.8; www=(float)(width*bigger); hhh=(float)(height*bigger); System.out.println(www+""+hhh); Math.pow(www,hhh); image = image.getScaledInstance((int)www, (int)hhh, Image.SCALE_DEFAULT); imageIcon = new ImageIcon(image); jLabel.setIcon(imageIcon); }}
2026年09月24日 11:15
有1个网友回答
网友(1):

供你参考一下:


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;
    }
    
}