import javax.swing.JApplet;
import java.awt.*;
public class Snowman extends JApplet {
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
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); // ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40); // head
page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
page.fillOval (MID-50, TOP+80, 100, 60); // lower torso
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); // smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect(MID-15, TOP-20, 30, 25); // top of hat
}
}
这是所有代码。在我声明了两个最终变量后声明了setBackground,提前感谢,我从一本书“Java软件解决方案”中得到了这个代码,我一遍又一遍地看着它,没有运气:/提前感谢:)
答案 0 :(得分:5)
//<applet code='Snowman' width=300 height=200></applet>
import javax.swing.*;
import java.awt.*;
public class Snowman extends JApplet {
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
public void init() {
add(new SnowmanPanel());
validate();
}
}
class SnowmanPanel extends JPanel {
final int MID = 150;
final int TOP = 50;
SnowmanPanel() {
setBackground (Color.cyan);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.setColor(Color.blue);
page.fillRect(0, 175, 300, 50); // ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40); // head
page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
page.fillOval (MID-50, TOP+80, 100, 60); // lower torso
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); // smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect(MID-15, TOP-20, 30, 25); // top of hat
}
}
JPanel
或JComponent
并在那里绘画。对于后者的自定义绘画,请覆盖paintComponent(Graphics)
super.paintComponent(Graphics)
init()
)中设置颜色,而不是在paint方法中设置颜色。BufferedImage
并将图片放在ImageIcon
的{{1}}中。哪个更简单。答案 1 :(得分:3)
试试此代码
import java.awt.*;
import javax.swing.JApplet;
public class SnowMan extends JApplet
{
public SnowMan()
{
setBackground(Color.cyan);
}
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
@Override
public void paint(Graphics page)
{
final int MID = 150;
final int TOP = 50;
page.setColor(Color.blue);
page.fillRect(0, 175, 300, 50); // ground
page.setColor(Color.yellow);
page.fillOval(-40, -40, 80, 80); // sun
page.setColor(Color.white);
page.fillOval(MID - 20, TOP, 40, 40); // head
page.fillOval(MID - 35, TOP + 35, 70, 50); // upper torso
page.fillOval(MID - 50, TOP + 80, 100, 60); // lower torso
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); // smile
page.drawLine(MID - 25, TOP + 60, MID - 50, TOP + 40); // left arm
page.drawLine(MID + 25, TOP + 60, MID + 55, TOP + 60); // right arm
page.drawLine(MID - 20, TOP + 5, MID + 20, TOP + 5); // brim of hat
page.fillRect(MID - 15, TOP - 20, 30, 25); // top of hat
}
}
答案 2 :(得分:1)
setBackground (Color.cyan);
它在我的IDE中正常运行。我也改变了背景的颜色。它工作得很好,也很合适。无需更改代码。确保在创建课程时。
答案 3 :(得分:1)
paint(Graphics)
方法仅用于绘制参数(在您的情况下为page
)。
应用程序小程序背景颜色已在此阶段处理。
这就是为什么你可以通过在构造函数中设置它来解决问题:
public Snowman()
{
this.setBackground(Color.cyan);
}
答案 4 :(得分:1)
我认为你需要使用,getContentPane()。setBackground()