Java 带立根的抽绳构件

Java 带立根的抽绳构件,java,swing,graphics,jcomponent,setbounds,Java,Swing,Graphics,Jcomponent,Setbounds,我有一个似乎无法用Java代码解决的问题 我创建了一个显示地图的程序,当你点击地图时,你会在地图上得到一个虚线的边/节点。。当我将两个节点相互连接时,将在连接的点之间显示一条线 到目前为止还不错,或者说我是这么想的,但是。。。当x1,y1坐标的值小于x2,y2时,这是正常的 我发现是挫折让我害怕。。。但我不知道如何解决我的问题,而且我似乎在任何地方都找不到任何类似的问题。。。 有人遇到过这种问题吗?如果有,你是如何解决的 import java.awt.*; import javax.swing

我有一个似乎无法用Java代码解决的问题

我创建了一个显示地图的程序,当你点击地图时,你会在地图上得到一个虚线的边/节点。。当我将两个节点相互连接时,将在连接的点之间显示一条线

到目前为止还不错,或者说我是这么想的,但是。。。当
x1,y1
坐标的值小于
x2,y2
时,这是正常的

我发现是挫折让我害怕。。。但我不知道如何解决我的问题,而且我似乎在任何地方都找不到任何类似的问题。。。 有人遇到过这种问题吗?如果有,你是如何解决的

import java.awt.*;
import javax.swing.*;

public class DrawMyLine extends JComponent{
    private int fx, fy, tx, ty;
    private int h,w;
    private int m;
    private double k;
    private Destinations from;
    private Destinations to;

public DrawMyLine(Destinations from, Destinations to){
    this.from=from;
    this.to=to;
    setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

    fx = from.getX();
    fy = from.getY();
    tx = to.getX();
    ty = to.getY();


    //w = Math.abs(tx - fx);
    //h = Math.abs(ty - fy);
    w = tx - fx;
    h = ty - fy;

    int x,y;

    if(ty>fy){ //This is my, not so great solution so far...
        x = fx;
        y = fy;
    }
    else{
        x = tx;
        y = ty;
    }

    setBounds(x+5,y+5, w, h); //How do I reverse the boundary?
    setPreferredSize(new Dimension(w, h));
    setMinimumSize(new Dimension(w, h));
    setMaximumSize(new Dimension(w, h));
    }

//@Override
protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.drawLine(0,0,w,h);
}

//Method to reduce the clickable area to 5 pixels from the line
public boolean contains(int x, int y){

    k = ((ty-fy)/(tx-fx));
    if(k >= 0){
        m = 0;
    }
    else{
        m = ty - fy;
    }
    return Math.abs(y - k * x - m) < 5;
}//contains 

public Destinations getFrom(){
    return from;
}
public Destinations getTo(){
    return to;
}

}

有人能帮我吗?求你了

不要反转边界,而是反转渲染

想想这个

在上面的图片中,唯一改变的是起点和终点。矩形的大小没有改变

所有边界都必须使用正值。在Swing中,没有大小为负的矩形

现在,我的示例是使用
java.awt.Point
呈现的,但基本概念仍然是

// Find the smallest point between the two
int x = Math.min(p1.x, p2.x);
int y = Math.min(p1.y, p2.y);
// Size is based on the maximum value of the two points differences...
int width = Math.max(p1.x - p2.x, p2.x - p1.x);
int height = Math.max(p1.y - p2.y, p2.y - p1.y);

现在,这将为您提供效果区域的大小。画一条线只是在两点之间画一条线(而不是你用过的
0,0,宽度,高度)

好的,thx,我想我理解这个逻辑,但是当我测试我的程序时,它似乎超出了界限

import java.awt.*;
import javax.swing.*;

public class DrawMyLine extends JComponent{
    private int fx, fy, tx, ty;
    private int h,w;
    private int m;
    private double k;
    private Destinations from;
    private Destinations to;

