我有一个问题,我似乎无法修复我的Java代码。
我创建了一个显示地图的程序,当你点击地图时,你会在地图上点缀一个边/节点。当我相互连接两个节点时,会显示一条线连接点。
到目前为止,我还是这么好,但是......当x1,y1
坐标的值小于x2,y2
时,它很好......
我发现这是setBounds一直在欺骗我......但我不知道如何解决我的问题,我似乎无法在任何地方找到任何类似的问题... 有没有人遇到过这种问题,如果有的话,你是怎么解决这个问题的?
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;
}
}
在主要(连接两个节点时):
DrawMyLine dml = new DrawMyLine(from,to);
panel.add(dml);
panel.repaint();
dml.addMouseListener(lineListener);
任何人都可以帮助我吗?请!
答案 0 :(得分:1)
不要反转边界,反转渲染。
想一想......
在上面的图片中,唯一改变的是起点和终点。矩形的大小没有改变。
所有界限必须与正值一起使用。在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, width, height
)
答案 1 :(得分:0)
好的,我认为我理解逻辑,但是当我测试我的程序时,它似乎就像线条越界了。
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;
}
}
有些线条显示的只是一小块......见图片......
为什么会这样?