【问题标题】:How to make my snowman wave his hands (drawline) using button in applet如何使用小程序中的按钮让我的雪人挥手(画线)
【发布时间】:2014-03-09 15:10:37
【问题描述】:

你可以运行这个程序......但是,我在理解图形类中的一些代码时遇到了问题......我不会为此责怪任何人,但这是实验室练习的一部分......我需要让我的雪人挥动他的(g.drawline)手。使用按钮.. 或者如果你能提供另一种方式.. 请这样做.. 我会很感激的。

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)
{

}


}



}

【问题讨论】:

    标签: java japplet


    【解决方案1】:

    首先,你的空白让我有点害怕。您可能需要考虑修复缩进和跳行。如果您的代码简洁,人们更有可能帮助您。

    更重要的是,您需要做的是更改手臂尖端的位置,并将其他两个手指的位置基于该位置。只需根据变量位置重写手指点的位置。然后你所要做的就是使用一个布尔值和你已经设置的按钮来触发一个动画。

    对于像这样简单的项目,您应该可以在绘制函数中添加一小段代码来增加位置(如果您想要圆周运动,请考虑手臂的 x 和 y 值的正弦和余弦)。只需增加它,直到达到所需的限制,然后转身(也许使用 delta 变量来巩固它)。

    【讨论】:

    • thnx .. 我只是一个初学者.. 呵呵抱歉跳行了。
    • umm .. 还有一个问题,我在这个程序上设置了一个计数器.. 我在绘画类中设置了 repaint 方法,但是它闪烁.. 我应该怎么做才能摆脱闪烁。我已经搜索过双 bufferImage 但是,我不知道如何使用它..请帮助
    • 还有,使用布尔值和按钮触发动画是什么意思
    • 对于双缓冲,这仅仅意味着您正在绘制图像而不是实际屏幕。你这样做的原因是因为在清除屏幕和绘制所有你需要绘制的瞬间之间,用户会看到所有这些,从而导致闪烁。另一方面,如果您绘制图像并简单地将其绘制在屏幕上已经存在的所有内容之上,则不会出现闪烁,因为它会立即发生。对于布尔值,我的意思是添加一个布尔值,如果需要,可以称为“动画”,然后在 ActionPerformed 代码中添加一行使其变为真。
    • 我终于明白了 .. 感谢您的建议,^^ .. 我只需要在绘画类之外设置重绘.. 以及您的递增建议 .. 和布尔值 .. 我终于明白了。谢谢老兄的帮助..
    【解决方案2】:

    这是我的最终输出^^ ...

     /**
     *
     *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;
             }  
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多