import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import
ID: 3911900 • Letter: I
Question
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;/*A component that draws an alien face*/public class FaceComponent extends JComponent{public void paintComponent(Graphics g){// Recover Graphics2DGraphics2D g2 = (Graphics2D) g;// Draw the headEllipse2D.Double head = new Ellipse2D.Double(5, 10, 100, 150);g2.draw(head);// Draw the eyesg2.setColor(Color.GREEN);Rectangle eye = new Rectangle(25, 70, 15, 15);g2.fill(eye);eye.translate(50, 0);g2.fill(eye);// Draw the mouthLine2D.Double mouth = new Line2D.Double(30, 110, 80, 110);g2.setColor(Color.RED);g2.draw(mouth); Modify the FaceComponent class to create your own version of the alien face. Your modified code should make use of the following classes: Rectangle, Ellipse2D.Double, Line2D.Double. In addition, you should also use the following methods from the Graphics2D class: draw, fill, setColor. When you modify the existing FaceComponent class, please add at least two new objects, i.e., rectangle, ellipse, line, or text. This is a meant to be a fun assignment to help you get to know classes, objects, Graphics2d. We will be making use of Graphics2D in the future, so get some experience with it! Please submit your FaceComponent class only. I already have the FaceViewer.
Explanation / Answer
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
/*
A Component that draws as alien face.
*/
public class FaceComponent extends JComponent
{
public void painComponent(Graphics g)
{
//Recover Graphics2D
Graphics2D g2 = (Graphics2D) g;
// Draw the head
Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 100, 150);
g2.draw(head);
// Draw the eyes
g2.setColor(Color.GREEN);
Rectangle eye = new Rectangle(25, 70, 15, 15);
g2.fill(eye);
eye.translate(50, 0);
g2.fill(eye);
// Draw the mouth
Line2D.Double mouth = new Line2D.Double(30, 110, 80, 110);
g2.setColor(Color.RED);
g2.draw(mouth);
// Draw the greeting(Text)
g2.setColor(Color.Blue);
g2.drawString("hello, World", 5, 175);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.