浏览 2311 次
锁定老帖子 主题:怎么让矩形顺着我画的线溜下来
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-04-09
最后修改:2009-04-09
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.BorderFactory; import javax.swing.SwingUtilities; class MouseLinePanel extends JPanel { private List<Line2D> lineList; // 线集合 private Point2D lastPoint; // 保存最后点 private Rectangle2D rec; // 矩形 private static final int LENGTH = 10;// 矩形长宽 private List<Point2D> pointList; //所有线段的起始点集合 public MouseLinePanel() { setLayout(null); lineList = new ArrayList<Line2D>(); pointList = new ArrayList<Point2D>(); MouseLineEvent mle = new MouseLineEvent(); addMouseListener(mle); addMouseMotionListener(mle); addMouseWheelListener(mle); rec = new Rectangle2D.Double(); } // 重绘线条 @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; for (Line2D line : lineList) { g2.draw(line); } g2.draw(rec); } // 监听器 private class MouseLineEvent extends MouseAdapter { // 按下时调用 @Override public void mousePressed(MouseEvent e) { super.mousePressed(e); lastPoint = e.getPoint();// 保存按下的点 } // 松开时调用 @Override public void mouseReleased(MouseEvent e) { super.mouseReleased(e); // 取集合中所有线段的起始点 for (Line2D line : lineList) { pointList.add(line.getP1()); pointList.add(line.getP2()); } for (Point2D p : pointList) { //重新设置矩形的加载点 rec.setFrame( p.getX(), p.getY(), LENGTH, LENGTH); repaint();//刷新面板 } } // 拖动 @Override public void mouseDragged(MouseEvent e) { super.mouseDragged(e); Point2D newPoint = e.getPoint(); // 创建点 Line2D line = new Line2D.Double(lastPoint, newPoint); // 创建线段 lineList.add(line); lastPoint = newPoint; repaint(); } } } class MouseLineFrame extends JFrame { public MouseLineFrame() { add(new MouseLinePanel()); setSize(700,500); setVisible(true); } } public class MouseLineTest { public static void main(String[] args) { new MouseLineFrame(); } } 附件是源码 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-04-09
单写个线程修改小方块的坐标变量
|
|
返回顶楼 | |
发表时间:2009-04-10
// 松开时调用 @Override public void mouseReleased(MouseEvent e) { super.mouseReleased(e); // 取集合中所有线段的起始点 for (Line2D line : lineList) { pointList.add(line.getP1()); pointList.add(line.getP2()); } for (Point2D p : pointList) { try { Thread.sleep(250); //重新设置矩形的加载点 rec.setFrame( p.getX(), p.getY(), LENGTH, LENGTH); repaint();//刷新面板 } catch (InterruptedException e1) { e1.printStackTrace(); } } } 我把松开鼠标时的事件改成这样了,可是好像还是不行,请指教 |
|
返回顶楼 | |
发表时间:2009-04-10
最后修改:2009-04-10
// 松开时调用 private class BoxMover implements Runnable{ public void run() { // 取集合中所有线段的起始点 for (Line2D line : lineList) { pointList.add(line.getP1()); pointList.add(line.getP2()); } for (Point2D p : pointList) { try { Thread.sleep(250); //重新设置矩形的加载点 rec.setFrame(p.getX(), p.getY(), LENGTH, LENGTH); repaint();//刷新面板 } catch (InterruptedException e1) { e1.printStackTrace(); } } } } =================================== public void mouseReleased(MouseEvent e) { super.mouseReleased(e); new Thread(new BoxMover()).start(); } |
|
返回顶楼 | |
发表时间:2009-04-11
非常感谢,已经可以了,谢谢指点
|
|
返回顶楼 | |