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
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started