画线后访问线

扫码查看
本文介绍了画线后访问线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经有人问过了,但是我没有一个明确的答案.

我已经创建了Line类并使用绘制方法来绘制线条.

我想知道画线后如何访问.我有两个JLabel标记该行的终点.所以我可以使用它们来调整线的大小,但是我不能选择那条线.

我还尝试过围绕该线创建一个面板,并向其添加mouseListener,但这并不是很有效.

有人可以建议一种更聪明的方法吗?

这是该行的构造函数

 public Line(Point a,Point b,JLabel l1,JLabel l2){
    this(a,b);
    this.l1=l1;
    this.l2=l2;
    this.addMouseListener(this);
 }
解决方案

@StanislavL具有使用实现contains()方法的正确想法javase/7/docs/api/java/awt/Shape.html"rel =" nofollow noreferrer> Shape ,但是@Catalina观察到 Line2D .而是使用带有非空边界的Shape渲染端点.例如, GraphPanel 使用Rectangle允许拖动每个端点. /p>

This question has already been asked by someone else but i've not got a clear answer.

I have created a Line class and paint method for it to draw a line.

I want to know how we can access the line after it is painted. I have two JLabels that mark the end points of the line. So i can able to resize the line using them but i cannot select that line.

I have also tried creating a panel around the line and added mouseListener to it but it is not very efficient.

Can anyone suggest a smarter way?

Here is the constructor for the line

 public Line(Point a,Point b,JLabel l1,JLabel l2){
    this(a,b);
    this.l1=l1;
    this.l2=l2;
    this.addMouseListener(this);
 }
解决方案

@StanislavL has the right idea to use the contains() method of a class that implements Shape, but @Catalina observes that it's always false for Line2D. Instead, render the endpoints using a Shape with a non-empty boundary. For example, GraphPanel uses a Rectangle to allow dragging each endpoint.

这篇关于画线后访问线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:21
查看更多