Question

In: Computer Science

Create a project called FaceViewer with the following data: FaceComponent class will have: Make a new...

Create a project called FaceViewer with the following data:

FaceComponent class will have:

Make a new class called FaceComponent that extends JComponent

Create a paintComponent method that has a parameter of Graphics type

Cast the Graphics variable to Graphics2D

Create a circle Face that contains the following:

Blue Circle right eye. Green Square left eye. Left & right eye should be same distance from sides of face. Ellipse red nose with the center of the nose being the center of the face. Black line Mouth equal distance from both sides of the face.

The Tester Class needs to:

Contain a main() method that:

Construct a JFrame object.

Set the size of the frame.

Set the default close operation to exit when clicked

Set the title of the frame to FaceViewer.

Construct a FaceComponent object.

Add the component to the JFrame.

Make sure frame is visible

Solutions

Expert Solution

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;

/*
 * This component will draw an "Alien" face
 */ 

public class FaceComponent extends JComponent 
{

//Create a constructor that will create a face and place it in a variable.

public void paintComponent(Graphics g)
{
    //Recover Graphics2D
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    //Construct the alien face

    //Draw the  head
    Ellipse2D.Double head = new Ellipse2D.Double (5, 80, 100, 150);
    g2.draw(head);

    //Draw the set of eyes
    g2.setColor(Color.GREEN);
    Rectangle eye = new Rectangle(25, 140, 15, 15);
    g2.fill(eye);
    eye.translate(50, 0);
    g2.fill(eye);

    //Draw the mouth
    Line2D.Double mouth = new Line2D.Double(30, 180, 80, 180);
    g2.setColor(Color.RED);
    g2.draw(mouth);

    //Draw the greeting
    g2.setColor(Color.BLUE);
    g2.drawString("Hello, World!", 20, 245);
}
}
class Face {

    private Point location;

    Face(Point location) {
        this.location = location;
    }

    /**
     * @param location the location to set
     */
    public void setLocation(Point location) {
        this.location = location;
    }

    public void draw(Graphics2D g) {
        /* save this to reset it after painting. */
        AffineTransform transform = g.getTransform();        

        AffineTransform move = AffineTransform.getTranslateInstance(
                location.getX(), location.getY());
        g.setTransform(move);

        //Construct the alien face
        //Draw the  head
        g.setColor(Color.DARK_GRAY); // explicitly set a color
        Ellipse2D.Double head = new Ellipse2D.Double(5, 80, 100, 150);
        g.fill(head);
        g.setColor(Color.LIGHT_GRAY);
        g.draw(head);

        //Draw the set of eyes
        g.setColor(Color.GREEN);
        Rectangle eye = new Rectangle(25, 140, 15, 15);
        g.fill(eye);
        eye.translate(50, 0);
        g.fill(eye);

        //Draw the mouth
        Line2D.Double mouth = new Line2D.Double(30, 180, 80, 180);
        g.setColor(Color.RED);
        g.draw(mouth);

        //Draw the greeting
        g.setColor(Color.BLUE);
        g.drawString("Hello, World!", 20, 245);

        // reset the transform to the original (so later painting is not moved)
        g.setTransform(transform);
    }
}
/*
 * This component will draw "Alien" faces
 */
public class FaceComponent extends JComponent {

    Face[] faces = new Face[3];

    FaceComponent() {
        Random r = new Random();
        for (int ii=0; ii<faces.length; ii++) {
            Point p = new Point(r.nextInt(200), r.nextInt(100));
            faces[ii] = new Face(p);
        }
    }

//Create a constructor that will create a face and place it in a variable.
    public void paintComponent(Graphics g) {
        //Recover Graphics2D
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        for (Face face : faces) {
            face.draw(g2);
        }
    }

    Dimension prefSize = new Dimension(180, 260);

    @Override
    public Dimension getPreferredSize() {
        return prefSize;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JOptionPane.showMessageDialog(null, new FaceComponent());
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Related Solutions

Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT