你可以运行这个程序......但是,我在理解Graphics类中的一些代码时遇到了问题。我不会责怪任何人,但这是实验练习的一部分..我需要让我的雪人挥动他(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)
{
}
}
}
答案 0 :(得分:0)
首先,你的白色空间让我有点害怕。您可能想要考虑修复缩进和行跳过。如果您的代码整洁,人们更有可能帮助您。
更重要的是,您需要做的是更改手臂尖端的位置,并将其他两根手指的位置定位在该位置。只需根据可变位置重写手指点的位置即可。然后,您所要做的就是使用布尔值和您已设置的按钮来触发动画。
对于一个这么简单的项目你应该没关系,只需在paint函数中添加一小段代码就可以增加位置(如果你想要圆周运动,可以考虑你的x和y值的sin和余弦值) )。只需增加此值,直到达到所需的限制然后转身(可能使用delta变量来合并)。
答案 1 :(得分:0)
这是我的最终输出^^ ...
/**
*
*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;
}
}
}
}