当我右键单击 Jframe 时,会出现这个饼图。我想要的是在点击事件发生时计算每个饼图的角度。
Color[] c = {Color.BLACK, Color.RED, Color.BLUE, Color.YELLOW,
Color.GREEN, Color.CYAN, Color.MAGENTA, Color.PINK};
for(int i=0; i<8; ++i){
g.setColor(c[i]);
g.fillArc(x, y, w, h, i*45, 45);
}
这是我尝试过的
public void mouseClicked(MouseEvent e)
{
PointerInfo a = MouseInfo.getPointerInfo();
Point d = a.getLocation();
x1 = (int)d.getX();
y1 = (int)d.getY();
int base=x1-CenterX;
int prep=CenterY-y1;
double tan=prep/base;
double angle=Math.atan( tan);
}
但计算出的角度不正确。它有时让我除以零例外。这是我的右键单击事件,它显示了菜单。
public void mousePressed(MouseEvent e) {
if(e.isPopupTrigger())
{
CenterX=e.getX();
CenterY=e.getY();
try {
Thread.sleep(300L);
} catch (InterruptedException ex) {
Logger.getLogger(animate.class.getName()).log(Level.SEVERE, null, ex);
}
p.repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger()){
s=e.getX();
as=e.getY();
p.mx=e.getX();
p.my=e.getY();