From 8903bffad2b96051dd27fb248a4f7fb950945c85 Mon Sep 17 00:00:00 2001 From: Nastya_Kozlova Date: Sat, 23 Dec 2023 14:40:14 +0400 Subject: [PATCH] =?UTF-8?q?1=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirFighter/src/DirectionType.java | 6 ++ AirFighter/src/DrawingAirFighter.java | 138 ++++++++++++++++++++++++++ AirFighter/src/DrawingEngines.java | 26 +++++ AirFighter/src/EngineNumber.java | 5 + AirFighter/src/EntityAirFighter.java | 39 ++++++++ AirFighter/src/FrameAirFighter.java | 109 ++++++++++++++++++++ AirFighter/src/Main.java | 6 +- 7 files changed, 326 insertions(+), 3 deletions(-) create mode 100644 AirFighter/src/DirectionType.java create mode 100644 AirFighter/src/DrawingAirFighter.java create mode 100644 AirFighter/src/DrawingEngines.java create mode 100644 AirFighter/src/EngineNumber.java create mode 100644 AirFighter/src/EntityAirFighter.java create mode 100644 AirFighter/src/FrameAirFighter.java diff --git a/AirFighter/src/DirectionType.java b/AirFighter/src/DirectionType.java new file mode 100644 index 0000000..0d664bb --- /dev/null +++ b/AirFighter/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + UP, + DOWN, + LEFT, + RIGHT +} \ No newline at end of file diff --git a/AirFighter/src/DrawingAirFighter.java b/AirFighter/src/DrawingAirFighter.java new file mode 100644 index 0000000..c53444d --- /dev/null +++ b/AirFighter/src/DrawingAirFighter.java @@ -0,0 +1,138 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawingAirFighter extends JPanel { + private EntityAirFighter entityAirFighter; + public EntityAirFighter getEntityAirFighter(){ + return entityAirFighter; + } + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private final int _PlaneWidth = 160; + private final int _PlaneHeight = 160; + private DrawingEngines drawingEngines; + public boolean init(int speed, double weight, Color bodyColor, Color + additionalColor, boolean rockets, boolean wings, int width, int height, int enginesNumber) + { + if (_PlaneWidth > width || _PlaneHeight > height) + return false; + _pictureWidth = width; + _pictureHeight = height; + entityAirFighter = new EntityAirFighter(); + entityAirFighter.init(speed, weight, bodyColor, additionalColor, + rockets, wings); + drawingEngines = new DrawingEngines(); + drawingEngines.setNumber(enginesNumber); + return true; + } + + public void setPosition(int x, int y) + { + _startPosX = x; + _startPosY = y; + + if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _PlaneWidth) || _startPosY > (_pictureHeight - _PlaneHeight)) + { + _startPosX = 50; + _startPosY = 50; + } + } + public void moveTransport(DirectionType direction) + { + if (entityAirFighter == null) + return; + int step = entityAirFighter.step.get().intValue(); + switch (direction) + { + case LEFT: + if (_startPosX - step > 0) + _startPosX -= step; + break; + case UP: + if (_startPosY - step > 0) + _startPosY -= step; + break; + case RIGHT: + if (_startPosX + _PlaneWidth + step < _pictureWidth) + _startPosX += step; + break; + case DOWN: + if (_startPosY + _PlaneHeight + step < _pictureHeight) + _startPosY += step; + break; + } + } + public void drawTransport(Graphics gr) + { + super.paintComponent(gr); + Graphics2D g = (Graphics2D) gr; + if (entityAirFighter == null) + return; + BasicStroke pen = new BasicStroke(2); + Color bodyColor = entityAirFighter.getBodyColor(); + Color additionalColor = entityAirFighter.getAdditionalColor(); + g.setStroke(pen); + + + // тело + g.setColor(bodyColor); + int[] pointX = new int[]{ _startPosX, _startPosX+25, _startPosX+25}; + int[] pointY = new int[]{ _startPosY + 80, _startPosY+69, _startPosY+91}; + g.fillPolygon(pointX, pointY, 3); + g.setColor(Color.BLACK); + g.drawRect( _startPosX + 25, _startPosY + 70, 135, 20); + + //Крылья + g.setColor(Color.BLACK); + + pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80}; + pointY = new int[] { _startPosY+70, _startPosY, _startPosY, _startPosY+70}; + g.drawPolygon(pointX, pointY, 4); + + + pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80}; + pointY = new int[] { _startPosY+90, _startPosY+160, _startPosY+160, _startPosY+90}; + g.drawPolygon(pointX, pointY, 4); + + pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160}; + pointY = new int[] { _startPosY+70, _startPosY+50, _startPosY+30, _startPosY+70}; + g.drawPolygon(pointX, pointY, 4); + + + pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160}; + pointY = new int[] { _startPosY+90, _startPosY+110, _startPosY+130, _startPosY+90}; + g.drawPolygon(pointX, pointY, 4); + + + // топливо + if (entityAirFighter.getWings()) + { + g.setColor(additionalColor); + + pointX = new int[]{_startPosX + 100, _startPosX + 100, _startPosX + 110, _startPosX + 120}; + pointY = new int[]{_startPosY + 70, _startPosY+30, _startPosY+30, _startPosY + 70}; + g.fillPolygon(pointX, pointY, 4); + + pointX = new int[]{_startPosX + 100, _startPosX + 100, _startPosX + 110, _startPosX + 120}; + pointY = new int[]{_startPosY + 90, _startPosY + 130, _startPosY + 130, _startPosY + 90}; + g.fillPolygon(pointX, pointY, 4); + } + //ракеты + if (entityAirFighter.getRockets()) + { + g.setColor(additionalColor); + + pointX = new int[]{_startPosX+30, _startPosX+40, _startPosX+90, _startPosX+90, _startPosX+40}; + pointY = new int[]{_startPosY+25, _startPosY+20, _startPosY+20, _startPosY+30, _startPosY+30}; + g.fillPolygon(pointX, pointY, 5); + + pointX = new int[]{_startPosX+30, _startPosX+40, _startPosX+90, _startPosX+90, _startPosX+40}; + pointY = new int[]{_startPosY+135, _startPosY+130, _startPosY+130, _startPosY+140, _startPosY+140}; + g.fillPolygon(pointX, pointY, 5); + } + g.setColor(Color.BLACK); + drawingEngines.drawEngines(g, _startPosX, _startPosY); + } +} \ No newline at end of file diff --git a/AirFighter/src/DrawingEngines.java b/AirFighter/src/DrawingEngines.java new file mode 100644 index 0000000..1ded451 --- /dev/null +++ b/AirFighter/src/DrawingEngines.java @@ -0,0 +1,26 @@ +import java.awt.*; + +public class DrawingEngines { + private EngineNumber number; + public void setNumber(int x){ + if(x <= 2) + number = EngineNumber.TWO; + if(x == 4) + number = EngineNumber.FOUR; + if(x >= 6) + number = EngineNumber.SIX; + } + public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillRect(_startX+50, _startY+35, 10, 10); + graphics2D.fillRect(_startX+50, _startY+115, 10, 10); + + if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+50, _startY+50, 10, 10); + graphics2D.fillRect(_startX+50, _startY+100, 10, 10); + } + if (number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+90, _startY+45, 10, 10); + graphics2D.fillRect(_startX+90, _startY+105, 10, 10); + } + } +} \ No newline at end of file diff --git a/AirFighter/src/EngineNumber.java b/AirFighter/src/EngineNumber.java new file mode 100644 index 0000000..dea68cb --- /dev/null +++ b/AirFighter/src/EngineNumber.java @@ -0,0 +1,5 @@ +public enum EngineNumber { + TWO, + FOUR, + SIX +} \ No newline at end of file diff --git a/AirFighter/src/EntityAirFighter.java b/AirFighter/src/EntityAirFighter.java new file mode 100644 index 0000000..2bfa9aa --- /dev/null +++ b/AirFighter/src/EntityAirFighter.java @@ -0,0 +1,39 @@ +import java.awt.*; +import java.util.function.Supplier; + +public class EntityAirFighter { + private int speed; + public int getSpeed(){ + return speed; + } + private double weight; + public double getWeight(){ + return weight; + } + private Color bodyColor; + public Color getBodyColor(){ + return bodyColor; + } + private Color additionalColor; + public Color getAdditionalColor(){ + return additionalColor; + } + private boolean isWings; + public boolean getWings() { + return isWings; + } + private boolean isRockets; + public boolean getRockets() { + return isRockets; + } + public Supplier step = () -> (double) speed * 100 / weight; + public void init(int speed, double weight, Color bodyColor, Color + additionalColor, boolean isWings, boolean isRockets) { + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + this.additionalColor = additionalColor; + this.isWings = isWings; + this.isRockets = isRockets; + } +} \ No newline at end of file diff --git a/AirFighter/src/FrameAirFighter.java b/AirFighter/src/FrameAirFighter.java new file mode 100644 index 0000000..f48485e --- /dev/null +++ b/AirFighter/src/FrameAirFighter.java @@ -0,0 +1,109 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.io.File; +import java.io.IOException; +import java.util.Random; +public class FrameAirFighter extends JFrame { + private DrawingAirFighter drawingAirFighter; + private final JComponent pictureBox; + public FrameAirFighter() throws IOException { + super("Истребитель"); + setSize(new Dimension(900,500)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + pictureBox = new JComponent(){ + public void paintComponent(Graphics graphics){ + super.paintComponent(graphics); + Graphics2D graphics2D = (Graphics2D) graphics; + if (drawingAirFighter != null) drawingAirFighter.drawTransport(graphics2D); + super.repaint(); + } + }; + JButton createButton = new JButton("Создать"); + JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\right.png")))); + JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\left.png")))); + JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\up.png")))); + JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\down.png")))); + pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); + createButton.addActionListener(e -> buttonCreateClick()); + rightButton.setActionCommand("right"); + rightButton.addActionListener(this::buttonMoveClick); + leftButton.setActionCommand("left"); + leftButton.addActionListener(this::buttonMoveClick); + upButton.setActionCommand("up"); + upButton.addActionListener(this::buttonMoveClick); + downButton.setActionCommand("down"); + downButton.addActionListener(this::buttonMoveClick); + //component addition + setLayout(new BorderLayout()); + JPanel panelBattleship = new JPanel(new BorderLayout()); + JPanel createPanel = new JPanel(new BorderLayout()); + createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); + createPanel.add(createButton, BorderLayout.SOUTH); + JPanel movementPanel = new JPanel(new GridBagLayout()); + JPanel rightPanel = new JPanel(new BorderLayout()); + rightPanel.add(movementPanel, BorderLayout.SOUTH); + rightButton.setPreferredSize(new Dimension(30,30)); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.gridx = 2; + constraints.gridy = 1; + constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2; + movementPanel.add(rightButton, constraints); + leftButton.setPreferredSize(new Dimension(30,30)); + constraints.gridx = 0; + constraints.gridy = 1; + movementPanel.add(leftButton, constraints); + upButton.setPreferredSize(new Dimension(30,30)); + constraints.gridx = 1; + constraints.gridy = 0; + movementPanel.add(upButton, constraints); + downButton.setPreferredSize(new Dimension(30,30)); + constraints.gridx = 1; + constraints.gridy = 1; + movementPanel.add(downButton, constraints); + add(pictureBox); + panelBattleship.add(rightPanel, BorderLayout.EAST); + panelBattleship.add(createPanel, BorderLayout.WEST); + add(panelBattleship,BorderLayout.CENTER); + setVisible(true); + } + private void buttonCreateClick() { + Random random = new Random(); + drawingAirFighter = new DrawingAirFighter(); + pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + drawingAirFighter.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(), pictureBox.getWidth(), pictureBox.getHeight(), + (random.nextInt(3)+1)*2); + drawingAirFighter.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + + draw(); + } + private void buttonMoveClick(ActionEvent event) { + if(drawingAirFighter == null || drawingAirFighter.getEntityAirFighter() == null) + return; + switch (event.getActionCommand()) + { + case "left": + drawingAirFighter.moveTransport(DirectionType.LEFT); + break; + case "right": + drawingAirFighter.moveTransport(DirectionType.RIGHT); + break; + case "up": + drawingAirFighter.moveTransport(DirectionType.UP); + break; + case "down": + drawingAirFighter.moveTransport(DirectionType.DOWN); + break; + } + draw(); + } + private void draw() { + if (drawingAirFighter == null) + return; + pictureBox.repaint(); + } +} \ No newline at end of file diff --git a/AirFighter/src/Main.java b/AirFighter/src/Main.java index 52f889c..89e4b8f 100644 --- a/AirFighter/src/Main.java +++ b/AirFighter/src/Main.java @@ -1,5 +1,5 @@ +import java.io.IOException; + public class Main { - public static void main(String[] args) throws Exception { - System.out.println("Hello, World!"); - } + public static void main(String[] args) throws IOException { new FrameAirFighter(); } }