我的Java程序有点问题。我想要显示一个滚动圆圈的动画,并且该圆圈在边缘上有一根棍子,而这个圆圈上的小圆圈正从endOfTheStick
移动到contactPoint
并再次返回。问题在于移动这个较小的圆圈。
这些功能为我画了一个旋转的圆圈,圆圈边缘的一根棍子和棍子末端的一个小圆圈:
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
// Drawing slippery slope
g2d.drawPolygon(polyX, polyY, 3);
// Set start position
g2d.translate(50-r1/2, startY-r1);
g2d.rotate(polyAngle, r1/2, r1);
/* Drawing circle
x-value of shift
*/
g2d.translate(x, 0);
g2d.rotate(rotateAngle, r1/2, r1/2);
g2d.drawOval(0, 0, r1, r1);
g2d.drawLine(0, r1/2, r1, r1/2);
g2d.drawLine(r1/2, 0, r1/2, r1);
//Rotating stick
AffineTransform.getRotateInstance(speedStickAngle, stickContact[0], stickContact[1]).transform(stickEnd, 0, stickEnd, 0, 1);
g2d.drawLine(stickContact[0], stickContact[1], (int)stickEnd[0], (int)stickEnd[1]);
//Rotating and moving small circle
AffineTransform.getRotateInstance(speedStickAngle, stickContact[0], stickContact[1]).transform(centerOfSmallCircle, 0, centerOfSmallCircle, 0, 1);
g2d.drawOval((int)centerOfSmallCircle[0]-r2/2, (int)centerOfSmallCircle[1]-r2/2, r2, r2);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
tm.start();
}
public void actionPerformed(ActionEvent e){
if(x<0||x>(getWidth())){
xSpeed =- xSpeed;
speedAngle =- speedAngle;
speedStickAngle =- speedStickAngle;
}
x = x + xSpeed;
rotateAngle = rotateAngle + speedAngle;
repaint();
}
}