Course: MCA CSM-2201: Object Oriented Programming Using JAVA

Course: MCA CSM-2201: Object Oriented Programming Using JAVA

ALIGARH MUSLIM UNIVERSITY

Department of Computer Science

Course: MCA CSM-2201: Object Oriented Programming Using JAVA

Academic Session 2017-2018

Topic: Tutorial on Java Applets

Handout-15

Dr. Rafiqul Zaman Khan, Professor (Computer Science).

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Objective:

  • To learn more about Java Applets

Q1.

Who calls the paint method of an applet? When does the call to the paint method occur?

Ans.

The Java graphical user interface system calls the paint method. Just like you don't see anyone calling the main method of a program, you don't see anyone calling the paint method of an applet. However, unlike the main method, which can be called only once per program run, the paint method can be called repeatedly, to redraw the surface of the applet whenever it was obscured or corrupted.

Q2.

Why does the parameter of the paint method have type Graphics and not Graphics2D?

Ans.

The parameter of the paint method has type Graphics for historical reasons. Applets were invented before the 2D graphics library.

Q3.

How do you specify a text color in java?

Ans.

You specify a text color simply by calling the setColor method of the graphics context before calling the drawString method.

Q4.

Ans.

A logical font face is one of the font faces

Serif, SansSerif, Monospaced, Dialog, DialogInput

Q5.

You want to plot a bar chart showing the grade distribution of all students in your class (where A=4.0, F=0). What coordinate system would you choose to make the plotting as simple as possible?

Ans.

Choose x-units from 0 to 4 and y-units from 1 to n, where n is the number of students in your class.

Q6.

Let e be an ellipse. Write java code to plot the ellipse e and another ellipse of the same size that touches e.

Ans.

g2.draw(e);

Ellipse2D.Double e2 = new Ellipse2D.Double(e.getX() + e.getWidth(),

e.getY(), e.getWidth(), e.getHeight());

g2.draw(e2);

Q7.

Write java instructions to display the letters X and T in a graphics window, by plotting line segments.

Ans.

final double WIDTH = 20;

final double HEIGHT = 30;

// draw X

Line2D.Double up = new Line2D.Double(0, HEIGHT, WIDTH, 0);

Line2D.Double down = new Line2D.Double(0, 0, WIDTH, HEIGHT);

g2.draw(up);

g2.draw(down);

// draw T

Line2D.Double horiz = new Line2D.Double(0, 0, WIDTH, 0);

Line2D.Double vert = new Line2D.Double(WIDTH / 2, 0, WIDTH / 2, HEIGHT);

g2.draw(horiz);

g2.draw(vert);

Q8.

Write a java graphics program that draws your name in red, centered inside a blue rectangle.

Ans.

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Font;

import java.awt.font.FontRenderContext;

import java.awt.font.TextLayout;

import java.awt.geom.Rectangle2D;

public class ExP4_1 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

// select the font into the graphics context

Font f = new Font("Serif", Font.BOLD, 48);

g2.setFont(f);

String message = "Your Name";

// create a text layout to measure the string

FontRenderContext context = g2.getFontRenderContext();

TextLayout layout

= new TextLayout(message, f, context);

// measure the message width and height

float xMessageWidth = layout.getAdvance();

float yMessageHeight

= layout.getAscent() + layout.getDescent();

// center the message in the window

float xLeft = 0.5F * (getWidth() - xMessageWidth);

float yTop = 0.5F * (getHeight() - yMessageHeight);

float yBase = yTop + layout.getAscent();

g2.setColor(Color.blue);

final int EXTRA = 20;

Rectangle2D.Double r = new Rectangle2D.Double(xLeft - EXTRA,

yTop - EXTRA, xMessageWidth + 2 * EXTRA, yMessageHeight + 2 * EXTRA);

g2.fill(r);

g2.setColor(Color.red);

g2.drawString(message, xLeft, yBase);

}

}

Q9.

Write a graphics program that draws your name four times, in a large serif font, in plain, bold, italic, and bold italic. The names should be stacked on top of each other, with equal distance between them. Each of them should be centered horizontally, and the entire stack should be centered vertically.

Ans.

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Font;

import java.awt.font.FontRenderContext;

import java.awt.font.TextLayout;

public class ExP4_2 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

String message = "Your Name";

FontRenderContext context = g2.getFontRenderContext();

Font f1 = new Font("Serif", Font.PLAIN, 36);

