Лабораторная работа №1

This commit is contained in:
Олег Кудринский 2024-06-08 03:38:07 +04:00
parent a54f498a15
commit 2d2c0e9714
14 changed files with 482 additions and 13 deletions

View File

@ -2,7 +2,10 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/ProjectContainerShip/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/ProjectContainerShip/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

View File

@ -0,0 +1,16 @@
import javax.swing.*;
import java.awt.*;
public class CanvasContainerShip extends JComponent {
public DrawingContainerShip _drawingContainerShip;
public CanvasContainerShip(){}
public void paintComponent(Graphics g) {
if (_drawingContainerShip == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingContainerShip.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,10 @@
public enum DecksCount {
OneDeck(1),
TwoDecks(2),
ThreeDecks(3);
private int deckcount;
DecksCount(int deckcount) {
this.deckcount = deckcount;
}
public int getDeckcount() {return deckcount;}
}

View File

@ -0,0 +1,6 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,145 @@
import javax.swing.*;
import java.awt.*;
public class DrawingContainerShip extends JPanel {
public EntityContainerShip EntityContainerShip;
public Integer picture_width;
private Integer picture_height;
private Integer _StartPosX;
private Integer _StartPosY;
private int drawingShipWidth = 150;
private int drawingShipHeight = 80;
public DrawingDecks drawingDecks = null;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean crane, boolean container) {
EntityContainerShip = new EntityContainerShip();
EntityContainerShip.Init(speed, weight, bodycolor, additionalcolor, crane, container);
picture_width = null;
picture_height = null;
_StartPosX = null;
_StartPosY = null;
drawingDecks = new DrawingDecks();
drawingDecks.setNumberOfDecks((int)(Math.random() * 4 + 0));
}
public boolean SetPictureSize(int width, int height) {
if (width < drawingShipWidth || height < drawingShipHeight) return false;
picture_width = width;
picture_height = height;
if (_StartPosX != null || _StartPosY != null) {
if (_StartPosX + drawingShipWidth > picture_width)
{
_StartPosX = _StartPosX - (_StartPosX + drawingShipWidth - picture_width);
}
else if (_StartPosX < 0) _StartPosX = 0;
if (_StartPosY + drawingShipHeight > picture_height)
{
_StartPosY = _StartPosY - (_StartPosY + drawingShipHeight - picture_height);
}
else if (_StartPosY < 0) _StartPosY = 0;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(picture_width != null && picture_height != null)) return;
if (x + drawingShipWidth > picture_width)
{
_StartPosX = x - (x + drawingShipWidth - picture_width);
}
else if (x < 0) _StartPosX = 0;
else _StartPosX = x;
if (y + drawingShipHeight > picture_height)
{
_StartPosY = y - (y + drawingShipHeight - picture_height);
}
else if (y < 0) _StartPosY = 0;
else _StartPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityContainerShip == null || _StartPosX == null || _StartPosY == null) return false;
switch (direction) {
case Left:
if (_StartPosX - EntityContainerShip.Step > 0) {
_StartPosX -= (int)EntityContainerShip.Step;
}
return true;
case Up:
if (_StartPosY - EntityContainerShip.Step > 0)
{
_StartPosY -= (int)EntityContainerShip.Step;
}
return true;
case Right:
if (_StartPosX + drawingShipWidth + (int)EntityContainerShip.Step < picture_width - EntityContainerShip.Step)
{
_StartPosX += (int)EntityContainerShip.Step;
}
return true;
case Down:
if (_StartPosY + drawingShipHeight + (int)EntityContainerShip.Step < picture_height - EntityContainerShip.Step)
{
_StartPosY += (int)EntityContainerShip.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g) {
if (EntityContainerShip == null || _StartPosX == null || _StartPosY == null) return;
int y = _StartPosY;
if (EntityContainerShip.getCrane()) {
//контейнер
g.setColor(Color.BLACK);
g.drawRect(_StartPosX + 70, _StartPosY + 15, 40, 15);
g.setColor(EntityContainerShip.getAdditionalColor());
g.fillRect(_StartPosX + 70, _StartPosY + 15, 40, 15);
g.setColor(Color.BLACK);
g.drawRect(_StartPosX + 90, _StartPosY + 20, 40, 15);
g.setColor(EntityContainerShip.getAdditionalColor());
g.fillRect(_StartPosX + 90, _StartPosY + 20, 40, 15);
y += 30;
}
g.setColor(EntityContainerShip.getBodyColor());
if (drawingDecks.getNumberOfDecks() != null) {
switch (drawingDecks.getNumberOfDecks()) {
case OneDeck:
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityContainerShip.getBodyColor());
y += 15;
break;
case TwoDecks:
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityContainerShip.getBodyColor());
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityContainerShip.getBodyColor());
y += 30;
break;
case ThreeDecks:
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityContainerShip.getBodyColor());
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityContainerShip.getBodyColor());
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 30, 100, 15, EntityContainerShip.getBodyColor());
y += 45;
break;
}
}
int[] arrayX = {_StartPosX, _StartPosX+150, _StartPosX+150, _StartPosX+120, _StartPosX+120, _StartPosX+30, _StartPosX+30, _StartPosX};
int[] arrayY = {y, y, y, y + 40, y + 40, y + 40, y + 40, y};
Polygon poly = new Polygon(arrayX, arrayY, 8);
g.fillPolygon(poly);
g.setColor(EntityContainerShip.getAdditionalColor());
if (EntityContainerShip.getContainer()) {
int craneX = _StartPosX + drawingShipWidth - 40;
int craneY = _StartPosY + drawingShipHeight - 90;
// основа крана
g.setColor(Color.BLACK);
g.drawLine(craneX + 10, craneY + 10, craneX + 10, craneY);
g.drawLine(craneX + 10, craneY, craneX + 50, craneY);
g.drawLine(craneX + 10, craneY, craneX + 50, craneY + 5);
g.drawLine(craneX + 50, craneY, craneX + 50, craneY + 30);
// хваталка крана
g.drawLine(craneX + 40, craneY + 30, craneX + 60, craneY + 30);
g.drawLine(craneX + 40, craneY + 30, craneX + 40, craneY + 35);
g.drawLine(craneX + 60, craneY + 30, craneX + 60, craneY + 35);
y += 30;
}
drawingShipHeight = y + 50 - _StartPosY;
}
}

