java 实现图片的任意拖动
2013-10-14 14:54
281 查看
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PanelTest extends JPanel
{
static int frame_width = 600;
static int frame_height = 300;
PicPanel pic = null;
private int pic_x;
private int pic_y;
//前一个位置
int begin_x = 0;
int begin_y = 0;
boolean inThePic = false;
public PanelTest()
{
pic = new PicPanel("pic.jpg");
setLayout(null);
add(pic);
pic_x = (int)((frame_width - pic.getWidth())/2);
pic_y = (int)((frame_height - pic.getHeight())/2);
pic.setLocation(pic_x, pic_y); //定位
//鼠标动作 监听器 注册
addMouseListener(
new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
//检测 落点 是否在图片上,只有落点在图片上时 才起作用
if(inPicBounds(e.getX(), e.getY()))
{
begin_x = e.getX();
begin_y = e.getY();
inThePic = true;
}
//记录当前坐标
}
//释放
public void mouseReleased(MouseEvent e)
{
inThePic = false;
}
}
);
//鼠标移动 监听器 注册
addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
if(inThePic && checkPoint(e.getX(),e.getY()))
{
//边界 检查
pic_x =pic_x - (begin_x - e.getX());
pic_y =pic_y - (begin_y - e.getY());
//System.out.println("pic_x=" + pic_x);
begin_x = e.getX();
begin_y = e.getY();
pic.setLocation(pic_x, pic_y);
}
}
}
);
}
//-------------帮助方法-----------------//
//检测 点(px,py) 是否在图片上
private boolean inPicBounds(int px,int py)
{
if(px >= pic_x && px <= pic_x + pic.getWidth() &&
py >= pic_y && py <= pic_y + pic.getHeight())
return true;
else
return false;
}
//越界 检查
private boolean checkPoint(int px, int py)
{
if(px <0 || py <0)
return false;
if(px >getWidth() || py > getHeight())
return false;
return true;
}
public static void main(String[] args)
{
JPanel jpanel = new PanelTest();
JFrame jframe = new JFrame("图片拖动");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.getContentPane().add(jpanel);
jframe.setSize(frame_width, frame_height);
jframe.setVisible(true);
}
}
//图片面板,只是用来放置图片
class PicPanel extends JPanel
{
int p_width = 0;
int p_height = 0;
Image im = null;
int i = 0; //temp var
public PicPanel(String picName)
{
ImageIcon imageIcon = new ImageIcon(picName);
im = imageIcon.getImage();
p_width = imageIcon.getIconWidth();
p_height = imageIcon.getIconHeight();
setBounds(0,0,p_width, p_height);
}
public void paint(Graphics g)
{
g.drawImage(im,0,0,p_width,p_height,null);
}
}
import java.awt.*;
import java.awt.event.*;
public class PanelTest extends JPanel
{
static int frame_width = 600;
static int frame_height = 300;
PicPanel pic = null;
private int pic_x;
private int pic_y;
//前一个位置
int begin_x = 0;
int begin_y = 0;
boolean inThePic = false;
public PanelTest()
{
pic = new PicPanel("pic.jpg");
setLayout(null);
add(pic);
pic_x = (int)((frame_width - pic.getWidth())/2);
pic_y = (int)((frame_height - pic.getHeight())/2);
pic.setLocation(pic_x, pic_y); //定位
//鼠标动作 监听器 注册
addMouseListener(
new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
//检测 落点 是否在图片上,只有落点在图片上时 才起作用
if(inPicBounds(e.getX(), e.getY()))
{
begin_x = e.getX();
begin_y = e.getY();
inThePic = true;
}
//记录当前坐标
}
//释放
public void mouseReleased(MouseEvent e)
{
inThePic = false;
}
}
);
//鼠标移动 监听器 注册
addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
if(inThePic && checkPoint(e.getX(),e.getY()))
{
//边界 检查
pic_x =pic_x - (begin_x - e.getX());
pic_y =pic_y - (begin_y - e.getY());
//System.out.println("pic_x=" + pic_x);
begin_x = e.getX();
begin_y = e.getY();
pic.setLocation(pic_x, pic_y);
}
}
}
);
}
//-------------帮助方法-----------------//
//检测 点(px,py) 是否在图片上
private boolean inPicBounds(int px,int py)
{
if(px >= pic_x && px <= pic_x + pic.getWidth() &&
py >= pic_y && py <= pic_y + pic.getHeight())
return true;
else
return false;
}
//越界 检查
private boolean checkPoint(int px, int py)
{
if(px <0 || py <0)
return false;
if(px >getWidth() || py > getHeight())
return false;
return true;
}
public static void main(String[] args)
{
JPanel jpanel = new PanelTest();
JFrame jframe = new JFrame("图片拖动");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.getContentPane().add(jpanel);
jframe.setSize(frame_width, frame_height);
jframe.setVisible(true);
}
}
//图片面板,只是用来放置图片
class PicPanel extends JPanel
{
int p_width = 0;
int p_height = 0;
Image im = null;
int i = 0; //temp var
public PicPanel(String picName)
{
ImageIcon imageIcon = new ImageIcon(picName);
im = imageIcon.getImage();
p_width = imageIcon.getIconWidth();
p_height = imageIcon.getIconHeight();
setBounds(0,0,p_width, p_height);
}
public void paint(Graphics g)
{
g.drawImage(im,0,0,p_width,p_height,null);
}
}
相关文章推荐
- Java实现图片随鼠标拖动
- Java基础(五):Java实现图片无损任意角度旋转
- Android下图片或按钮等可拖动到任意位置的效果实现源码
- Android实现动态任意拖动图片
- Java实现图片内容无损任意角度旋转
- Android下图片可拖动到任意位置的效果的实现源码
- Android下图片或按钮等可拖动到任意位置的效果实现源码
- actionscript3.0实现鼠标任意拖动图片
- Android下图片或按钮等可拖动到任意位置的效果实现源码
- 图片的任意拖动JLabel实现
- Android下图片可拖动到任意位置的效果的实现源码
- Java实现图片任意角度旋转
- Java实现图片内容无损任意角度旋转
- java 实现图片任意角度旋转,及背景透明
- 实现图片的任意拖动
- Java实现图片内容无损任意角度旋转
- Java实现图片内容无损任意角度旋转
- Java实现图片上传到服务器,并把上传的图片读取出来
- 关于拖动(实现窗体拖动、不允许窗体拖动、任意控件运行时拖动)
- JAVA实现对图片的剪切