LabWork01 is completed

This commit is contained in:
rozenkranzz 2025-02-05 19:36:28 +04:00
parent f99f5b8da2
commit 6c478bb1a6
12 changed files with 493 additions and 0 deletions

View File

@ -3,6 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 332 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 372 B

View File

@ -0,0 +1,53 @@
package projectliner;
import java.awt.*;
public class DeckDrawing {
private DeckEnum deckEnum;
public void setDeckEnum(int value) {
for (DeckEnum deck : DeckEnum.values()) {
if (deck.getDeckNumber() == value) {
this.deckEnum = deck;
return;
}
}
throw new IllegalArgumentException("Invalid deck number: " + value);
}
public DeckEnum getDeckEnum() {
return deckEnum;
}
public void drawDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
switch (deckEnum) {
case UPPER:
drawUpperDeck(x, y, borderColor, fillColor, g);
case MIDDLE:
drawMiddleDeck(x, y, borderColor, fillColor, g);
case LOWER:
drawLowerDeck(x, y, borderColor, fillColor, g);
}
}
private void drawLowerDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
g.setColor(fillColor);
g.fillRect(x + 30, y + 20, 100, 10);
g.setColor(borderColor);
g.drawRect(x + 30, y + 20, 100, 10);
}
private void drawMiddleDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
g.setColor(fillColor);
g.fillRect(x + 70, y + 10, 50, 10);
g.setColor(borderColor);
g.drawRect(x + 70, y + 10, 50, 10);
}
private void drawUpperDeck(int x, int y, Color borderColor, Color fillColor, Graphics g) {
g.setColor(fillColor);
g.fillRect(x + 85, y, 25, 10);
g.setColor(borderColor);
g.drawRect(x + 85, y, 25, 10);
}
}

View File

@ -0,0 +1,17 @@
package projectliner;
public enum DeckEnum {
LOWER(1),
MIDDLE(2),
UPPER(3);
private final int deckNumber;
DeckEnum(int deckNumber) {
this.deckNumber = deckNumber;
}
public int getDeckNumber() {
return deckNumber;
}
}

View File

@ -0,0 +1,8 @@
package projectliner;
public enum DirectionType {
UP,
DOWN,
LEFT,
RIGHT
}

View File

@ -0,0 +1,152 @@
package projectliner;
import java.awt.*;
public class DrawingLiner {
private LinerEntity linerEntity;
private DeckDrawing deckDrawing;
private Integer pictureWidth;
private Integer pictureHeight;
private Integer startPosX;
private Integer startPosY;
private final int drawingLinerWidth = 140;
private final int drawingLinerHeight = 60;
public void init(int speed, double weight, Color primaryColor,
Color secondaryColor, LinerEntityType type,
int capacity, int maxPassengers,
boolean hasExtraDeck, boolean hasPool) {
linerEntity = new LinerEntity();
deckDrawing = new DeckDrawing();
linerEntity.init(speed, weight, primaryColor, secondaryColor,
type, capacity, maxPassengers, hasExtraDeck, hasPool);
pictureWidth = null;
pictureHeight = null;
startPosX = null;
startPosY = null;
}
public boolean setPictureSize(int width, int height) {
if (drawingLinerWidth <= width && drawingLinerHeight <= height) {
pictureWidth = width;
pictureHeight = height;
return true;
} else {
return false;
}
}
public void setPosition(int x, int y) {
if (pictureHeight == null || pictureWidth == null) {
return;
}
if (x < 0) {
x = 0;
} else if (x > pictureWidth - drawingLinerWidth) {
x = pictureWidth - drawingLinerWidth;
}
if (y < 0) {
y = 0;
} else if (y > pictureHeight - drawingLinerHeight) {
y = pictureHeight - drawingLinerHeight;
}
startPosX = x;
startPosY = y;
}
public boolean moveTransport(DirectionType direction) {
if (linerEntity == null ||
startPosX == null || startPosY == null ||
pictureWidth == null || pictureHeight == null) {
return false;
}
switch (direction) {
case LEFT:
if (startPosX - linerEntity.getStep() > 0) {
startPosX -= (int) linerEntity.getStep();
}
break;
case UP:
if (startPosY - linerEntity.getStep() > 0) {
startPosY -= (int) linerEntity.getStep();
}
break;
case RIGHT:
if (startPosX + linerEntity.getStep() < pictureWidth - drawingLinerWidth) {
startPosX += (int) linerEntity.getStep();
}
break;
case DOWN:
if (startPosY + linerEntity.getStep() < pictureHeight - drawingLinerHeight) {
startPosY += (int) linerEntity.getStep();
}
break;
default:
return false;
}
return true;
}
public void setDeckNum(int num) {
deckDrawing.setDeckEnum(num);
}
public void drawTransport(Graphics g) {
if (linerEntity == null || startPosX == null || startPosY == null)
return;
int x = startPosX;
int y = startPosY;
Graphics2D g2d = (Graphics2D) g;
// Define colors
Color bodyColor = linerEntity.getPrimaryColor();
Color deckColor = linerEntity.getSecondaryColor();
Color borderColor = Color.BLACK;
// Body (hull)
Point[] hullPoints = {
new Point(x + 20, y + 60), // bottom left
new Point(x + 120, y + 60), // bottom right
new Point(x + 140, y + 30), // top right
new Point(x, y + 30) // top left
};
g2d.setColor(bodyColor);
g2d.fillPolygon(new int[]{hullPoints[0].x, hullPoints[1].x,
hullPoints[2].x, hullPoints[3].x},
new int[]{hullPoints[0].y, hullPoints[1].y,
hullPoints[2].y, hullPoints[3].y}, 4);
g2d.setColor(borderColor);
g2d.drawPolygon(new int[]{hullPoints[0].x, hullPoints[1].x,
hullPoints[2].x, hullPoints[3].x},
new int[]{hullPoints[0].y, hullPoints[1].y,
hullPoints[2].y, hullPoints[3].y}, 4);
// // First deck
// g2d.setColor(deckColor);
// g2d.fillRect(x + 30, y + 10, 100, 10);
// g2d.setColor(borderColor);
// g2d.drawRect(x + 30, y + 10, 100, 10);
deckDrawing.drawDeck(x, y, borderColor, deckColor, g);
// Additional drawing logic for pool and extra deck
if (linerEntity.hasPool()) {
g2d.setColor(Color.CYAN);
g2d.fillOval(x + 35, y + 15, 30, 10);
g2d.setColor(Color.BLACK);
g2d.drawOval(x + 35, y + 15, 30, 10);
}
// if (linerEntity.hasExtraDeck()) {
// g2d.setColor(linerEntity.getSecondaryColor());
// g2d.fillRect(x + 71, y + 1, 49, 9);
// g2d.setColor(Color.BLACK);
// g2d.drawRect(x + 70, y, 50, 10);
// }
}
}

