java小程序图片查看器代码,实现放大、缩小、旋转功能
2个回答
推荐于2017-12-15
展开全部
我只实现了放大缩小功能,旋转还有点问题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();
}
}
}
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();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-08
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询