提问了很多次的问题,Java,如何修改下面的程序,才能让图片可以选择

新增加一个旋转按钮,当点击按钮的时候,图片向左转30度importjavax.swing.*;importjava.awt.*;importjava.awt.event.... 新增加一个旋转按钮,当点击按钮的时候,图片向左转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);
}

}
展开
 我来答
王勃啊
2015-01-06 · TA获得超过1.1万个赞
知道大有可为答主
回答量:5015
采纳率:62%
帮助的人:4101万
展开全部

供你参考一下吧。。。。



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;
    }
    
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式