View File

@ -0,0 +1,184 @@
package projectliner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Random;
public class FormLiner extends JFrame {
private DrawingLiner drawingLiner;
private JPanel pictureBoxLiner;
private Timer moveTimer;
private DirectionType currentDirection;
private int numberOfDecks;
public FormLiner() {
initializeComponent();
}
private void initializeComponent() {
pictureBoxLiner = new JPanel();
pictureBoxLiner.setPreferredSize(new Dimension(900, 500));
add(pictureBoxLiner, BorderLayout.CENTER);
JButton buttonCreateLiner = new JButton("Create Liner");
buttonCreateLiner.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonCreateLiner_Click(e);
}
});
JButton buttonSetDecks = new JButton("Set Decks");
buttonSetDecks.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonSetDecks_Click(e);
}
});
// Create a panel for the bottom region with BorderLayout
JPanel bottomPanel = new JPanel(new BorderLayout());
// Create a panel with FlowLayout.LEFT for the "Create Liner" button
JPanel createLinerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
createLinerPanel.add(buttonCreateLiner);
createLinerPanel.add(buttonSetDecks);
// Add the "Create Liner" panel to the left side of the bottom panel
bottomPanel.add(createLinerPanel, BorderLayout.WEST);
// Load arrow images using ClassLoader with the correct path
URL upIconURL = getClass().getClassLoader().getResource("icons8-arrow-up-60.png");
URL downIconURL = getClass().getClassLoader().getResource("icons8-arrow-down-60.png");
URL leftIconURL = getClass().getClassLoader().getResource("icons8-arrow-left-60.png");
URL rightIconURL = getClass().getClassLoader().getResource("icons8-arrow-right-60.png");
ImageIcon upIcon = new ImageIcon(upIconURL);
ImageIcon downIcon = new ImageIcon(downIconURL);
ImageIcon leftIcon = new ImageIcon(leftIconURL);
ImageIcon rightIcon = new ImageIcon(rightIconURL);
// Create buttons with arrow images
JButton buttonMoveUp = new JButton(upIcon);
buttonMoveUp.setName("buttonMoveUp");
addMoveButtonListeners(buttonMoveUp, DirectionType.UP);
JButton buttonMoveDown = new JButton(downIcon);
buttonMoveDown.setName("buttonMoveDown");
addMoveButtonListeners(buttonMoveDown, DirectionType.DOWN);
JButton buttonMoveLeft = new JButton(leftIcon);
buttonMoveLeft.setName("buttonMoveLeft");
addMoveButtonListeners(buttonMoveLeft, DirectionType.LEFT);
JButton buttonMoveRight = new JButton(rightIcon);
buttonMoveRight.setName("buttonMoveRight");
addMoveButtonListeners(buttonMoveRight, DirectionType.RIGHT);
// Create a panel for the arrow buttons
JPanel arrowPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
arrowPanel.add(buttonMoveUp);
arrowPanel.add(buttonMoveDown);
arrowPanel.add(buttonMoveLeft);
arrowPanel.add(buttonMoveRight);
bottomPanel.add(arrowPanel, BorderLayout.EAST);
add(bottomPanel, BorderLayout.SOUTH);
moveTimer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveTransport(currentDirection);
}
});
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void addMoveButtonListeners(JButton button, DirectionType direction) {
button.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
currentDirection = direction;
moveTimer.start();
}
@Override
public void mouseReleased(MouseEvent e) {
moveTimer.stop();
}
});
}
private void drawTransport() {
if (drawingLiner == null) {
return;
}
BufferedImage bitmap = new BufferedImage(pictureBoxLiner.getWidth(),
pictureBoxLiner.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics graphics = bitmap.getGraphics();
graphics.setColor(pictureBoxLiner.getBackground());
graphics.fillRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
drawingLiner.drawTransport(graphics);
pictureBoxLiner.getGraphics().drawImage(bitmap, 0, 0, null);
}
private void buttonCreateLiner_Click(ActionEvent e) {
Random random = new Random();
drawingLiner = new DrawingLiner();
drawingLiner.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
LinerEntityType.CARGO, random.nextInt(9000) + 1000,
random.nextInt(90) + 10, random.nextBoolean(), random.nextBoolean());
drawingLiner.setPictureSize(pictureBoxLiner.getWidth(),
pictureBoxLiner.getHeight());
drawingLiner.setPosition(random.nextInt(pictureBoxLiner.getWidth()),
random.nextInt(pictureBoxLiner.getHeight()));
drawingLiner.setDeckNum(numberOfDecks);
drawTransport();
}
private void buttonSetDecks_Click(ActionEvent e) {
String input = JOptionPane.showInputDialog(this, "Enter number of decks:");
try {
numberOfDecks = Integer.parseInt(input);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid number format", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void moveTransport(DirectionType direction) {
if (drawingLiner == null) {
return;
}
boolean result = switch (direction) {
case UP -> drawingLiner.moveTransport(DirectionType.UP);
case DOWN -> drawingLiner.moveTransport(DirectionType.DOWN);
case LEFT -> drawingLiner.moveTransport(DirectionType.LEFT);
case RIGHT -> drawingLiner.moveTransport(DirectionType.RIGHT);
};
if (result) {
drawTransport();
}
}
public static void main(String[] args) {
new FormLiner();
}
}