Font f2 = new Font("Serif", Font.BOLD, 36);

Font f3 = new Font("Serif", Font.ITALIC, 36);

Font f4 = new Font("Serif", Font.BOLD + Font.ITALIC, 36);

// create a text layout to measure the string

TextLayout layout = new TextLayout(message, f1, context);

float xMessageWidth1 = layout.getAdvance();

float yAscent1 = layout.getAscent();

float yDescent1 = layout.getAscent();

float xMessageWidth = xMessageWidth1;

float yMessageHeight = yAscent1 + yDescent1;

layout = new TextLayout(message, f2, context);

float xMessageWidth2 = layout.getAdvance();

float yAscent2 = layout.getAscent();

float yDescent2 = layout.getAscent();

xMessageWidth = Math.max(xMessageWidth, xMessageWidth2);

yMessageHeight = yMessageHeight + yAscent2 + yDescent2;

layout = new TextLayout(message, f3, context);

float xMessageWidth3 = layout.getAdvance();

float yAscent3 = layout.getAscent();

float yDescent3 = layout.getAscent();

xMessageWidth = Math.max(xMessageWidth, xMessageWidth3);

yMessageHeight = yMessageHeight + yAscent3 + yDescent3;

layout = new TextLayout(message, f4, context);

float xMessageWidth4 = layout.getAdvance();

float yAscent4 = layout.getAscent();

float yDescent4 = layout.getAscent();

xMessageWidth = Math.max(xMessageWidth, xMessageWidth4);

yMessageHeight = yMessageHeight + yAscent4 + yDescent4;

// center the message in the window

float xLeft = 0.5F * (getWidth() - xMessageWidth);

float yBase = 0.5F * (getHeight() - yMessageHeight) + yAscent1;

g2.setFont(f1);

g2.drawString(message, xLeft + 0.5F * (xMessageWidth - xMessageWidth1), yBase);

yBase += yDescent1 + yAscent2;

g2.setFont(f2);

g2.drawString(message, xLeft + 0.5F * (xMessageWidth - xMessageWidth2), yBase);

yBase += yDescent2 + yAscent3;

g2.setFont(f3);

g2.drawString(message, xLeft + 0.5F * (xMessageWidth - xMessageWidth3), yBase);

yBase += yDescent3 + yAscent4;

g2.setFont(f4);

g2.drawString(message, xLeft + 0.5F * (xMessageWidth - xMessageWidth4), yBase);

}

}

Q11.

Write a graphics program that draws twelve strings, one each for the 12 standard colors besides Color.white, each in its own color.

Ans.

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Font;

import java.awt.font.FontRenderContext;

import java.awt.font.TextLayout;

public class ExP4_3 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

final float DISTANCE = 20;

float yBase = DISTANCE / 2;

g2.setColor(Color.black);

g2.drawString("black", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.blue);

g2.drawString("blue", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.cyan);

g2.drawString("cyan", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.gray);

g2.drawString("gray", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.darkGray);

g2.drawString("darkGray", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.lightGray);

g2.drawString("lightGray", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.green);

g2.drawString("green", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.magenta);

g2.drawString("magenta", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.orange);

g2.drawString("orange", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.pink);

g2.drawString("pink", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.red);

g2.drawString("red", 0, yBase);

yBase = yBase + DISTANCE;

g2.setColor(Color.yellow);

g2.drawString("yellow", 0, yBase);

yBase = yBase + DISTANCE;

}

}

Q12.

Write a graphics program that prompts the user to enter a radius. Draw a circle with that radius.

Ans.

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

import javax.swing.JOptionPane;

public class ExP4_4 extends Applet

{ public void init()

{ String input

= JOptionPane.showInputDialog("radius:");

radius = Integer.parseInt(input);

}

public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

// draw the circle

Ellipse2D.Double circle

= new Ellipse2D.Double(0, 0, 2 * radius, 2 * radius);

g2.draw(circle);

}

private double radius;

}

Q13.

Write a program that draws two solid circles, one in pink and one in purple. Use a standard color for one of them and a custom color for the other.

Ans.

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

import javax.swing.JOptionPane;

public class ExP4_5 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

double radius = 50;

Ellipse2D.Double circle

= new Ellipse2D.Double(0, 0, 2 * radius, 2 * radius);

g2.setColor(Color.pink);

g2.fill(circle);

