Christmas Project Snowman – Java Swing

A fairly easy christmas project where I had to create a Swing project which was christmas related. The user can choose which weather they want they can either have it snowing or raining.

What it looked like

This slideshow requires JavaScript.

Code


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * User: Zara
 * Date: 28/01/2013
 * Time: 20:09
 * A program which enables the user to change the weather to either snow or rain.
 */
public class ChristmasPage extends JFrame implements ActionListener

{
    private boolean weather = true; // will determine the weather

    //Creating the buttons
    private JButton snowButton = new JButton("It's Snowing!");
    private JButton rainButton = new JButton("It's Raining!");


    public ChristmasPage()
    {
        // set the title
        setTitle("Christmas Page!");

        // choose a Flow Layout policy
        setLayout(new FlowLayout());

        //Adding buttons
        add(snowButton);
        add(rainButton);

        // enable the buttons to listen for a mouse-click
        snowButton.addActionListener(this);
        rainButton.addActionListener(this);

        // configure the frame
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(390, 280);

        setLocation(300,300);

        setVisible(true);

    }//constructor

    public void paint(Graphics g)

    {
        // call the paint method of the superclass, JFrame
        super.paint(g);

        //Setting the size
        final int MID = 150;
        final int TOP = 50;

        //Setting background
        setBackground (Color.gray);

        //Setting the ground
        g.setColor(Color.green);
        g.fillRect(0, 230, 390, 55);    //location //length //thickness

        //Setting the sun
        g.setColor(Color.yellow);
        g.fillOval(-40, -40, 100, 100);

        //Setting the Head etc.
        g.setColor(Color.white);

        //Head of snowman
        g.fillOval(MID - 20, TOP +42, 100, 60);

        //Middle of snowman
        g.fillOval (MID -35, TOP +80, 140, 70);

        //Bottom of snowman
        g.fillOval(MID - 50, TOP +125, 180, 80);

        //setting the color to black for the eyes, buttons, etc.
        g.setColor(Color.BLACK);

        //Setting the buttons on the snowman
        g.fillOval( MID +30, TOP + 80,5,5);
        g.fillOval( MID +30, TOP + 90,5,5);
        g.fillOval( MID +30, TOP + 100,5,5);
        g.fillOval( MID +30, TOP + 110,5,5);
        g.fillOval( MID +30, TOP + 120,5,5);
        g.fillOval( MID +30, TOP + 130,5,5);
        g.fillOval( MID +30, TOP + 140,5,5);
        g.fillOval( MID +30, TOP + 150,5,5);
        g.fillOval( MID +30, TOP + 160,5,5);
        g.fillOval( MID +30, TOP + 170,5,5);

        //Snowman left eye
        g.fillOval(MID + 10, TOP + 50, 5, 5);

        //Snowman right eye
        g.fillOval(MID + 42, TOP + 50, 5, 5);

        //Snowmans smile
        g.drawArc(MID + 10, TOP + 60, 40, 10, 195, 160);

        //Snowman left arm
        g.drawLine(MID - 25, TOP + 100, MID - 50, TOP + 40);

        //snowman right arm
        g.drawLine(MID + 100, TOP + 100, MID + 100, TOP + 60);

        //Snowman top of hat
        g.fillRect(MID + 15, TOP +20, 35, 25);

        if(weather == true)
        {
            //snow flakes
            g.setColor(Color.white);
            g.fillOval(MID +100, TOP, 5 ,10);
            g.fillOval(MID +200, TOP, 5 ,10);
            g.fillOval(MID +300, TOP, 5 ,10);
            g.fillOval(MID +200, TOP, 10 ,20);
            g.fillOval(MID +300, TOP, 50 ,20);
            g.fillOval(MID -100, TOP, 5 ,10);
            g.fillOval(MID -100, TOP +5, 5 ,10);
            g.fillOval(MID -100, TOP +10, 5 ,10);
            g.fillOval(MID -115, TOP +35, 5 ,10);
            g.fillOval(MID -100, TOP + 40, 5 ,10);
            g.fillOval(MID -100, TOP +50, 5 ,10);
            g.fillOval(MID -55, TOP +80, 5 ,10);
            g.fillOval(MID -100, TOP +90, 5 ,10);
            g.fillOval(MID -100, TOP +110, 5 ,10);
            g.fillOval(MID -100, TOP -110, 5 ,10);


        }//if

        else
        {
            //Rain
            g.setColor(Color.BLUE);
            g.fillOval(MID +100, TOP, 5 ,10);
            g.fillOval(MID +200, TOP, 5 ,10);
            g.fillOval(MID +300, TOP, 5 ,10);
            g.fillOval(MID +200, TOP, 10 ,20);
            g.fillOval(MID +300, TOP, 50 ,20);
            g.fillOval(MID -100, TOP, 5 ,10);
            g.fillOval(MID -100, TOP +5, 5 ,10);
            g.fillOval(MID -100, TOP +10, 5 ,10);
            g.fillOval(MID -115, TOP +35, 5 ,10);
            g.fillOval(MID -100, TOP + 40, 5 ,10);
            g.fillOval(MID -100, TOP +50, 5 ,10);
            g.fillOval(MID -55, TOP +80, 5 ,10);
            g.fillOval(MID -100, TOP +90, 5 ,10);
            g.fillOval(MID -100, TOP +110, 5 ,10);
        }//else

    }//paint


    public void actionPerformed (ActionEvent event)

    {
        if(event.getSource() == snowButton)
        {
            weather = true;
            repaint();
        }//if

        else if(event.getSource() == rainButton)
             {
                weather = false;
                repaint();
             }//if

    }//actionevent

    public static void main(String [] args) throws Exception

    {
        ChristmasPage GUI = new ChristmasPage();


    }//main

}//class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s