java对图片任意大小的缩小放大操作

代码如下:

 

package relevantTest;
/*
 * 该类实现了对图片的任意大小的缩放处理,滚动鼠标滚轮对图片进行缩放处理,图片的高度和宽度最好不要超过屏幕的宽高
 */
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;

public class TestMouseWheelEvent extends JFrame{ 
 private static final long serialVersionUID = 1L;
 static Image img=(new ImageIcon("images/王梓盈.jpg")).getImage();
 static int imgWidth=img.getWidth(null);//获得图片的宽度
 static int imgHeight=img.getHeight(null);//获得图片的高度
 static int ScreenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;//获得屏宽
 static int ScreenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;//获得屏高
 static int xCoor=(ScreenWidth-imgWidth)/2;
 static int yCoor=(ScreenHeight-imgHeight)/2;
 public TestMouseWheelEvent(){
  PicsPanel jp=new PicsPanel();
  jp.setBackground(new Color(51,51,51));
  /*
   * 添加鼠标滚轮事件
   */
  jp.addMouseWheelListener(new MouseWheelListener(){
   public void mouseWheelMoved(MouseWheelEvent e){
    if(e.getWheelRotation()<0){//如果鼠标滚轮的“咔哒声”小于零则表示是向上滚动
    imgWidth+=e.getScrollAmount()*10;
    imgHeight+=e.getScrollAmount()*10;
    xCoor=(ScreenWidth-imgWidth)/2;
    yCoor=(ScreenHeight-imgHeight)/2;
    repaint();
    }
    else{//否则表示向下滚动
     imgWidth-=e.getScrollAmount()*3;
     imgHeight-=e.getScrollAmount()*3;
     xCoor=(ScreenWidth-imgWidth)/2;
     yCoor=(ScreenHeight-imgHeight)/2;
     repaint();//e.getScrollAmount()=3,如电脑的设置有关,一般的电脑默认是3
    }
   }
  });
  /**
   * 在该面板上创建弹出右键菜单,“悬浮面板"
   */
  
  JPopupMenu jpm=new JPopupMenu();
     jpm.setBackground(new Color(51,51,51));   
  JMenuItem copy=new JMenuItem("复制(C)");
  JMenuItem cut=new JMenuItem("剪切(X)");
  JMenuItem open=new JMenuItem("打开图片所在的文件夹(O)");
  JMenu setBackgroundColor=new JMenu("设置背景色(S)");
  JMenuItem delete=new JMenuItem("删除(D)");
  JMenuItem property=new JMenuItem("属性");  
  copy.setBackground(new Color(51,51,51));
  copy.setForeground(new Color(204,204,204));
  //copy.setFont(new Font("楷体",Font.BOLD,12));//为什么设置字体后导致弹出菜单出现变慢???
  cut.setBackground(new Color(51,51,51));
  cut.setForeground(new Color(204,204,204));
  open.setBackground(new Color(51,51,51));
  open.setForeground(new Color(204,204,204));
  setBackgroundColor.setBackground(new Color(51,51,51));
  setBackgroundColor.setForeground(new Color(204,204,204));
  delete.setBackground(new Color(51,51,51));
  delete.setForeground(new Color(204,204,204));
  property.setBackground(new Color(51,51,51));
  property.setForeground(new Color(204,204,204));  
  jpm.add(copy);
  jpm.add(cut);
  jpm.addSeparator();  
  jpm.add(open);
  jpm.addSeparator();
  jpm.add(setBackgroundColor);
  jpm.addSeparator();
  jpm.add(delete);
  jpm.addSeparator();
  jpm.add(property);
  jp.setComponentPopupMenu(jpm);//确定弹出菜单的父组件
  /**
   * 添加菜单子项事件监听代码  
   */  
  this.add(jp);
  this.setTitle("测试鼠标滚轮事件");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //默认的框架显示位置是桌面左上角
  this.setSize(1366,768);
  this.setVisible(true);
  
 }
 static class PicsPanel extends JPanel{  
  private static final long serialVersionUID = 1L;
  protected void paintComponent(Graphics g){
   super.paintComponent(g);
   g.drawImage(img,xCoor,yCoor,imgWidth,imgHeight,this);
  }
  
 }
 
 
 
 public static void main(String[] args){  
  new TestMouseWheelEvent();
 }

}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值