Explanation of Solution
Drawings in java:
- When a user has to develop a drawing that contains multiple shapes, or parts that are made of multiple shapes, it is advised to create separate classes for each purpose or part.
- In the given scenario,
- The “Car” class contains the code that are used to draw a single car.
- The next class “CarComponent” is contains the code used to display the drawing.
- The last class “CarViewer” displays a frame that contains the “CarComponent”.
- Given program:
//import the required packages
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
//define a class named "car"
public class Car
{
//declare the required variables
private int xLeft;
private int yTop;
//define a method to assign the values
public Car(int x, int y)
{
//define the variables to store the values of x-
coordinate and y-coordinate
xLeft = x;
yTop = y;
}
//define the method to draw
public void draw(Graphics2D g2)
{
//create required objects
Rectangle body = new Rectangle(xLeft, yTop + 10,
60, 10);
Ellipse2D.Double frontTire = new
Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);
Ellipse2D.Double rearTire = new
Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);
// The bottom of the front windshield
Point2D.Double r1 = new Point2D.Double(xLeft +
10, yTop + 10);
// The front of the roof
Point2D...
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Big Java, Binder Ready Version: Early Objects
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education