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

This is all the code. The setBackground is stated after I declare the two final variables, thanks in advance, I got this code from a book, "Java Software Solutions", I looked over it over and over, and no luck :/ thanks in advance :)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Enigmatic Wang
  • 725
  • 2
  • 8
  • 13

5 Answers5

5

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

General advice.

  • Don't paint in a top-level Swing component. Instead move the custom painting into a JPanel or JComponent and paint there. For custom painting in the latter, override paintComponent(Graphics)
  • When doing custom painting, remember to call super.paintComponent(Graphics)
  • Set the color in the constructor (or init()) rather than in the paint method.

Other advice

  • For a static image, you can also draw it to a BufferedImage and put the image in an ImageIcon in a JLabel. Which is simpler.
  • If this book has rushed into creating applets, toss it away. applets are much more difficult than standard apps., and should not be attempted by newbies.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    @V1rtualCurry: Note that the `applet` tag in the source make it easy to test from the command line, e.g. `/usr/bin/appletviewer SnowmanPanel.java`. +1 – trashgod Feb 08 '12 at 05:30
3

Try this code

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
    }
}
Sunil Kumar B M
  • 2,735
  • 1
  • 24
  • 31
  • "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[Painting in AWT and Swing: The Paint Methods](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks). – trashgod Feb 08 '12 at 05:32
  • 2
    @trashgod Generally good advice, though `JApplet` does not have a `paintComponent(Graphics)` method. (Which is why the first part of my advice re. Swing custom painting is "Don't do it in a top-level container" immediately followed by "override `paintComponent(Graphics)`".) ;) – Andrew Thompson Feb 08 '12 at 05:37
1
setBackground (Color.cyan);

It is working properly in my IDE. I also changed the color of background.It works nice and properly. No need to change the code.Make sure when you are creating the class.

SilentBomb
  • 168
  • 2
  • 11
1

The paint(Graphics) method is used only to paint the parameter (in your case page). The application applet background color has already been processed by this stage.

That is why you can fix the problem by setting it in the constructor:

public Snowman()
{
    this.setBackground(Color.cyan);
}
J Barclay
  • 481
  • 2
  • 7
  • 18
1

I think you need to use, getContentPane().setBackground()

Navneeth G
  • 7,255
  • 1
  • 15
  • 18