In: Computer Science
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class Mickey extends Canvas {
public static void main(String[] args) {
JFrame frame = new JFrame("Mickey Mouse");
Canvas canvas = new Mickey();
canvas.setSize(400, 400);
canvas.setBackground(Color.white);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
}
public void paint(Graphics g) {
Rectangle bb = new Rectangle(100, 100, 200, 200);
mickey(g, bb);
}
public void boxOval(Graphics g, Rectangle bb) {
g.fillOval(bb.x, bb.y, bb.width, bb.height);
g.drawArc(130, 180, 50, 20, 180, 180);
}
public void mickey(Graphics g, Rectangle bb) {
boxOval(g, bb);
int dx = bb.width / 2;
int dy = bb.height / 2;
Rectangle half = new Rectangle(bb.x, bb.y, dx, dy);
half.translate(-dx / 2, -dy / 2);
boxOval(g, half);
half.translate(dx * 2, 0);
boxOval(g, half);
}
}
As per the problem statement I have solve the problem. Please let
me know if you have any doubts or you want me to modify the answer.
And if you find this answer useful then don't forget to rate my
answer as thumps up. Thank you! :)
//Mickey.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
//Class Mickey
public class Mickey extends JComponent {
static final int WIDTH = 750;
static final int HEIGHT = 750;
String title = "Mickey Mouse";
Color faceskin = new Color(239, 195, 129);
//constructor
public Mickey() {
//frame
JFrame frame = new
JFrame(title);
// sets the size of
my game
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
// adds the game to the
window
frame.add(this);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
//paint component
@Override
public void paintComponent(Graphics graphics)
{
graphics.clearRect(0, 0,
WIDTH, HEIGHT);
graphics.setColor(Color.BLACK);
graphics.fillOval(125,
200, 500, 450);
//ears
graphics.fillOval(45,
30, 275, 275);
graphics.fillOval(400,
30, 275, 275);
//face
graphics.setColor(faceskin);
graphics.fillOval(125,
450, 500, 200);
graphics.setColor(Color.BLACK);
graphics.drawOval(125,
450, 500, 200);
graphics.setColor(faceskin);
graphics.fillOval(200,
250, 200, 300);
graphics.fillOval(350,
250, 200, 300);
graphics.fillOval(275,
550, 200, 150);
graphics.setColor(Color.BLACK);
graphics.drawArc(275,
550, 200, 150, 180, 180);
//eyes
graphics.setColor(Color.WHITE);
graphics.fillOval(250,
310, 100, 160);
graphics.fillOval(400,
310, 100, 160);
graphics.setColor(Color.BLACK);
graphics.drawOval(250,
310, 100, 160);
graphics.drawOval(400,
310, 100, 160);
//pupils
graphics.setColor(Color.BLACK);
graphics.setColor(Color.WHITE);
//nose
graphics.setColor(Color.BLACK);
graphics.setColor(Color.WHITE);
//mouth
graphics.setColor(Color.BLACK);
graphics.fillArc(300,
525, 150, 150, 180, 180);
graphics.setColor(Color.PINK);
graphics.fillArc(325,
560, 100, 100, 180, 180);
graphics.setColor(Color.RED);
graphics.fillOval(345,
630, 60, 30);
}
public static void main(String[] args) {
//instatiate the
class
Mickey mickey = new
Mickey();
}
}