Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>JAVA基础>>用Swing处理图片缩放和拖动
用Swing处理图片缩放和拖动
2005-08-17   来源:CSDN  作者:javamxj

好久没发表文章了!

最近在研究applet怎么处理图片缩放,现简单做了一个例子,与大家分享!

package applet;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class ImageApplet extends JApplet {

 private JPanel jContentPane = null;

 private JPanel jPanel = null;

 

 ImageIcon imageIcon = new ImageIcon("images/icon/common/det_dis.gif");

 /**
  * This is the default constructor
  */
 public ImageApplet() {
  super();
 }

 /**
  * This method initializes this
  *
  * @return void
  */
 public void init() {
  this.setSize(640, 480);
  this.setContentPane(getJContentPane());
 }

 /**
  * This method initializes jContentPane
  *
  * @return javax.swing.JPanel
  */
 private JPanel getJContentPane() {
  if (jContentPane == null) {
   jContentPane = new JPanel();
   jContentPane.setLayout(null);
   jContentPane.add(new MainPanel(imageIcon), null);
  }
  return jContentPane;
 }

 /**
  * This method initializes jPanel
  *
  * @return javax.swing.JPanel
  */
 class MainPanel extends JPanel implements MouseListener,
   MouseMotionListener {
  int x1, y1;
//用来表示向东、南、西、北、东、南、东、北、西南、西北八个方向缩放点
  private JLabel northWest = null;

  private JLabel north = null;

  private JLabel northEast = null;

  private JLabel east = null;

  private JLabel southEast = null;

  private JLabel south = null;

  private JLabel southWest = null;

  private JLabel west = null;
//图片
  ImageIcon imageIcon = null;
//是否可缩放标记  
  boolean isSelected = false;
  
  public MainPanel(ImageIcon imageIcon) {
   this.imageIcon = imageIcon;
   this.setBounds(256,107,40,30);
   x1 = this.getWidth();
   y1 = this.getHeight();
   //初始化东、南、西、北、东、南、东、北、西南、西北八个方向缩放点
   northWest = new JLabel();
   northWest.setText("");
   northWest.setBorder(new LineBorder(Color.WHITE, 1));
   northWest.addMouseListener(this);
   northWest.addMouseMotionListener(this);
   northWest.setVisible(false);
   
   north = new JLabel();
   north.setText("");
   north.setBorder(new LineBorder(Color.WHITE, 1));
   north.addMouseListener(this);
   north.addMouseMotionListener(this);
   north.setVisible(false);
   
   northEast = new JLabel();
   northEast.setText("");
   northEast.setBorder(new LineBorder(Color.WHITE, 1));
   northEast.addMouseListener(this);
   northEast.addMouseMotionListener(this);
   northEast.setVisible(false);

   east = new JLabel();
   east.setText("");
   east.setBorder(new LineBorder(Color.WHITE, 1));
   east.addMouseListener(this);
   east.addMouseMotionListener(this);
   east.setVisible(false);

   southEast = new JLabel();
   southEast.setText("");
   southEast.setBorder(new LineBorder(Color.WHITE, 1));
   southEast.addMouseListener(this);
   southEast.addMouseMotionListener(this);
   southEast.setVisible(false);

   south = new JLabel();
   south.setText("");
   south.setBorder(new LineBorder(Color.WHITE, 1));
   south.addMouseListener(this);
   south.addMouseMotionListener(this);
   south.setVisible(false);

   southWest = new JLabel();
   southWest.setText("");
   southWest.setBorder(new LineBorder(Color.WHITE, 1));
   southWest.addMouseListener(this);
   southWest.addMouseMotionListener(this);
   southWest.setVisible(false);

   west = new JLabel();
   west.setText("");
   west.setBorder(new LineBorder(Color.WHITE, 1));
   west.addMouseListener(this);
   west.addMouseMotionListener(this);
   west.setVisible(false);
//东、南、西、北、东、南、东、北、西南、西北八个方向缩放点的坐标
   northWest.setBounds(0, 0, 5, 5);
   north.setBounds((x1 - 3) / 2, 0, 5, 5);
   northEast.setBounds(x1 - 5, 0, 5, 5);
   east.setBounds(x1 - 5, (y1 - 3) / 2, 5, 5);
   southEast.setBounds(x1 - 5, y1 - 5, 5, 5);
   south.setBounds((x1 - 3) / 2, y1 - 5, 5, 5);
   southWest.setBounds(0, y1 - 5, 5, 5);
   west.setBounds(0, (y1 - 3) / 2, 5, 5);

   this.setLayout(null);
   this.setMinimumSize(new Dimension(20, 20));
   this.addMouseListener(this);
   this.addMouseMotionListener(this);
//加入缩放点到jpanel中   
   this.add(northWest, null);
   this.add(north, null);
   this.add(northEast, null);
   this.add(east, null);
   this.add(southEast, null);
   this.add(south, null);
   this.add(southWest, null);
   this.add(west, null);
  }
//单击后可以进行缩放处理
  public void mouseClicked(MouseEvent e) {
   isSelected = true;
   this.setBorder(new LineBorder(Color.BLACK, 4));
   northWest.setVisible(true);
   north.setVisible(true);
   northEast.setVisible(true);
   east.setVisible(true);
   southEast.setVisible(true);
   south.setVisible(true);
   southWest.setVisible(true);
   west.setVisible(true);
   this.repaint();
  }

  public void mousePressed(MouseEvent e) {
   // TODO Auto-generated method stub

  }
//防止鼠标在缩放时移出了方向点区域,图片的缩放不能进行
  public void mouseReleased(MouseEvent e) {
   if(isSelected){
    if(getCursor().getType()!=Cursor.DEFAULT_CURSOR){
     setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }
   }

  }
//鼠标进入哪个缩放点区域
  public void mouseEntered(MouseEvent e) {   
   if (isSelected) {
    if (e.getComponent() == northWest) {
     setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
    } else if (e.getComponent() == north) {
     setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
    } else if (e.getComponent() == northEast) {
     setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
    } else if (e.getComponent() == east) {
     setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
    } else if (e.getComponent() == southEast) {
     setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
    } else if (e.getComponent() == south) {
     setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
    } else if (e.getComponent() == southWest) {
     setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
    } else if (e.getComponent() == west) {
     setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
    } /*else if(e.getComponent() == this){     
     setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }*/
   }
  }

  public void mouseExited(MouseEvent e) {
  }
//缩放及拖动的操作
  public void mouseDragged(MouseEvent e) {
   if (isSelected) {
    int type = getCursor().getType();
    int x, y, w, h;
    x = this.getX();
    y = this.getY();    
    w = this.getWidth();
    h = this.getHeight();
    switch (type) {
    case Cursor.NW_RESIZE_CURSOR://西北方向缩放
     if(w - e.getX() >20&& h - e.getY()>20){
     w = w - e.getX();
     h = h - e.getY();
       this.setBounds(x + e.getX(), y+ e.getY(), w, h);
     }    
     break;
    case Cursor.N_RESIZE_CURSOR://北
     if(h-e.getY()>20){
     h = h - e.getY();
     this.setBounds(x, y + e.getY(),w, h);
     }
     break;
    case Cursor.NE_RESIZE_CURSOR://东北
     if(w + e.getX()>20 && h - e.getY()>20){
     w = w + e.getX();
     h = h - e.getY();
     this.setBounds(x, y + e.getY(),w, h);
     }
     break;
    case Cursor.E_RESIZE_CURSOR://东
     if(w + e.getX()>20){
     w = w + e.getX();
     this.setBounds(x, y, w, h);
     }
     break;
    case Cursor.SE_RESIZE_CURSOR://东南
     if(w + e.getX()>20&& h + e.getY()>20){
     w = w + e.getX();
     h = h + e.getY();
     this.setBounds(x, y, w, h);
     }
     break;
    case Cursor.S_RESIZE_CURSOR://南
     if(h+e.getY()>20){
     h = h + e.getY();
     this.setBounds(x, y, w, h);
     }
     break;
    case Cursor.SW_RESIZE_CURSOR://西南
     if(w-e.getX()>20&& h+e.getY()>20){
     w = w - e.getX();
     h = h + e.getY();
     this.setBounds(x + e.getX(), y,w, h);
     }
     break;
    case Cursor.W_RESIZE_CURSOR://西
     if(w-e.getX()>20){
     w = w - e.getX();
     this.setBounds(x + e.getX(), y,w, h);
     }
     break;
    case Cursor.DEFAULT_CURSOR://拖动操作
     if(e.getComponent()==this){
     this.setBounds(x+e.getX(),y+e.getY(),w,h);
     }
    default:     
     break;
    }
    //重定位各缩放点坐标位置
    northWest.setBounds(0, 0, 5, 5);
    north.setBounds((w - 3) / 2, 0, 5, 5);
    northEast.setBounds(w - 5, 0, 5, 5);
    east.setBounds(w - 5, (h - 3) / 2,5, 5);
    southEast.setBounds(w - 5, h - 5, 5, 5);
    south.setBounds((w - 3) / 2, h - 5,5, 5);
    southWest.setBounds(0, h - 5, 5, 5);
    west.setBounds(0, (h - 3) / 2, 5, 5);
    //重绘
    this.repaint();    
   }
  }

  public void mouseMoved(MouseEvent e) {
   // TODO Auto-generated method stub

  }

  public void paint(Graphics g) {
   super.paint(g);
   //绘制图片在中央
   g.drawImage(this.imageIcon.getImage(), 5, 5, this.getWidth() - 10,
     this.getHeight() - 10, this);
  }
 }
}

  --相关文章--
· J2EE全面介绍(二) (2007-04-13)
· 项目经验二则:读取war包中的文件及Ant使用中的OutOfMemoryError解决 (2007-04-13)
· 走向J2EE,漫长的道路 (2007-04-13)
· 步入J2EE架构和过程(2) (2007-04-13)
· 步入J2EE架构和过程(1) (2007-04-13)
· 方兴未艾的CORBA (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接