如何在java swing的图片浏览器中添加一个能实现图片旋转的按钮 5
privatevoidinitComponents(){jToolBar1=newjavax.swing.JToolBar();jButton1=newjavax.swi...
private void initComponents() {
jToolBar1 = new javax.swing.JToolBar();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jLabel1 = new javax.swing.JLabel();
jToolBar1.setRollover(true);
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Picture/2.JPG"))); // NOI18N
jButton1.setFocusable(false);
jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jToolBar1.add(jButton1);
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Picture/1.jpg"))); // NOI18N
jButton2.setFocusable(false);
jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
}); 展开
jToolBar1 = new javax.swing.JToolBar();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jLabel1 = new javax.swing.JLabel();
jToolBar1.setRollover(true);
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Picture/2.JPG"))); // NOI18N
jButton1.setFocusable(false);
jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jToolBar1.add(jButton1);
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Picture/1.jpg"))); // NOI18N
jButton2.setFocusable(false);
jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
}); 展开
1个回答
展开全部
package gui;
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Bean extends JFrame implements ActionListener {
JPanel jPanel;
JLabel jLabel;
Image image;
ImageIcon imageIcon;
JButton jButton;
JButton jButton1;
int w;
int h;
int f;
public Bean() {
super("图片");
jButton = new JButton("变大");
jButton1 = new JButton("反转");
jButton.addActionListener(this);
jButton1.addActionListener(this);
image = Toolkit.getDefaultToolkit().getImage("src/img1.jpg");
jLabel = new JLabel();
jPanel = new JPanel();
ImageIcon imageIcon = new ImageIcon(image);
jLabel.setIcon(imageIcon);
jPanel.add(jLabel);
w = image.getWidth(jLabel);
h = image.getHeight(jLabel);
this.add(jPanel, BorderLayout.NORTH);
this.add(jButton, BorderLayout.SOUTH);
this.add(jButton1, BorderLayout.EAST);
this.setVisible(true);
this.setSize(600, 600);
}
public static void main(String[] args) {
new Bean();
}
public void big() {
w += 20;
h += 20;
BufferedImage bffimage = new BufferedImage(w, h,
BufferedImage.TYPE_3BYTE_BGR);
// 把图片读到bufferedImage中
bffimage.getGraphics().drawImage(image, 0, 0, w, h, null);
// 得到转换后的Image图片
Image realImage = bffimage;
// 可以把图片转化为ImageIcon,(Swing中运用)
ImageIcon img = new ImageIcon(realImage);
jLabel.setIcon(img);
}
public void fz() {
BufferedImage bffimage = new BufferedImage(w, h,
BufferedImage.TYPE_3BYTE_BGR);
// 把图片读到bufferedImage中
bffimage.getGraphics().drawImage(image, 0, 0, w, h, null);
f+=45;
// 得到转换后的Image图片
bffimage=rotateImage(bffimage, f);
Image realImage = bffimage;
// 可以把图片转化为ImageIcon,(Swing中运用)
ImageIcon img = new ImageIcon(realImage);
jLabel.setIcon(img);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton) {
big();
}
if (e.getSource() == jButton1) {
fz();
}
}
/**
* 旋转图片为指定角度
*
* @param bufferedimage
* 目标图像
* @param degree
* 旋转角度
* @return
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
}
追问
谢谢,我试了一下,还是不行,能不能帮我修改一下呢?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |