[go: up one dir, main page]

0% found this document useful (0 votes)
4 views3 pages

Homework Lesson 4

The document contains two Java classes, AutoPainterL4 and AutoPainterTrain, which create graphical representations of cargo and passenger train cars, respectively. AutoPainterL4 draws a series of cargo cars with wheels and connectors, while AutoPainterTrain creates a passenger car with windows and wheels, including a warning if the windows may not fit. Both classes implement a window that closes upon user interaction and utilize the AWT library for drawing shapes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Homework Lesson 4

The document contains two Java classes, AutoPainterL4 and AutoPainterTrain, which create graphical representations of cargo and passenger train cars, respectively. AutoPainterL4 draws a series of cargo cars with wheels and connectors, while AutoPainterTrain creates a passenger car with windows and wheels, including a warning if the windows may not fit. Both classes implement a window that closes upon user interaction and utilize the AWT library for drawing shapes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Task 1

package autoPainterL4;
import java.awt.*;
import java.awt.event.*;

public class AutoPainterL4 extends Frame {

///////////////////// CONSTRUCTOR //////////////////////////

AutoPainterL4(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
setSize(1500, 300);
setVisible(true);
}
/////////////// METHOD MAIN FOR CODE RUNNING ////////////////

public static void main(String[] args) {


AutoPainterL4 painter = new AutoPainterL4("AutoPainterL4");
}
//////variables for initialization ////////////
int cargoCarWidth = 150;
int cargoCarHeight = 70;
int wheelDiameter = 30;
int connectorLength = 20;
int carSpacing = 20;
int numberOfCargoCars = 4;

////// variables for calculation //////////////////


int startX = 50;
int startY = 100;

public void paint(Graphics painter) {

painter.setColor(new Color(139, 69, 19));

for (int i = 0; i < numberOfCargoCars; i++) {


int currentCarX = startX + i * (cargoCarWidth + carSpacing);

painter.fillRect(currentCarX, startY, cargoCarWidth,


cargoCarHeight);
painter.drawRect(currentCarX, startY, cargoCarWidth,
cargoCarHeight);

painter.setColor(Color.BLACK);
painter.fillOval(currentCarX + 10, startY + cargoCarHeight -
wheelDiameter / 2, wheelDiameter, wheelDiameter);
painter.fillOval(currentCarX + cargoCarWidth - wheelDiameter - 10,
startY + cargoCarHeight - wheelDiameter / 2, wheelDiameter, wheelDiameter);
painter.setColor(new Color(139, 69, 19));

if (i < numberOfCargoCars - 1) {
painter.setColor(Color.GRAY);
painter.fillRect(currentCarX + cargoCarWidth, startY +
cargoCarHeight / 2 - 5, connectorLength, 10);
painter.setColor(new Color(139, 69, 19));
}
}
}
}

Task 2

package autoPainterTrain;
import java.awt.*;
import java.awt.event.*;

public class AutoPainterTrain extends Frame {

///////////////////// CONSTRUCTOR //////////////////////////

AutoPainterTrain(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
setSize(1000, 300);
setBackground(Color.LIGHT_GRAY);
setVisible(true);
}

/////////////// METHOD MAIN FOR CODE RUNNING ////////////////

public static void main(String[] args) {


AutoPainterTrain painter = new AutoPainterTrain("AutoPainterTrain");
}

////// variables for initialization ////////////


int passengerCarWidth = 610;
int passengerCarHeight = 80;
int windowWidth = 50;
int windowHeight = 40;
int windowSpacing = 15;
int numberOfWindows = 9;

////// variables for calculation //////////////////


int startX = 50;
int startY = 100;
int wheelDiameter = 30;

public void paint(Graphics painter) {

painter.setColor(new Color(60, 179, 113));


painter.fillRect(startX, startY, passengerCarWidth,
passengerCarHeight);
painter.setColor(Color.DARK_GRAY);
painter.drawRect(startX, startY, passengerCarWidth,
passengerCarHeight);

painter.setColor(Color.BLACK);
painter.fillOval(startX + 30, startY + passengerCarHeight -
wheelDiameter / 2, wheelDiameter, wheelDiameter);
painter.fillOval(startX + passengerCarWidth - 60, startY +
passengerCarHeight - wheelDiameter / 2, wheelDiameter, wheelDiameter);
painter.fillOval(startX + passengerCarWidth / 2 - wheelDiameter / 2,
startY + passengerCarHeight - wheelDiameter / 2, wheelDiameter,
wheelDiameter);

int paddingX = 20;


int usableCarWidth = passengerCarWidth - 2 * paddingX;

if (numberOfWindows * windowWidth + (numberOfWindows - 1) *


windowSpacing > usableCarWidth) {
System.out.println("Попередження: Вікна можуть не поміститися у
вагоні повністю.");

int totalWindowsWidth = numberOfWindows * windowWidth +


(numberOfWindows - 1) * windowSpacing;
int initialWindowX = startX + paddingX + (usableCarWidth -
totalWindowsWidth) / 2;

if (totalWindowsWidth > usableCarWidth) {


initialWindowX = startX + paddingX;
}

for (int i = 0; i < numberOfWindows; i++) {


int currentWindowX = initialWindowX + i * (windowWidth +
windowSpacing);
int currentWindowY = startY + (passengerCarHeight - windowHeight)
/ 2;

painter.setColor(Color.YELLOW);
painter.fillRect(currentWindowX, currentWindowY, windowWidth,
windowHeight);

painter.setColor(Color.RED);
painter.drawRect(currentWindowX, currentWindowY, windowWidth,
windowHeight);

// System.out.println("Window " + i + ": X=" + currentWindowX +


", Y=" + currentWindowY +
// ", Width=" + windowWidth + ", Height=" +
windowHeight);
}
}
}

You might also like