发布网友 发布时间:2022-04-22 02:05
共2个回答
热心网友 时间:2023-09-09 16:24
我只实现了放大缩小功能,旋转还有点问题package com.lzw;import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/*
* @功能:实现图片的放大缩小与旋转功能
* @日期:2011.6.29
*/
public class OperationOnPic extends JFrame{
private JPanel imageJpanel;
private JPanel southJpanel;
private JButton rorateButton;
private Mycanvas canvas ;
private int imgWidth, imgHeight;
private JSlider jSlide; //滑动块组建
// private JScrollPane jScrollPane ;
Image img;
private static int num=0;
private static int newWidth=0;
private static int newHeigth=0;
public static boolean isroate=false;
public OperationOnPic(){
setTitle("图片操作");
//设置窗体大小
setSize(800, 600);
//设置窗体显示在*位置
setLocationRelativeTo(null);
//URL imgUrl = OperationOnPic.class.getResource("2.jpg"); //获得图片路径
//img = Toolkit.getDefaultToolkit().getImage(imgUrl);// 获取图片资源
ImageIcon image=new ImageIcon(OperationOnPic.class.getResource("2.jpg"));
img=image.getImage();
canvas=new Mycanvas();
//调用布局方法
init();
//设置窗体可见
setVisible(true);
}
public void init(){
setDefaultCloseOperation(this.EXIT_ON_CLOSE); //设置窗体可关闭
imageJpanel=new JPanel();
southJpanel=new JPanel();
imageJpanel.setLayout(new BorderLayout());
imageJpanel.add(canvas,BorderLayout.CENTER);
imageJpanel.add(getJSlider(),BorderLayout.SOUTH);
rorateButton=new JButton("图片旋转");
rorateButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
num+=5;
isroate=true;
canvas.repaint();
isroate=false;
}
});
southJpanel.add(rorateButton); //将按钮添加到面板上
this.getContentPane().add(imageJpanel,BorderLayout.CENTER);
this.getContentPane().add(southJpanel,BorderLayout.SOUTH);
}
public JSlider getJSlider(){
if(jSlide==null){
jSlide=new JSlider(); //实例化一个滑动块对象
jSlide.setMaximum(500); //设置滑动块的最大取值
jSlide.setMinimum(1); //设置滑动块的最小取值
jSlide.setValue(50); //设置滑动块当前值
jSlide.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
canvas.repaint(); //重新绘制图像
}
});
}
return jSlide;
}
public static void main(String[] args) {
new OperationOnPic();
}
class Mycanvas extends Canvas{
public void paint(final Graphics g){ //重写paint
if(isroate==false){
imgWidth=img.getWidth(this);
imgHeight=img.getHeight(this);
float value=jSlide.getValue(); //取得滑动块的值
newWidth=(int)(imgWidth*value/100);
newHeigth=(int)(imgHeight*value/100);
g.drawImage(img,0,0,newWidth,newHeigth,this);
g.dispose();
}else{
num+=5;
Graphics2D g2=(Graphics2D)g;
g2.rotate(Math.toRadians(num));
g2.drawImage(img,0,0,400,400,this);
g.dispose();
}
}
}
热心网友 时间:2023-09-09 16:24
http://wenku.baidu.com/view/20e00e05cc1755270722081a.html 自己去看看吧,蛮详细的