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(Color.ORANGE);
g.fillRect(p.x - 2, p.y - 2, 100, 20);
g.setColor(Color.BLACK);
g.drawRect(p.x - 2, p.y - 2, 100, 20);

}
} private Point getPointAtSameLocation(Point p) {
for (Point point : points) {
if (Math.abs(p.x - point.x) <= 100 && Math.abs(p.y - point.y) <= 20) {
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); }
}
如果不用java 2d  可不可以實現這樣拖拉的效果(不用畫線)。只要矩形框就行~

解决方案 »

  1.   

    這個代碼是在csdn上找的     我想知道能不能不用java 2d  也能實現類似的功能,(畫線功能不用實現~)
      

  2.   

    不用java2d的话,那么在点击的界面上放置个自己定义的JLabel应该可以吧,当然要用null布局,然后绝对定位,
    点击的位置new个JLabel放上(已经存在的再另想办法)
      

  3.   

    增加一个标志位,默认为false
    添加2个监听器mainPanel.addMouseListener(new MouseInputListener(){
    public void mousePressed(MouseEvent e) {
    Rectangle rect = label.getBounds();
    if (!flag&&rect.contains(new Point(e.getX(),e.getY()))) {
    flag = true;
    }
    }
    public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub
    flag = false;
    }
    }); mainPanel.addMouseMotionListener(new MouseMotionListener(){
    public void mouseDragged(MouseEvent e) {
    System.out.println(flag);
    if (flag) {
    label.setBounds(e.getX(), e.getY(), 40, 15);
    }
    } public void mouseMoved(MouseEvent e) {
    }
    });
    这样的话效果拖动的效果就实现了
      

  4.   

    补充一下,label是绝对定位的JLabel
      

  5.   

    謝謝樓上的   我先試試~   現在打算用另一種方式實現了  具體用JTable實現類似的功能·~ 正在做 ,謝謝大家捧場~~