在Java中实现2D Polygon2D [英] Implementing Polygon2D in Java 2D

查看:743
本文介绍了在Java中实现2D Polygon2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Java2D库绘制创建Java中的2D游戏,我真的需要一个引脚悬空precision多边形,我可以同时使用来绘制游戏对象,并做他们的碰撞检测对象。不幸的是,Java的多边形对象只进来INT precision,并没有等同Polygon2D像有与矩形和矩形。我已经做了足够的研究,看看我有几个选择,但没有人似乎很不错。

I'm creating a 2D game in Java using the Java2D library for drawing, and I really need a float-precision Polygon object that I can use both to draw game objects and to do collision detection on them. Unfortunately, Java's Polygon object comes in int precision only, and there is no equivalent Polygon2D like there is with Rectangle and Rectangle2D. I've already done enough research to see that I have a few options, but none of them seem very good.


  1. 使用Path2D 。据一个Java开发人员发布此论坛,缺乏Polygon2D的是一个疏忽,但其建议的更换Path2D。不幸的是,Path2D不提供方式来访问其个人顶点或边,这是我需要为了做到碰撞检测(具体我需要获得一个矢量正交的边)。

  1. Use Path2D. According to a Java developer posting in this forum, the lack of Polygon2D was an oversight, but its suggested replacement is Path2D. Unfortunately, Path2D doesn't provide a way to access its individual vertices or edges, which I need in order to do collision detection (specifically I need to get a vector orthogonal to each edge).

实现自己Polygon2D 实现Shape接口,这样我还可以把它传递给 Graphics2D.draw(形状)。这看起来像这将是pretty困难。 Shape接口需要技巧对实现诸如方法包含(矩形)的getPathIterator(的AffineTransform)。对于的getPathIterator 特别,好像是为了实现它我需要返回类型的PathIterator的对象,但也有形状的PathIterator接口在没有提供具体的实现公共AWT包。

Implement my own Polygon2D that implements the Shape interface so that I can still pass it to Graphics2D.draw(Shape). This looks like it would be pretty difficult. The Shape interface requires tricky-to-implement methods like contains(Rectangle2D) and getPathIterator(AffineTransform). For getPathIterator in particular, it seems that in order to implement it I'd need to return an object of type PathIterator, but there are no concrete implementations of the PathIterator interface available in the public AWT packages.

裹Path2D 在一个对象,它会记住各个顶点,并将其提供给客户端。这为我工作,当我需要的想起它的组成形状的面积:我把它包在实现Shape接口,并转发所有形状的方法,以区域的实现他们的CompoundShape类,同时保持已添加到每个形状的轨道区一个ArrayList。这里的问题是,如果我跟踪的各个顶点的浮动的S两个数组,有没有办法将它们暴露给用户,而用户的可能性改变了顶点 - 而因为这将直接数组访问发生,内部Path2D不会得到通知的修改

Wrap Path2D in an object that "remembers" the individual vertices and provides them to the client. This worked for me when I needed an Area that remembered its component shapes: I wrapped it in a CompoundShape class that implemented the Shape interface and forwarded all the Shape methods to Area's implementation of them, while keeping track of each Shape that was added to the Area in an ArrayList. The problem with this is that if I keep track of the individual vertices in two arrays of floats, there is no way to expose them to the user without the possibility of the user changing the vertices - and since that would happen by direct array access, the internal Path2D wouldn't get notified of the changes.

复制Polygon.java 。 Java的多边形类的实际来源$ C ​​$ C可以用grep的code.com,我可以简单地用<$ C替换顶点相关的 INT 取值$ C>浮动取值在整个获得Polygon2D。不幸的是,当我尝试这样做,行进口sun.awt.geom.Crossings; 扔了一个编译器错误说的类型错爱是无法访问的,由于所需库C限制:\\ Program Files文件\\的Java \\ jre7 \\ lib目录\\ rt.jar中。据<一个href=\"http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar\">this问题出现这种情况是因为Sun的许可协议prevents你用自己的替换核心Java类,但多边形不会尝试做到这一点 - 它只是创造型sun.awt.geom.Crossings的对象,没有更换或延长发生,我确信把我的多边形的副本包不叫的java。

Copy Polygon.java. The actual source code of Java's Polygon class is available on grepcode.com, and I could simply replace the vertex-related ints with floats throughout to get a Polygon2D. Unfortunately, when I tried this, the line import sun.awt.geom.Crossings; threw a compiler error saying "The type Crossings is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar." According to this question that happens because Sun's license agreement prevents you from replacing core Java classes with your own, but Polygon doesn't try to do that - it simply creates an object of type sun.awt.geom.Crossings, no replacing or extending happens, and I made sure to put my copy of Polygon in a package not called "java".

什么是着手最好的方法?我倒是AP preciate或者建议,如何使这些选项的一个或对于不具有这些问题,遇到的另一种选择的想法。

What's the best way to proceed with this? I'd appreciate either suggestions for how make one of these options work or an idea for another option that doesn't have the problems these encounter.

推荐答案

我也建议的 Path2D 的GeneralPath 是一个传统类;不使用它。

I would also recommend Path2D. GeneralPath is a legacy class; don't use it.

Path2D确实提供接入到顶点值,尽管它迂回的方式。您需要使用的PathIterator

Path2D does provide access to the vertex values, albeit it a roundabout fashion. You need to use a PathIterator:

PathIterator pi = path.getPathIterator(null);
float[] value = new float[6];
float x = 0, y = 0;

while (!pi.isDone()) {
    int type = pi.currentSegment(values);
    if (type == PathIterator.SEG_LINETO) {
        x = values[0];
        y = values[1];
    }
    else if (type == PathIterator.SEG_CLOSE) {
        x = 0;
        y = 0;
    }
    else {
        // SEG_MOVETO, SEG_QUADTO, SEG_CUBICTO
    }
    pi.next();
}

当你准备一些花样,可以展开的其他,以支持二次和三次曲线。
我假设你正在谈论的多边形你不需要那些在这个时候。

When you're ready to get fancy, you can expand that else to support the quadratic and cubic curves. I assume you don't need those at this time as you're talking about polygons.

此外,Path2D有用于测试一些方便​​的静态方法是否路径<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Path2D.html#intersects%28double,%20double,%20double,%20double%29\"相对=nofollow>相交一个矩形和路径是否<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Path2D.html#contains%28double,%20double%29\"相对=nofollow>包含矩形或点。可悲的是,有用于测试的路径相交或含有另一条路径没有方法。

Also, Path2D has some handy static methods for testing whether the path intersects a rectangle and whether the path contains a rectangle or point. Sadly, there are no methods for testing for a path intersecting or containing another path.

这篇关于在Java中实现2D Polygon2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