Ellipse2D.Double circle2

= new Ellipse2D.Double(0, 2 * radius, 2 * radius, 2 * radius);

g2.setColor(new Color(0.7F, 0, 0.7F));

g2.fill(circle2);

}

}

Q14.

Draw a "bull's eye" –a set of concentric rings in alternating black and white colors.

Hint: Fill a black circle, then fill a smaller white circle on top, and so on.

Ans.

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

public class ExP4_6 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

double radius = 100;

double xCenter = 100;

double yCenter = 100;

Ellipse2D.Double circle

= new Ellipse2D.Double(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius);

g2.setColor(Color.black);

g2.fill(circle);

radius = radius - 20;

circle = new Ellipse2D.Double(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius);

g2.setColor(Color.white);

g2.fill(circle);

radius = radius - 20;

circle = new Ellipse2D.Double(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius);

g2.setColor(Color.black);

g2.fill(circle);

radius = radius - 20;

circle = new Ellipse2D.Double(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius);

g2.setColor(Color.white);

g2.fill(circle);

radius = radius - 20;

circle = new Ellipse2D.Double(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius);

g2.setColor(Color.black);

g2.fill(circle);

}

}

Sample output:

Q15.

Write a program that fills the applet window with a large ellipse, filled with your favorite color, that touches the window boundaries. The ellipse should resize itself when you resize the window.

Ans.

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

public class ExP4_7 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

Ellipse2D.Double ellipse = new Ellipse2D.Double(0, 0,

getWidth(), getHeight());

g2.setColor(Color.pink);

g2.fill(ellipse);

}

}

Q16.

Write a program that draws the picture of a house.

Ans.

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Line2D;

import java.awt.geom.Rectangle2D;

public class ExP4_8 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

final double WIDTH = 100;

final double HEIGHT = 150;

double x = 0;

double y = 200;

Rectangle2D.Double front = new Rectangle2D.Double(x, y - WIDTH, WIDTH, WIDTH);

Rectangle2D.Double door = new Rectangle2D.Double(x + WIDTH / 5, y - WIDTH / 2, WIDTH / 5, WIDTH / 2);

Rectangle2D.Double window = new Rectangle2D.Double(x + WIDTH * 3 / 5, y - WIDTH / 2, WIDTH / 5, WIDTH / 5);

Line2D.Double roofLeft = new Line2D.Double(x, y - WIDTH, x + WIDTH / 2, y - HEIGHT);

Line2D.Double roofRight = new Line2D.Double(x + WIDTH, y - WIDTH, x + WIDTH / 2, y - HEIGHT);

g2.draw(front);

g2.draw(door);

g2.draw(window);

g2.draw(roofLeft);

g2.draw(roofRight);

}

}

Sample Output:

Q17.

Write a java graphics program to plot the following face:

Ans.

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Line2D;

import java.awt.geom.Ellipse2D;

public class ExP4_9 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

final double FACE_RADIUS = 100;

final double EYE_RADIUS = 5;

Ellipse2D.Double face = new Ellipse2D.Double(0, 0, 2 * FACE_RADIUS, 2 * FACE_RADIUS);

double xEye1 = FACE_RADIUS / 2;

double yEye = FACE_RADIUS * 2 / 3;

Ellipse2D.Double eye1 = new Ellipse2D.Double(xEye1 - EYE_RADIUS, yEye - EYE_RADIUS, 2 * EYE_RADIUS, 2 * EYE_RADIUS);

double xEye2 = FACE_RADIUS * 3 / 2;

Ellipse2D.Double eye2 = new Ellipse2D.Double(xEye2 - EYE_RADIUS, yEye - EYE_RADIUS, 2 * EYE_RADIUS, 2 * EYE_RADIUS);

double yMouth = FACE_RADIUS * 4 / 3;

Line2D.Double mouth = new Line2D.Double(xEye1, yMouth, xEye2, yMouth);

g2.draw(face);

g2.draw(eye1);

g2.draw(eye2);

g2.draw(mouth);

}

}

Q18.

Write java graphics program to plot the string "HELLO", using just lines and circles. Do not call drawstring, and do not use System.out.

Ans.

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Line2D;

import java.awt.geom.Ellipse2D;

public class ExP4_10 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

final int WIDTH = 30;

final int HEIGHT = 50;

final int SPACING = 5;

double x = 0;

double y = 0;