    public DrawMyLine(Destinations from, Destinations to){
    this.from=from;
    this.to=to;
    setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

    fx = from.getX();
    fy = from.getY();
    tx = to.getX();
    ty = to.getY();


    // Find the smallest point between the two
    x1 = Math.min(fx, tx);
    y1 = Math.min(fy, fy);
    // Size is based on the maximum value of the two points differences...
    int w = Math.max(fx - tx, tx - fx);
    int h = Math.max(fy - ty, ty - fy);

    setBounds(x1,y1, w, h);
    setPreferredSize(new Dimension(w, h));
    setMinimumSize(new Dimension(w, h));
    setMaximumSize(new Dimension(w, h));
}

//Method to reduce the clickable area to 5 pixels from the line
public boolean contains(int x, int y){

    k = ((ty-fy)/(tx-fx));
    if(k >= 0){
        m = 0;
    }
    else{
        m = ty - fy;
    }
    return Math.abs(y - k * x - m) < 5;
}//contains 

//@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);

    if(fx<tx || fy<ty){ // Something like this?
    g.drawLine(fx,fy,tx,ty);
    }
    else{
    g.drawLine(tx,ty,fx,fy);
    }
}

public Destinations getFrom(){
return from;
}
public Destinations getTo(){
return to;
}

}
import java.awt.*;
导入javax.swing.*;
公共类DrawMyLine扩展JComponent{
私人国际外汇,财政年度,德克萨斯州,泰州;
私人住宅区h,w;
私有INTM;
私人双k;
私人目的地;
私人目的地;
公共提取路线(目的地自、目的地至){
this.from=from;
这个。to=to;
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_Cursor));
fx=from.getX();
fy=from.getY();
tx=to.getX();
ty=to.getY();
//找到两者之间的最小点
x1=数学最小值(fx,tx);
y1=数学最小值(fy,fy);
//大小是基于两点差异的最大值。。。
int w=数学最大值(fx-tx,tx-fx);
inth=Math.max(fy-ty,ty-fy);
立根(x1,y1,w,h);
设置首选尺寸(新尺寸(w,h));
设置最小尺寸(新尺寸(w,h));
设置最大尺寸(新尺寸(w,h));
}
//方法将可单击区域从线条减少到5像素
公共布尔包含(int x,int y){
k=((财政年度)/德克萨斯州外汇);
如果(k>=0){
m=0;
}
否则{
m=ty-fy;
}
返回数学值abs(y-k*x-m)<5;
}//包含
//@凌驾
受保护组件(图形g){
超级组件(g);
g、 设置颜色(颜色为黑色);

如果(fx)不反转边界,请反转该行。。。
import java.awt.*;
import javax.swing.*;

public class DrawMyLine extends JComponent{
    private int fx, fy, tx, ty;
    private int h,w;
    private int m;
    private double k;
    private Destinations from;
    private Destinations to;

    public DrawMyLine(Destinations from, Destinations to){
    this.from=from;
    this.to=to;
    setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

    fx = from.getX();
    fy = from.getY();
    tx = to.getX();
    ty = to.getY();


    // Find the smallest point between the two
    x1 = Math.min(fx, tx);
    y1 = Math.min(fy, fy);
    // Size is based on the maximum value of the two points differences...
    int w = Math.max(fx - tx, tx - fx);
    int h = Math.max(fy - ty, ty - fy);

    setBounds(x1,y1, w, h);
    setPreferredSize(new Dimension(w, h));
    setMinimumSize(new Dimension(w, h));
    setMaximumSize(new Dimension(w, h));
}

//Method to reduce the clickable area to 5 pixels from the line
public boolean contains(int x, int y){

    k = ((ty-fy)/(tx-fx));
    if(k >= 0){
        m = 0;
    }
    else{
        m = ty - fy;
    }
    return Math.abs(y - k * x - m) < 5;
}//contains 

//@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);

    if(fx<tx || fy<ty){ // Something like this?
    g.drawLine(fx,fy,tx,ty);
    }
    else{
    g.drawLine(tx,ty,fx,fy);
    }
}

public Destinations getFrom(){
return from;
}
public Destinations getTo(){
return to;
}

}