diff --git a/OOP_Hard/src/CanvasAccordionBus.java b/OOP_Hard/src/CanvasAccordionBus.java new file mode 100644 index 0000000..69d2c71 --- /dev/null +++ b/OOP_Hard/src/CanvasAccordionBus.java @@ -0,0 +1,16 @@ +import javax.swing.*; +import java.awt.*; + +public class CanvasAccordionBus extends JComponent { + public DrawningAccordionBus _drawningAccordionBus; + public CanvasAccordionBus(){} + public void paintComponent(Graphics g) { + if (_drawningAccordionBus == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawningAccordionBus.DrawTransport(g2d); + super.repaint(); + } +} diff --git a/OOP_Hard/src/CountEntrances.java b/OOP_Hard/src/CountEntrances.java new file mode 100644 index 0000000..e46c317 --- /dev/null +++ b/OOP_Hard/src/CountEntrances.java @@ -0,0 +1,5 @@ +public enum CountEntrances { + Three, + Four, + Five +} diff --git a/OOP_Hard/src/DirectionType.java b/OOP_Hard/src/DirectionType.java new file mode 100644 index 0000000..35657f0 --- /dev/null +++ b/OOP_Hard/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} diff --git a/OOP_Hard/src/DrawingEntrances.java b/OOP_Hard/src/DrawingEntrances.java new file mode 100644 index 0000000..f245ca1 --- /dev/null +++ b/OOP_Hard/src/DrawingEntrances.java @@ -0,0 +1,56 @@ +import java.awt.*; +import java.util.Random; + +public class DrawingEntrances { + private CountEntrances countEntrances; + + Random rand = new Random(); + int randomNum = rand.nextInt(5); + + public CountEntrances getCount() { + return countEntrances; + } + public void setCountEntrances() { + switch (randomNum) { + case 3: + countEntrances = CountEntrances.Three; + break; + case 4: + countEntrances = CountEntrances.Four; + break; + case 5: + countEntrances = CountEntrances.Five; + break; + default: + countEntrances = CountEntrances.Five; + break; + } + } + public void DrawEntrances(Graphics2D g, int _StartPosX, int _StartPosY) { + if (countEntrances == null) { + return; + } + + g.setColor(Color.BLACK); + g.drawRect(_StartPosX + 74, _StartPosY + 18, 5, 20); + g.drawRect(_StartPosX + 84, _StartPosY + 18, 5, 20); + + if (countEntrances == CountEntrances.Three) { + g.setColor(Color.BLACK); + g.drawRect(_StartPosX + 94, _StartPosY + 18, 5, 20); + } + + if (countEntrances == CountEntrances.Four) { + g.setColor(Color.BLACK); + g.drawRect(_StartPosX + 94, _StartPosY + 18, 5, 20); + g.drawRect(_StartPosX + 104, _StartPosY + 18, 5, 20); + } + + if (countEntrances == CountEntrances.Five) { + g.setColor(Color.BLACK); + g.drawRect(_StartPosX + 94, _StartPosY + 18, 5, 20); + g.drawRect(_StartPosX + 104, _StartPosY + 18, 5, 20); + g.drawRect(_StartPosX + 112, _StartPosY + 18, 5, 20); + } + } +} diff --git a/OOP_Hard/src/DrawningAccordionBus.java b/OOP_Hard/src/DrawningAccordionBus.java new file mode 100644 index 0000000..7ffe5f1 --- /dev/null +++ b/OOP_Hard/src/DrawningAccordionBus.java @@ -0,0 +1,138 @@ +import java.awt.*; +import javax.swing.*; +import java.util.Random; + +public class DrawningAccordionBus extends JPanel { + public EntityAccordionBus entityAccordionBus; + public DrawingEntrances drawingEntrances = null; + + private Integer picture_width; + private Integer picture_height; + + private Integer _StartPosX; + private Integer _StartPosY; + + private int drawingBusWidth = 60; + private int drawingBusHeight = 40; + + public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean compartment, boolean entrance, boolean windows) { + entityAccordionBus = new EntityAccordionBus(); + entityAccordionBus.Init(speed, weight, bodycolor, additionalcolor, compartment, entrance, windows); + picture_width = null; + picture_height = null; + _StartPosX = null; + _StartPosY = null; + drawingEntrances = new DrawingEntrances(); + Random rand = new Random(); + drawingEntrances.setCountEntrances(); + } + + public boolean SetPictureSize(int width, int height) { + if (width < drawingBusWidth || height < drawingBusHeight) return false; + picture_width = width; + picture_height = height; + if (_StartPosX != null || _StartPosY != null) { + if (_StartPosX + drawingBusWidth > picture_width) { + _StartPosX = _StartPosX - (_StartPosX + drawingBusWidth - picture_width); + } + else if (_StartPosX < 0) _StartPosX = 0; + if (_StartPosY + drawingBusHeight > picture_height) { + _StartPosY = _StartPosY - (_StartPosY + drawingBusHeight - 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 + drawingBusWidth > picture_width) { + _StartPosX = x - (x + drawingBusWidth - picture_width); + } + else if (x < 0) _StartPosX = 0; + else _StartPosX = x; + if (y + drawingBusHeight > picture_height) { + _StartPosY = y - (y + drawingBusHeight - picture_height); + } + else if (y < 0) _StartPosY = 0; + else _StartPosY = y; + } + + public boolean MoveTransport(DirectionType direction) { + if (entityAccordionBus == null || _StartPosX == null || _StartPosY == null) return false; + switch (direction) { + case Left: + if (_StartPosX - entityAccordionBus.Step > 0) { + _StartPosX -= (int) entityAccordionBus.Step; + } + return true; + case Up: + if (_StartPosY - entityAccordionBus.Step > 0) { + _StartPosY -= (int) entityAccordionBus.Step; + } + return true; + case Right: + if (_StartPosX + drawingBusWidth + (int) entityAccordionBus.Step < picture_width - entityAccordionBus.Step) { + _StartPosX += (int) entityAccordionBus.Step; + } + return true; + case Down: + if (_StartPosY + drawingBusHeight + (int) entityAccordionBus.Step < picture_height - entityAccordionBus.Step) { + _StartPosY += (int) entityAccordionBus.Step; + } + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics2D g) { + if (entityAccordionBus == null || _StartPosX == null || _StartPosY == null) return; + + //вагончик + g.setColor(entityAccordionBus.getBodyColor()); + g.fillRect(_StartPosX + 10, _StartPosY + 10, 50, 30); + g.setColor(Color.BLACK); + g.drawRect(_StartPosX + 10, _StartPosY + 10, 50, 30); + + //двери + g.setColor(Color.BLACK); + g.drawRect(_StartPosX + 26, _StartPosY + 18,10, 20); + + //окна + g.setColor(Color.BLUE); + g.fillOval(_StartPosX + 15, _StartPosY + 15, 5, 10); + g.fillOval(_StartPosX + 40, _StartPosY + 15, 5, 10); + g.fillOval(_StartPosX + 50, _StartPosY + 15, 5, 10); + g.setColor(Color.BLACK); + g.drawOval(_StartPosX + 15, _StartPosY + 15, 5, 10); + g.drawOval(_StartPosX + 40, _StartPosY + 15, 5, 10); + g.drawOval(_StartPosX + 50, _StartPosY + 15, 5, 10); + + //колеса + g.setColor(Color.BLACK); + g.fillOval(_StartPosX + 15, _StartPosY + 35, 10, 10); + g.fillOval(_StartPosX + 45, _StartPosY + 35, 10, 10); + g.drawOval(_StartPosX + 15, _StartPosY + 35, 10, 10); + g.drawOval(_StartPosX + 45, _StartPosY + 35, 10, 10); + + if(entityAccordionBus.withCompartment()) { + drawingBusWidth = 130; + g.setColor(entityAccordionBus.getAdditionalColor()); + g.fillRect(_StartPosX + 70, _StartPosY + 10, 50, 30); + g.fillRect(_StartPosX + 60, _StartPosY + 15, 10, 20); + g.setColor(Color.BLACK); + g.drawRect(_StartPosX + 70, _StartPosY + 10, 50, 30); + g.drawRect(_StartPosX + 60, _StartPosY + 15, 10, 20); + + g.setColor(Color.BLACK); + g.fillOval(_StartPosX + 75, _StartPosY + 35, 10, 10); + g.fillOval(_StartPosX + 105, _StartPosY + 35, 10, 10); + g.drawOval(_StartPosX + 75, _StartPosY + 35, 10, 10); + g.drawOval(_StartPosX + 105, _StartPosY + 35, 10, 10); + + drawingEntrances.DrawEntrances(g, _StartPosX, _StartPosY); + + } + } +} diff --git a/OOP_Hard/src/EntityAccordionBus.java b/OOP_Hard/src/EntityAccordionBus.java new file mode 100644 index 0000000..0c8c6ca --- /dev/null +++ b/OOP_Hard/src/EntityAccordionBus.java @@ -0,0 +1,47 @@ +import java.awt.*; + +public class EntityAccordionBus { + + private int Speed; + + private double Weight; + + private Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + + private Color AdditionalColor; + public Color getAdditionalColor() { + return AdditionalColor; + } + + public double Step; + + private boolean Compartment; + public boolean withCompartment() { + return Compartment; + } + + private boolean Entrance; + public boolean withEntrance() { + return Entrance; + } + + private boolean Windows; + public boolean withWindows() { + return Windows; + } + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, + boolean compartment, boolean entrance, boolean windows) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Compartment = compartment; // отсек + Entrance = entrance; // крылья + Windows = windows; + Step = Speed * 100 / weight; + } +} diff --git a/OOP_Hard/src/FormAccordionBus.java b/OOP_Hard/src/FormAccordionBus.java new file mode 100644 index 0000000..aba3be0 --- /dev/null +++ b/OOP_Hard/src/FormAccordionBus.java @@ -0,0 +1,124 @@ +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 FormAccordionBus extends JFrame { + private String title; + private Dimension dimension; + private int Width, Height; + private CanvasAccordionBus canvasAccordionBus = new CanvasAccordionBus(); + private JButton CreateButton = new JButton("Create");; + private JButton UpButton = new JButton(); + private JButton DownButton = new JButton();; + private JButton LeftButton = new JButton();; + private JButton RightButton = new JButton(); + public FormAccordionBus(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\\images\\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 compartment = new Random().nextBoolean(); + boolean entrance = new Random().nextBoolean(); + boolean windows = new Random().nextBoolean(); + canvasAccordionBus._drawningAccordionBus = new DrawningAccordionBus(); + canvasAccordionBus._drawningAccordionBus.Init(speed, weight, bodyColor, additionalColor, compartment, entrance, windows); + canvasAccordionBus._drawningAccordionBus.SetPictureSize(Width, Height); + canvasAccordionBus._drawningAccordionBus.SetPosition( StartPositionX, StartPositionY); + canvasAccordionBus.repaint(); + } + }); + + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + if (canvasAccordionBus._drawningAccordionBus == null) return; + boolean result = false; + switch ((((JButton)(event.getSource())).getName())) { + case "UP": + result = canvasAccordionBus._drawningAccordionBus.MoveTransport(DirectionType.Up); + break; + case "DOWN": + result = canvasAccordionBus._drawningAccordionBus.MoveTransport(DirectionType.Down); + break; + case "LEFT": + result = canvasAccordionBus._drawningAccordionBus.MoveTransport(DirectionType.Left); + break; + case "RIGHT": + result = canvasAccordionBus._drawningAccordionBus.MoveTransport(DirectionType.Right); + break; + } + if (result) { + canvasAccordionBus.repaint(); + } + } + }; + UpButton.addActionListener(actionListener); + DownButton.addActionListener(actionListener); + LeftButton.addActionListener(actionListener); + RightButton.addActionListener(actionListener); + + setSize(dimension.width,dimension.height); + setLayout(null); + canvasAccordionBus.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(canvasAccordionBus); + setVisible(true); + //обработка события изменения размеров окна + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + Width = getWidth() - 15; + Height = getHeight() - 35; + if (canvasAccordionBus._drawningAccordionBus != null) + canvasAccordionBus._drawningAccordionBus.SetPictureSize(Width, Height); + canvasAccordionBus.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/OOP_Hard/src/Main.java b/OOP_Hard/src/Main.java index 930198c..b758c6e 100644 --- a/OOP_Hard/src/Main.java +++ b/OOP_Hard/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); - } + FormAccordionBus form = new FormAccordionBus("Автобус", new Dimension(500,300)); + form.Init(); } } \ No newline at end of file diff --git a/OOP_Hard/src/images/down.jpg b/OOP_Hard/src/images/down.jpg new file mode 100644 index 0000000..fc21b6a Binary files /dev/null and b/OOP_Hard/src/images/down.jpg differ diff --git a/OOP_Hard/src/images/left.jpg b/OOP_Hard/src/images/left.jpg new file mode 100644 index 0000000..453db80 Binary files /dev/null and b/OOP_Hard/src/images/left.jpg differ diff --git a/OOP_Hard/src/images/right.jpg b/OOP_Hard/src/images/right.jpg new file mode 100644 index 0000000..1f9989a Binary files /dev/null and b/OOP_Hard/src/images/right.jpg differ diff --git a/OOP_Hard/src/images/up.jpg b/OOP_Hard/src/images/up.jpg new file mode 100644 index 0000000..bfdaef6 Binary files /dev/null and b/OOP_Hard/src/images/up.jpg differ