View File

@ -0,0 +1,23 @@
import java.awt.*;
public class DrawingDecks {
private DecksCount decksCount;
public void setNumberOfDecks(int numberofdeck) {
for (DecksCount numofenum : decksCount.values()) {
if (numofenum.getDeckcount() == numberofdeck) {
decksCount = numofenum;
return;
}
}
}
public DecksCount getNumberOfDecks() {
return decksCount;
}
public void DrawDecks(Graphics g, int x, int y, int width, int height, Color bodyColor) {
g.setColor(bodyColor);
g.fillRect(x, y, width, height);
g.setColor(Color.BLACK);
g.drawRect(x, y, width, height);
g.setColor(bodyColor);
}
}

View File

@ -0,0 +1,26 @@
import java.awt.*;
public class EntityContainerShip {
private int Speed;
private double Weight;
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
private Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
private boolean Crane;
public boolean getCrane() {return Crane;}
private boolean Container;
public boolean getContainer() {return Container;}
public double Step;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean crane, boolean container)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
AdditionalColor = additionalcolor;
Crane = crane;
Container = container;
Step = Speed * 100 / Weight;
}
}

View File

@ -0,0 +1,123 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormContainerShip extends JFrame{
private String title;
private Dimension dimension;
private int Width, Height;
private CanvasContainerShip canvasContainerShip = new CanvasContainerShip();
private JButton CreateButton = new JButton("Создать");;
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton();
public FormContainerShip(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init() {
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 15;
Height = getHeight();
CreateButton.setName("CREATE");
Icon iconUp = new ImageIcon("resources\\up.jpg");
UpButton.setIcon(iconUp);
UpButton.setName("Up");
DownButton.setName("Down");
Icon iconDown = new ImageIcon("resources\\down.jpg");
DownButton.setIcon(iconDown);
LeftButton.setName("Left");
Icon iconLeft = new ImageIcon("resources\\left.jpg");
LeftButton.setIcon(iconLeft);
RightButton.setName("Right");
Icon iconRight = new ImageIcon("resources\\right.jpg");
RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int StartPositionX = (int)(Math.random() * 90 + 10);
int StartPositionY = (int)(Math.random() * 90 + 10);
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
boolean sheepPipes = new Random().nextBoolean();
boolean fuelTank = new Random().nextBoolean();;
canvasContainerShip._drawingContainerShip = new DrawingContainerShip();
canvasContainerShip._drawingContainerShip.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
canvasContainerShip._drawingContainerShip.SetPictureSize(Width, Height);
canvasContainerShip._drawingContainerShip.SetPosition( StartPositionX, StartPositionY);
canvasContainerShip.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasContainerShip._drawingContainerShip == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "Up":
result = canvasContainerShip._drawingContainerShip.MoveTransport(DirectionType.Up);
break;
case "Down":
result = canvasContainerShip._drawingContainerShip.MoveTransport(DirectionType.Down);
break;
case "Left":
result = canvasContainerShip._drawingContainerShip.MoveTransport(DirectionType.Left);
break;
case "Right":
result = canvasContainerShip._drawingContainerShip.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasContainerShip.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
canvasContainerShip.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
add(CreateButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(canvasContainerShip);
setVisible(true);
//обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasContainerShip._drawingContainerShip != null)
canvasContainerShip._drawingContainerShip.SetPictureSize(Width, Height);
canvasContainerShip.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
}
});
}
}

View File

@ -1,15 +1,8 @@
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import java.awt.*;
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
FormContainerShip form = new FormContainerShip("Контейнеровоз", new Dimension(500, 500));
form.Init();
}
}
}