JAVA Write a class that represents a digital snowman. Your class should follow t
ID: 3862560 • Letter: J
Question
JAVA
Write a class that represents a digital snowman. Your class should follow these guidelines: Store the following private class variables a. bodyColor of type Color. b. int x, int y for the upper left comer. c. Graphics g. Create two different constructor methods. a. A (int x, int y, Graphics myG) parameter constructor that makes the snowman a light gray color by default and makes x and y the upper left comer and saves the graphics panel to draw on. b. A (int x, int y, Graphics myG, Color c) parameterized constructor that requires a color as an input and then saves the color given in addition to the x and y and the graphics panel. c. Each constructor should set/save the appropriate bodyColor to the necessary Color Create a public drawSnowMan() method a. Instantiate three Ovals that draw the body of the snowman. b. Make sure to shift/translate the ovals based upon the proper x and y. c. Your Ovals should be filled with whatever color bodyColor is set to. d. Create several Objects to draw eyes, nose and mouth on the snowman e. Create Lines to draw stick arms for the snowman. Create a simple main program (main()) that displays several snowmen of different colors. a. Create an array of 3 snowmen. b. Snowman 0 should use the default constructor and hence should be gray. c. Snowman 1 and 2 should be set to a non-gray color of your choice. d. After making the snowmen, make sure to draw them to the screen. Add a changes no ManColor() method to the Snowman. a. This should change the snowman to a random color. b. Then it redraws the snowman Create a loop in the main program that will change the color of one of the snowmen to a random color 10 times. Sample output:Explanation / Answer
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class SnowMan extends JPanel{
private Color color;
private int x;
private int y;
private Graphics g;
public SnowMan(int x,int y, Graphics myG){
this.setX(x);
this.setY(y);
this.setG(myG);
this.setBounds(x, y, 200, 400);
//this.setBackground(Color.gray);
}
public SnowMan(int x,int y, Graphics myG, Color c){
this(x, y, myG);
this.setColor(c);
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Graphics getG() {
return g;
}
public void setG(Graphics g) {
this.g = g;
}
@Override
public void paintComponent(Graphics gr) {
super.paintComponent(gr);
gr.setColor(Color.gray);
Graphics2D g2 = (Graphics2D) gr;
g2.setStroke(new BasicStroke(1));
gr.fillArc(70, 85, 50, 50, 0, 360);
gr.fillArc(50, 130, 90, 80, 0, 360);
gr.setColor(Color.black);
gr.drawArc(50, 130, 90, 80, 0, 360);
gr.setColor(Color.gray);
gr.fillArc(40, 200, 110, 100, 0, 360);
gr.setColor(Color.black);
gr.drawArc(40, 200, 110, 100, 0, 360);
g2.setStroke(new BasicStroke(3));
gr.drawLine(40, 90, 70, 150);
gr.drawLine(160, 90, 130, 150);
gr.setColor(Color.white);
gr.fillOval(80, 100, 10, 10);
gr.fillOval(100, 100, 10, 10);
gr.fillArc(85, 115, 20, 10,0,-180);
}
public void DrawSnowMan(){
//paintComponent(g);
Graphics gr = getGraphics();
super.paintComponent(gr);
gr.setColor(color);
Graphics2D g2 = (Graphics2D) gr;
g2.setStroke(new BasicStroke(1));
gr.fillArc(70, 85, 50, 50, 0, 360);
gr.fillArc(50, 130, 90, 80, 0, 360);
gr.setColor(Color.black);
gr.drawArc(50, 130, 90, 80, 0, 360);
gr.setColor(color);
gr.fillArc(40, 200, 110, 100, 0, 360);
gr.setColor(Color.black);
gr.drawArc(40, 200, 110, 100, 0, 360);
g2.setStroke(new BasicStroke(3));
gr.drawLine(40, 90, 70, 150);
gr.drawLine(160, 90, 130, 150);
gr.setColor(Color.white);
gr.fillOval(80, 100, 10, 10);
gr.fillOval(100, 100, 10, 10);
gr.fillArc(85, 115, 20, 10,0,-180);
}
public void ChangeSnowManColor(){
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
//System.out.println("r"+r+" g"+g+" b"+b);
this.color=Color.getHSBColor(r, g, b);
}
public static void main(String[] argv){
JFrame window = new JFrame();
window.setSize(700, 600);
window.setVisible(true);
window.setLayout(null);
window.setBackground(Color.white);
SnowMan snowMan0 = new SnowMan(10, 50, null,Color.WHITE);
SnowMan snowMan1 = new SnowMan(220, 50, null,Color.WHITE);
SnowMan snowMan2 = new SnowMan(440, 50, null,Color.WHITE);
window.add(snowMan0);
window.add(snowMan1);
window.add(snowMan2);
for(int i= 0;i<10;i++){
snowMan1.ChangeSnowManColor();
snowMan2.ChangeSnowManColor();
snowMan1.DrawSnowMan();
snowMan2.DrawSnowMan();
try{
Thread.sleep(1000);
}catch(Exception e){
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.