当前位置: 技术问答>java相关
Java刚入门,鼠标如何托动一个东西的移动
来源: 互联网 发布时间:2015-09-01
本文导语: 我要实现如下功能: 在一张地图上,画有清华、北大、北外等学校的位置, 如何实现用鼠标托动清华从地图的一个位置到另一个位置 | 给一个例子你看看: package helloworld; import java.awt.*; import javax.sw...
我要实现如下功能:
在一张地图上,画有清华、北大、北外等学校的位置,
如何实现用鼠标托动清华从地图的一个位置到另一个位置
在一张地图上,画有清华、北大、北外等学校的位置,
如何实现用鼠标托动清华从地图的一个位置到另一个位置
|
给一个例子你看看:
package helloworld;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Frame1 extends JFrame
{
private MyPanel jPanel1 = new MyPanel();
private JButton jButton1 = new JButton();
private int focusx,focusy,x,y;
private static Frame1 frame1 = null ;
private int px,py;
public Frame1()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
frame1 = new Frame1();
frame1.setSize(400,300);
frame1.show();
}
private void jbInit() throws Exception
{
border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(103, 101, 98),new Color(148, 145, 140));
jButton1.setBounds(new Rectangle(88, 87, 79, 29));
jButton1.setText("jButton1");
jButton1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
jButton1_mouseDragged(e);
}
});
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
jButton1_mousePressed(e);
}
});
jButton2.setBounds(new Rectangle(30, 15, 79, 29));
jButton2.setText("jButton2");
jButton3.setBounds(new Rectangle(104, 76, 79, 29));
jButton3.setText("jButton3");
jPanel1.setBorder(border1);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, null);
jPanel1.add(jButton2, null);
jPanel1.add(jButton3, null);
px = jPanel1.getX() ;
px = jPanel1.getY() ;
}
void jButton1_mousePressed(MouseEvent e)
{
focusx = e.getX();
focusy = e.getY();
}
void jButton1_mouseDragged(MouseEvent e)
{
Container container = jButton1.getParent();
int width = container.getWidth() ;
int height = container.getHeight() ;
x = jButton1.getX() + e.getX() - focusx ;
y = jButton1.getY() + e.getY() - focusy ;
jButton1.setLocation(x,y);
container.repaint();
container= null;
}
private JButton jButton2 = new JButton();
private JButton jButton3 = new JButton();
private Border border1;
class MyPanel extends JLayeredPane{
public MyPanel() {
super();
}
public void paint(Graphics g){
Point p1 = new Point(jButton2.getX() + jButton2.getWidth()-jPanel1.getX(),jButton2.getY()+jButton2.getHeight() - jPanel1.getY());
Point p2 = new Point(x,y);
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
g2.setColor(Color.darkGray);
g2.drawLine((int)p1.getX(),(int)p1.getY(),(int)p2.getX(),(int)p2.getY());
g2.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
g2.setColor(Color.gray);
g2.drawLine((int)p1.getX(),(int)p1.getY(),(int)p2.getX(),(int)p2.getY());
super.paint(g);
}
}
}
package helloworld;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Frame1 extends JFrame
{
private MyPanel jPanel1 = new MyPanel();
private JButton jButton1 = new JButton();
private int focusx,focusy,x,y;
private static Frame1 frame1 = null ;
private int px,py;
public Frame1()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
frame1 = new Frame1();
frame1.setSize(400,300);
frame1.show();
}
private void jbInit() throws Exception
{
border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.white,Color.white,new Color(103, 101, 98),new Color(148, 145, 140));
jButton1.setBounds(new Rectangle(88, 87, 79, 29));
jButton1.setText("jButton1");
jButton1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
jButton1_mouseDragged(e);
}
});
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
jButton1_mousePressed(e);
}
});
jButton2.setBounds(new Rectangle(30, 15, 79, 29));
jButton2.setText("jButton2");
jButton3.setBounds(new Rectangle(104, 76, 79, 29));
jButton3.setText("jButton3");
jPanel1.setBorder(border1);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, null);
jPanel1.add(jButton2, null);
jPanel1.add(jButton3, null);
px = jPanel1.getX() ;
px = jPanel1.getY() ;
}
void jButton1_mousePressed(MouseEvent e)
{
focusx = e.getX();
focusy = e.getY();
}
void jButton1_mouseDragged(MouseEvent e)
{
Container container = jButton1.getParent();
int width = container.getWidth() ;
int height = container.getHeight() ;
x = jButton1.getX() + e.getX() - focusx ;
y = jButton1.getY() + e.getY() - focusy ;
jButton1.setLocation(x,y);
container.repaint();
container= null;
}
private JButton jButton2 = new JButton();
private JButton jButton3 = new JButton();
private Border border1;
class MyPanel extends JLayeredPane{
public MyPanel() {
super();
}
public void paint(Graphics g){
Point p1 = new Point(jButton2.getX() + jButton2.getWidth()-jPanel1.getX(),jButton2.getY()+jButton2.getHeight() - jPanel1.getY());
Point p2 = new Point(x,y);
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
g2.setColor(Color.darkGray);
g2.drawLine((int)p1.getX(),(int)p1.getY(),(int)p2.getX(),(int)p2.getY());
g2.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
g2.setColor(Color.gray);
g2.drawLine((int)p1.getX(),(int)p1.getY(),(int)p2.getX(),(int)p2.getY());
super.paint(g);
}
}
}
|
去看 java 自带的 demo
|
判断鼠标的坐标,然后擦掉原来的旧坐标的图象,在在新的坐标的地方重绘图象就行了。
|
如果是awt的话就使用java.awt.dnd 这个包,里面都是关于这个的类。
|
在paint(Graphics g)方法中调用drawImage在清华的坐标处画出图像,然后在鼠标拖动事件mouseDragged中调用repaint()重绘方法即可。