-1

You Can run this program ... but , i have a problems in understanding some of the codes in Graphics class .. I don't blame anyone for this but this is a part of a lab exercise .. i need to make my snowman wave his (g.drawline) hands . using a button.. or if you can provide another way .. please do .. i'll appreciate it.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;


public class SnowMan extends Applet implements  ActionListener
{

    Button button1 = new Button ("Wave!");
    public void init()
    {
        setLayout(new BorderLayout());
        add(button1, BorderLayout.NORTH);
        button1.addActionListener(this);
    }

    public void paint(Graphics g)
    {   



            g.fillRoundRect(240,100,10,10,10,10);
            g.fillRoundRect(264,100,10,10,10,10);
            /**
             *eyes
             */


            g.fillRoundRect(248,180,10,10,10,10);
            g.fillRoundRect(248,210,10,10,10,10);
            g.fillRoundRect(248,240,10,10,10,10);
            g.fillRoundRect(248,270,10,10,10,10);

            g.drawArc(232,65,50,70,245,50);

            /**
             *mouth
             */

            g.drawLine(200,169, 150, 125);
            g.drawLine(165,139, 145, 139);
            g.drawLine(165,139, 160, 120);

                /**
                 *
                 *right hands
                 */
            g.drawLine(300,169, 350, 125);  
            g.drawLine(333,139, 338, 120);                       


g.drawLine(333,139, 355, 138);


g.drawRoundRect(219,74,75,75,75,75);

g.setXORMode(Color.red);    


g.fillRoundRect(175,148,150,150,150,150);      

g.setPaintMode();
}

public void actionPerformed (ActionEvent e)
{       



Object source = e.getSource(); 

if (button1 == source)
{

}


}



}
mds
  • 13
  • 5

2 Answers2

0

For starters your white space scares me a little bit. You might want to think about fixing your indentation and line skips. People are much more likely to help you if your code is neat.

More to the point, what you need to do is change the location of the tip of the arm and base the location of the other two fingers on that location. Simply rewrite the location of the points of the fingers in terms of the variable location. Then all you have to do is trigger an animation by using a boolean and the button you already set up.

For a project as simple as this you should be okay with just adding in a little block of code in the paint function which increments the location (if you want circular motion think sin and cosine for your x and y values of the arm). Just increment this until you reach the desired limit then turn around (maybe use a delta variable to consolidate this).

keeslinp
  • 176
  • 1
  • 5
  • thnx .. im just a starter .. hehe sorry for the line skips. – mds Mar 09 '14 at 16:55
  • umm .. still have a problem , i've set a counter on this program.. i set the repaint method in the paint class , but it flickers .. what should i do to get rid of the flickering. i've searched about the double bufferImage but , i don't any idea how to use it .. please help – mds Mar 11 '14 at 06:47
  • and , what do you mean by trigger an animation by using a boolean and the button – mds Mar 11 '14 at 06:48
  • For double buffering that simply means that you are drawing to an image instead of the actual screen. The reason you do this is because in the split second between clearing the screen and drawing all you need to draw the user will see all of that, causing the flicker. On the other hand if you draw to an image and simply draw that over top everything that is already on the screen there will be no flicker because it happens instantly. For the boolean I simply mean add a boolean could be called "anim" if you want, then you add a line in your ActionPerformed code that makes it true. – keeslinp Mar 12 '14 at 16:28
  • I finally got it .. thanks for the advise , ^^ .. i just need to set the repaint.. outside the paint class.. and for your incrementing suggestion .. and boolean .. i finally understand it. Thnx for the help dude .. – mds Mar 13 '14 at 16:30
  • It's what I'm here for! Ya know, there's a magic thank you button next to my post ;) haha. – keeslinp Mar 13 '14 at 16:51
  • haha .. can i ask for a help next time? will you be there if i need you? .. cheezy question.. LOL :D – mds Mar 13 '14 at 16:58
  • btw, who the hell.. downgraded my reputation.. please give it back T_T ... Im just a start .. lol – mds Mar 13 '14 at 17:00
0

This is my final output ^^ ...

 /**
 *
 *Author : Mark Dave Soriano 
 *Studied @ STI General Santos City , Philippines
 *Copro - Exercise 
 * "Snow Man"
 *Objective: Wave the hands of the snowman
 *
 *
 */

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class SnowMan extends Applet implements  ActionListener
{
            Button button2Wave = new Button ("Wave");   
            int xStart = 198;
            int yEnd = 169;
            int xEndingsStart = 150;
            int yStartEndings = 125;
            int ctr;
            int ctr2nd;

      public void init ()
      { 
            setLayout(new BorderLayout());
            add(button2Wave, BorderLayout.SOUTH);
            button2Wave.addActionListener(this);
      }
      public void paint(Graphics g)
    {   
       g.setColor(Color.blue);
       g.fillRect(0, 250, 500,150 ); // ground

       g.setColor (Color.yellow);
       g.fillOval (-40, -40, 80, 80); // sun

       g.setColor(Color.white);     
       g.fillRoundRect(219,74,75,75,75,75);//head
       g.fillRoundRect(175,148,150,150,150,150); //body
       g.fillRoundRect(63,10,130,60,80,80);//dialog oval

       g.setColor(Color.black);
       g.fillRoundRect(248,180,10,10,10,10);//body buttons
       g.fillRoundRect(248,210,10,10,10,10);//body buttons
       g.fillRoundRect(248,240,10,10,10,10);//body buttons
       g.fillRoundRect(248,270,10,10,10,10);//body buttons
       g.drawString("Hi! I'm Olaf ", 97,35);//body buttons
       g.drawString("and I like Warm Hugs ", 70,45);

       g.fillRoundRect(240,100,10,10,10,10);//eyes
       g.fillRoundRect(264,100,10,10,10,10);//eyes
       g.drawArc(232,65,50,70,245,50);//smile

       g.drawLine (230, 75, 280, 75); // brim of hat
       g.fillRect(235,50, 40, 25); // top of hat

       g.drawLine(xStart,yEnd, xEndingsStart, yStartEndings);//right arm
       g.drawLine(300,169, 350, 125);//left arm
       setBackground( Color.cyan);

       if (ctr==1)
       {
            g.clearRect(98,70,100,100);
            ctr +=1;
            g.clearRect(300,70,100,100);
            if (ctr==2)
            {
                g.drawLine(xStart,yEnd, 131, 169);
                g.drawLine(300,169,369,169);
                ctr2nd+=1;
                if(ctr2nd==4)
                {
                    ctr2nd-=3;
                }
            }
        }
        //wave down
       if(ctr2nd==2)
       {
            g.clearRect(98,70,100,100);
            ctr2nd +=1;
            g.clearRect(300,70,100,100);

            if(ctr2nd==3)
            {
                g.drawLine(xStart,yEnd, xEndingsStart, yStartEndings);
                g.drawLine(300,169, 350, 125);
                ctr-=2;
            }
       }
         //wave up

    }

    public void actionPerformed (ActionEvent e)
    {   
        Object source = e.getSource();
        if (source==button2Wave)
        {   
         System.out.println ("Counter Value: " + ctr);
         if (ctr==0)
         {
         this.repaint(98,70,100,100);
         this.repaint(300,70,100,100);
         ctr +=1;
         }
         if(ctr2nd==1)
         {
         this.repaint(98,70,100,100);
         this.repaint(300,70,100,100);
         ctr2nd+=1;
         }  
        }

    }
}
mds
  • 13
  • 5