diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..002da1d --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Main.java \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..1acf042 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/images/down.png b/images/down.png new file mode 100644 index 0000000..dbc9177 Binary files /dev/null and b/images/down.png differ diff --git a/images/left.png b/images/left.png new file mode 100644 index 0000000..71d88ac Binary files /dev/null and b/images/left.png differ diff --git a/images/right.png b/images/right.png new file mode 100644 index 0000000..15838e0 Binary files /dev/null and b/images/right.png differ diff --git a/images/up.png b/images/up.png new file mode 100644 index 0000000..4b79487 Binary files /dev/null and b/images/up.png differ diff --git a/src/DirectionType.java b/src/DirectionType.java new file mode 100644 index 0000000..0d664bb --- /dev/null +++ b/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + UP, + DOWN, + LEFT, + RIGHT +} \ No newline at end of file diff --git a/src/DrawingAirBomber.java b/src/DrawingAirBomber.java new file mode 100644 index 0000000..5c06bf7 --- /dev/null +++ b/src/DrawingAirBomber.java @@ -0,0 +1,141 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawingAirBomber extends JPanel { + private EntityAirBomber entityAirBomber; + public EntityAirBomber getEntityAirBomber(){ + return entityAirBomber; + } + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private final int PLANE_WIDTH = 160; + private final int PLANE_HEIGHT = 160; + private DrawingEngines drawingEngines; + public boolean init(int speed, double weight, Color bodyColor, Color + additionalColor, boolean bombs, boolean fuel, int width, int height, int blocksNumber) + { + if (PLANE_WIDTH > width || PLANE_HEIGHT > height) + return false; + _pictureWidth = width; + _pictureHeight = height; + entityAirBomber = new EntityAirBomber(); + entityAirBomber.init(speed, weight, bodyColor, additionalColor, + bombs, fuel); + drawingEngines = new DrawingEngines(); + drawingEngines.setNumber(blocksNumber); + return true; + } + public void setPosition(int x, int y) + { + if (x < 0 || y < 0 || x + PLANE_WIDTH >= _pictureWidth || y + PLANE_HEIGHT >= _pictureHeight) + x = y = 2; + _startPosX = x; + _startPosY = y; + } + public void moveTransport(DirectionType direction) + { + if (entityAirBomber == null) + return; + switch (direction) + { + //влево + case LEFT: + if (_startPosX - entityAirBomber.step.get().intValue() > 0) + _startPosX -= entityAirBomber.step.get().intValue(); + break; + //вверх + case UP: + if (_startPosY - entityAirBomber.step.get().intValue() > 0) + _startPosY -= entityAirBomber.step.get().intValue(); + break; + // вправо + case RIGHT: + if (_startPosX + PLANE_WIDTH + entityAirBomber.step.get().intValue() < _pictureWidth) + _startPosX += entityAirBomber.step.get().intValue(); + break; + //вниз + case DOWN: + if (_startPosY + PLANE_HEIGHT + entityAirBomber.step.get().intValue() < _pictureHeight) + _startPosY += entityAirBomber.step.get().intValue(); + break; + } + } + public void drawTransport(Graphics gr) + { + super.paintComponent(gr); + Graphics2D g = (Graphics2D) gr; + if (entityAirBomber == null) + return; + BasicStroke pen = new BasicStroke(2); + Color penColor = Color.BLACK; + Color bodyColor = entityAirBomber.getBodyColor(); + Color additionalColor = entityAirBomber.getAdditionalColor(); + g.setStroke(pen); + g.setColor(bodyColor); + //фюзеляж + g.fillRect( _startPosX + 20, _startPosY + 70, 140, 20); + //кабина + int[] pointX = new int[]{ _startPosX, _startPosX+20, _startPosX+20}; + int[] pointY = new int[]{ _startPosY + 80, _startPosY+70, _startPosY+90}; + g.setColor(Color.BLUE); + g.fillPolygon(pointX, pointY, 3); + //границы самолета + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 3); + g.drawRect(_startPosX + 20, _startPosY + 70, 140, 20); + //Крылья + pointX = new int[] {_startPosX+70, _startPosX+70, _startPosX + 90, _startPosX + 100}; + pointY = new int[] { _startPosY+70, _startPosY, _startPosY, _startPosY+70}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + pointX = new int[] {_startPosX+70, _startPosX+70, _startPosX + 90, _startPosX + 100}; + pointY = new int[] { _startPosY+90, _startPosY+160, _startPosY+160, _startPosY+90}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + pointX = new int[] {_startPosX+130, _startPosX+130, _startPosX + 160, _startPosX + 160}; + pointY = new int[] { _startPosY+70, _startPosY+50, _startPosY+30, _startPosY+70}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + pointX = new int[] {_startPosX+130, _startPosX+130, _startPosX + 160, _startPosX + 160}; + pointY = new int[] { _startPosY+90, _startPosY+110, _startPosY+130, _startPosY+90}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + // топливо + if (entityAirBomber.getFuel()) + { + g.setColor(additionalColor); + g.fillOval(_startPosX + 60, _startPosY - 1, 40, 10); + g.fillOval(_startPosX + 60, _startPosY + 150, 40, 10); + g.setColor(penColor); + g.drawOval(_startPosX + 60, _startPosY - 1, 40, 10); + g.drawOval(_startPosX + 60, _startPosY + 150, 40, 10); + } + //бомбы + if (entityAirBomber.getBombs()) + { + pointX = new int[]{_startPosX+50, _startPosX+70, _startPosX+80, _startPosX+90, _startPosX+90, _startPosX+80, _startPosX+70, _startPosX+50}; + pointY = new int[]{_startPosY+75, _startPosY+75, _startPosY+80, _startPosY+75, _startPosY+85, _startPosY+80, _startPosY+85, _startPosY+85}; + g.setColor(additionalColor); + g.fillPolygon(pointX, pointY, 8); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 8); + pointX = new int[]{_startPosX+100, _startPosX+120, _startPosX+130, _startPosX+140, _startPosX+140, _startPosX+130, _startPosX+120, _startPosX+100}; + pointY = new int[]{_startPosY+75, _startPosY+75, _startPosY+80, _startPosY+75, _startPosY+85, _startPosY+80, _startPosY+85, _startPosY+85}; + g.setColor(additionalColor); + g.fillPolygon(pointX, pointY, 8); + g.setColor(penColor); + g.drawPolygon(pointX, pointY,8); + } + drawingEngines.drawBlocks(g, _startPosX, _startPosY); + } +} \ No newline at end of file diff --git a/src/DrawingEngines.java b/src/DrawingEngines.java new file mode 100644 index 0000000..51366dc --- /dev/null +++ b/src/DrawingEngines.java @@ -0,0 +1,31 @@ +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 drawBlocks(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillRect(_startX+70, _startY+20, 20, 15); + graphics2D.fillOval(_startX+80, _startY+20, 20, 15); + graphics2D.fillRect(_startX+70, _startY+125, 20, 15); + graphics2D.fillOval(_startX+80, _startY+125, 20, 15); + if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+70, _startY+40, 20, 15); + graphics2D.fillOval(_startX+80, _startY+40, 20, 15); + graphics2D.fillRect(_startX+70, _startY+105, 20, 15); + graphics2D.fillOval(_startX+80, _startY+105, 20, 15); + } + if (number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+130, _startY+50, 25, 15); + graphics2D.fillOval(_startX+145, _startY+50, 20, 15); + graphics2D.fillRect(_startX+130, _startY+95, 25, 15); + graphics2D.fillOval(_startX+145, _startY+95, 20, 15); + } + } +} \ No newline at end of file diff --git a/src/EngineNumber.java b/src/EngineNumber.java new file mode 100644 index 0000000..dea68cb --- /dev/null +++ b/src/EngineNumber.java @@ -0,0 +1,5 @@ +public enum EngineNumber { + TWO, + FOUR, + SIX +} \ No newline at end of file diff --git a/src/EntityAirBomber.java b/src/EntityAirBomber.java new file mode 100644 index 0000000..b4d7405 --- /dev/null +++ b/src/EntityAirBomber.java @@ -0,0 +1,39 @@ +import java.awt.*; +import java.util.function.Supplier; + +public class EntityAirBomber { + 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 isFuel; + public boolean getFuel() { + return isFuel; + } + private boolean isBombs; + public boolean getBombs() { + return isBombs; + } + public Supplier step = () -> (double) speed * 100 / weight; + public void init(int speed, double weight, Color bodyColor, Color + additionalColor, boolean isFuel, boolean isBombs) { + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + this.additionalColor = additionalColor; + this.isFuel = isFuel; + this.isBombs = isBombs; + } +} \ No newline at end of file diff --git a/src/FrameAirBomber.java b/src/FrameAirBomber.java new file mode 100644 index 0000000..bfe2a19 --- /dev/null +++ b/src/FrameAirBomber.java @@ -0,0 +1,107 @@ +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 FrameAirBomber extends JFrame { + private DrawingAirBomber drawingAirBomber; + private final JComponent pictureBox; + public FrameAirBomber() throws IOException { + super("Бомбардировщик"); + setSize(new Dimension(900,500)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //components initialisation + pictureBox = new JComponent(){ + public void paintComponent(Graphics graphics){ + super.paintComponent(graphics); + Graphics2D graphics2D = (Graphics2D) graphics; + if (drawingAirBomber != null) drawingAirBomber.drawTransport(graphics2D); + super.repaint(); + } + }; + JButton createButton = new JButton("Создать"); + JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png")))); + JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png")))); + JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png")))); + JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png")))); + pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); + //ActionListeners and ActionCommand addition + 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(); + drawingAirBomber = new DrawingAirBomber(); + pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + drawingAirBomber.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); + drawingAirBomber.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + draw(); + } + private void buttonMoveClick(ActionEvent event) { + if(drawingAirBomber == null || drawingAirBomber.getEntityAirBomber() == null) + return; + switch (event.getActionCommand()) + { + case "left": + drawingAirBomber.moveTransport(DirectionType.LEFT); + break; + case "right": + drawingAirBomber.moveTransport(DirectionType.RIGHT); + break; + case "up": + drawingAirBomber.moveTransport(DirectionType.UP); + break; + case "down": + drawingAirBomber.moveTransport(DirectionType.DOWN); + break; + } + draw(); + } + private void draw() { + if (drawingAirBomber == null) + return; + pictureBox.repaint(); + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index db0fcda..b191374 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ -public class Main { - public static void main(String[] args){ +import java.io.IOException; - } +public class Main { + public static void main(String[] args) throws IOException { new FrameAirBomber(); } }