用Swing处理图片缩放和拖动!!!
2008-02-23 09:33:07来源:互联网 阅读 ()
好久没发表文章了!
最近在研究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);
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash