有关于图形拖动的问题
我想实现这样一个问题:
比如说两个矩形用一条连线连接,当我拖动矩形时,屏幕上会显示一个阴影矩形表示被拖动的矩形。但是连线不能随着这个阴影矩形移动,只能在图形移动后放开鼠标后,重新绘制连线。但我想这条线也能同样的做移动,该怎么解决?
是不是解决锚点和鼠标同时计算位置,然后刷新?
问题点数:20、回复次数:8Top
1 楼terry_yip(我只回答引起我思考的问题)回复于 2005-11-05 10:47:30 得分 0
这个用delphi来做,有很多例子,但是,我从来没见到用java可以做到这种程序的,甚至,连可以用鼠标拖放的都没见过。JAVA在这方面是很弱的。
Top
2 楼NetMan707(网人)回复于 2005-11-07 09:56:46 得分 0
我是在eclipse上做的,使用gef插件,不知道那位能帮我解决这个问题?谢谢Top
3 楼java_augur(听着音乐 ☆☆☆☆☆☆)回复于 2005-11-07 14:52:10 得分 20
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
public class LineTest extends JPanel implements MouseInputListener
{
private List<Point> points = new LinkedList<Point>();
private Point draggingPoint;
public LineTest()
{
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Point lastPoint = null;
g.setColor(Color.BLACK);
for (Point p : points) {
if (lastPoint != null) {
g.drawLine(lastPoint.x, lastPoint.y, p.x, p.y);
}
lastPoint = p;
}
for (Point p : points) {
g.setColor(getBackground());
g.fillRect(p.x-2, p.y-2, 4, 4);
g.setColor(Color.BLACK);
g.drawRect(p.x-2, p.y-2, 4, 4);
}
}
private Point getPointAtSameLocation(Point p)
{
for (Point point : points) {
if (Math.abs(p.x - point.x) <= 2 && Math.abs(p.y - point.y) <= 2) {
return point;
}
}
return null;
}
private int getLineAtLocation(Point p)
{
int index = -1;
Point lastPoint = null;
for (Point point : points) {
if (lastPoint != null) {
Line2D line = new Line2D.Float(lastPoint, point);
if (line.intersects(p.x - 2, p.y - 2, 4, 4)) {
return index;
}
}
lastPoint = point;
index++;
}
return -1;
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
Point point = e.getPoint();
if (e.getButton() == MouseEvent.BUTTON1) {
Point p = getPointAtSameLocation(point);
if (p != null) {
this.draggingPoint = p;
return;
}
int index = getLineAtLocation(point);
if (index != -1) {
points.add(index + 1, point);
this.draggingPoint = point;
repaint();
return;
}
if (draggingPoint == null) {
points.add(e.getPoint());
repaint();
}
}
else if (e.getButton() == MouseEvent.BUTTON3) {
Point p = getPointAtSameLocation(point);
if (p != null) {
points.remove(p);
repaint();
}
}
}
public void mouseReleased(MouseEvent e) {
draggingPoint = null;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseDragged(MouseEvent e)
{
if (this.draggingPoint != null) {
this.draggingPoint.x = e.getX();
this.draggingPoint.y = e.getY();
repaint();
}
}
public void mouseMoved(MouseEvent e)
{
Point p = getPointAtSameLocation(e.getPoint());
if (p != null) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
return;
}
int index = getLineAtLocation(e.getPoint());
if (index != -1) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else {
setCursor(Cursor.getDefaultCursor());
}
}
public static void main(String[] args)
{
JFrame f = new JFrame("Points");
f.getContentPane().add(new LineTest(), BorderLayout.CENTER);
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Top
4 楼hubo2003hq(rain)回复于 2005-11-08 01:39:42 得分 0
private List<Point> points = new LinkedList<Point>();
没见过这种语句哦
能不能解释下?
thanksTop
5 楼hubo2003hq(rain)回复于 2005-11-08 02:18:10 得分 0
for (Point p : points)
这种循环也没见到过哦!!Top
6 楼java_augur(听着音乐 ☆☆☆☆☆☆)回复于 2005-11-08 07:47:59 得分 0
这是java5.0中的特殊语句,如果你没有安装,你可以自己改造一下。
不过,为了这段代码,安装java5.0也不为过。
private List points = new LinkedList();
Iterator it = points.iterator();
while(it.hasNext()){
Point p = (Point)it.next();
}Top
7 楼NetMan707(网人)回复于 2005-11-08 10:50:44 得分 0
谢谢Top
8 楼quleaf(auleaf)回复于 2005-11-14 17:20:22 得分 0
haoTop




