Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I make this image of the smiley faces change colors randomly? import java.awt.*; import javax.swing.*; //smiley class public class Smiley extends JPanel {

How can I make this image of the smiley faces change colors randomly?

import java.awt.*; import javax.swing.*; //smiley class public class Smiley extends JPanel { //declare required variable int sad = 0, happy = 180; //set the width of window public static final int w = 400; //set the height of window public static final int h = 400; //assign face diameter public static final int fd = 180; //initializes the face of x position public static final int xp = 10; //initializes the face of y position public static final int yp = 10; //initializes the width of eye public static final int we = 20; //initializes the height of eye public static final int he = 20; //set the Right eye's position on the x and y public static final int xre = xp + 40; public static final int yre = yp + 60; //set the left eye's position on the x and y public static final int xle = xp + 120; public static final int yle = yp + 60; //initialzes the width and height of the mouth public static final int mw = 80; public static final int mh = 50; //initializes the x and y position of mouth on the face public static final int xm = xp + 50; public static final int ym = yp + 90; //define the class variables of type Color public Color profile, nface, fsmile, eye; //smiley constructor with four parameters public Smiley(Color profile, Color nface, Color fsmile, Color eye) { //set the layout setLayout(new BorderLayout()); //initialize the parameters this.profile = profile; this.nface = nface; this.fsmile = fsmile; this.eye = eye; //call paint() method repaint(); } public void paintComponent(Graphics gr) { super.paintComponent(gr); //set the color of profile of the face gr.setColor(profile); //draw the face gr.fillOval(xp, yp,fd+7, fd+7); //fill the color of face gr.setColor(nface); gr.fillOval(xp+3, yp+3,fd, fd); //fill the eye color gr.setColor(eye); //for draw right eye gr.fillOval(xre, yre,we, he); gr.setColor(eye); //for draw left eye gr.fillOval(xle, yle,we, he); //for smile color gr.setColor(fsmile); gr.drawArc(xm, ym,mw, mh, happy, 180); } }

SmileyGrid.java:

import java.awt.*; import java.awt.event.*; import javax.swing.*; //class for SmileyGrid public class SmileyGrid extends JFrame implements ActionListener { //object for SmileGrid class static SmileyGrid gs = new SmileyGrid(); //main function public static void main(String[] args) { gs.setSize(400, 400); //set grid layout for 3 rows and 3 colums gs.setLayout(new GridLayout(3,3)); //call createFace function() gs.createFace(); gs.setVisible(true); } //constructor public SmileyGrid() { } //function definition for createFace() private void createFace() { for(int a=0;a<9;a++) { //create object for Smiley class Smiley sp = new Smiley(Color.BLUE, Color.YELLOW,Color.RED, Color.black); //add gs.add(sp); } setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent ae) { } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions