Created
January 30, 2015 22:39
-
-
Save shannah/7cae9fcb568e822c48ab to your computer and use it in GitHub Desktop.
This file contains 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
public class DrawingCanvasBezier extends Component { | |
GeneralPath p = new GeneralPath(); | |
int strokeColor = 0x0000ff; | |
int strokeWidth = 10; | |
private float lastX = -1; | |
private float lastY = -1; | |
private boolean odd=true; | |
public void addPoint(float x, float y){ | |
if ( lastX == -1 ){ | |
p.moveTo(x, y); | |
} else { | |
float controlX = odd ? lastX : x; | |
float controlY = odd ? y : lastY; | |
p.quadTo(controlX, controlY, x, y); | |
} | |
odd = !odd; | |
lastX = x; | |
lastY = y; | |
repaint(); | |
} | |
@Override | |
protected void paintBackground(Graphics g) { | |
super.paintBackground(g); | |
Stroke stroke = new Stroke( | |
strokeWidth, | |
Stroke.CAP_BUTT, | |
Stroke.JOIN_ROUND, 1f | |
); | |
g.setColor(strokeColor); | |
// Draw the shape | |
g.drawShape(p, stroke); | |
} | |
@Override | |
public void pointerPressed(int x, int y) { | |
addPoint(x-getParent().getAbsoluteX(), y-getParent().getAbsoluteY()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment