我的Java程序怎么修改,点击按钮时才可以旋转图片
importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;publicclasslianxi5extends...
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class lianxi5 extends JFrame{
JPanel jPanel;
JButton jButton;
JLabel jLabel;
Image image;
ImageIcon imageIcon;
float scalew=1;
float scaleh=1;
float xuzh=0;
public lianxi5(){
super("dda");
jPanel=new JPanel();
jButton=new JButton("旋转");
image=Toolkit.getDefaultToolkit().getImage("src/img1.jpg");
imageIcon=new ImageIcon(image);
jLabel=new JLabel();
jLabel.setIcon(imageIcon);
jPanel.add(jLabel);
this.add(jPanel,BorderLayout.NORTH);
this.add(jLabel,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(600,600);
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
turn();
}
});
}
public static void main(String[] args) {
new lianxi5();
}
public void turn(){
int width=image.getWidth(jLabel);
int height=image.getHeight(jLabel);
double scale=1;
scalew=(float)(scalew*scale);
scaleh=(float)(scaleh*scale);
Math ma=new Math();
ma.pow(scalew, scaleh);
ma.round(xuzh=xuzh-5);
Bitmap bitmap=Bitmap
}
}
请你修改一下 展开
import java.awt.event.*;
import java.awt.*;
public class lianxi5 extends JFrame{
JPanel jPanel;
JButton jButton;
JLabel jLabel;
Image image;
ImageIcon imageIcon;
float scalew=1;
float scaleh=1;
float xuzh=0;
public lianxi5(){
super("dda");
jPanel=new JPanel();
jButton=new JButton("旋转");
image=Toolkit.getDefaultToolkit().getImage("src/img1.jpg");
imageIcon=new ImageIcon(image);
jLabel=new JLabel();
jLabel.setIcon(imageIcon);
jPanel.add(jLabel);
this.add(jPanel,BorderLayout.NORTH);
this.add(jLabel,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(600,600);
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
turn();
}
});
}
public static void main(String[] args) {
new lianxi5();
}
public void turn(){
int width=image.getWidth(jLabel);
int height=image.getHeight(jLabel);
double scale=1;
scalew=(float)(scalew*scale);
scaleh=(float)(scaleh*scale);
Math ma=new Math();
ma.pow(scalew, scaleh);
ma.round(xuzh=xuzh-5);
Bitmap bitmap=Bitmap
}
}
请你修改一下 展开
展开全部
又是你,哈哈哈,我把图片放大和翻转都给你粘出来吧,代码如下:
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;
}
}
追问
好像你之前也回答过我的问题额,我有点印象
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询