|
Re: Own widget [message #1227416 is a reply to message #1226185] |
Sat, 04 January 2014 12:26 |
Martin Bayer Messages: 32 Registered: December 2012 |
Member |
|
|
I finally solved it by setting the Region. Here is method I implemented to get the region around line painted by gc.drawLine. Do not set the region as part of paint listener because it invokes repaint of the component:
private int[] drawLine(Point startPoint, Point endPoint, int thickness) {
int dX = Math.abs(startPoint.x - endPoint.x);
int dY = Math.abs(startPoint.y - endPoint.y);
double aTan;
aTan = Math.atan((double) dY / (double) dX);
int offsetX = (int) Math.ceil(Math.sin(aTan) * thickness / 2);
int offsetY = (int) Math.ceil(Math.cos(aTan) * thickness / 2);
boolean startPointLower = startPoint.y > endPoint.y;
if (startPointLower) {
p1 = new Point(startPoint.x - offsetX, startPoint.y - offsetY);
p2 = new Point(endPoint.x - offsetX, endPoint.y - offsetY);
p3 = new Point(endPoint.x + offsetX, endPoint.y + offsetY);
p4 = new Point(startPoint.x + offsetX, startPoint.y + offsetY);
} else {
p1 = new Point(startPoint.x + offsetX, startPoint.y - offsetY);
p2 = new Point(endPoint.x + offsetX, endPoint.y - offsetY);
p3 = new Point(endPoint.x - offsetX, endPoint.y + offsetY);
p4 = new Point(startPoint.x - offsetX, startPoint.y + offsetY);
}
int minX = getMinX();
int minY = getMinY();
if (minX < 0) {
this.offsetX = Math.abs(minX);
} else {
this.offsetY = 0;
}
if (minY < 0) {
this.offsetY = Math.abs(minY);
} else {
this.offsetY = 0;
}
return new int[] { p1.x + this.offsetX, p1.y + this.offsetY,
p2.x + this.offsetX, p2.y + this.offsetY, p3.x + this.offsetX,
p3.y + this.offsetY, p4.x + this.offsetX, p4.y + this.offsetY };
}
|
|
|
Powered by
FUDForum. Page generated in 0.03903 seconds