diff --git a/.idea/PIbd-11_Kudrinsky_O.S._ContainerShip_Hard.iml b/.idea/PIbd-11_Kudrinsky_O.S._ContainerShip_Hard.iml index d6ebd48..56d8ac1 100644 --- a/.idea/PIbd-11_Kudrinsky_O.S._ContainerShip_Hard.iml +++ b/.idea/PIbd-11_Kudrinsky_O.S._ContainerShip_Hard.iml @@ -2,7 +2,10 @@ - + + + + diff --git a/ProjectContainerShip/.idea/uiDesigner.xml b/ProjectContainerShip/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/ProjectContainerShip/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ProjectContainerShip/resources/down.jpg b/ProjectContainerShip/resources/down.jpg new file mode 100644 index 0000000..fc21b6a Binary files /dev/null and b/ProjectContainerShip/resources/down.jpg differ diff --git a/ProjectContainerShip/resources/left.jpg b/ProjectContainerShip/resources/left.jpg new file mode 100644 index 0000000..453db80 Binary files /dev/null and b/ProjectContainerShip/resources/left.jpg differ diff --git a/ProjectContainerShip/resources/right.jpg b/ProjectContainerShip/resources/right.jpg new file mode 100644 index 0000000..1f9989a Binary files /dev/null and b/ProjectContainerShip/resources/right.jpg differ diff --git a/ProjectContainerShip/resources/up.jpg b/ProjectContainerShip/resources/up.jpg new file mode 100644 index 0000000..bfdaef6 Binary files /dev/null and b/ProjectContainerShip/resources/up.jpg differ diff --git a/ProjectContainerShip/src/CanvasContainerShip.java b/ProjectContainerShip/src/CanvasContainerShip.java new file mode 100644 index 0000000..a0c8384 --- /dev/null +++ b/ProjectContainerShip/src/CanvasContainerShip.java @@ -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(); + } +} \ No newline at end of file diff --git a/ProjectContainerShip/src/DecksCount.java b/ProjectContainerShip/src/DecksCount.java new file mode 100644 index 0000000..7dfc72b --- /dev/null +++ b/ProjectContainerShip/src/DecksCount.java @@ -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;} +} diff --git a/ProjectContainerShip/src/DirectionType.java b/ProjectContainerShip/src/DirectionType.java new file mode 100644 index 0000000..35657f0 --- /dev/null +++ b/ProjectContainerShip/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} diff --git a/ProjectContainerShip/src/DrawingContainerShip.java b/ProjectContainerShip/src/DrawingContainerShip.java new file mode 100644 index 0000000..c5287e2 --- /dev/null +++ b/ProjectContainerShip/src/DrawingContainerShip.java @@ -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; + } +} diff --git a/ProjectContainerShip/src/DrawingDecks.java b/ProjectContainerShip/src/DrawingDecks.java new file mode 100644 index 0000000..2c307a1 --- /dev/null +++ b/ProjectContainerShip/src/DrawingDecks.java @@ -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); + } +} diff --git a/ProjectContainerShip/src/EntityContainerShip.java b/ProjectContainerShip/src/EntityContainerShip.java new file mode 100644 index 0000000..bc166a6 --- /dev/null +++ b/ProjectContainerShip/src/EntityContainerShip.java @@ -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; + } +} diff --git a/ProjectContainerShip/src/FormContainerShip.java b/ProjectContainerShip/src/FormContainerShip.java new file mode 100644 index 0000000..a295dd3 --- /dev/null +++ b/ProjectContainerShip/src/FormContainerShip.java @@ -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); + } + }); + } +} diff --git a/ProjectContainerShip/src/Main.java b/ProjectContainerShip/src/Main.java index 930198c..dde7b33 100644 --- a/ProjectContainerShip/src/Main.java +++ b/ProjectContainerShip/src/Main.java @@ -1,15 +1,8 @@ -//TIP To Run code, press or -// click the icon in the gutter. +import java.awt.*; + public class Main { public static void main(String[] args) { - //TIP Press 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 to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } + FormContainerShip form = new FormContainerShip("Контейнеровоз", new Dimension(500, 500)); + form.Init(); } -} \ No newline at end of file +}