0

I'm trying to create a simple bowling ball animation. I would like the finger holes in the ball to turn about the center of the ball for the effect of rolling. I've casted my object to Graphics2D and used rotate, but I don't think I'm utilizing it correctly.

public void move( )
{
    setX( getX( ) + speed);
}

public void draw( Graphics g )
{
    Graphics2D g2d = (Graphics2D) g;

    int startY = getY( );
    int startX = getX( );

    //body
    g.setColor( Color.BLACK );
    g.fillOval( startX - 30, startY,  30,  30 );

    //holes
    g.setColor( Color.GRAY );
    g2d.rotate(Math.toRadians(2));
    g.fillOval( startX - 14, startY + 2,  6,  6 );
    g.fillOval( startX - 8, startY + 10,  6,  6 );
    g.fillOval( startX - 16, startY + 9,  6,  6 );

}

This is part of a larger program that moves and then draws the image incrementally. I can post more code if needed. Here's what my attempt looks like: bowling balls Thank you

  • You aren't really rotating anything since you always use an angle of "2". You should be continually increments the angle, the same way you increment the "x" location. I would suggest that you want to create an Image or an Icon of a bowling ball. Then you rotate the entire Image/Icon before repainting it. You will then need to update the "angle" of rotation each time your repaint the image, in the same way that you update the "X" location. – camickr Feb 26 '21 at 01:01
  • That makes sense @camickr, although I'm a little confused on where to place these arguments. I'm not having success with adding the following: `g2d.rotate(Math.toRadians(rad));` `rad += 45;` – JoshALogs Feb 26 '21 at 03:08
  • 1) Don't post code in a comment. As you can see the code is not readable. 2) You have a move method where you update the "x" location". That is where you would also update the angle of rotation. – camickr Feb 26 '21 at 03:10
  • Alright @camickr, so I have `g2d.rotate(Math.toRadians(rad));` in the `draw()` method and `rad += 45;` in the `move()` method. The images are aligned now, but they are still rotating around some other point – JoshALogs Feb 26 '21 at 03:17
  • See: 1) https://stackoverflow.com/a/52571767/131872 for an example of rotating an image about its center, You would then need to also increment the "x" value for movement. 2) https://stackoverflow.com/a/29689059/131872 for an example of rotating an Icon. Instead of using a JLabel to display the Icon you would paint the Icon using the `paintIcon(...)` method of the Icon to control the "x" location. This example will also show how you can created a BufferedImage to use as the Image in the first example. – camickr Feb 26 '21 at 03:34
  • *but they are still rotating around some other point* - they rotate around the (0, 0) point by default. – camickr Feb 26 '21 at 03:40
  • This sounds like homework? But any case, here is an example of [rotating a star](https://stackoverflow.com/a/66108980/1552534) about it's center. For a bowling ball you need to remember than the horizontal travel is proportion to the angle of rotation. And you need to update the center of rotation as the ball moves. I would focus on simple concepts at first. Getting famiiar with `Graphics2D.rotate()` and `Graphics2D.translate()` would be a good start. – WJS Feb 26 '21 at 14:03
  • And don't use magic numbers (constants with no name attached). Try to relate the bowling ball characteristics to the diameter. Then you can change the diameter (if desired) and everything should scale accordingly. Even the horizontal travel. – WJS Feb 26 '21 at 14:08

0 Answers0