PIbd-12 Stryukov A.A. LabWork01 Advanced #1
@ -3,6 +3,7 @@
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
|
BIN
Liner_Advanced/res/icons8-arrow-down-60.png
Normal file
BIN
Liner_Advanced/res/icons8-arrow-down-60.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 387 B |
BIN
Liner_Advanced/res/icons8-arrow-left-60.png
Normal file
BIN
Liner_Advanced/res/icons8-arrow-left-60.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 332 B |
BIN
Liner_Advanced/res/icons8-arrow-right-60.png
Normal file
BIN
Liner_Advanced/res/icons8-arrow-right-60.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 294 B |
BIN
Liner_Advanced/res/icons8-arrow-up-60.png
Normal file
BIN
Liner_Advanced/res/icons8-arrow-up-60.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 372 B |
53
Liner_Advanced/src/projectliner/DeckDrawing.java
Normal file
53
Liner_Advanced/src/projectliner/DeckDrawing.java
Normal 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);
|
||||
}
|
||||
}
|
17
Liner_Advanced/src/projectliner/DeckEnum.java
Normal file
17
Liner_Advanced/src/projectliner/DeckEnum.java
Normal 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;
|
||||
}
|
||||
}
|
8
Liner_Advanced/src/projectliner/DirectionType.java
Normal file
8
Liner_Advanced/src/projectliner/DirectionType.java
Normal file
@ -0,0 +1,8 @@
|
||||
package projectliner;
|
||||
|
||||
public enum DirectionType {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
141
Liner_Advanced/src/projectliner/DrawingLiner.java
Normal file
141
Liner_Advanced/src/projectliner/DrawingLiner.java
Normal file
@ -0,0 +1,141 @@
|
||||
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, int deckNum) {
|
||||
linerEntity = new LinerEntity();
|
||||
deckDrawing = new DeckDrawing();
|
||||
linerEntity.init(speed, weight, primaryColor, secondaryColor,
|
||||
type, capacity, maxPassengers, hasExtraDeck, hasPool);
|
||||
deckDrawing.setDeckEnum(deckNum);
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
182
Liner_Advanced/src/projectliner/FormLiner.java
Normal file
182
Liner_Advanced/src/projectliner/FormLiner.java
Normal file
@ -0,0 +1,182 @@
|
||||
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(), numberOfDecks);
|
||||
drawingLiner.setPictureSize(pictureBoxLiner.getWidth(),
|
||||
pictureBoxLiner.getHeight());
|
||||
drawingLiner.setPosition(random.nextInt(pictureBoxLiner.getWidth()),
|
||||
random.nextInt(pictureBoxLiner.getHeight()));
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
70
Liner_Advanced/src/projectliner/LinerEntity.java
Normal file
70
Liner_Advanced/src/projectliner/LinerEntity.java
Normal 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);
|
||||
}
|
||||
}
|
8
Liner_Advanced/src/projectliner/LinerEntityType.java
Normal file
8
Liner_Advanced/src/projectliner/LinerEntityType.java
Normal file
@ -0,0 +1,8 @@
|
||||
package projectliner;
|
||||
|
||||
public enum LinerEntityType {
|
||||
PASSENGER,
|
||||
CARGO,
|
||||
MILITARY,
|
||||
MIXED
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user