仿淘宝图片放大(java GUI实现)

原创 2012年03月27日 14:30:43

全图为原图的缩小

图片并非真的放大(图片真正意义上的放大会失真),而是截取原图的一部分显示

放大后的图片可拖动

 

package come.wang.picture1;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class MagnifyPicture {

 /**
  * 方法的用途:仿淘宝图片放大
  *
  * @param args
  */
 private JFrame frame;
 private TuPian panel1;
 
 private TuPian2 panel2;
 private String imageAddrs = "images\\1.jpg";

 public MagnifyPicture() {
  super();
  frame = new JFrame("图片放大");
  frame.setBounds(300, 200, 900, 600);
  frame.setVisible(true);
  int panel1Width = 300;
  // imageAddrs 图片路径、600,宽、400,高、panel1Width小图宽,小图高是根据原图计算出来的。
  panel2 = new TuPian2(400, 300);
  panel1 = new TuPian(imageAddrs, panel1Width, panel2);
  panel1.setLocation(20, 20);
  panel2.setLocation(320, 20);
  frame.setLayout(null);
  frame.add(panel1);
  frame.add(panel2);
  frame.validate();
  frame.repaint();
  frame.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent arg0) {
    System.exit(0);
   }
  });
 }

 public static void main(String[] args) {
  new MagnifyPicture();
 }

}

 

package come.wang.picture1;

import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class TuPian extends JPanel implements MouseMotionListener {
 private static final long serialVersionUID = 1L;
 protected ImageIcon icon;
 private int size1 = 300;
 private TuPian2 panel2 ;
 private JPanel filedPanel ;
 private Image image ;
 //构造函数的参数:str 图片的路径,size1 面板的宽,高根据图片比例计算得来
 // panel2 显示放大图片面板
 public TuPian(final String str,final int size1,final TuPian2 panel2) {
  super();
  icon = new ImageIcon(str);
  image = icon.getImage();
  panel2.createImage(str);
  panel2.setSourceSize(size1);
  
  this.size1 = size1;
  this.setSize(size1, size1 * image.getHeight(null) / image.getWidth(null));
  
  this.panel2 = panel2;
  filedPanel = new JPanel();
  int fpw = this.getWidth()*panel2.getWidth()/image.getWidth(null);
  int fph = this.getHeight()*panel2.getHeight()/image.getHeight(null);
  filedPanel.setSize(fpw,fph);
  filedPanel.setOpaque(false);
  filedPanel.setBorder(BorderFactory.createEtchedBorder());
  filedPanel.setVisible(false);
  this.setLayout(null);
  this.add(filedPanel);
  //自定义鼠标样式
  Toolkit kit = Toolkit.getDefaultToolkit();   
  Image img = kit.getImage("images\\1.png");
  Cursor dynamiteCuror = kit.createCustomCursor(img, new Point(10,10),"dynamite stick") ;     
  setCursor(dynamiteCuror);
  //自定义鼠标样式
  
  this.addMouseMotionListener(this);
  this.addMouseListener(new MouseAdapter(){
   public void mouseEntered(MouseEvent e) {
    panel2.setVisible(true);
    panel2.createImage(str);
    panel2.setSourceSize(size1);
    
   }
   public void mouseExited(MouseEvent e) {
    filedPanel.setVisible(false);
   
   }
  });
  setOpaque(false);//设置panel为透明的。显示图片是不闪动
 }
 
 protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  // 图片缩放到panel大小
  g.drawImage(image, 0, 0, size1, size1* image.getHeight(null)
    / image.getWidth(null), null);
  this.repaint();

 }

 public void mouseDragged(MouseEvent e) {
 }
 public void mouseMoved(MouseEvent e) {
  Point point = this.getMousePosition();
 // System.out.println("X:"+point.getX()+"Y:"+point.getY());
  int xx = (int)point.getX();
  int yy = (int)point.getY();
  int xxx = xx-filedPanel.getWidth()/2;
  int yyy = yy-filedPanel.getHeight()/2;
  //处理边界
  xxx = xxx<=0?0:xxx;
  yyy = yyy<=0?0:yyy;
  xxx = xxx>=this.getWidth()-filedPanel.getWidth()?this.getWidth()-filedPanel.getWidth():xxx;
  yyy = yyy>=this.getHeight()-filedPanel.getHeight()?this.getHeight()-filedPanel.getHeight():yyy;
  filedPanel.setVisible(true);
  filedPanel.setLocation(xxx,yyy );
  
  panel2.changeImage(xx,yy);
 }
 
 

}

 

