当前位置: 技术问答>java相关
在applet中如何实现一副图片的拖动?
来源: 互联网 发布时间:2015-01-17
本文导语: 或者java中有没有类似C++的ICON,可以直接移动的? 我需要为一图片写一个移动函数! | import java.awt.*; import java.awt.image.*; import java.applet.*; import java.awt.event.*; public class DragImage extends Applet { ...
或者java中有没有类似C++的ICON,可以直接移动的?
我需要为一图片写一个移动函数!
我需要为一图片写一个移动函数!
|
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.awt.event.*;
public class DragImage extends Applet
{
public void init()
{
Image img = getImage(getCodeBase(),"test3.jpg");
MediaTracker tracker;
tracker = new MediaTracker(this);
tracker.addImage(img, 0);
ImagePanel ip=new ImagePanel();
ip.img = img;
setLayout(new BorderLayout());
add(ip,BorderLayout.CENTER);
}
}
class ImagePanel extends Panel implements MouseMotionListener,MouseListener
{
Image img;
Point p;
Point lastpos;
boolean isDrag=false;
public ImagePanel()
{
addMouseListener(this);
addMouseMotionListener(this);
lastpos=new Point();
p=new Point();
}
public void paint(Graphics g)
{
g.fillRect(0,0,getWidth(),getHeight());
if(img!=null)
g.drawImage(img,p.x,p.y,this);
g.setColor(Color.red);
g.drawString("Mouse press inside the image to drag",0,120);
}
public void update(Graphics g)
{
paint(g);
}
public void mouseClicked(MouseEvent e)
{ }
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
lastpos = e.getPoint();
isDrag=isMouseInsideImage(lastpos);
repaint();
}
public void mouseReleased(MouseEvent e)
{}
public void mouseMoved(MouseEvent e)
{ }
public void mouseDragged(MouseEvent e)
{
Point newpos =e.getPoint();
if(isDrag)
{
p.x = p.x+newpos.x-lastpos.x;
p.y = p.y+newpos.y-lastpos.y;
lastpos = newpos;
repaint();
}
}
protected boolean isMouseInsideImage(Point pos)
{
int width =img.getWidth(null);
int height =img.getHeight(null);
Rectangle rect=new Rectangle(p.x,p.y,width,height);
return rect.contains(pos);
}
}
import java.awt.image.*;
import java.applet.*;
import java.awt.event.*;
public class DragImage extends Applet
{
public void init()
{
Image img = getImage(getCodeBase(),"test3.jpg");
MediaTracker tracker;
tracker = new MediaTracker(this);
tracker.addImage(img, 0);
ImagePanel ip=new ImagePanel();
ip.img = img;
setLayout(new BorderLayout());
add(ip,BorderLayout.CENTER);
}
}
class ImagePanel extends Panel implements MouseMotionListener,MouseListener
{
Image img;
Point p;
Point lastpos;
boolean isDrag=false;
public ImagePanel()
{
addMouseListener(this);
addMouseMotionListener(this);
lastpos=new Point();
p=new Point();
}
public void paint(Graphics g)
{
g.fillRect(0,0,getWidth(),getHeight());
if(img!=null)
g.drawImage(img,p.x,p.y,this);
g.setColor(Color.red);
g.drawString("Mouse press inside the image to drag",0,120);
}
public void update(Graphics g)
{
paint(g);
}
public void mouseClicked(MouseEvent e)
{ }
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
lastpos = e.getPoint();
isDrag=isMouseInsideImage(lastpos);
repaint();
}
public void mouseReleased(MouseEvent e)
{}
public void mouseMoved(MouseEvent e)
{ }
public void mouseDragged(MouseEvent e)
{
Point newpos =e.getPoint();
if(isDrag)
{
p.x = p.x+newpos.x-lastpos.x;
p.y = p.y+newpos.y-lastpos.y;
lastpos = newpos;
repaint();
}
}
protected boolean isMouseInsideImage(Point pos)
{
int width =img.getWidth(null);
int height =img.getHeight(null);
Rectangle rect=new Rectangle(p.x,p.y,width,height);
return rect.contains(pos);
}
}
|
这里有一个使用拖动层的例子。
控制两个层上的两个图片的拖动。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public void init() {
setContentPane(new CustomContentPane());
}
}
class CustomContentPane extends JLayeredPane {
private ImageIcon rain = new ImageIcon("rain.gif");
private ImageIcon punch = new ImageIcon("punch.gif");
private ImageIcon skelly = new ImageIcon("skelly.gif");
private int rainw = rain.getIconWidth();
private int rainh = rain.getIconHeight();
private JLabel[] labels = {
new JLabel("I stay put", punch, SwingConstants.RIGHT),
new JLabel("Drag me around!",
skelly, SwingConstants.RIGHT),
};
public CustomContentPane() {
Dragger listener = new Dragger();
JCheckBox onDragLayer = new JCheckBox("Drag Layer");
// JLayeredPane has a null layout by default
setLayout(new FlowLayout());
onDragLayer.setOpaque(false);
add(onDragLayer);
add(labels[0]);
add(labels[1]);
labels[1].addMouseMotionListener(listener);
labels[1].addMouseListener(listener);
setLabelText();
onDragLayer.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
setLayer(labels[1],
JLayeredPane.DRAG_LAYER.intValue());
}
else {
setLayer(labels[1],
JLayeredPane.DEFAULT_LAYER.intValue());
}
setLabelText();
validate();
}
});
}
public void paintComponent(Graphics g) {
Dimension size = getSize();
for(int row=0; row
控制两个层上的两个图片的拖动。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet {
public void init() {
setContentPane(new CustomContentPane());
}
}
class CustomContentPane extends JLayeredPane {
private ImageIcon rain = new ImageIcon("rain.gif");
private ImageIcon punch = new ImageIcon("punch.gif");
private ImageIcon skelly = new ImageIcon("skelly.gif");
private int rainw = rain.getIconWidth();
private int rainh = rain.getIconHeight();
private JLabel[] labels = {
new JLabel("I stay put", punch, SwingConstants.RIGHT),
new JLabel("Drag me around!",
skelly, SwingConstants.RIGHT),
};
public CustomContentPane() {
Dragger listener = new Dragger();
JCheckBox onDragLayer = new JCheckBox("Drag Layer");
// JLayeredPane has a null layout by default
setLayout(new FlowLayout());
onDragLayer.setOpaque(false);
add(onDragLayer);
add(labels[0]);
add(labels[1]);
labels[1].addMouseMotionListener(listener);
labels[1].addMouseListener(listener);
setLabelText();
onDragLayer.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
setLayer(labels[1],
JLayeredPane.DRAG_LAYER.intValue());
}
else {
setLayer(labels[1],
JLayeredPane.DEFAULT_LAYER.intValue());
}
setLabelText();
validate();
}
});
}
public void paintComponent(Graphics g) {
Dimension size = getSize();
for(int row=0; row