JAVA求线段长度
我想找到一条线的长度。我已经使用鼠标事件初始化了坐标 x1,y1,x2,y2。当我尝试找到如下所示的距离时:
public class LineFunctions {
static int dpi;
static double pixel_per_cm;
public LineFunctions(){
Toolkit tk;
tk=Main.frame.getToolkit();
dpi=tk.getScreenResolution();
pixel_per_cm=dpi/2.54;
}
public double lengthInPixel(int x1,int y1,int x2,int y2){
double length_in_pixel;
double x=Math.pow((x2-x1), 2);
double y=Math.pow((y2-y1), 2);
length_in_pixel = Math.sqrt(x+y);
return length_in_pixel;
}
public double lengthInCm(int x1,int y1,int x2,int y2){
double length_in_pixel=lengthInPixel(x1,y1,x2,y2);
double length_in_cm = length_in_pixel / pixel_per_cm;
System.out.println("Length in Cm= "+length_in_cm);
return length_in_cm;
}
}
Main.java
public class Main {
public static JFrame frame;
public static void main(String ar[]) {
frame=new JFrame("LINE FRAME");
}
static class ImageComponent extends JComponent implements MouseListener {
Line2D line;
int x1,y1,x2,y2;
public ImageComponent() {
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D gd=(Graphics2D)g;
line=new Line2D.Double(x1,y1,x2,y2);
gd.draw(line);
}
@Override
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
LineFunctions lf=new LineFunctions();
double length=lf.lengthInCm(x1,y1,x2,y2);
System.out.println("Line Length="+length);
repaint();
}
.......................
........................
}
此代码从坐标中查找线的长度,但是当我从 RULER 找到屏幕上绘制的线的长度时,它不会我看到的长度与我从上面的代码中得到的长度相同。那么,这有什么问题以及如何找到线的确切长度?
I want to find a length of line. I have initialize a coordinates x1,y1,x2,y2 using mouse Events. When i try to find distance as shown below:
public class LineFunctions {
static int dpi;
static double pixel_per_cm;
public LineFunctions(){
Toolkit tk;
tk=Main.frame.getToolkit();
dpi=tk.getScreenResolution();
pixel_per_cm=dpi/2.54;
}
public double lengthInPixel(int x1,int y1,int x2,int y2){
double length_in_pixel;
double x=Math.pow((x2-x1), 2);
double y=Math.pow((y2-y1), 2);
length_in_pixel = Math.sqrt(x+y);
return length_in_pixel;
}
public double lengthInCm(int x1,int y1,int x2,int y2){
double length_in_pixel=lengthInPixel(x1,y1,x2,y2);
double length_in_cm = length_in_pixel / pixel_per_cm;
System.out.println("Length in Cm= "+length_in_cm);
return length_in_cm;
}
}
Main.java
public class Main {
public static JFrame frame;
public static void main(String ar[]) {
frame=new JFrame("LINE FRAME");
}
static class ImageComponent extends JComponent implements MouseListener {
Line2D line;
int x1,y1,x2,y2;
public ImageComponent() {
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D gd=(Graphics2D)g;
line=new Line2D.Double(x1,y1,x2,y2);
gd.draw(line);
}
@Override
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
LineFunctions lf=new LineFunctions();
double length=lf.lengthInCm(x1,y1,x2,y2);
System.out.println("Line Length="+length);
repaint();
}
.......................
........................
}
This code finds the length of line from the coordinates, but when i find the length of a line drawn on my screen from the RULER it doesn't saw me the same length as i got from the above code. So, What is the Problem with this and how can i find the exact length of line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
来确定所需的几何图形。特别是,身份变换表示“用户空间中的 72 个单位等于设备空间中的 1 英寸”。GraphicsDevice
的GraphicsConfiguration
的 getNormalizingTransform()另请考虑
Math.hypot()
以获得更高的准确性。You can use the
getNormalizingTransform()
of theGraphicsConfiguration
of yourGraphicsDevice
to determine the required geometry. In particular, an identity transform indicates that "72 units in user space equals 1 inch in device space."Also consider
Math.hypot()
for more accuracy.