In this assignment, you will need to create an application in Java that draws ra
ID: 3700004 • Letter: I
Question
In this assignment, you will need to create an application in Java that draws random faces on a window. The application will draw 3 to 10 faces each time it is run. The faces will have a random width and height that are set to reasonable and visually appealing ranges. The application window size should be initially set to a reasonable size and all faces should draw entirely within the window. You do not have to worry about how to handle the resizing of the application window. Assume that it will remain the same size. Each face should have two eyes and a mouth. The mouth should be randomly smiling, frowning, or in-between.
Explanation / Answer
Face Model Class
package Graphics;
public class Face {
int width;
int height;
int x,y;
String smileStatus;
//default Constructor
public Face()
{
}
//Getters and setters for Width, height, x, y and smile status
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
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 String getSmileStatus() {
return smileStatus;
}
public void setSmileStatus(String smileStatus) {
this.smileStatus = smileStatus;
}
//toString method
public String toString()
{
return super.toString();
}
}
FacePanel Class
package Graphics;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
public class FacePanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
ArrayList<Face> faces;
public FacePanel(ArrayList<Face> faces) {
this.faces = faces;
}
@Override
protected void paintComponent(Graphics graph) {
// TODO Auto-generated method stub
//super.paintComponent(graph);
graph.setColor(Color.RED);
// For each Dimension in the Arraylist
for(int i=0;i<faces.size();i++)
{
String smileStatus = faces.get(i).getSmileStatus();
if(smileStatus != null)
{
if(smileStatus == "Smile")
graph.drawArc(faces.get(i).getX(), faces.get(i).getY(), faces.get(i).getWidth(), faces.get(i).getHeight(), 0, -180);
else
graph.drawArc(faces.get(i).getX(), faces.get(i).getY(), faces.get(i).getWidth(), faces.get(i).getHeight(), 0, 180);
System.out.println(smileStatus);
}
else
graph.drawOval(faces.get(i).getX(), faces.get(i).getY(), faces.get(i).getWidth(), faces.get(i).getHeight());
}
}
}
FaceFrame class
package Graphics;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JFrame;
public class FaceFrame extends JFrame{
/**
* Class for creating
*/
private static final long serialVersionUID = 1L;
public static ArrayList<Face> facelist = new ArrayList<Face>();
public FaceFrame(String title) {
new JFrame(title);
setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(1500, 700);
add(new FacePanel(facelist));
}
public static void addToList(Face face)
{
facelist.add(face);
}
public static void main(String args[])
{
//First Face
Face face1 = new Face();
//Lip dimensions
face1.setX(180);
face1.setY(350);
face1.setWidth(150);
face1.setHeight(20);
face1.setSmileStatus("Smile");
// face dimensions
Face face2 = new Face();
face2.setX(100);
face2.setY(100);
face2.setWidth(300);
face2.setHeight(400);
//Left Eye
Face face3 = new Face();
face3.setX(195);
face3.setY(210);
face3.setWidth(20);
face3.setHeight(20);
//Right eye
Face face4 = new Face();
face4.setX(295);
face4.setY(210);
face4.setWidth(20);
face4.setHeight(20);
//Second Face
Face face5 = new Face();
//Lip dimensions
face5.setX(580);
face5.setY(350);
face5.setWidth(150);
face5.setHeight(40);
face5.setSmileStatus("Frown");
// face dimensions
Face face6 = new Face();
face6.setX(500);
face6.setY(100);
face6.setWidth(300);
face6.setHeight(400);
//Left Eye
Face face7 = new Face();
face7.setX(595);
face7.setY(210);
face7.setWidth(20);
face7.setHeight(20);
//Right eye
Face face8 = new Face();
face8.setX(695);
face8.setY(210);
face8.setWidth(20);
face8.setHeight(20);
//Third Face
Face face9 = new Face();
//Lip dimensions
face9.setX(980);
face9.setY(350);
face9.setWidth(150);
face9.setHeight(0);
face9.setSmileStatus("Neutral");
// face dimensions
Face face10 = new Face();
face10.setX(900);
face10.setY(100);
face10.setWidth(300);
face10.setHeight(400);
//Left Eye
Face face11 = new Face();
face11.setX(995);
face11.setY(210);
face11.setWidth(20);
face11.setHeight(20);
//Right eye
Face face12 = new Face();
face12.setX(1095);
face12.setY(210);
face12.setWidth(20);
face12.setHeight(20);
//Adding all the face objects to list
addToList(face1);
addToList(face2);
addToList(face3);
addToList(face4);
addToList(face5);
addToList(face6);
addToList(face7);
addToList(face8);
addToList(face9);
addToList(face10);
addToList(face11);
addToList(face12);
new FaceFrame("Draw Face Example");
//drawing face
}
}