diff --git a/ProjectLiner/.vscode/settings.json b/ProjectLiner/.vscode/settings.json new file mode 100644 index 0000000..e112a70 --- /dev/null +++ b/ProjectLiner/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/ProjectLiner/README.md b/ProjectLiner/README.md new file mode 100644 index 0000000..7c03a53 --- /dev/null +++ b/ProjectLiner/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/ProjectLiner/src/CanvasLiner.java b/ProjectLiner/src/CanvasLiner.java new file mode 100644 index 0000000..b393a44 --- /dev/null +++ b/ProjectLiner/src/CanvasLiner.java @@ -0,0 +1,19 @@ +import javax.swing.*; +import java.awt.*; + +public class CanvasLiner extends JComponent { + public DrawningLiner _drawingLiner; + + public CanvasLiner() { + } + + public void paintComponent(Graphics g) { + if (_drawingLiner == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawingLiner.DrawTransport(g2d); + super.repaint(); + } +} \ No newline at end of file diff --git a/ProjectLiner/src/CountDeck.java b/ProjectLiner/src/CountDeck.java new file mode 100644 index 0000000..ea5a1c0 --- /dev/null +++ b/ProjectLiner/src/CountDeck.java @@ -0,0 +1,6 @@ +public enum CountDeck { + One, + Two, + Three, + Four; +} diff --git a/ProjectLiner/src/DirectionType.java b/ProjectLiner/src/DirectionType.java new file mode 100644 index 0000000..35657f0 --- /dev/null +++ b/ProjectLiner/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} diff --git a/ProjectLiner/src/DrawningDeck.java b/ProjectLiner/src/DrawningDeck.java new file mode 100644 index 0000000..3d2fa35 --- /dev/null +++ b/ProjectLiner/src/DrawningDeck.java @@ -0,0 +1,42 @@ +import java.awt.*; + +public class DrawningDeck { + private CountDeck _deck; + + public CountDeck getCount() { + return _deck; + } + + public void SetCount(int count) { + switch (count) { + case 1: + _deck = CountDeck.One; + break; + case 2: + _deck = CountDeck.Two; + break; + default: + _deck = CountDeck.Three; + break; + } + } + + public void Draw(Graphics2D g, int _StartPosX, int _StartPosY) { + if (_deck == null) { + return; + } + + if (_deck == CountDeck.One) { + g.fillRect(_StartPosX + 50, _StartPosY + 70, 100, 10); + } + if (_deck == CountDeck.Two) { + g.fillRect(_StartPosX + 50, _StartPosY + 70, 100, 10); + g.fillRect(_StartPosX + 60, _StartPosY + 60, 80, 15); + } + if (_deck == CountDeck.Three) { + g.fillRect(_StartPosX + 50, _StartPosY + 70, 100, 10); + g.fillRect(_StartPosX + 60, _StartPosY + 60, 80, 15); + g.fillRect(_StartPosX + 70, _StartPosY + 50, 60, 15); + } + } +} diff --git a/ProjectLiner/src/DrawningLiner.java b/ProjectLiner/src/DrawningLiner.java new file mode 100644 index 0000000..f434b6a --- /dev/null +++ b/ProjectLiner/src/DrawningLiner.java @@ -0,0 +1,131 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawningLiner extends JPanel { + public EntityLiner EntityLiner; + public DrawningDeck _deck = null; + private Integer picture_width; + private Integer picture_height; + private Integer _StartPosX; + private Integer _StartPosY; + private int drawningCarWidth = 120; + private int drawningCarHeight = 90; + + public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean pipe, boolean anchor) { + EntityLiner = new EntityLiner(); + EntityLiner.Init(speed, weight, bodycolor, additionalcolor, pipe, anchor); + picture_width = null; + picture_height = null; + _StartPosX = null; + _StartPosY = null; + _deck = new DrawningDeck(); + Random rand = new Random(); + int randomNum = rand.nextInt(3); + _deck.SetCount(randomNum); + } + + public boolean SetPictureSize(int width, int height) { + if (width < drawningCarWidth || height < drawningCarHeight) + return false; + picture_width = width; + picture_height = height; + if (_StartPosX != null || _StartPosY != null) { + if (_StartPosX + drawningCarWidth > picture_width) { + _StartPosX = _StartPosX - (_StartPosX + drawningCarWidth - picture_width); + } else if (_StartPosX < 0) + _StartPosX = 0; + if (_StartPosY + drawningCarHeight > picture_height) { + _StartPosY = _StartPosY - (_StartPosY + drawningCarHeight - 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 + drawningCarWidth > picture_width) { + _StartPosX = x - (x + drawningCarWidth - picture_width); + } else if (x < 0) + _StartPosX = 0; + else + _StartPosX = x; + if (y + drawningCarHeight > picture_height) { + _StartPosY = y - (y + drawningCarHeight - picture_height); + } else if (y < 0) + _StartPosY = 0; + else + _StartPosY = y; + } + + public boolean MoveTransport(DirectionType direction) { + if (EntityLiner == null || _StartPosX == null || _StartPosY == null) + return false; + switch (direction) { + case Left: + if (_StartPosX - EntityLiner.Step > 0) { + _StartPosX -= (int) EntityLiner.Step; + } + return true; + case Up: + if (_StartPosY - EntityLiner.Step > 0) { + _StartPosY -= (int) EntityLiner.Step; + } + return true; + case Right: + if (_StartPosX + drawningCarWidth + 2 * (int) EntityLiner.Step < picture_width + - EntityLiner.Step) { + _StartPosX += (int) EntityLiner.Step; + } + return true; + case Down: + if (_StartPosY + drawningCarHeight + 2 * (int) EntityLiner.Step < picture_height + - EntityLiner.Step) { + _StartPosY += (int) EntityLiner.Step; + } + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics2D g) { + + if (EntityLiner == null) { + return; + } + ; + g.setColor(EntityLiner.getAdditionalColor()); + _deck.Draw(g, _StartPosX, _StartPosY); + g.setColor(EntityLiner.getBodyColor()); + + int[] xPoints = { _StartPosX + 40, _StartPosX + 60, _StartPosX + 60 }; + int[] yPoints = { _StartPosY + 80, _StartPosY + 80, _StartPosY + 120 }; + g.drawPolygon(xPoints, yPoints, 3); + g.fillPolygon(xPoints, yPoints, 3); + + xPoints = new int[] { _StartPosX + 40 + 100, _StartPosX + 60 + 100, _StartPosX + 40 + 100 }; + yPoints = new int[] { _StartPosY + 80, _StartPosY + 80, _StartPosY + 120 }; + g.drawPolygon(xPoints, yPoints, 3); + g.fillPolygon(xPoints, yPoints, 3); + + g.drawRect(_StartPosX + 60, _StartPosY + 80, 80, 40); + g.fillRect(_StartPosX + 60, _StartPosY + 80, 80, 40); + + if (EntityLiner.getAnchor()) { + g.setColor(EntityLiner.getAdditionalColor()); + g.drawLine(_StartPosX + 70, _StartPosY + 85, _StartPosX + 70, _StartPosY + 110); + g.drawLine(_StartPosX + 60, _StartPosY + 90, _StartPosX + 80, _StartPosY + 90); + g.drawLine(_StartPosX + 60, _StartPosY + 100, _StartPosX + 70, _StartPosY + 110); + g.drawLine(_StartPosX + 70, _StartPosY + 110, _StartPosX + 80, _StartPosY + 100); + } + // отрисовка шлюпок + if (EntityLiner.getBoats()) { + g.setColor(EntityLiner.getAdditionalColor()); + g.fillOval(_StartPosX + 80, _StartPosY + 80, 30, 20); + g.fillOval(_StartPosX + 120, _StartPosY + 80, 30, 20); + } + } +} diff --git a/ProjectLiner/src/EntityLiner.java b/ProjectLiner/src/EntityLiner.java new file mode 100644 index 0000000..fa8e760 --- /dev/null +++ b/ProjectLiner/src/EntityLiner.java @@ -0,0 +1,37 @@ +import java.awt.*; + +public class EntityLiner { + private int Speed; + private double Weight; + private Color BodyColor; + private Color AdditionalColor; + private boolean Boats; + private boolean Anchor; + public double Step; + + public Color getBodyColor() { + return BodyColor; + } + + public Color getAdditionalColor() { + return AdditionalColor; + } + + public boolean getBoats() { + return Boats; + } + + public boolean getAnchor() { + return Anchor; + } + + public void Init(int Speed, double Weight, Color Bodycolor, Color Additionalcolor, boolean Boats, boolean Anchor) { + this.Speed = Speed; + this.Weight = Weight; + this.BodyColor = Bodycolor; + this.AdditionalColor = Additionalcolor; + this.Boats = Boats; + this.Anchor = Anchor; + Step = Speed * 100 / Weight; + } +} diff --git a/ProjectLiner/src/FormLiner.java b/ProjectLiner/src/FormLiner.java new file mode 100644 index 0000000..116e619 --- /dev/null +++ b/ProjectLiner/src/FormLiner.java @@ -0,0 +1,131 @@ +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 FormLiner extends JFrame { + private String title; + private Dimension dimension; + private int Width, Height; + private CanvasLiner canvasLiner = new CanvasLiner(); + 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 FormLiner(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() - 35; + + CreateButton.setName("CREATE"); + Icon iconUp = new ImageIcon("src\\imges\\up.jpg"); + UpButton.setIcon(iconUp); + UpButton.setName("UP"); + DownButton.setName("DOWN"); + Icon iconDown = new ImageIcon("src\\images\\down.jpg"); + DownButton.setIcon(iconDown); + LeftButton.setName("LEFT"); + Icon iconLeft = new ImageIcon("src\\images\\left.jpg"); + LeftButton.setIcon(iconLeft); + RightButton.setName("RIGHT"); + Icon iconRight = new ImageIcon("src\\images\\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 tower = new Random().nextBoolean(); + boolean radar = new Random().nextBoolean(); + ; + canvasLiner._drawingLiner = new DrawningLiner(); + canvasLiner._drawingLiner.Init(speed, weight, bodyColor, additionalColor, tower, + radar); + canvasLiner._drawingLiner.SetPictureSize(Width, Height); + canvasLiner._drawingLiner.SetPosition(StartPositionX, StartPositionY); + canvasLiner.repaint(); + } + }); + + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + if (canvasLiner._drawingLiner == null) + return; + boolean result = false; + switch ((((JButton) (event.getSource())).getName())) { + case "UP": + result = canvasLiner._drawingLiner.MoveTransport(DirectionType.Up); + break; + case "DOWN": + result = canvasLiner._drawingLiner.MoveTransport(DirectionType.Down); + break; + case "LEFT": + result = canvasLiner._drawingLiner.MoveTransport(DirectionType.Left); + break; + case "RIGHT": + result = canvasLiner._drawingLiner.MoveTransport(DirectionType.Right); + break; + } + if (result) { + canvasLiner.repaint(); + } + } + }; + UpButton.addActionListener(actionListener); + DownButton.addActionListener(actionListener); + LeftButton.addActionListener(actionListener); + RightButton.addActionListener(actionListener); + + setSize(dimension.width, dimension.height); + setLayout(null); + canvasLiner.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(canvasLiner); + setVisible(true); + // обработка события изменения размеров окна + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + Width = getWidth() - 15; + Height = getHeight() - 35; + if (canvasLiner._drawingLiner != null) + canvasLiner._drawingLiner.SetPictureSize(Width, Height); + canvasLiner.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); + } + }); + } +} diff --git a/ProjectLiner/src/Main.java b/ProjectLiner/src/Main.java new file mode 100644 index 0000000..a7fd124 --- /dev/null +++ b/ProjectLiner/src/Main.java @@ -0,0 +1,8 @@ +import java.awt.*; + +public class Main { + public static void main(String[] args) { + FormLiner form = new FormLiner("Лайнер", new Dimension(600, 400)); + form.Init(); + } +} diff --git a/ProjectLiner/src/images/down.jpg b/ProjectLiner/src/images/down.jpg new file mode 100644 index 0000000..fc21b6a Binary files /dev/null and b/ProjectLiner/src/images/down.jpg differ diff --git a/ProjectLiner/src/images/left.jpg b/ProjectLiner/src/images/left.jpg new file mode 100644 index 0000000..453db80 Binary files /dev/null and b/ProjectLiner/src/images/left.jpg differ diff --git a/ProjectLiner/src/images/right.jpg b/ProjectLiner/src/images/right.jpg new file mode 100644 index 0000000..1f9989a Binary files /dev/null and b/ProjectLiner/src/images/right.jpg differ diff --git a/ProjectLiner/src/images/up.jpg b/ProjectLiner/src/images/up.jpg new file mode 100644 index 0000000..bfdaef6 Binary files /dev/null and b/ProjectLiner/src/images/up.jpg differ