# Drawing java.awt.Shape

**URL:** <https://discourse.processing.org/t/drawing-java-awt-shape/12572>\
**Category:** Coding Questions\
**Created:** [July 7, 2019, 7:44pm UTC](https://discourse.processing.org/t/drawing-java-awt-shape/12572 "2019-07-07T19:44:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sayeth\_We](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@Sayeth\_We](https://discourse.processing.org/u/Sayeth_We)\
**Post date:** [July 7, 2019, 7:44pm UTC](https://discourse.processing.org/t/drawing-java-awt-shape/12572/1 "2019-07-07T19:44:05Z")

</div>

I’m using the java.awt.Shape class for collision detection with intersects() and contains(). However, this means that I’m duplication code, and in particular, magic numbers, when drawing vs checking collisions.

an Example:  
a bit of the collider code

```auto
  public boolean contains(float x, float y, float facing, float checkX, float checkY) {
    AffineTransform at = new AffineTransform();
    at.translate(x,y);
    at.rotate(facing);
    Shape collider = at.createTransformedShape(collision);
    return collider.contains(checkX,checkY);
  }

```

the code to create the collider:

```auto
  protected Shape createCollision() {
    int[] xPoints = new int[3];
    int[] yPoints = new int[3];
    for(int vertex=0; vertex<3; vertex++) {
      float angle=vertex*TWO_PI/3;
      xPoints[vertex]=int(20*cos(angle));
      yPoints[vertex]=int(20*sin(angle));
    }
    Polygon collision = new Polygon(xPoints,yPoints,3);
    return collision;
  }

```

and the render code

```auto
  public void render(float x, float y, float facing) {
    pushMatrix();
    translate(x,y);
    rotate(facing);
    fill(110,82,255);
    beginShape();
    for(int vertex=0; vertex<3; vertex++) {
      float angle=vertex*TWO_PI/3;
      vertex(20*cos(angle),20*sin(angle));
    }
    endShape(CLOSE);
    popMatrix();
  }

```

so as you can see, there’s quit a bit of code duplication there.  
To fix this, I’d like to either:

- Draw the awt Shape directly (this is preferable)  
or
- Create the Vertices/PShape based on the collider (or the inverse, create the collider from the render shape during construction)

Unfortunately, I know how to do neither of those using processing functions.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [July 8, 2019, 10:23am UTC](https://discourse.processing.org/t/drawing-java-awt-shape/12572/2 "2019-07-08T10:23:36Z")

</div>

I have some code to draw Shape in Processing inside PraxisLIVE.

Some of the code in this class might be helpful [https://github.com/praxis-live/praxis/blob/master/praxis.video.pgl/src/org/praxislive/video/pgl/ops/PGLShapeRenderOp.java](https://github.com/praxis-live/praxis/blob/master/praxis.video.pgl/src/org/praxislive/video/pgl/ops/PGLShapeRenderOp.java)

PGLGraphics is basically PGraphics.

EDIT : incidentally, I’m using this with OpenGL. If you’re using the Java2D renderer, you can cast to PGraphicsJava2D, access the Graphics2D and do the same as [https://github.com/processing/processing/blob/master/core/src/processing/awt/PGraphicsJava2D.java#L1358](https://github.com/processing/processing/blob/master/core/src/processing/awt/PGraphicsJava2D.java#L1358)
