Skip to content

Instantly share code, notes, and snippets.

@codenameone
Created March 17, 2016 05:43
Show Gist options
  • Save codenameone/3f2f8cdaabb7780eae6f to your computer and use it in GitHub Desktop.
Save codenameone/3f2f8cdaabb7780eae6f to your computer and use it in GitHub Desktop.
Usage of the Shape drawing API in Codename One and GeneralPath
Form hi = new Form("Shape");
// We create a 50 x 100 shape, this is arbitrary since we can scale it easily
GeneralPath path = new GeneralPath();
path.moveTo(20,0);
path.lineTo(30, 0);
path.lineTo(30, 100);
path.lineTo(20, 100);
path.lineTo(20, 15);
path.lineTo(5, 40);
path.lineTo(5, 25);
path.lineTo(20,0);
hi.getContentPane().getUnselectedStyle().setBgPainter((Graphics g, Rectangle rect) -> {
g.setColor(0xff);
float widthRatio = ((float)rect.getWidth()) / 50f;
float heightRatio = ((float)rect.getHeight()) / 100f;
g.scale(widthRatio, heightRatio);
g.translate((int)(((float)rect.getX()) / widthRatio), (int)(((float)rect.getY()) / heightRatio));
g.fillShape(path);
g.resetAffine();
});
hi.show();
@codenameone
Copy link
Author

Sample usage of Shape & GeneralPath.

From the Codename One project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment