坐标转换困境,用户坐标到设备坐标?
我正在处理一项任务,其中包括诸如道路“长度”(以“毫米”为单位)等数据。根据规范,我的 JFrame 应该是 4 米 * 3 米(如何将其转换为设备坐标......?)。
现在,我至少知道这些数据是真实世界的数据,而不是设备的像素数据。然而,在找到一些来源后,您实际上可以通过 Graphics2D 类将这些数据转换为像素。
我找到这些信息的资源位于,
Core Java 2,卷 II - 高级功能,作者 - Cay S. Horstmann &加里·康奈尔
然而,我在谷歌上搜索了实际的例子,但没有成功。
有谁知道我可以阅读更多相关信息的来源。如果你有任何有人实际做过类似事情的例子,那就会提供更多信息。
到目前为止的研究:到目前为止,我知道我们需要对 Graphics2D 类的像素数(以米为单位)做出假设。在 Graphics2D 中,有一种方法可以帮助您设置此假设。方法是,
g2D.scale(pixelsPerMeter, pixelsPerMeter);
//And than I can draw a line with meter or millimetre coordinates
g2D.draw(new Line2D.Double(coordinates in meter));
我尝试了上面的代码,但很难在我真正想要的地方画线。
任何理解帮助都会非常有帮助。
谢谢
I am working on a task which includes data such as "length" of a road in "millimetre". And as per the specification my JFrame should be 4meter * 3meter ( How to convert this into device coordinate...?).
Now, I know at least that this data is real world data and not device's pixel data. However after finding out few sources you can actually transform these data into pixel by Graphics2D class.
Resource I found these information is in,
Core Java 2, Volume II - Advanced Feature, Author - Cay S. Horstmann & Gary Cornell
However, I googled for actual examples with no luck.
Does anyone know any source where I can read more about this. And if you have any examples where someone has actually did something like this, That would be more informative.
Research so far : So far I know that we need to give a assumptions to Graphics2D class of number of pixels in meters. In Graphics2D there is method which helps you to set this assumption. Method is ,
g2D.scale(pixelsPerMeter, pixelsPerMeter);
//And than I can draw a line with meter or millimetre coordinates
g2D.draw(new Line2D.Double(coordinates in meter));
I tried above code but really hard to draw line where I actually want to.
Any help of understanding will be very helpful.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现创建一个更容易
java.awt.geom.AffineTransform
对象并自己将坐标从米转换为像素。
在您的情况下,请使用
AffineTransform.getScaleInstance(sx, sy)
。然后我调用 Graphics2D 函数来绘制线条和多边形
使用像素坐标。
这使得调试更容易:您可以看到之前的坐标值
以及转型之后。
Java 教程中的高级主题部分描述了如何
使用 Graphics2D 进行转换,示例:
http://download.oracle.com/javase/tutorial/2d/index.html
I find it easier to create an
java.awt.geom.AffineTransform
object and transform the coordinates from meters to pixels myself.
In your case, use
AffineTransform.getScaleInstance(sx, sy)
.Then I call
Graphics2D
functions to draw my lines and polygonsusing pixel coordinates.
This makes debugging easier: you can see the coordinate values before
and after the transformation.
The Advanced Topics section in the Java Tutorial describes how
to do transformations with
Graphics2D
, with examples:http://download.oracle.com/javase/tutorial/2d/index.html
您可以获得任何特定
GraphicsDevice 的隐式几何图形
通过getNormalizingTransform()
方法。您还应该抽象所需的模型<->视图转换。此示例明确使用四种
*scale*
方法来实现此目的。You can obtain the implicit geometry of any particular
GraphicsDevice
via thegetNormalizingTransform()
method.You should also abstract the required model <-> view transformations. This example does so explicitly using four
*scale*
methods.我认为让 UI 工具包为您完成所有缩放工作并不是一个好主意。您可能找不到能够实际显示 4×3 米窗口的硬件,因此如何将现实世界坐标缩放或裁剪为实际向用户显示的内容的决定在逻辑上属于您的代码而不是通用 UI层。例如,如果在项目后期您想要重用一些通用 UI 元素,可能很难说服他们应该尝试绘制一个一米高的按钮,上面有 40 厘米的字母。
I don't think it's a good idea to leave it to the UI toolkit to do all of the scaling for you. You probably won't find hardware that can actually show a 4×3 meter window, so the decision of how to scale or crop the real-world coordinates to something you actually display to your users logically belongs in your code rather than a generic UI layer. For example, if later in the project you want to reuse some generic UI elements, it might be difficult to convince them that they should try to draw a button a meter high, with 40-cm letters on it.
试试这个
try this