I have partially completed Java code, but I don\'t know how to add the final por
ID: 664142 • Letter: I
Question
I have partially completed Java code, but I don't know how to add the final portions. The goal was to:
1.Draw a snowman in a Java applet
2. Add side-scrolling text which says "I'm melting!!" moving from right to left on the top
3. Add snowflakes flashing around the snowman
I have built the snowman here:
import javax.swing.JApplet;
import java.awt.*;
import java.applet.*;
import java.io.*;
public class Snowman extends Applet
{
//-----------------------------------------------------------------
// Draws a snowman.
//-----------------------------------------------------------------
public void paint (Graphics page)
{
final int MID = 150;
final int TOP = 50;
setBackground (Color.cyan);
page.setColor (Color.green); //changed ground to green so my name was readable
page.fillRect (0, 175, 300, 50); // ground
page.setColor (Color.yellow);
page.fillOval (+260, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID, TOP, 40, 40); // head shifted right 20 pixels
page.fillOval (MID-15, TOP+35, 70, 50); // upper torso shifted right
page.setColor (Color.red);
page.fillOval (MID+12, TOP+45, 5, 5); //Red Buttons now on upper torso
page.fillOval (MID+28, TOP+45, 5, 5);
page.setColor (Color.white);
page.fillOval (MID-30, TOP+80, 100, 60); // lower torso shifted right
page.setColor (Color.black);
page.fillOval (MID+10, TOP+10, 5, 5); // left eye shifted right
page.fillOval (MID+25, TOP+10, 5, 5); // right eye shifted right
page.drawArc (MID+10, TOP+20, 20, 10, 15, 160); // smile is now a frown
page.drawLine (MID-5, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+45, TOP+60, MID+75, TOP+60); // right arm
page.drawLine (MID, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect (MID+5, TOP-20, 30, 25); // top of hat
page.drawString ("Name goes here", 10, 220); //changed ground to green
//so it was readable
}
}
I also have a separate program for the side-scrolling text which works alone:
import java.awt.*;
import java.applet.*;
import java.io.*;
public class animation extends Applet implements Runnable
{
Thread runner;
int Xpos = 200;
public void start()
{
if (runner ==null)
{
runner = new Thread(this);
runner.start();
}
}
public void stop()
{
if (runner != null)
{
runner = null;
}
}
public void run()
{
while(true)
{
repaint();
try
{
Thread.sleep(10);
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public void paint(Graphics g)
{
g.drawString("Hello Java", Xpos, 30);
Xpos--;
if(Xpos <-50)
Xpos = 500;
}
}
Is there a way to combine these? Or to add in the snowflakes?
Explanation / Answer
import javax.swing.*; import java.awt.event.*; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; public class SnowMan extends Applet implements ActionListener { Button button2Wave = new Button ("Wave"); int xStart = 198; int yEnd = 169; int xEndingsStart = 150; int yStartEndings = 125; int ctr; int ctr2nd; public void init () { setLayout(new BorderLayout()); add(button2Wave, BorderLayout.SOUTH); button2Wave.addActionListener(this); } public void paint(Graphics g) { g.setColor(Color.blue); g.fillRect(0, 250, 500,150 ); // ground g.setColor (Color.yellow); g.fillOval (-40, -40, 80, 80); // sun g.setColor(Color.white); g.fillRoundRect(219,74,75,75,75,75);//head g.fillRoundRect(175,148,150,150,150,150); //body g.fillRoundRect(63,10,130,60,80,80);//dialog oval g.setColor(Color.black); g.fillRoundRect(248,180,10,10,10,10);//body buttons g.fillRoundRect(248,210,10,10,10,10);//body buttons g.fillRoundRect(248,240,10,10,10,10);//body buttons g.fillRoundRect(248,270,10,10,10,10);//body buttons g.drawString("Hi! I'm Olaf ", 97,35);//body buttons g.drawString("and I like Warm Hugs ", 70,45); g.fillRoundRect(240,100,10,10,10,10);//eyes g.fillRoundRect(264,100,10,10,10,10);//eyes g.drawArc(232,65,50,70,245,50);//smile g.drawLine (230, 75, 280, 75); // brim of hat g.fillRect(235,50, 40, 25); // top of hat g.drawLine(xStart,yEnd, xEndingsStart, yStartEndings);//right arm g.drawLine(300,169, 350, 125);//left arm setBackground( Color.cyan); if (ctr==1) { g.clearRect(98,70,100,100); ctr +=1; g.clearRect(300,70,100,100); if (ctr==2) { g.drawLine(xStart,yEnd, 131, 169); g.drawLine(300,169,369,169); ctr2nd+=1; if(ctr2nd==4) { ctr2nd-=3; } } } //wave down if(ctr2nd==2) { g.clearRect(98,70,100,100); ctr2nd +=1; g.clearRect(300,70,100,100); if(ctr2nd==3) { g.drawLine(xStart,yEnd, xEndingsStart, yStartEndings); g.drawLine(300,169, 350, 125); ctr-=2; } } //wave up } public void actionPerformed (ActionEvent e) { Object source = e.getSource(); if (source==button2Wave) { System.out.println ("Counter Value: " + ctr); if (ctr==0) { this.repaint(98,70,100,100); this.repaint(300,70,100,100); ctr +=1; } if(ctr2nd==1) { this.repaint(98,70,100,100); this.repaint(300,70,100,100); ctr2nd+=1; } } } }