is there any equivalent to java GeneralPath , because awt classes not allowed in Android , in my program I am drawing a shape from data file and add these data to GeneralPath as MoveTo and LineTo and then draw using java.awt.Graphics2D fill(Shape) .
One of the option is the ShapeRenderer and its polygon function. All you have to do is just pass into it a float array in a format
[x1, y1, x2, y2, x3, y3...]
The only problem is that you cannot draw filled polygons.
Code example to draw a pickup:
@Override
public void show()
{
shapeRenderer = new ShapeRenderer();
}
@Override
public void render(float delta)
{
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(Color.WHITE);
shapeRenderer.polygon(new float[]{ 20, 20, 20, 40, 60, 40, 60, 60, 90, 60, 110, 40, 110, 20, 100, 20, 90, 10, 80, 20, 60, 20, 50, 10, 40, 20 });
shapeRenderer.end();
}
and the rezult:
Another solution allows you to draw filled shapes is to use PolygonSpriteBatch and generating PolygonRegion for it. The following code is based on this question Mikael Mayer's comment and it will draw a pickup - the filled one this time:
@Override
public void show()
{
//creating a pixmap - array of pixels
Pixmap pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
//setting it white color and fill format
pix.setColor(Color.WHITE.toIntBits());
pix.fill();
//now we are passing vertices and triangles
PolygonRegion polyReg = new PolygonRegion(new TextureRegion( new Texture(pix) ),
new float[] //vertices
{
20, 20, 20, 40, 60, 40, 60, 60, 90, 60, 110, 40, 110, 20, 100, 20, 90, 10, 80, 20, 60, 20, 50, 10, 40, 20
}
, new short[] //triangles
{
0, 1, 2,
2, 3, 4,
4, 5, 2,
5, 6, 2,
6, 7, 2,
7, 8, 9,
9, 2, 7,
9, 10, 2,
10, 11, 12,
10, 0, 2
});
//creating a sprite based on polygonRegion above
poly = new PolygonSprite(polyReg);
polyBatch = new PolygonSpriteBatch();
}
@Override
public void render(float delta)
{
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
polyBatch.begin();
poly.draw(polyBatch);
polyBatch.end();
}
and the result is:
And now here:
PolygonRegion polyReg = new PolygonRegion(new TextureRegion( new Texture(pix) ), ...
we have to pass to the method:
We need to define what triangles should be draw as filled - application don't know this. So we are passing the short array with vertices numbers - every next three for one triangle - in a counter-clockwise direction.
Look at this:
Having this vertices we have to "fill" the pickup with triangles. How to do this explains an image beneath:
Compare the image with short array in a code and you should understand how it works.