`
zhanghaidang
  • 浏览: 39343 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

怎么让矩形顺着我画的线溜下来

阅读更多
怎么让矩形顺着我画的线溜下来,鼠标按下,拖动画线,松开鼠标后,产生一矩形,顺着线段跑一圈,以下为我的代码,但是有点问题,看不到跑的过程,只是在线的末尾添加了一矩形.

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();
}
}
附件是源码
分享到:
评论
4 楼 zhanghaidang 2009-04-11  
非常感谢,已经可以了,谢谢指点
3 楼 robin_luyi 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();

        }

2 楼 zhanghaidang 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();
				}				
			}
		}

我把松开鼠标时的事件改成这样了,可是好像还是不行,请指教
1 楼 robin_luyi 2009-04-09  
单写个线程修改小方块的坐标变量

相关推荐

Global site tag (gtag.js) - Google Analytics