Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

I'm supposed to make a SmileyGrid class that follows these requirements: This class needs to extend JFrame Use a GridLayout to divide the frame into

I'm supposed to make a SmileyGrid class that follows these requirements:

This class needs to extend JFrame

Use a GridLayout to divide the frame into a 3 by 3 grid

Use a loop to create an instance of the Smiley class for each square in the grid, using random colors for each instance.

I have this SmileyGrid setup but i need help organizing it so it follows the requirements above:

public class SmileyGrid extends JFrame implements ActionListener { public static void main(String[] args) { SmileyGrid myGrid = new SmileyGrid(); myGrid.setSize(800, 800); myGrid.createGUI(); myGrid.setVisible(true); }

public SmileyGrid() {

}

private void createGUI() { setDefaultCloseOperation(EXIT_ON_CLOSE);

} public void actionPerformed(ActionEvent ae) {

} }

Here is my Smiley class code:

import java.awt.Color; import java.awt.Graphics; import java.awt.Polygon; import javax.swing.JFrame; import javax.swing.JPanel;

public class Smiley extends JPanel { public void paintComponent(Graphics gObject) { super.paintComponent(gObject); int heightObject = 50; int widthObject = 50; int faceTopObject = heightObject / 10; int faceLeftObject = widthObject / 10; int faceHeightObject = heightObject - heightObject / 5; int faceWidthObject = widthObject - widthObject / 5; gObject.setColor(Color.yellow); gObject.fillOval(faceLeftObject, faceTopObject, faceWidthObject, faceHeightObject); //Set new color. gObject.setColor(Color.blue); gObject.drawOval(faceLeftObject, faceTopObject, faceWidthObject, faceHeightObject); //Draw non-filled oval. In other words, draw boundary. gObject.setColor(Color.red); gObject.fillOval(faceLeftObject + (int) (widthObject * 0.2), faceTopObject + (int) (heightObject * 0.2), widthObject / 10, heightObject / 10); gObject.fillOval(faceLeftObject + (int) (widthObject * 0.5), faceTopObject + (int) (heightObject * 0.2), widthObject / 10, heightObject / 10); gObject.setColor(Color.green); gObject.fillArc(faceLeftObject + (int) (widthObject * 0.25), faceTopObject + (int) (heightObject * 0.5), (int) (widthObject * 0.3), (int) (heightObject * 0.2), 0, -180);

} public static void main(String[] args) {

JFrame windowObject = new JFrame("Smiley world"); Smiley panelObject = new Smiley(); windowObject.add(panelObject); windowObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); windowObject.getContentPane().setBackground(Color.CYAN); windowObject.setSize(400, 400); windowObject.setVisible(true); } }

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_2

Step: 3

blur-text-image_3

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

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students explore these related Databases questions

Question

3. How effective is the team?

Answered: 3 weeks ago