In: Computer Science
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
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); } }