Last active
July 8, 2020 09:57
-
-
Save codenameone/65f531adae2e8c22afc8 to your computer and use it in GitHub Desktop.
Demonstration of shaped clipping effects in Codename One
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Image duke = null; | |
try { | |
// duke.png is just the default Codename One icon copied into place | |
duke = Image.createImage("/duke.png"); | |
} catch(IOException err) { | |
Log.e(err); | |
} | |
final Image finalDuke = duke; | |
Form hi = new Form("Shape Clip"); | |
// 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); | |
Stroke stroke = new Stroke(0.5f, Stroke.CAP_ROUND, Stroke.JOIN_ROUND, 4); | |
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.setClip(path); | |
g.setAntiAliased(true); | |
g.drawImage(finalDuke, 0, 0, 50, 100); | |
g.setClip(path.getBounds()); | |
g.drawShape(path, stroke); | |
g.translate(-(int)(((float)rect.getX()) / widthRatio), -(int)(((float)rect.getY()) / heightRatio)); | |
g.resetAffine(); | |
}); | |
hi.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage of Shape & GeneralPath.
From the Codename One project