Task 1
package autoTrainPainter;
import java.awt.*;
import java.awt.event.*;
public class AutoTrainPainter extends Frame {
///////////////////// CONSTRUCTOR //////////////////////////
AutoTrainPainter(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
setSize(1360, 600);
setBackground(Color.LIGHT_GRAY);
setVisible(true);
}
/////////////// METHOD MAIN FOR CODE RUNNING ////////////////
public static void main(String[] args) {
AutoTrainPainter painter = new AutoTrainPainter("AutoTrainPainter");
}
////// variables for initialization ////////////
int passengerCarWidth = 610;
int passengerCarHeight = 80;
int windowWidth = 50;
int windowHeight = 40;
int windowSpacing = 15;
int numberOfWindows = 9;
int carHorizontalSpacing = 40;
int carVerticalSpacing = 80;
////// variables for calculation //////////////////
int initialStartX = 50;
int initialStartY = 100;
int wheelDiameter = 30;
public void paint(Graphics painter) {
int carsPerRow = 2;
int numberOfRows = 3;
for (int row = 0; row < numberOfRows; row++) {
for (int col = 0; col < carsPerRow; col++) {
int currentStartX = initialStartX + col * (passengerCarWidth
+ carHorizontalSpacing);
int currentStartY = initialStartY + row * (passengerCarHeight
+ carVerticalSpacing);
drawPassengerCar(painter, currentStartX, currentStartY);
}
}
}
private void drawPassengerCar(Graphics painter, int x, int y) {
painter.setColor(new Color(60, 179, 113));
painter.fillRect(x, y, passengerCarWidth, passengerCarHeight);
painter.setColor(Color.DARK_GRAY);
painter.drawRect(x, y, passengerCarWidth, passengerCarHeight);
painter.setColor(Color.BLACK);
painter.fillOval(x + 30, y + passengerCarHeight - wheelDiameter / 2,
wheelDiameter, wheelDiameter);
painter.fillOval(x + passengerCarWidth - 60, y + passengerCarHeight -
wheelDiameter / 2, wheelDiameter, wheelDiameter);
painter.fillOval(x + passengerCarWidth / 2 - wheelDiameter / 2, y +
passengerCarHeight - wheelDiameter / 2, wheelDiameter, wheelDiameter);
int paddingX = 20;
int usableCarWidth = passengerCarWidth - 2 * paddingX;
if (numberOfWindows * windowWidth + (numberOfWindows - 1) *
windowSpacing > usableCarWidth) {
int totalWindowsWidth = numberOfWindows * windowWidth +
(numberOfWindows - 1) * windowSpacing;
int initialWindowX = x + paddingX + (usableCarWidth -
totalWindowsWidth) / 2;
if (totalWindowsWidth > usableCarWidth) {
initialWindowX = x + paddingX;
}
for (int i = 0; i < numberOfWindows; i++) {
int currentWindowX = initialWindowX + i * (windowWidth +
windowSpacing);
int currentWindowY = y + (passengerCarHeight - windowHeight) / 2;
painter.setColor(Color.YELLOW);
painter.fillRect(currentWindowX, currentWindowY, windowWidth,
windowHeight);
painter.setColor(Color.RED);
painter.drawRect(currentWindowX, currentWindowY, windowWidth,
windowHeight);
}
}
}
Task 2
package autoHouseGenerator;
import java.awt.*;
import java.awt.event.*;
public class AutoHouseGenerator extends Frame {
// Constructor
AutoHouseGenerator(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
setSize(frameLength, frameHeight);
setVisible(true);
}
public static void main(String[] args) {
AutoHouseGenerator painter = new
AutoHouseGenerator("AutoHouseGenerator");
}
/////Variables for Initialization///////////
int frameLength = 700, frameHeight = 1000;
int houseWidth = 150;
int houseHeight = 160;
int roofHeight = 60;
int floorHeight = 40;
int windowWidth = 20;
int windowHeight = 25;
int windowHorizontalSpacing = 10;
int windowVerticalPadding = 7;
int xStart = 50;
int yStart = 80;
int houseHorizontalDistance = 50;
int rowVerticalDistance = 70;
int housesPerRow = 3;
int numberOfRows = 3;
int floorsPerHouse = 4;
int windowsPerFloor = 4;
////// variables for calculation //////////////////
public void paint(Graphics g) {
if (g instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
for (int row = 0; row < numberOfRows; row++) {
int currentY = yStart + (row * (houseHeight + roofHeight +
rowVerticalDistance));
for (int houseNum = 0; houseNum < housesPerRow; houseNum++)
{
int currentX = xStart + (houseNum * (houseWidth +
houseHorizontalDistance));
g.setColor(new Color(139, 69, 19));
g.fillRect(currentX, currentY + roofHeight, houseWidth,
houseHeight);
g.setColor(Color.BLACK);
g.drawRect(currentX, currentY + roofHeight, houseWidth,
houseHeight);
g.setColor(Color.RED);
int[] xPoints = {currentX, currentX + houseWidth / 2,
currentX + houseWidth};
int[] yPoints = {currentY + roofHeight, currentY,
currentY + roofHeight};
g.fillPolygon(xPoints, yPoints, 3);
g.setColor(Color.BLACK);
g.drawPolygon(xPoints, yPoints, 3);
g.setColor(Color.BLUE);
for (int floor = 0; floor < floorsPerHouse; floor++) {
int floorY = currentY + roofHeight + (floor *
floorHeight);
int startWindowX = currentX + (houseWidth -
(windowsPerFloor * windowWidth) -
((windowsPerFloor -
1) * windowHorizontalSpacing)) / 2;
for (int window = 0; window < windowsPerFloor;
window++) {
int windowX = startWindowX + (window * (windowWidth +
windowHorizontalSpacing));
int windowY = floorY + windowVerticalPadding;
g.fillRect(windowX, windowY, windowWidth,
windowHeight);
g.setColor(Color.BLACK);
g.drawRect(windowX, windowY, windowWidth,
windowHeight);
g.setColor(Color.BLUE);
}
}
}
}
}
}