gpt4 book ai didi

java - setBackground 语句不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:40 26 4
gpt4 key购买 nike

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 软件解决方案”中获得了这段代码,我一遍又一遍地查看它,但没有运气:/提前感谢:)

最佳答案

Snowman image

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

一般建议。

  • 不要在顶级 Swing 组件中绘制。而是将自定义绘画移动到 JPanelJComponent 中并在那里绘画。对于后者的自定义绘画,覆盖 paintComponent(Graphics)
  • 在做自定义绘画的时候,记得调用super.paintComponent(Graphics)
  • 在构造函数(或 init())中而不是在 paint 方法中设置颜色。

其他建议

  • 对于静态图像,您还可以将其绘制到 BufferedImage 并将图像放在 JLabel 中的 ImageIcon 中。哪个更简单。
  • 如果这本书急于创建小程序,请将其扔掉。小程序比标准应用程序难得多,新手不应尝试。

关于java - setBackground 语句不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9188034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com