diff --git a/ProjectAntiAircraftGun/.idea/uiDesigner.xml b/ProjectAntiAircraftGun/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/ProjectAntiAircraftGun/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ProjectAntiAircraftGun/.idea/vcs.xml b/ProjectAntiAircraftGun/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ProjectAntiAircraftGun/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ProjectAntiAircraftGun/src/CanvasAntiAircraftGun.java b/ProjectAntiAircraftGun/src/CanvasAntiAircraftGun.java new file mode 100644 index 0000000..bbe4120 --- /dev/null +++ b/ProjectAntiAircraftGun/src/CanvasAntiAircraftGun.java @@ -0,0 +1,15 @@ +import javax.swing.*; +import java.awt.*; +public class CanvasAntiAircraftGun extends JComponent { + public DrawningAntiAircraftGun _drawingAntiAircraftGun; + public CanvasAntiAircraftGun(){} + public void paintComponent(Graphics g) { + if (_drawingAntiAircraftGun == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawingAntiAircraftGun.DrawTransport(g2d); + super.repaint(); + } +} diff --git a/ProjectAntiAircraftGun/src/CountRollers.java b/ProjectAntiAircraftGun/src/CountRollers.java new file mode 100644 index 0000000..1a04e48 --- /dev/null +++ b/ProjectAntiAircraftGun/src/CountRollers.java @@ -0,0 +1,5 @@ +public enum CountRollers { + Four, + Five, + Six; +} diff --git a/ProjectAntiAircraftGun/src/DirectionType.java b/ProjectAntiAircraftGun/src/DirectionType.java new file mode 100644 index 0000000..35657f0 --- /dev/null +++ b/ProjectAntiAircraftGun/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} diff --git a/ProjectAntiAircraftGun/src/DrawningAntiAircraftGun.java b/ProjectAntiAircraftGun/src/DrawningAntiAircraftGun.java new file mode 100644 index 0000000..4d68bfc --- /dev/null +++ b/ProjectAntiAircraftGun/src/DrawningAntiAircraftGun.java @@ -0,0 +1,143 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; +public class DrawningAntiAircraftGun extends JPanel{ + public EntityAntiAircraftGun EntityAntiAircraftGun; + public DrawningRollers _rollers = null; + private Integer picture_width; + private Integer picture_height; + private Integer _StartPosX; + private Integer _StartPosY; + private int drawningCarWidth = 185; + private int drawningCarHeight = 100; + public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean tower, boolean radar) { + EntityAntiAircraftGun = new EntityAntiAircraftGun(); + EntityAntiAircraftGun.Init(speed, weight, bodycolor, additionalcolor, tower, radar); + picture_width = null; + picture_height = null; + _StartPosX = null; + _StartPosY = null; + _rollers = new DrawningRollers(); + Random rand = new Random(); + int randomNum = rand.nextInt(4); + _rollers.SetCount(randomNum); + } + public boolean SetPictureSize(int width, int height) { + if (width < drawningCarWidth || height < drawningCarHeight) return false; + picture_width = width; + picture_height = height; + if (_StartPosX != null || _StartPosY != null) { + if (_StartPosX + drawningCarWidth > picture_width) + { + _StartPosX = _StartPosX - (_StartPosX + drawningCarWidth - picture_width); + } + else if (_StartPosX < 0) _StartPosX = 0; + if (_StartPosY + drawningCarHeight > picture_height) + { + _StartPosY = _StartPosY - (_StartPosY + drawningCarHeight - picture_height); + } + else if (_StartPosY < 0) _StartPosY = 0; + } + return true; + } + public void SetPosition(int x, int y) { + if (!(picture_width != null && picture_height != null)) return; + if (x + drawningCarWidth > picture_width) + { + _StartPosX = x - (x + drawningCarWidth - picture_width); + } + else if (x < 0) _StartPosX = 0; + else _StartPosX = x; + if (y + drawningCarHeight > picture_height) + { + _StartPosY = y - (y + drawningCarHeight - picture_height); + } + else if (y < 0) _StartPosY = 0; + else _StartPosY = y; + } + + public boolean MoveTransport(DirectionType direction) { + if (EntityAntiAircraftGun == null || _StartPosX == null || _StartPosY == null) return false; + switch (direction) { + case Left: + if (_StartPosX - EntityAntiAircraftGun.Step > 0) { + _StartPosX -= (int)EntityAntiAircraftGun.Step; + } + return true; + case Up: + if (_StartPosY - EntityAntiAircraftGun.Step > 0) + { + _StartPosY -= (int)EntityAntiAircraftGun.Step; + } + return true; + case Right: + if (_StartPosX + drawningCarWidth + (int)EntityAntiAircraftGun.Step < picture_width - EntityAntiAircraftGun.Step) + { + _StartPosX += (int)EntityAntiAircraftGun.Step; + } + return true; + case Down: + if (_StartPosY + drawningCarHeight + (int)EntityAntiAircraftGun.Step < picture_height - EntityAntiAircraftGun.Step) + { + _StartPosY += (int)EntityAntiAircraftGun.Step; + } + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics2D g) { + + if (EntityAntiAircraftGun == null) { + return; + } + // иллюминаторы + _rollers.Draw(g, _StartPosX+30, _StartPosY+34); + + g.setColor(Color.BLACK); + //Тело + g.drawRect(_StartPosX + 5, _StartPosY + 50, 170, 30); + g.drawArc(_StartPosX - 5, _StartPosY + 50, 20, 30, 90, 180); + //Заднее крыло + g.drawLine( _StartPosX, _StartPosY, _StartPosX + 50, _StartPosY + 50); + g.drawLine( _StartPosX, _StartPosY, _StartPosX, _StartPosY + 52); + //Заднее боковые крылья + g.drawOval(_StartPosX - 7, _StartPosY + 45, 30, 8); + g.fillOval(_StartPosX - 7, _StartPosY + 45, 30, 8); + //Нос + g.drawLine( _StartPosX + 175, _StartPosY + 50, _StartPosX + 200, _StartPosY + 65); + g.drawLine( _StartPosX + 200, _StartPosY + 65, _StartPosX + 175, _StartPosY + 80); + g.drawLine( _StartPosX + 175, _StartPosY + 50, _StartPosX + 175, _StartPosY + 80); + g.drawLine( _StartPosX + 175, _StartPosY + 65, _StartPosX + 200, _StartPosY + 65); + //Крылья + g.drawArc(_StartPosX + 49, _StartPosY + 62, 5, 5, 90, 180); + g.drawLine( _StartPosX + 51, _StartPosY + 62, _StartPosX + 140, _StartPosY + 62); + g.drawArc( _StartPosX + 137, _StartPosY + 62, 5, 5, 2790, 180); + g.drawLine( _StartPosX + 51, _StartPosY + 67, _StartPosX + 140, _StartPosY + 67); + //Задние шасси + g.drawLine(_StartPosX + 55, _StartPosY + 80, _StartPosX + 55, _StartPosY + 90); + g.drawOval(_StartPosX + 47, _StartPosY + 90, 5, 5); + g.drawOval( _StartPosX + 57, _StartPosY + 90, 5, 5); + //Передние шасси + g.drawLine( _StartPosX + 165, _StartPosY + 80, _StartPosX + 165, _StartPosY + 90); + g.drawOval( _StartPosX + 163, _StartPosY + 91, 5, 5); + //Пассажирсакий доп. отсек + if (EntityAntiAircraftGun.getIsTower()) + { + g.setColor(EntityAntiAircraftGun.getAdditionalColor()); + g.drawArc(_StartPosX + 60, _StartPosY + 28, 115, 45, 0, 180); + g.fillArc(_StartPosX + 60, _StartPosY + 28, 115, 45, 0, 180); + } + // Доп. двигатели + if (EntityAntiAircraftGun.getIsRadar()) + { + g.drawLine(_StartPosX + 95, _StartPosY + 68, _StartPosX + 95, _StartPosY + 75); + g.setColor(EntityAntiAircraftGun.getAdditionalColor()); + int[] xPolygon = { _StartPosX + 83, _StartPosX + 103, _StartPosX + 103, _StartPosX + 83, _StartPosX + 83}; + int[] yPolygon = {_StartPosY + 78, _StartPosY + 73, _StartPosY + 93, _StartPosY + 88, _StartPosY + 78}; + g.drawPolygon(xPolygon, yPolygon, xPolygon.length); + g.fillPolygon(xPolygon, yPolygon, xPolygon.length); + } + } +} diff --git a/ProjectAntiAircraftGun/src/DrawningRollers.java b/ProjectAntiAircraftGun/src/DrawningRollers.java new file mode 100644 index 0000000..4aa460b --- /dev/null +++ b/ProjectAntiAircraftGun/src/DrawningRollers.java @@ -0,0 +1,62 @@ +import java.awt.*; +public class DrawningRollers { + private CountRollers _rollers; + + public CountRollers getCount() { + return _rollers; + } + + public void SetCount(int count) { + switch (count) { + case 1: // 4 + _rollers = CountRollers.Four; + break; + case 2: // 5 + _rollers = CountRollers.Five; + break; + case 3: // 6 + _rollers = CountRollers.Six; + break; + default: + _rollers = CountRollers.Four; + break; + } + } + + public void Draw(Graphics2D g, int _startPosx, int _startPoxY) { + g.setColor(Color.BLACK); + if (_rollers == null) { + return; + } + + for (int i = 0; i < 10; ++i) { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 19 + i * 8, _startPoxY + 21, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 19 + i * 8, _startPoxY + 21, 3, 3); + } + + if (_rollers != CountRollers.Four) { + for (int i = 0; i < 5; ++i) { + g.setColor(Color.cyan); + g.fillOval(_startPosx - 15 + i * 5, _startPoxY + 28, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx - 15 + i * 5, _startPoxY + 28, 3, 3); + } + for (int i = 0; i < 5; ++i) { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 115 + i * 5, _startPoxY + 28, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 115 + i * 5, _startPoxY + 28, 3, 3); + } + } + if (_rollers == CountRollers.Six) { + for (int i = 0; i < 10; ++i) { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 19 + i * 8, _startPoxY + 37, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 19 + i * 8, _startPoxY + 37, 3, 3); + } + } + } +} diff --git a/ProjectAntiAircraftGun/src/EntityAntiAircraftGun.java b/ProjectAntiAircraftGun/src/EntityAntiAircraftGun.java new file mode 100644 index 0000000..cfe2bd6 --- /dev/null +++ b/ProjectAntiAircraftGun/src/EntityAntiAircraftGun.java @@ -0,0 +1,24 @@ +import java.awt.*; +public class EntityAntiAircraftGun { + private int Speed; + private double Weight; + private Color BodyColor; + public Color getBodyColor() {return BodyColor;} + private Color AdditionalColor; + public Color getAdditionalColor() {return AdditionalColor;} + private boolean IsTower; + public boolean getIsTower() {return IsTower;} + private boolean IsRadar; + public boolean getIsRadar() {return IsRadar;} + public double Step; + public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean isTower, boolean isRadar) + { + Speed = speed; + Weight = weight; + BodyColor = bodycolor; + AdditionalColor = additionalcolor; + IsTower = isTower; + IsRadar = isRadar; + Step = Speed * 100 / Weight; + } +} diff --git a/ProjectAntiAircraftGun/src/FormAntiAircraftGun.java b/ProjectAntiAircraftGun/src/FormAntiAircraftGun.java new file mode 100644 index 0000000..2d8df3f --- /dev/null +++ b/ProjectAntiAircraftGun/src/FormAntiAircraftGun.java @@ -0,0 +1,123 @@ +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 FormAntiAircraftGun extends JFrame { + private String title; + private Dimension dimension; + private int Width, Height; + private CanvasAntiAircraftGun canvasAntiAircraftGun = new CanvasAntiAircraftGun(); + 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 FormAntiAircraftGun(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\\up.jpg"); + UpButton.setIcon(iconUp); + UpButton.setName("UP"); + DownButton.setName("DOWN"); + Icon iconDown = new ImageIcon("src\\down.jpg"); + DownButton.setIcon(iconDown); + LeftButton.setName("LEFT"); + Icon iconLeft = new ImageIcon("src\\left.jpg"); + LeftButton.setIcon(iconLeft); + RightButton.setName("RIGHT"); + Icon iconRight = new ImageIcon("src\\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 tower = new Random().nextBoolean(); + boolean radar = new Random().nextBoolean();; + canvasAntiAircraftGun._drawingAntiAircraftGun = new DrawningAntiAircraftGun(); + canvasAntiAircraftGun._drawingAntiAircraftGun.Init(speed, weight, bodyColor, additionalColor, tower, radar); + canvasAntiAircraftGun._drawingAntiAircraftGun.SetPictureSize(Width, Height); + canvasAntiAircraftGun._drawingAntiAircraftGun.SetPosition( StartPositionX, StartPositionY); + canvasAntiAircraftGun.repaint(); + } + }); + + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + if (canvasAntiAircraftGun._drawingAntiAircraftGun == null) return; + boolean result = false; + switch ((((JButton)(event.getSource())).getName())) { + case "UP": + result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Up); + break; + case "DOWN": + result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Down); + break; + case "LEFT": + result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Left); + break; + case "RIGHT": + result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Right); + break; + } + if (result) { + canvasAntiAircraftGun.repaint(); + } + } + }; + UpButton.addActionListener(actionListener); + DownButton.addActionListener(actionListener); + LeftButton.addActionListener(actionListener); + RightButton.addActionListener(actionListener); + + setSize(dimension.width,dimension.height); + setLayout(null); + canvasAntiAircraftGun.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(canvasAntiAircraftGun); + setVisible(true); + //обработка события изменения размеров окна + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + Width = getWidth() - 15; + Height = getHeight() - 35; + if (canvasAntiAircraftGun._drawingAntiAircraftGun != null) + canvasAntiAircraftGun._drawingAntiAircraftGun.SetPictureSize(Width, Height); + canvasAntiAircraftGun.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/ProjectAntiAircraftGun/src/Main.java b/ProjectAntiAircraftGun/src/Main.java index 1e21156..347c8f1 100644 --- a/ProjectAntiAircraftGun/src/Main.java +++ b/ProjectAntiAircraftGun/src/Main.java @@ -1,5 +1,8 @@ +import java.awt.*; + public class Main { public static void main(String[] args) { - System.out.println("Hello and welcome!"); + FormAntiAircraftGun form = new FormAntiAircraftGun("Зенитная установка", new Dimension(500, 500)); + form.Init(); } } \ No newline at end of file