diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/PIBD-12_Bugrov_D.A._Hard.iml b/.idea/PIBD-12_Bugrov_D.A._Hard.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/PIBD-12_Bugrov_D.A._Hard.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..2928d94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c343bca --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ProjectElectricLocomotive/.gitignore b/ProjectElectricLocomotive/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/ProjectElectricLocomotive/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/ProjectElectricLocomotive/.idea/.gitignore b/ProjectElectricLocomotive/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/ProjectElectricLocomotive/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/ProjectElectricLocomotive/.idea/misc.xml b/ProjectElectricLocomotive/.idea/misc.xml new file mode 100644 index 0000000..f88369a --- /dev/null +++ b/ProjectElectricLocomotive/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ProjectElectricLocomotive/.idea/modules.xml b/ProjectElectricLocomotive/.idea/modules.xml new file mode 100644 index 0000000..3f7836a --- /dev/null +++ b/ProjectElectricLocomotive/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/ProjectElectricLocomotive/.idea/uiDesigner.xml b/ProjectElectricLocomotive/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/ProjectElectricLocomotive/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive.iml b/ProjectElectricLocomotive/ProjectElectricLocomotive.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/ProjectElectricLocomotive/src/AdditionalClass.java b/ProjectElectricLocomotive/src/AdditionalClass.java new file mode 100644 index 0000000..230b178 --- /dev/null +++ b/ProjectElectricLocomotive/src/AdditionalClass.java @@ -0,0 +1,56 @@ +import java.awt.*; + +public class AdditionalClass { + private AdditionalType _additionalType; + + public AdditionalType getAdditionalType() + { + return _additionalType; + } + public void setAdditonalType(int value) + { + switch (value) + { + case 1: + _additionalType = AdditionalType.ONE; + break; + case 2: + _additionalType = AdditionalType.TWO; + break; + case 3: + _additionalType = AdditionalType.THREE; + break; + case 4: + _additionalType = AdditionalType.FOURTH; + break; + default: + _additionalType = AdditionalType.ONE; + break; + } + } + + public void drawingWheels(Graphics g, Double startPosX, Double startPosY, Color wheelsColor) + { + Graphics2D g2d = (Graphics2D) g; + + + g.setColor(wheelsColor); + + switch (_additionalType) + { + case FOURTH: + g.fillOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 10, 10); + g.drawOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 8, 8); + case THREE: + g.fillOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 10, 10); + g.drawOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 8, 8); + case TWO: + g.fillOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 10, 10); + g.drawOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 8, 8); + case ONE: + g.fillOval(startPosX.intValue() + 9, startPosY.intValue() + 37, 10, 10); + g.drawOval(startPosX.intValue() + 9, startPosY.intValue() + 37, 8, 8); + break; + } + } +} diff --git a/ProjectElectricLocomotive/src/AdditionalType.java b/ProjectElectricLocomotive/src/AdditionalType.java new file mode 100644 index 0000000..c786aab --- /dev/null +++ b/ProjectElectricLocomotive/src/AdditionalType.java @@ -0,0 +1,6 @@ +public enum AdditionalType { + ONE, + TWO, + THREE, + FOURTH; +} diff --git a/ProjectElectricLocomotive/src/DirectionType.java b/ProjectElectricLocomotive/src/DirectionType.java new file mode 100644 index 0000000..e9b856e --- /dev/null +++ b/ProjectElectricLocomotive/src/DirectionType.java @@ -0,0 +1,9 @@ +public enum DirectionType { + Up, + + Down, + + Left, + + Right, +} diff --git a/ProjectElectricLocomotive/src/DrawningElectricLocomotive.java b/ProjectElectricLocomotive/src/DrawningElectricLocomotive.java new file mode 100644 index 0000000..879cbd2 --- /dev/null +++ b/ProjectElectricLocomotive/src/DrawningElectricLocomotive.java @@ -0,0 +1,231 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; + +public class DrawningElectricLocomotive { + + public EntityElectricLocomotive EntityElectricLocomotive; + + private Double pictureWidth; + private Double pictureHeight; + private Double startPosX; + private Double startPosY; + + + + public AdditionalClass _additionalClass; + + private final int drawningElectricLocomotiveWidth = 83; + private final int drawningElectricLocomotiveHeight = 45; + + public void init(int speed, double weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement, int wheelsCount) { + EntityElectricLocomotive = new EntityElectricLocomotive(); + _additionalClass = new AdditionalClass(); + _additionalClass.setAdditonalType(wheelsCount); + EntityElectricLocomotive.init(speed, weight, bodyColor, additionalColor, electricHorns, batteryPlacement); + pictureWidth = null; + pictureHeight = null; + startPosX = null; + startPosY = null; + } + + public boolean setPictureSize(int width, int height) { + if (drawningElectricLocomotiveHeight > height || drawningElectricLocomotiveWidth > width) { + return false; + } + pictureWidth = (double)width; + pictureHeight = (double)height; + if (startPosY != null && startPosY + drawningElectricLocomotiveHeight < pictureHeight) { + startPosY = pictureHeight - drawningElectricLocomotiveHeight; + } + if (startPosX != null && startPosX + drawningElectricLocomotiveWidth < pictureWidth) { + startPosX = pictureWidth - drawningElectricLocomotiveWidth; + } + return true; + } + + public void setPosition(int x, int y) { + if (pictureHeight == null || pictureWidth == null) { + return; + } + + startPosX = (double)x; + startPosY = (double)y; + + if (drawningElectricLocomotiveHeight + y > pictureHeight) { + startPosY = pictureHeight - drawningElectricLocomotiveHeight; + } + if (drawningElectricLocomotiveWidth + x > pictureWidth) { + startPosX = pictureWidth - drawningElectricLocomotiveWidth; + } + if (x < 0) { + startPosX = (double)0; + } + if (y < 0) { + startPosY = (double)0; + } + } + + public boolean moveTransport(DirectionType direction) { + if (EntityElectricLocomotive == null || startPosX == null || startPosY == null) { + return false; + } + + switch (direction) { + case Left: + if (startPosX - EntityElectricLocomotive.getStep() > 0) { + startPosX -= EntityElectricLocomotive.getStep(); + } + return true; + case Up: + if (startPosY - EntityElectricLocomotive.getStep() > 0) { + startPosY -= EntityElectricLocomotive.getStep(); + } + return true; + case Right: + if (startPosX + drawningElectricLocomotiveWidth + EntityElectricLocomotive.getStep() < pictureWidth) { + startPosX += EntityElectricLocomotive.getStep(); + } + return true; + case Down: + if (startPosY + drawningElectricLocomotiveHeight + EntityElectricLocomotive.getStep() < pictureHeight) { + startPosY += EntityElectricLocomotive.getStep(); + } + return true; + default: + return false; + } + } + + public Image getImage() { + // Создаем новое изображение с заданными размерами + BufferedImage image = new BufferedImage(pictureWidth.intValue(), pictureHeight.intValue(), BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = image.createGraphics(); + + // Вызываем метод прорисовки нашего объекта DrawningElectricLocomotive + drawTransport(g2d); + + _additionalClass.drawingWheels(g2d, startPosX, startPosY, EntityElectricLocomotive.getAdditionalColor()); + + // Освобождаем ресурсы графического контекста + g2d.dispose(); + + return image; + } + + public void drawTransport(Graphics g) { + if (EntityElectricLocomotive == null || startPosX == null || startPosY == null) { + return; + } + + // Создание перьев и кистей для прорисовки электровоза + Color blackColor = Color.BLACK; + Color deepSkyBlueColor = new Color(0, 191, 255); + Color yellowColor = Color.YELLOW; + + Graphics2D g2d = (Graphics2D) g; + + // Инициализация опорных точек для прорисовки корпуса + int pointStartX = startPosX.intValue() + 75; + int pointStartY = startPosY.intValue() + 20; + + Point pointStart = new Point(pointStartX, pointStartY); + Point point1 = new Point(pointStartX, pointStartY - 15); + Point point2 = new Point(startPosX.intValue() + 10, pointStartY - 15); + Point pointFinish = new Point(startPosX.intValue() + 5, pointStartY); + + // Инициализация опорных точек для прорисовки первой "юбки" + Point point3 = new Point(startPosX.intValue() + 7, startPosY.intValue() + 37); + Point point4 = new Point(startPosX.intValue(), startPosY.intValue() + 43); + Point point5 = new Point(startPosX.intValue() + 7, startPosY.intValue() + 43); + + // Инициализация опорных точек для прорисовки второй "юбки" + Point point6 = new Point(startPosX.intValue() + 69, startPosY.intValue() + 37); + Point point7 = new Point(startPosX.intValue() + 82, startPosY.intValue() + 43); + Point point8 = new Point(startPosX.intValue() + 69, startPosY.intValue() + 43); + + // Совокупность точек полигона уголка первой "юбки" + Point[] firstTrianglePoints = { point3, point4, point5 }; + + // Совокупность точек полигона уголка второй "юбки" + Point[] secondTrianglePoints = { point6, point7, point8 }; + + // Прорисовка уголков "юбок" + g.setColor(blackColor); + g.fillPolygon(new Polygon(new int[]{ point3.x, point4.x, point5.x }, new int[]{ point3.y, point4.y, point5.y }, 3)); + g.fillPolygon(new Polygon(new int[]{ point6.x, point7.x, point8.x }, new int[]{ point6.y, point7.y, point8.y }, 3)); + + // Прорисовка корпуса + g.drawRect(startPosX.intValue() + 5, startPosY.intValue() + 20, 70, 17); + g.drawLine(point3.x, point3.y, point4.x, point4.y); + g.drawLine(pointStart.x, pointStart.y, point1.x, point1.y); + g.drawLine(point1.x, point1.y, point2.x, point2.y); + g.drawLine(point2.x, point2.y, pointFinish.x, pointFinish.y); + + // Прорисовка окон + g.setColor(deepSkyBlueColor); + g.drawRect(startPosX.intValue() + 12, startPosY.intValue() + 9, 8, 8); + g.drawRect(startPosX.intValue() + 63, startPosY.intValue() + 9, 8, 8); + + // Прорисовка двух передних колёс + + //g.fillOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 10, 10); + //g.setColor(blackColor); + + //g.drawOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 8, 8); + + // Прорисовка двух задних колёс + //g.setColor(Color.YELLOW); + //g.fillOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 10, 10); + //g.fillOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 10, 10); + g.setColor(blackColor); + //g.drawOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 8, 8); + //g.drawOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 8, 8); + + // Прорисовка заднего "шлюза" + g.fillRect(startPosX.intValue() + 75, startPosY.intValue() + 9, 6, 27); + + + + + + + + + if (EntityElectricLocomotive.isElectricHorns()) { + // Инициализация опорных точек для прорисовки "рогов" + Point pointHorns1 = new Point(startPosX.intValue() + 13, startPosY.intValue() + 5); + Point pointHorns2 = new Point(startPosX.intValue() + 16, startPosY.intValue() + 2); + Point pointHorns3 = new Point(startPosX.intValue() + 11, startPosY.intValue()); + + g.setColor(EntityElectricLocomotive.getAdditionalColor()); + + // Прорисовка "рогов" электровоза + g.drawLine(pointHorns1.x, pointHorns1.y, pointHorns2.x, pointHorns2.y); + g.drawLine(pointHorns2.x, pointHorns2.y, pointHorns3.x, pointHorns3.y); + + g.setColor(blackColor); + } + + if (EntityElectricLocomotive.isBatteryPlacement()) { + // Инициализация опорных точек для прорисовки молнии на хранилище батарей + Point pointLightning1 = new Point(startPosX.intValue() + 39, startPosY.intValue() + 37); + Point pointLightning2 = new Point(startPosX.intValue() + 37, startPosY.intValue() + 39); + Point pointLightning3 = new Point(startPosX.intValue() + 39, startPosY.intValue() + 40); + Point pointLightning4 = new Point(startPosX.intValue() + 37, startPosY.intValue() + 41); + + g.setColor(EntityElectricLocomotive.getAdditionalColor()); + + // Прорисовка "хранилища батарей" электровоза + + g.fillRect(startPosX.intValue() + 36, startPosY.intValue() + 37, 8, 4); + g.setColor(yellowColor); + g.drawLine(pointLightning1.x, pointLightning1.y, pointLightning2.x, pointLightning2.y); + g.drawLine(pointLightning2.x, pointLightning2.y, pointLightning3.x, pointLightning3.y); + g.drawLine(pointLightning3.x, pointLightning3.y, pointLightning4.x, pointLightning4.y); + + g.setColor(blackColor); + } + } + +} diff --git a/ProjectElectricLocomotive/src/EntityElectricLocomotive.java b/ProjectElectricLocomotive/src/EntityElectricLocomotive.java new file mode 100644 index 0000000..60fa485 --- /dev/null +++ b/ProjectElectricLocomotive/src/EntityElectricLocomotive.java @@ -0,0 +1,117 @@ + +import java.awt.Color; + +/** + * Класс-сущность "Тепловоз" + */ +public class EntityElectricLocomotive { + + /** + * Скорость + */ + private int speed; + + private int wheelsCount; + + /** + * Вес + */ + private double weight; + + /** + * Основной цвет + */ + private Color bodyColor; + + /** + * Дополнительный цвет (для дополнительных элементов) + */ + private Color additionalColor; + + /** + * Признак (опция) наличия "корпус" + */ + private boolean electricHorns; + + /** + * Признак (опция) наличия места под батарею + */ + private boolean batteryPlacement; + + /** + * Инициализация полей объекта-класса тепловоза + * + * @param speed Скорость + * @param weight Вес + * @param bodyColor Основной цвет + * @param additionalColor Дополнительный цвет + * @param electricHorns Наличие "корпуса" + * @param batteryPlacement Наличие места под батарею + */ + public void init(int speed, double weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement) { + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + this.additionalColor = additionalColor; + this.electricHorns = electricHorns; + this.batteryPlacement = batteryPlacement; + } + + /** + * Получение шага перемещения объекта + * + * @return Шаг перемещения + */ + public double getStep() { + return speed * 100 / weight; + } + + // Геттеры и сеттеры для приватных полей + public int getSpeed() { + return speed; + } + + public void setSpeed(int speed) { + this.speed = speed; + } + + public double getWeight() { + return weight; + } + + public void setWeight(double weight) { + this.weight = weight; + } + + public Color getBodyColor() { + return bodyColor; + } + + public void setBodyColor(Color bodyColor) { + this.bodyColor = bodyColor; + } + + public Color getAdditionalColor() { + return additionalColor; + } + + public void setAdditionalColor(Color additionalColor) { + this.additionalColor = additionalColor; + } + + public boolean isElectricHorns() { + return electricHorns; + } + + public void setElectricHorns(boolean electricHorns) { + this.electricHorns = electricHorns; + } + + public boolean isBatteryPlacement() { + return batteryPlacement; + } + + public void setBatteryPlacement(boolean batteryPlacement) { + this.batteryPlacement = batteryPlacement; + } +} diff --git a/ProjectElectricLocomotive/src/FormElectricLocomotive.form b/ProjectElectricLocomotive/src/FormElectricLocomotive.form new file mode 100644 index 0000000..39b18a6 --- /dev/null +++ b/ProjectElectricLocomotive/src/FormElectricLocomotive.form @@ -0,0 +1,62 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectElectricLocomotive/src/FormElectricLocomotive.java b/ProjectElectricLocomotive/src/FormElectricLocomotive.java new file mode 100644 index 0000000..36b96c7 --- /dev/null +++ b/ProjectElectricLocomotive/src/FormElectricLocomotive.java @@ -0,0 +1,121 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormElectricLocomotive extends JFrame { + + private DrawningElectricLocomotive _drawningElectricLocomotive; + + private JPanel panel; + private JButton buttonCreate; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + private JLabel pictureBoxElectricLocomotive; + + public FormElectricLocomotive() { + initComponents(); + } + + private void initComponents() { + setTitle("Electric Locomotive"); + setSize(520, 300); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + panel = new JPanel(); + buttonCreate = new JButton("Create"); + buttonUp = new JButton("Up"); + buttonDown = new JButton("Down"); + buttonLeft = new JButton("Left"); + buttonRight = new JButton("Right"); + pictureBoxElectricLocomotive = new JLabel(); + + panel.setLayout(new FlowLayout()); + panel.add(buttonCreate); + panel.add(buttonUp); + panel.add(buttonDown); + panel.add(buttonLeft); + panel.add(buttonRight); + add(panel, BorderLayout.NORTH); + add(pictureBoxElectricLocomotive, BorderLayout.CENTER); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + createElectricLocomotive(); + } + }); + + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + moveTransport(DirectionType.Up); + } + }); + + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + moveTransport(DirectionType.Down); + } + }); + + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + moveTransport(DirectionType.Left); + } + }); + + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + moveTransport(DirectionType.Right); + } + }); + } + + private void createElectricLocomotive() { + Random random = new Random(); + _drawningElectricLocomotive = new DrawningElectricLocomotive(); + _drawningElectricLocomotive.init( + random.nextInt(200) + 100, + random.nextInt(2000) + 1000, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + random.nextBoolean(), + random.nextBoolean(), + random.nextInt(0,5) + ); + _drawningElectricLocomotive.setPictureSize(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight()); + _drawningElectricLocomotive.setPosition(random.nextInt(pictureBoxElectricLocomotive.getWidth()), random.nextInt(pictureBoxElectricLocomotive.getHeight())); + + draw(); + } + + private void moveTransport(DirectionType direction) { + if (_drawningElectricLocomotive != null) { + if (_drawningElectricLocomotive.moveTransport(direction)) { + draw(); + } + } + } + + private void draw() { + if (_drawningElectricLocomotive != null) { + Image image = new ImageIcon(_drawningElectricLocomotive.getImage()).getImage(); + Image scaledImage = image.getScaledInstance(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight(), Image.SCALE_SMOOTH); + pictureBoxElectricLocomotive.setIcon(new ImageIcon(scaledImage)); + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + FormElectricLocomotive form = new FormElectricLocomotive(); + form.setVisible(true); + }); + } +}