Java 绘制线后访问线
这个问题已经被别人问过了,但我没有得到一个明确的答案 我已经为它创建了一个Line类和paint方法来绘制一条线 我想知道画好后我们怎样才能接近这条线。我有两个jlabel标记线的端点。因此,我可以使用它们调整线的大小,但我无法选择该线 我还尝试在生产线周围创建一个面板,并在面板上添加了MouseStener,但效率不高 有人能提出一个更聪明的方法吗 这是该行的构造函数Java 绘制线后访问线,java,swing,mouselistener,Java,Swing,Mouselistener,这个问题已经被别人问过了,但我没有得到一个明确的答案 我已经为它创建了一个Line类和paint方法来绘制一条线 我想知道画好后我们怎样才能接近这条线。我有两个jlabel标记线的端点。因此,我可以使用它们调整线的大小,但我无法选择该线 我还尝试在生产线周围创建一个面板,并在面板上添加了MouseStener,但效率不高 有人能提出一个更聪明的方法吗 这是该行的构造函数 public Line(Point a,Point b,JLabel l1,JLabel l2){ this(a,b
public Line(Point a,Point b,JLabel l1,JLabel l2){
this(a,b);
this.l1=l1;
this.l2=l2;
this.addMouseListener(this);
}
从你发布的几行文字中,我无法确定你到底有什么。行的选择不是现成的。您需要实现它,但它很容易。 无论如何,您应该保留该行的坐标,并在鼠标侦听器中检查鼠标在该行上的单击
public void mousePressed(MouseEvent ev)
{
double TOLERANCE = 5; // Small tolerance measured in pixels
if (ev.getClickCount() == 1)
{
Point cursor = ev.getPoint();
if (Math.abs(cursor.x - a.x) < TOLERANCE &&
Math.abs(cursor.y - a.y) < TOLERANCE)
{
// Clicked on the first point
}
else if (Math.abs(cursor.x - b.x) < TOLERANCE &&
Math.abs(cursor.y - b.y) < TOLERANCE)
{
// Clicked on the second point
}
}
}
public void鼠标按下(MouseEvent ev)
{
双公差=5;//以像素为单位测量的小公差
如果(ev.getClickCount()==1)
{
点光标=ev.getPoint();
如果(数学abs(cursor.x-a.x)<公差&&
Math.abs(cursor.y-a.y)<公差)
{
//点击第一点
}
否则如果(数学abs(cursor.x-b.x)<公差&&
数学abs(cursor.y-b.y)<公差)
{
//点击第二点
}
}
}
从您发布的几行内容中,我无法确定您实际拥有什么。行的选择不是现成的。您需要实现它,但它很容易。
无论如何,您应该保留该行的坐标,并在鼠标侦听器中检查鼠标在该行上的单击
public void mousePressed(MouseEvent ev)
{
double TOLERANCE = 5; // Small tolerance measured in pixels
if (ev.getClickCount() == 1)
{
Point cursor = ev.getPoint();
if (Math.abs(cursor.x - a.x) < TOLERANCE &&
Math.abs(cursor.y - a.y) < TOLERANCE)
{
// Clicked on the first point
}
else if (Math.abs(cursor.x - b.x) < TOLERANCE &&
Math.abs(cursor.y - b.y) < TOLERANCE)
{
// Clicked on the second point
}
}
}
public void鼠标按下(MouseEvent ev)
{
双公差=5;//以像素为单位测量的小公差
如果(ev.getClickCount()==1)
{
点光标=ev.getPoint();
如果(数学abs(cursor.x-a.x)<公差&&
Math.abs(cursor.y-a.y)<公差)
{
//点击第一点
}
否则如果(数学abs(cursor.x-b.x)<公差&&
数学abs(cursor.y-b.y)<公差)
{
//点击第二点
}
}
}
您可以从JComponent或一个合适的后续类(即JButton)扩展您的行
然后重写contains函数
Shape shape;
public boolean contains(int x, int y) {
// If the button has changed size, make a new shape object.
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new Line2D.Float(a.x, a.y, b.x, b.y);
}
return shape.contains(x, y);
}
注意:Line2D.Float构造函数参数可能是无序的,我是从内存中编写的。您可以从JComponent或一个适当的后续类(即JButton)扩展行 然后重写contains函数
Shape shape;
public boolean contains(int x, int y) {
// If the button has changed size, make a new shape object.
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new Line2D.Float(a.x, a.y, b.x, b.y);
}
return shape.contains(x, y);
}
注意:Line2D.Float构造函数参数可能是无序的,我是从内存中编写的。创建
Line2D
对象。然后使用BasicStroke
并定义所需的线条厚度。
使用该方法
public Shape createStrokedShape(Shape s)
以获得线条的笔划形状。
使用Line
Shape
的contains()
方法检查单击点是否在线。创建Line2D
对象。然后使用BasicStroke
并定义所需的线条厚度。
使用该方法
public Shape createStrokedShape(Shape s)
以获得线条的笔划形状。
Line
Shape
的usecontains()
方法用于检查单击点是否在线。@StanislavL使用实现类的contains()
方法的想法是正确的,但是@Catalina发现,对于。相反,使用具有非空边界的形状
渲染端点。例如,使用矩形
来允许拖动每个端点
@StanislavL使用实现类的
contains()
方法的想法是正确的,但是@Catalina观察到它对于类总是错误的。相反,使用具有非空边界的形状
渲染端点。例如,使用矩形
来允许拖动每个端点
要更快地获得更好的帮助,请发布一个。要更快地获得更好的帮助,请发布一个。我认为
contains
对于Line2D
总是错误的。我认为contains
对于Line2D
总是错误的。是的,您的示例说明了呈现提示的好处!是的,您的示例说明了呈现提示的好处!