diff --git a/AirplaneWithRadar_Hard.iml b/AirplaneWithRadar_Hard.iml new file mode 100644 index 0000000..c8b28fa --- /dev/null +++ b/AirplaneWithRadar_Hard.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/AirplaneWithRadar/AirplaneWithRadarEntity.java b/src/AirplaneWithRadar/AirplaneWithRadarEntity.java new file mode 100644 index 0000000..4193487 --- /dev/null +++ b/src/AirplaneWithRadar/AirplaneWithRadarEntity.java @@ -0,0 +1,70 @@ +package AirplaneWithRadar; + +import java.awt.Color; +public class AirplaneWithRadarEntity +{ + public int Speed; + public double Weight; + public Color BodyColor; + public Color AdditColor; + public boolean RadarOnBoard; + public boolean AdditFuelPod; + public int getSpeed() { + return Speed; + } + private void setSpeed(int speed) { + Speed = speed; + } + + public double getWeight() { + return Weight; + } + + private void setWeight(double weight) { + Weight = weight; + } + + public Color getBodyColor() { + return BodyColor; + } + + private void setBodyColor(Color bodyColor) { + BodyColor = bodyColor; + } + + public Color getAdditColor() { + return AdditColor; + } + + private void setAdditColor(Color additColor) { + AdditColor = additColor; + } + + public boolean isRadarOnBoard() { + return RadarOnBoard; + } + + private void setRadarOnBoard(boolean radarOnBoard) { + RadarOnBoard = radarOnBoard; + } + + public boolean isAdditFuelPod() { + return AdditFuelPod; + } + + private void setAdditFuelPod(boolean additFuelPod) { + AdditFuelPod = additFuelPod; + } + + public double Step; + public void Init(int speed, double weight, Color bodyColor, Color additColor, boolean radarOnBoard, boolean additFuelPod) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditColor = additColor; + RadarOnBoard = radarOnBoard; + AdditFuelPod = additFuelPod; + Step = (double)Speed * 100 / Weight; + } +} diff --git a/src/AirplaneWithRadar/AirplaneWithRadarForm.java b/src/AirplaneWithRadar/AirplaneWithRadarForm.java new file mode 100644 index 0000000..d9d708d --- /dev/null +++ b/src/AirplaneWithRadar/AirplaneWithRadarForm.java @@ -0,0 +1,162 @@ +package AirplaneWithRadar; + +import javax.swing.*; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.image.BufferedImage; +import java.util.Random; +public class AirplaneWithRadarForm extends JFrame { + private PaintAirplaneWithRadar PaintPlanes; + + private void Draw() + { + if (PaintPlanes == null) + return; + + BufferedImage bmp = new BufferedImage( + AirplanePictureBox.getWidth(), + AirplanePictureBox.getHeight(), + BufferedImage.TYPE_INT_ARGB + ); + Graphics2D g = bmp.createGraphics(); + PaintPlanes.DrawTransport(g); + + AirplanePictureBox.setIcon(new ImageIcon(bmp)); + } + public AirplaneWithRadarForm() + { + InitializeComponent(); + + } + + private void InitializeComponent() + { + AirplanePictureBox = new JLabel(); + CreateButton = new JButton(); + ButtonRight = new JButton(); + ButtonDown = new JButton(); + ButtonLeft = new JButton(); + ButtonUp = new JButton(); + AirplanePictureBox.setBounds(0, 0, 884, 461); + // + // CreateButton + // + CreateButton.setName("CreateButton"); + CreateButton.setBounds(12, 419, 80, 30); + CreateButton.setText("Создать"); + CreateButton.setBackground(new Color(225, 225, 225)); + CreateButton.setFont(new Font("Segoe UI", Font.PLAIN, 11)); + CreateButton.setFocusable(false); + CreateButton.addActionListener(e -> ButtonCreate_Click(e)); + // + // ButtonRight + // + ButtonRight.setName("ButtonRight"); + ButtonRight.setBounds(842, 419, 30, 30); + ButtonRight.setBackground(new Color(225, 225, 225)); + ButtonRight.setFont(new Font("Segoe UI", Font.PLAIN, 11)); + ButtonRight.setFocusable(false); + ButtonRight.setIcon(new ImageIcon("src/AirplaneWithRadar/Resources/ArrowRight.png")); + ButtonRight.addActionListener(e -> ButtonMove_Click(e)); + // + // ButtonDown + // + ButtonDown.setName("ButtonDown"); + ButtonDown.setBounds(806, 419, 30, 30); + ButtonDown.setBackground(new Color(225, 225, 225)); + ButtonDown.setFont(new Font("Segoe UI", Font.PLAIN, 11)); + ButtonDown.setFocusable(false); + ButtonDown.setIcon(new ImageIcon("src/AirplaneWithRadar/Resources/ArrowDown.png")); + ButtonDown.addActionListener(e -> ButtonMove_Click(e)); + // + // ButtonLeft + // + ButtonLeft.setName("ButtonLeft"); + ButtonLeft.setBounds(770, 419, 30, 30); + ButtonLeft.setBackground(new Color(225, 225, 225)); + ButtonLeft.setFont(new Font("Segoe UI", Font.PLAIN, 11)); + ButtonLeft.setFocusable(false); + ButtonLeft.setIcon(new ImageIcon("src/AirplaneWithRadar/Resources/ArrowLeft.png")); + ButtonLeft.addActionListener(e -> ButtonMove_Click(e)); + // + // ButtonUp + // + ButtonUp.setName("ButtonUp"); + ButtonUp.setBounds(806, 383, 30, 30); + ButtonUp.setBackground(new Color(225, 225, 225)); + ButtonUp.setFont(new Font("Segoe UI", Font.PLAIN, 11)); + ButtonUp.setFocusable(false); + ButtonUp.setIcon(new ImageIcon("src/AirplaneWithRadar/Resources/ArrowUp.png")); + ButtonUp.addActionListener(e -> ButtonMove_Click(e)); + // + // AirplaneWithRadarForm + // + setTitle("Самолет с радаром"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(900, 500); + setLayout(null); + setLocationRelativeTo(null); + setVisible(true); + add(ButtonUp); + add(ButtonLeft); + add(ButtonDown); + add(ButtonRight); + add(CreateButton); + add(AirplanePictureBox); + } + + private JLabel AirplanePictureBox; + private JButton CreateButton; + private JButton ButtonRight; + private JButton ButtonDown; + private JButton ButtonLeft; + private JButton ButtonUp; + + private void ButtonMove_Click(ActionEvent e) + { + if (PaintPlanes == null) + return; + + String ButtonName = ((JButton)e.getSource()).getName(); + + switch (ButtonName) + { + case "ButtonUp": + PaintPlanes.Move(Movement.Up); + break; + + case "ButtonDown": + PaintPlanes.Move(Movement.Down); + break; + + case "ButtonLeft": + PaintPlanes.Move(Movement.Left); + break; + + case "ButtonRight": + PaintPlanes.Move(Movement.Right); + break; + } + + Draw(); + } + private void ButtonCreate_Click(ActionEvent e) + { + Random random = new Random(); + PaintPlanes = new PaintAirplaneWithRadar(); + + PaintPlanes.Init( + random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + true, + true, + AirplanePictureBox.getWidth(), + AirplanePictureBox.getHeight() + ); + PaintPlanes.SetPosition(random.nextInt(20, 100), random.nextInt(20, 100)); + Draw(); + } +} diff --git a/src/AirplaneWithRadar/IlluminatorsCount.java b/src/AirplaneWithRadar/IlluminatorsCount.java new file mode 100644 index 0000000..69717c0 --- /dev/null +++ b/src/AirplaneWithRadar/IlluminatorsCount.java @@ -0,0 +1,7 @@ +package AirplaneWithRadar; + +public enum IlluminatorsCount { + Ten, + Twenty, + Thirty +} diff --git a/src/AirplaneWithRadar/Movement.java b/src/AirplaneWithRadar/Movement.java new file mode 100644 index 0000000..7447b89 --- /dev/null +++ b/src/AirplaneWithRadar/Movement.java @@ -0,0 +1,7 @@ +package AirplaneWithRadar; +public enum Movement { + Up, + Down, + Left, + Right +} diff --git a/src/AirplaneWithRadar/PaintAirplaneWithRadar.java b/src/AirplaneWithRadar/PaintAirplaneWithRadar.java new file mode 100644 index 0000000..fc7a6a6 --- /dev/null +++ b/src/AirplaneWithRadar/PaintAirplaneWithRadar.java @@ -0,0 +1,157 @@ +package AirplaneWithRadar; +import java.awt.*; +import java.util.Random; +public class PaintAirplaneWithRadar { + private AirplaneWithRadarEntity airplaneWithRadarEntity; + private int pictWidth; + private int pictHeight; + private int startPosX; + private int startPosY; + private final int planeWidth = 140; + private final int planeHeight = 60; + public PaintIlluminators paintIlluminators; + + public boolean Init(int speed, double weight, Color mainColor, Color additColor, boolean radarOnBoard, boolean additFuelPod, int width, int height) + { + if (planeWidth > width || planeHeight > height) { return false; } + + pictHeight = height; + pictWidth = width; + airplaneWithRadarEntity = new AirplaneWithRadarEntity(); + airplaneWithRadarEntity.Init(speed, weight, mainColor, additColor, radarOnBoard, additFuelPod); + + paintIlluminators = new PaintIlluminators(); + + Random rnd = new Random(); + paintIlluminators.setCount(rnd.nextInt(6, 35)); + + return true; + } + + public void SetPosition(int x, int y) + { + if (x + planeWidth < pictWidth && y + planeHeight < pictHeight) + { + startPosX = x; + startPosY = y; + } + else + { + if (x > pictWidth) + { + while (x + planeWidth > pictWidth) + { + x -= 1; + } + } + if (y > pictWidth) + { + while (y + planeHeight > pictHeight) + { + y -= 1; + } + } + } + } + + public void Move(Movement dir) + { + if (airplaneWithRadarEntity == null) + { + return; + } + switch (dir) + { + case Left: + if (startPosX - airplaneWithRadarEntity.Step > 0) + { + startPosX -= (int) airplaneWithRadarEntity.Step; + } + break; + case Up: + if (startPosY - airplaneWithRadarEntity.Step > 0) + { + startPosY -= (int) airplaneWithRadarEntity.Step; + } + break; + case Right: + if (startPosX + planeWidth + airplaneWithRadarEntity.Step < pictWidth) + { + startPosX += (int)airplaneWithRadarEntity.Step; + } + break; + case Down: + if (startPosY + planeHeight + airplaneWithRadarEntity.Step < pictHeight) + { + startPosY += (int)airplaneWithRadarEntity.Step; + } + break; + } + } + public void DrawTransport(Graphics g) + { + if (airplaneWithRadarEntity == null) + { + return; + } + + //основа + g.setColor(Color.BLACK); + g.drawLine(startPosX + planeWidth / 44, startPosY + planeHeight / 2, startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight / 2); + g.drawLine(startPosX + planeWidth / 44, startPosY + planeHeight - planeHeight / 8, startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight - planeHeight / 8); + g.setColor(airplaneWithRadarEntity.getBodyColor()); + g.fillRect(startPosX + planeWidth / 44, startPosY + planeHeight / 2, planeWidth - 2* planeWidth/11, planeHeight / 2 - planeHeight / 8); + g.fillOval(startPosX, startPosY + planeHeight / 2, planeWidth / 24, planeHeight / 2 - planeHeight / 8); + g.setColor(Color.BLACK); + + g.drawArc(startPosX, startPosY + planeHeight / 2, planeWidth / 24, planeHeight/2 - planeHeight / 8, 90, 180); + + DrawTriangle(g, + startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight / 2, // 1 + startPosX + planeWidth, startPosY + 5 * planeHeight / 7, // 2 + startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight - planeHeight / 8); // 3 + + DrawTriangle(g, + startPosX, startPosY, // 1 + startPosX + planeWidth/4, startPosY + planeHeight/2, // 2 + startPosX, startPosY + planeHeight / 2); // 3 + g.drawLine(startPosX + planeWidth - planeWidth / 6, startPosY + 5 * planeHeight / 7, + startPosX + planeWidth, startPosY + 5 * planeHeight / 7); + // колеса + g.drawLine(startPosX + planeWidth / 4, startPosY + planeHeight - planeHeight / 8, + startPosX + planeWidth / 4, startPosY + planeHeight - planeHeight / 11); + g.drawLine(startPosX + planeWidth - 2 * planeWidth / 9, startPosY + planeHeight - planeHeight / 8, + startPosX + planeWidth - 2 * planeWidth / 9, startPosY + planeHeight - planeHeight / 11); + g.drawOval(startPosX + planeWidth / 4 - planeWidth / 23, startPosY + 11 * planeHeight / 12, + planeHeight / 12, planeHeight / 12); + g.drawOval(startPosX + planeWidth / 4 + planeWidth / 72, startPosY + 11 * planeHeight / 12, + planeHeight / 12, planeHeight / 12); + + g.drawOval(startPosX + planeWidth - 5 * planeWidth / 21, startPosY + 11 * planeHeight / 12, + planeHeight / 12, planeHeight / 12); + + g.setColor(Color.BLACK); + g.fillRoundRect(startPosX + 2*planeWidth / 9, startPosY + planeHeight / 2 + planeHeight / 7, planeWidth/2 ,planeHeight/7, 5, 5); + g.fillRoundRect(startPosX, startPosY + planeHeight / 2 - planeHeight/9, planeWidth / 5, planeHeight / 6, 5, 5); + + if (airplaneWithRadarEntity.RadarOnBoard) { + g.setColor(airplaneWithRadarEntity.getAdditColor()); + g.drawLine(startPosX + planeWidth / 3, startPosY + planeHeight / 2, startPosX + planeWidth / 3, startPosY + planeHeight / 3); + g.fillArc(startPosX + planeWidth / 4, startPosY + planeHeight / 10, planeWidth / 6, planeHeight / 4, -30, -180); + } + if (airplaneWithRadarEntity.AdditFuelPod) + { + g.setColor(airplaneWithRadarEntity.getAdditColor()); + g.fillRoundRect(startPosX, startPosY + planeHeight - 2*planeHeight / 5, planeWidth / 5, planeHeight / 6, 5, 5); + } + + paintIlluminators.drawIlluminators(g, airplaneWithRadarEntity.getAdditColor(), startPosX, startPosY); + } + + private void DrawTriangle(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3) + { + g.drawLine(x1, y1, x2, y2); + g.drawLine(x2, y2, x3, y3); + g.drawLine(x3, y3, x1, y1); + } +} diff --git a/src/AirplaneWithRadar/PaintIlluminators.java b/src/AirplaneWithRadar/PaintIlluminators.java new file mode 100644 index 0000000..3b72be2 --- /dev/null +++ b/src/AirplaneWithRadar/PaintIlluminators.java @@ -0,0 +1,45 @@ +package AirplaneWithRadar; + +import java.awt.Graphics; +import java.awt.Color; + +public class PaintIlluminators { + private IlluminatorsCount count; + public void setCount(int count) { + if (count <= 10) + this.count = IlluminatorsCount.Ten; + else if (count <= 20) { + this.count = IlluminatorsCount.Twenty; + } + else + this.count = IlluminatorsCount.Thirty; + } + public void drawIlluminators(Graphics g, Color color, int startPosX, int startPosY) { + g.setColor(color); + + int x = startPosX; + + for (int i = 0; i < 10; i++) { + g.fillOval(x + 34, startPosY + 30, 5, 5); + x += 7; + } + + if (count == IlluminatorsCount.Ten) return; + + x = startPosX; + + for (int i = 0; i < 10; i++) { + g.fillOval(x + 34, startPosY + 38, 5, 5); + x += 7; + } + + if (count == IlluminatorsCount.Twenty) return; + + x = startPosX; + + for (int i = 0; i < 10; i++) { + g.fillOval(x + 34, startPosY + 46, 5, 5); + x += 7; + } + } +} diff --git a/src/AirplaneWithRadar/Resources/ArrowDown.png b/src/AirplaneWithRadar/Resources/ArrowDown.png new file mode 100644 index 0000000..1a2d552 Binary files /dev/null and b/src/AirplaneWithRadar/Resources/ArrowDown.png differ diff --git a/src/AirplaneWithRadar/Resources/ArrowLeft.png b/src/AirplaneWithRadar/Resources/ArrowLeft.png new file mode 100644 index 0000000..c982fa6 Binary files /dev/null and b/src/AirplaneWithRadar/Resources/ArrowLeft.png differ diff --git a/src/AirplaneWithRadar/Resources/ArrowRight.png b/src/AirplaneWithRadar/Resources/ArrowRight.png new file mode 100644 index 0000000..a9df929 Binary files /dev/null and b/src/AirplaneWithRadar/Resources/ArrowRight.png differ diff --git a/src/AirplaneWithRadar/Resources/ArrowUp.png b/src/AirplaneWithRadar/Resources/ArrowUp.png new file mode 100644 index 0000000..45ca860 Binary files /dev/null and b/src/AirplaneWithRadar/Resources/ArrowUp.png differ diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..87f89d9 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,7 @@ +import AirplaneWithRadar.AirplaneWithRadarForm; + +public class Main { + public static void main(String[] args) { + new AirplaneWithRadarForm(); + } +} \ No newline at end of file