如何在Java swing中构建点击组件?
作者:互联网
我已经构建了一个只显示一行的自定义组件.该线条从左上角到右下角绘制为paint方法中的Line2D.背景是透明的.我扩展了JComponent.当鼠标指针位于最大值时,这些线条组件可拖动并更改其线条颜色.距绘制线15个像素.
但是如果我将多个这些组件添加到另一个扩展JPanel的自定义组件中,它们有时会重叠.我想实现,如果鼠标指针距离线超过15个像素,鼠标事件应该通过组件落下.如何让它落空是我的问题.
这甚至可能吗?
提前致谢!
解决方法:
对于我在大学的最后一年项目,我做了一个白板程序并遇到了同样的问题.对于用户绘制的每个形状,我创建了一个JComponent,它在绘制矩形时很好,但使用自由形式线工具则更难.
我最终修复它的方法是完全取消JComponents.我有一个JPanel,它持有Vector(我认为)自定义Shape对象.每个物体都有自己的坐标和线条厚度等.当用户单击板时,JPanel上的鼠标侦听器触发并遍历每个Shape,在每个Shape上调用一个contains(int x,int y)方法(x和y是事件的坐标).因为Shapes在绘制时被添加到Vector中,所以我知道返回true的最后一个是最顶层的Shape.
这就是我用于直线包含方法的内容.数学可能有点不确定,但它对我有用.
public boolean contains(int x, int y) {
// Check if line is a point
if(posX == endX && posY == endY){
if(Math.abs(posY - y) <= lineThickness / 2 && Math.abs(posX - x) <= lineThickness / 2)
return true;
else
return false;
}
int x1, x2, y1, y2;
if(posX < endX){
x1 = posX;
y1 = posY;
x2 = endX;
y2 = endY;
}
else{
x1 = endX;
y1 = endY;
x2 = posX;
y2 = posY;
}
/**** USING MATRIX TRANSFORMATIONS ****/
double r_numerator = (x-x1)*(x2-x1) + (y-y1)*(y2-y1);
double r_denomenator = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
double r = r_numerator / r_denomenator;
// s is the position of the perpendicular projection of the point along
// the line: s < 0 = point is left of the line; s > 0 = point is right of
// the line; s = 0 = the point is along the line
double s = ((y1-y)*(x2-x1)-(x1-x)*(y2-y1) ) / r_denomenator;
double distance = Math.abs(s)*Math.sqrt(r_denomenator);
// Point is along the length of the line
if ( (r >= 0) && (r <= 1) )
{
if(Math.abs(distance) <= lineThickness / 2){
return true;
}
else
return false;
}
// else point is at one end of the line
else{
double dist1 = (x-x1)*(x-x1) + (y-y1)*(y-y1); // distance to start of line
double dist2 = (x-x2)*(x-x2) + (y-y2)*(y-y2); // distance to end of line
if (dist1 < dist2){
distance = Math.sqrt(dist1);
}
else{
distance = Math.sqrt(dist2);
}
if(distance <= lineThickness / 2){
return true;
}
else
return false;
}
/**** END USING MATRIX TRANSFORMATIONS****/
}
posX和posY组成行开头的坐标,endX和endY是行的结尾.如果单击位于线条中心的lineThickness / 2范围内,则返回true,否则必须沿着线条的正中间单击.
绘制形状是将JPanel的Graphics对象传递给每个Shape并使用它进行绘制的情况.
标签:java,swing,mouseevent,jcomponent 来源: https://codeday.me/bug/20190610/1209867.html