View File

@ -0,0 +1,70 @@
package projectliner;
import java.awt.Color;
public class LinerEntity {
private int speed;
private double weight;
private Color primaryColor;
private Color secondaryColor;
private LinerEntityType type;
private int capacity;
private int maxPassengers;
private boolean hasExtraDeck;
private boolean hasPool;
public void init(int speed, double weight, Color primaryColor,
Color secondaryColor, LinerEntityType type,
int capacity, int maxPassengers,
boolean hasExtraDeck, boolean hasPool) {
this.speed = speed;
this.weight = weight;
this.primaryColor = primaryColor;
this.secondaryColor = secondaryColor;
this.type = type;
this.capacity = capacity;
this.maxPassengers = maxPassengers;
this.hasExtraDeck = hasExtraDeck;
this.hasPool = hasPool;
}
public int getSpeed() {
return speed;
}
public double getWeight() {
return weight;
}
public Color getPrimaryColor() {
return primaryColor;
}
public Color getSecondaryColor() {
return secondaryColor;
}
public LinerEntityType getType() {
return type;
}
public int getCapacity() {
return capacity;
}
public int getMaxPassengers() {
return maxPassengers;
}
public boolean hasExtraDeck() {
return hasExtraDeck;
}
public boolean hasPool() {
return hasPool;
}
public double getStep() {
return speed / (weight / 100);
}
}

View File

@ -0,0 +1,8 @@
package projectliner;
public enum LinerEntityType {
PASSENGER,
CARGO,
MILITARY,
MIXED
}