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