PP2.14 按照以下要求修改程序Snowman: ·在其身上添加两个红色按钮。 ·将雪人的表情由笑脸变成皱眉。 ·把太阳移动到图片的右上角。 ·在图片左上角显示你的名字。 ·将整个雪人右移20个像素。
注意: 1. 在不同的IDE环境下,有部分代码可能需要变更。Java代码中的package和class名称自行设置,本文中采用Test。 2. 本程序应用到Applet,使用IDE时需要注意配置问题,具体请参考:https://blog.csdn.net/Trista_1999/article/details/103204112,如果还有其他配置问题,可自行搜索。 IDE工具:IntelliJ IDEA
代码块:
原本Snowman程序:
import javax.swing.JApplet;
import java.awt.*;
public class Test extends JApplet {
public void paint (Graphics page) {
final int MID = 150;
final int TOP = 50;
setBackground (Color.cyan);
page.setColor (Color.blue);
page.fillRect (0, 175, 300, 50);
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80);
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40);
page.fillOval (MID-35, TOP+35, 70, 50);
page.fillOval (MID-50, TOP+80, 100, 60);
page.setColor (Color.black);
page.fillOval (MID-10, TOP+10, 5, 5);
page.fillOval (MID+5, TOP+10, 5, 5);
page.drawArc (MID-10, TOP+20, 20, 10, 190, 160);
page.drawLine (MID-25, TOP+60, MID-50, TOP+40);
page.drawLine (MID+25, TOP+60, MID+55, TOP+60);
page.drawLine (MID-20, TOP+5, MID+20, TOP+5);
page.fillRect (MID-15, TOP-20, 30, 25);
}
}
原实现效果:
修改后的Snowman程序:
import javax.swing.JApplet;
import java.awt.*;
public class Test extends JApplet {
public void paint (Graphics page) {
final int MID = 150;
final int TOP = 50;
setBackground (Color.cyan);
page.setColor (Color.blue);
page.fillRect (0, 175, 300, 50);
page.setColor (Color.yellow);
page.fillOval (255, -40, 80, 80);
page.setColor (Color.white);
page.fillOval (MID, TOP, 40, 40);
page.fillOval (MID-15, TOP+35, 70, 50);
page.fillOval (MID-30, TOP+80, 100, 60);
page.setColor(Color.red); //Add the red button
page.fillOval (MID+14, TOP+55, 10, 10);
page.fillOval (MID+14, TOP+100, 10, 10);
page.setColor (Color.black);
page.fillOval (MID+10, TOP+10, 5, 5);
page.fillOval (MID+25, TOP+10, 5, 5);
page.drawArc (MID+10, TOP+20, 20, 10, 20, 160);
page.drawLine (MID-5, TOP+60, MID-30, TOP+40);
page.drawLine (MID+45, TOP+60, MID+75, TOP+60);
page.drawLine (MID, TOP+5, MID+40, TOP+5);
page.fillRect (MID+5, TOP-20, 30, 25);
page.drawLine(10, 10, 10, 20);
page.drawLine(10, 10, 15, 20);
page.drawLine(15, 10, 15, 20);
page.drawLine(22, 10, 20, 20);
page.drawLine(22, 10, 25, 20);
page.drawLine(21, 17, 23, 17);
page.drawLine(30, 10, 32, 20);
page.drawLine(35, 10, 32, 20);
page.drawLine(40, 10, 40, 10);
page.drawLine(40, 12, 40, 20);
}
}
实现效果:



