java button形状_java – 如何创建复杂的Button形状?

为以下区域定义接口:

//represents any clickable area.

public interface IButton{

boolean contains(int x, int y);

}

然后,如果要使圆形区域可单击,请定义一个类,用于检查x,y坐标是否在某个位置的某个距离内.

public class CircleButton implements IButton{

Point center;

double radius;

public CircleButton(int x, int y, double radius){

this.center = new Point(x,y);

this.radius = radius;

}

//check if x,y coords are within a radius

//from the center of this circle button

public boolean contains(int x, int y){

double dx = x-center.x;

double dy = y-center.y;

return (Math.sqrt(dx*dx+dy*dy) <= radius);

}

}

创建IButtons列表.您将迭代这些以查看用户是否单击了您的一个隐形按钮.

List buttons = new List();

buttons.add(new CircleButton(100,100,200);

然后,每当有人点击你的框架时,使用鼠标点击的位置迭代你的隐形按钮.

public void mouseReleased(MouseEvent e){

for(IButton b : buttons){

if(b.contains(evt.getX(),e.getY()){

//do something depending on what button was clicked.

}

}

}

您可以轻松地看到如何定义这样的隐形矩形按钮,甚至是不规则的多边形形状.您只需要正确实现contains方法.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值