Line2D.Double vert1 = new Line2D.Double(x, y, x, y + HEIGHT);

Line2D.Double vert2 = new Line2D.Double(x + WIDTH, y, x + WIDTH, y + HEIGHT);

Line2D.Double horiz = new Line2D.Double(x, y + HEIGHT / 2, x + WIDTH, y + HEIGHT / 2);

g2.draw(vert1);

g2.draw(vert2);

g2.draw(horiz);

x = x + WIDTH + SPACING;

Line2D.Double horiz1 = new Line2D.Double(x, y, x + WIDTH, y);

Line2D.Double horiz2 = new Line2D.Double(x, y + HEIGHT / 2, x + WIDTH, y + HEIGHT / 2);

Line2D.Double horiz3 = new Line2D.Double(x, y + HEIGHT, x + WIDTH, y + HEIGHT);

Line2D.Double vert = new Line2D.Double(x, y, x, y + HEIGHT);

g2.draw(horiz1);

g2.draw(horiz2);

g2.draw(horiz3);

g2.draw(vert);

x = x + WIDTH + SPACING;

vert = new Line2D.Double(x, y, x, y + HEIGHT);

horiz = new Line2D.Double(x, y + HEIGHT, x + WIDTH, y + HEIGHT);

g2.draw(vert);

g2.draw(horiz);

x = x + WIDTH + SPACING;

vert = new Line2D.Double(x, y, x, y + HEIGHT);

horiz = new Line2D.Double(x, y + HEIGHT, x + WIDTH, y + HEIGHT);

g2.draw(vert);

g2.draw(horiz);

x = x + WIDTH + SPACING;

Ellipse2D.Double e = new Ellipse2D.Double(x, y, WIDTH, HEIGHT);

g2.draw(e);

}

}

Q19.

Write a program that displays the Olympic rings. Make the rings colored in the Olympic colors.

Your output must be in the following format:

Ans.

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

public class ExP4_13 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

final int RADIUS = 50;

final int DISTANCE = RADIUS * 8 / 5;

double x = 0;

double y = 0;

drawRing(g2, x, y, RADIUS, Color.blue);

x = x + DISTANCE;

drawRing(g2, x, y, RADIUS, Color.black);

x = x + DISTANCE;

drawRing(g2, x, y, RADIUS, Color.red);

x = DISTANCE / 2;

y = DISTANCE;

drawRing(g2, x, y, RADIUS, Color.yellow);

x = x + DISTANCE;

drawRing(g2, x, y, RADIUS, Color.green);

}

public void drawRing(Graphics2D g2, double x, double y, double r, Color c)

{ g2.setColor(c);

Ellipse2D.Double e = new Ellipse2D.Double(x, y, 2 * r, 2 * r);

g2.draw(e);

}

}

Q20.

Write a graphics program that draws a clock face with a time that the user enters in a text field. (The user must enter the time in the format hh:mm, for example 09:45. ).

Hint: You need to find out the angles of the hour hand and the minute hand. The angle of the hour hand is harder; it travels 360 degree in 12 x 60 minutes.

Your output must be in the following format:

Ans.

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Line2D;

import javax.swing.JOptionPane;

public class ExP4_14 extends Applet

{ public void init()

{ String input

= JOptionPane.showInputDialog("Enter time as hh:mm");

hours = Integer.parseInt(input.substring(0, 2));

minutes = Integer.parseInt(input.substring(3, 5));

}

public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

final double RADIUS = 100;

final double MIN_HAND_LENGTH = 90;

final double HOUR_HAND_LENGTH = 80;

Ellipse2D.Double circle

= new Ellipse2D.Double(0, 0, 2 * RADIUS, 2 * RADIUS);

g2.draw(circle);

final double MINUTES_PER_HOUR = 60;

final double MINUTES_PER_360 = 60 * 12;

double minAngle = Math.PI / 2 - 2 * Math.PI * minutes / MINUTES_PER_HOUR;

Line2D.Double minHand = new Line2D.Double(RADIUS, RADIUS,

RADIUS + MIN_HAND_LENGTH * Math.cos(minAngle),

RADIUS - MIN_HAND_LENGTH * Math.sin(minAngle));

double hourAngle = Math.PI / 2 - 2 * Math.PI * (hours * MINUTES_PER_HOUR + minutes) / MINUTES_PER_360;

Line2D.Double hourHand = new Line2D.Double(RADIUS, RADIUS,

RADIUS + HOUR_HAND_LENGTH * Math.cos(hourAngle),

RADIUS - HOUR_HAND_LENGTH * Math.sin(hourAngle));

g2.draw(minHand);

g2.draw(hourHand);

}

