0

So I am currently creating a drawing that creates a Snowman, as well as some other objects in the background. I got the drawing part down, it looks just as it should, but I wanted to add a little bit of movement within it. I used Graphics2D to draw all my shapes, lines, ellipses, etc., but I'm not sure how/if I can make the Snowmans arms wave back and forth with the drawLine method.

I provided the code for the entire Snowman here. It's very simple, as I said it just uses Graphics2D to draw everything. The arm1 and arm2 are near the bottom.

public class Snowman {
    private int x; 
    private int y;
    private int width;
    private int height;
    
public Snowman(int x, int y, int width, int height) {
    this.x=x; 
    this.y=y;
    this.width=width;
    this.height=height;
}
public void draw(Graphics g) 
{
    Graphics2D g2 = (Graphics2D) g;
    
     // the body of the snowman
    g2.setColor(Color.WHITE);
    g2.fillOval(x+125, y+40, 200, 200);
    g2.fillOval(x+75, y+140, 300, 300);
    g2.fillOval(x+25, y+290, 400, 400);

    // the buttons
    g2.setColor(Color.BLACK);
    g2.fillOval(x+210, y+210, 40, 40);
    g2.fillOval(x+210, y+270, 40, 40);
    g2.fillOval(x+210, y+330, 40, 40);

    // the eyes and mouth
    g2.fillOval(x+190, y+98, 20, 20);
    g2.fillOval(x+245, y+98, 20, 20);
    g2.drawArc(x+180, y+120, 100, 50, 200, 140);

    // arm1
    g2.drawLine(x+75, y+270, x, y+90);
   
    //arm2
    g2.drawLine(x+375, y+270, x+450, y+90);
    
    // the hat
    g2.setColor(Color.BLACK);
    g2.fillRect(x+177, y+40, 100, 30);
    g2.fillRect(x+202, y, 50, 50);

  }
}

Is there a way I can implement something to make them move? Or would I have to draw these another way?

TeeDot
  • 1
  • JavaDocs are going to be your best friend here... take a look at the documentation to see what the library is capable of (here are the docs for [Java 17](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/Graphics2D.html) but you can Google if you are using a different version of Java). I'm seeing a couple methods that look promising. –  Feb 15 '23 at 18:32
  • 1) Check out: https://stackoverflow.com/a/75405991/131872 for an example showing how to draw the line at different angles of rotation. 2) Next you would need to add a property to your class to set the current angle of rotation. 3) Then you will need to use a Swing Timer to schedule the animation. Every time the Timer fires you would set the new angle of rotation and then invoke repaint() on your components Check out: https://stackoverflow.com/a/30417857/131872 for a simple example of using a Timer. – camickr Feb 15 '23 at 18:53

1 Answers1

0

you can add a method that adjusts the coordinates of the hand, and this method might contain some if statement so say if the hand reached specific x it will subtract from x to go to the other direction. And then call this method inside the draw function

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 20 '23 at 11:45