package come.wang.picture1;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class TuPian2 extends JPanel {
 private static final long serialVersionUID = 1L;

 protected ImageIcon icon;
 private int w = 200;// 放大面板的宽
 private int h = 200;// 放大面板的高
 private int sourceSize = 300;// 小图面板的宽
 private int XX;// 截图起始点x
 private int YY;// 截图起始点y
 private Image image;
 private int[] begin;//拖动图片起始点
 private int[] end;//拖动图片终点

 public void createImage(String str) {
  icon = new ImageIcon(str);
  image = icon.getImage();
 }

 public void setSourceSize(int sourceSize) {
  this.sourceSize = sourceSize;
 }

 public TuPian2(int w, int h) {// 初始化时候后需要两个参数:宽和高
  super();
  this.w = w;
  this.h = h;
  begin = new int[2];
  end = new int[2];

  // 根据图片的比例设置面板大小
  // h = (w* image.getHeight(null))/ image.getWidth(null);
  this.setSize(w, h);
  setOpaque(false);//设置panel透明
  setVisible(false);
  addMouseMotionListener(new MouseMotionAdapter() {
   public void mouseDragged(MouseEvent arg0) {
    Point  movePoint = getMousePosition();
    if(movePoint!=null){
     end[0] = (int) movePoint.getX();
     end[1] = (int) movePoint.getY();
     int changeX = end[0] - begin[0];
     int changeY = end[1] - begin[1];
     //此函数每调用一次,此次鼠标的结束位置,作为下一次鼠标的开始位置
     begin[0]=end[0];
     begin[1]=end[1];
     
     // 计算移动方向
     XX = changeX < 0 ? XX - changeX : changeX > 0 ? XX = XX
       - changeX : XX;
     YY = changeY < 0 ? YY - changeY : changeY > 0 ? YY = YY
       - changeY : YY;
     drawImage();
    }
   }

  });
  addMouseListener(new MouseAdapter() {
   public void mousePressed(MouseEvent arg0) {
    Point point = getMousePosition();
    begin[0] = (int) point.getX();
    begin[1] = (int) point.getY();
   }
   
  });

 }

 protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.drawImage(image, 0, 0, w, h, this.XX, this.YY, this.XX + w, this.YY
    + h, null);
 }

 public void changeImage(int XX, int YY) {

  // 按比例计算出鼠标“在”实际图像上的位置
  int xx2 = (XX * image.getWidth(null)) / sourceSize;
  int yy2 = (YY * image.getWidth(null)) / sourceSize;
  // 计算截图起始角,使得放大的图片以鼠标为中心
  this.XX = xx2 - w / 2;
  this.YY = yy2 - h / 2;
  drawImage();
 }

 public void drawImage() {
  // 处理边界
  this.YY = this.YY < 0 ? 0 : this.YY;
  this.XX = this.XX < 0 ? 0 : this.XX;
  this.XX = this.XX > image.getWidth(null) - w ? image.getWidth(null) - w
    : this.XX;
  this.YY = this.YY > image.getHeight(null) - h ? image.getHeight(null)
    - h : this.YY;
  // 根据计算参数,绘制放大后的图片(实际是取得图片的部分显示,不是真的放大,小面板上的图片是原图片的缩小)
  this.paintComponent(super.getGraphics());
 }

}

运行效果:

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

python爬虫淘宝图片+GUI+打包成exe

参考:勿在浮沙筑高台 一、操作工具      pycharm+Anaconda2(python2.7)     cmd输入python可以看到python版本号,若输出错误。在环境变量PATH中加分号...

仿淘宝放大图片

Android广告条效果--使用Volley实现网络图片的自动轮播(仿淘宝、网易广告页轮播效果)

Android广告条效果--使用Volley实现网络图片的自动轮播(仿淘宝、网易广告页轮播效果) 最近在做自定义控件时,发现一个极其常用的效果--广告条,即图片的自动轮播效果。现在市面上大多数的APP...

图片局部放大(仿淘宝)

  • 2013-06-03 15:51
  • 218KB
  • 下载

仿淘宝图片放大

  • 2013-05-05 09:16
  • 735KB
  • 下载

Java GUI编程之图片分摊小工具的实现(初级)

这篇文章里面主要分三部分:         第一、该工具的简单介绍;         第二、该工具中涉及到的技术;         第三、源码介绍;   ...

js放大镜效果。类似于淘宝产品内页多图展示代码,鼠标移到图片上有放大效果,方便查看产品细节,此代码适合网店产品展示,

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)