private int hours;

private int minutes;

}

Q21.

Write graphics program to draw cars as follows:

Ans.

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Line2D;

import java.awt.geom.Point2D;

public class ExP4_16 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

Car car1 = new Car(100, 100);

Car car2 = new Car(150, 200);

Car car3 = new Car(300, 100);

car1.draw(g2);

car2.draw(g2);

car3.draw(g2);

}

}

class Car

{ public Car(int x, int y)

{ xleft = x;

ytop = y;

}

public void draw(Graphics2D g2)

{ Rectangle body = new Rectangle(xleft, ytop + CAR_HEIGHT / 3, CAR_WIDTH, CAR_HEIGHT / 3);

Ellipse2D.Double frontTire

= new Ellipse2D.Double(xleft + CAR_WIDTH / 6, ytop + CAR_HEIGHT * 2 / 3, CAR_WIDTH / 6, CAR_WIDTH / 6);

Ellipse2D.Double rearTire

= new Ellipse2D.Double(xleft + CAR_WIDTH * 4 / 6, ytop + CAR_HEIGHT * 2 / 3, CAR_WIDTH / 6, CAR_WIDTH / 6);

Point2D.Double r1 = new Point2D.Double(xleft + CAR_WIDTH / 6, ytop + CAR_HEIGHT / 3);

// the bottom of the front windshield

Point2D.Double r2 = new Point2D.Double(xleft + CAR_WIDTH * 2 / 6, ytop);

// the front of the roof

Point2D.Double r3 = new Point2D.Double(xleft + CAR_WIDTH * 4 / 6, ytop);

// the rear of the roof

Point2D.Double r4 = new Point2D.Double(xleft + CAR_WIDTH * 5 / 6, ytop + CAR_HEIGHT / 3);

// the bottom of the rear windshield

Line2D.Double frontWindshield

= new Line2D.Double(r1, r2);

Line2D.Double roofTop = new Line2D.Double(r2, r3);

Line2D.Double rearWindshield

= new Line2D.Double(r3, r4);

g2.draw(body);

g2.draw(frontTire);

g2.draw(rearTire);

g2.draw(frontWindshield);

g2.draw(roofTop);

g2.draw(rearWindshield);

}

private final int CAR_WIDTH = 60;

private final int CAR_HEIGHT = CAR_WIDTH / 2 ;

private int xleft;

private int ytop;

}

Q22.

Extend the solution of above question (Q16) by implementing a class Hose. In the constructor of the House class, supply the bottom left corner of the house and its width and height. Supply a method draw(Graphics2D g2) that draws the house. Then populate your screen with a few houses of different sizes.

Your output must be in the following format:

Ans.

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Rectangle2D;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Line2D;

import java.awt.geom.Point2D;

public class ExP4_17 extends Applet

{ public void paint(Graphics g)

{ Graphics2D g2 = (Graphics2D)g;

House house1 = new House(100, 150, 100, 120);

House house2 = new House(200, 200, 50, 70);

House house3 = new House(260, 260, 20, 25);

house1.draw(g2);

house2.draw(g2);

house3.draw(g2);

}

}

class House

{ public House(int x, int y, int w, int h)

{ xleft = x;

ybottom = y;

width = w;

height = h;

}

public void draw(Graphics2D g2)

{ Rectangle2D.Double front = new Rectangle2D.Double(xleft, ybottom - width, width, width);

Rectangle2D.Double door = new Rectangle2D.Double(xleft + width / 5, ybottom - width / 2, width / 5, width / 2);

Rectangle2D.Double window = new Rectangle2D.Double(xleft + width * 3 / 5, ybottom - width / 2, width / 5, width / 5);

Line2D.Double roofLeft = new Line2D.Double(xleft, ybottom - width, xleft + width / 2, ybottom - height);

Line2D.Double roofRight = new Line2D.Double(xleft + width, ybottom - width, xleft + width / 2, ybottom - height);

g2.draw(front);

g2.draw(door);

g2.draw(window);

g2.draw(roofLeft);

g2.draw(roofRight);

}

private int xleft;

private int ybottom;

private int width;

private int height;

}

1