I need help with my java code i am having a hard time compiling it // Cristian B
ID: 3703423 • Letter: I
Question
I need help with my java code i am having a hard time compiling it
// Cristian Benitez
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
Face class
public class FaceDraw{
int width;
int height;
int x;
int y;
String smileStatus; //default constructor
public FaceDraw(){
}
// Getters and Setters for width,height,x,y
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;
}
// to my string method
public String toString(){
return super.toString();
}
}
FacePanel class
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) {
//Super paint component
graph.setColor(Color.BLUE);
// 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);
}
}
}
FaceFrame class
public class FaceFrame extends JFrame{
private static final long serialVersionUID = 1L;
public static ArrayList<Face> facelist = new ArrayList<Face>();
public FaceFrame(String title) {
new JFrame(title);
setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(1500, 700);
add(new FacePanel(facelist));
}
public static void addToList(Face face)
{
facelist.add(face);
}
private 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
}
}
}
}
Explanation / Answer
/*
copy this code into a new file and save as FaceFrame.java
Compile and execute it
*/
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.*;
//FaceFrame class
/*
You have to import import javax.swing.*; in FaceFrame.java
And You have implemented FaceDraw class and called Face()
and main function should be public else it gives main method not found
I just changed FaceDraw with Face
and it have compiled correctly
You have posted that it is not compiling now it is compiling correctly
*/
//Face class
class Face
{
int width;
int height;
int x;
int y;
String smileStatus; //default constructor
public Face()
{
}
// Getters and Setters for width,height,x,y
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;
}
// to my string method
public String toString()
{
return super.toString();
}
}
//FacePanel class
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)
{
//Super paint component
graph.setColor(Color.BLUE);
// 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);
}
}
}
}
public class FaceFrame extends JFrame
{
private static final long serialVersionUID = 1L;
public static ArrayList<Face> facelist = new ArrayList<Face>();
public FaceFrame(String title)
{
new JFrame(title);
setBackground(Color.WHITE);
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
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.