diff --git a/src/ProjectTankHard/DrawningObjects/DrawningTank.java b/src/ProjectTankHard/DrawningObjects/DrawningTank.java index 50aee85..5fe4291 100644 --- a/src/ProjectTankHard/DrawningObjects/DrawningTank.java +++ b/src/ProjectTankHard/DrawningObjects/DrawningTank.java @@ -39,7 +39,7 @@ public class DrawningTank extends DrawningTankBase { g2d.drawPolygon(xLeft, yLeft, xLeft.length); int[] xRight = {_startPosX + 59, _startPosX + 74, _startPosX + 74 + 5, _startPosX + 66}; - int[] yRight = {_startPosY, _startPosY + 16, _startPosY + 16, _startPosY}; + int[] yRight = {_startPosY + 2, _startPosY + 16, _startPosY + 16, _startPosY + 2}; g2d.drawPolygon(xRight, yRight, xRight.length); } diff --git a/src/ProjectTankHard/DrawningObjects/DrawningTankBase.java b/src/ProjectTankHard/DrawningObjects/DrawningTankBase.java index 49fc284..98e31c8 100644 --- a/src/ProjectTankHard/DrawningObjects/DrawningTankBase.java +++ b/src/ProjectTankHard/DrawningObjects/DrawningTankBase.java @@ -2,6 +2,8 @@ package ProjectTankHard.DrawningObjects; import ProjectTankHard.DirectionType; import ProjectTankHard.Entities.EntityTankBase; +import ProjectTankHard.MovementStrategy.DrawningObjectTank; +import ProjectTankHard.MovementStrategy.IMoveableObject; import java.awt.*; import java.awt.geom.RoundRectangle2D; @@ -9,8 +11,8 @@ import java.util.Random; public class DrawningTankBase { protected EntityTankBase EntityTankBase; - private int _pictureWidth; - private int _pictureHeight; + public int _pictureWidth; + public int _pictureHeight; protected int _startPosX = 0; protected int _startPosY = 0; private int _tankWidth = 150; @@ -19,6 +21,7 @@ public class DrawningTankBase { public EntityTankBase EntityTankBase(){ return EntityTankBase; } + public IMoveableObject GetMoveableObject(){ return new DrawningObjectTank(this); } public DrawningTankBase(int speed, double weight, Color bodyColor, int width, int height){ if(width <= _tankWidth || height <= _tankHeight) return; @@ -117,7 +120,7 @@ public class DrawningTankBase { g2d.setColor(EntityTankBase.BodyColor()); // гусеница - RoundRectangle2D caterpillars = new RoundRectangle2D.Double(_startPosX, _startPosY + 65, _tankWidth, 35, 20, 20); + RoundRectangle2D caterpillars = new RoundRectangle2D.Double(_startPosX + 2, _startPosY + 65, _tankWidth - 4, 33, 20, 20); g2d.draw(caterpillars); // колеса diff --git a/src/ProjectTankHard/Entities/EntityTankBase.java b/src/ProjectTankHard/Entities/EntityTankBase.java index 3ef2ddb..e806e25 100644 --- a/src/ProjectTankHard/Entities/EntityTankBase.java +++ b/src/ProjectTankHard/Entities/EntityTankBase.java @@ -6,6 +6,8 @@ public class EntityTankBase { private int Speed; private double Weight, Step; private Color BodyColor; + public double Weight(){ return Weight; } + public int Speed(){ return Speed; } public double Step(){ return Step; } diff --git a/src/ProjectTankHard/FormHard.java b/src/ProjectTankHard/FormHard.java new file mode 100644 index 0000000..a5615ff --- /dev/null +++ b/src/ProjectTankHard/FormHard.java @@ -0,0 +1,77 @@ +package ProjectTankHard; + +import ProjectTankHard.DrawningObjects.DrawningTankBase; +import ProjectTankHard.DrawningObjects.DrawningWheels; +import ProjectTankHard.DrawningObjects.IDraw; +import ProjectTankHard.Entities.EntityTankBase; +import ProjectTankHard.Generics.HardGeneric; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormHard { + private Canvas canv; + private int pictureBoxWidth; + private int pictureBoxHeight; + private EntityTankBase createEntity(){ + Random random = new Random(); + return new EntityTankBase(random.nextInt(100, 300), + random.nextDouble(1000, 3000), + Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301))); + } + void Draw(){ + if(canv == null) return; + canv.repaint(); + } + private IDraw makeIDraw(){ + Random random = new Random(); + return new DrawningWheels(pictureBoxWidth, pictureBoxHeight, 0, 0, + Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301))); + } + public FormHard(){ + Random rand = new Random(); + int hardGenericSize = rand.nextInt(1, 10); + + JFrame HardFrame = new JFrame(); + HardFrame.setSize(700, 400); + HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + + canv = new Canvas(); + canv.setBounds(0,0,pictureBoxWidth,pictureBoxHeight); + + JButton MakeObjectButton = new JButton("Создать"); + MakeObjectButton.setBounds(0, 0, 100, 40); + + HardFrame.setContentPane(canv); + HardFrame.setVisible(true); + + canv.add(MakeObjectButton); + pictureBoxHeight = canv.getHeight(); + pictureBoxWidth = canv.getWidth(); + + HardGeneric hardGeneric = new HardGeneric<>(hardGenericSize, hardGenericSize, pictureBoxWidth, pictureBoxHeight); + + for(int i = 0; i < hardGenericSize; i++){ + EntityTankBase entity = createEntity(); + hardGeneric.InsertFirst(entity); + hardGeneric.InsertSecond(makeIDraw()); + } + + MakeObjectButton.addActionListener( + new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + DrawningTankBase DrawningTankBase = hardGeneric.MakeObject(); + DrawningTankBase.SetPosition(pictureBoxWidth / 2 - DrawningTankBase.GetWidth()/2, + pictureBoxHeight / 2 - DrawningTankBase.GetHeight()/2); + canv.DrawningTankBase = DrawningTankBase; + Draw(); + } + } + ); + } +} + diff --git a/src/ProjectTankHard/FormTank.java b/src/ProjectTankHard/FormTank.java index b32fc5e..80b3b91 100644 --- a/src/ProjectTankHard/FormTank.java +++ b/src/ProjectTankHard/FormTank.java @@ -2,7 +2,6 @@ package ProjectTankHard; import ProjectTankHard.DrawningObjects.DrawningTank; import ProjectTankHard.DrawningObjects.DrawningTankBase; -import ProjectTankHard.DrawningObjects.DrawningWheels; import ProjectTankHard.MovementStrategy.*; import javax.swing.*; @@ -14,21 +13,33 @@ import java.util.Random; public class FormTank { public DrawningTankBase DrawningTankBase; private AbstractStrategy _abstractStrategy; + public JButton SelectButton; + public JFrame TankFrame; Canvas canv; static int pictureBoxWidth = 882; static int pictureBoxHeight = 453; public void Draw(){ + if(DrawningTankBase == null) return; canv.repaint(); } + public DrawningTankBase SelectedTank() { + return DrawningTankBase; + } + public Color ChooseColor(JFrame MonorailFrame) { + JColorChooser dialog = new JColorChooser(); + Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.BLACK); + return res; + } public FormTank() { - JFrame TankFrame = new JFrame(); + TankFrame = new JFrame(); - JButton RightButton = new JButton(new ImageIcon("resources/RightButton.png")); - JButton LeftButton = new JButton(new ImageIcon("resources/LeftButton.png")); - JButton UpButton = new JButton(new ImageIcon("resources/UpButton.png")); - JButton DownButton = new JButton(new ImageIcon("resources/DownButton.png")); + JButton RightButton = new JButton(new ImageIcon("../resources/RightButton.png")); + JButton LeftButton = new JButton(new ImageIcon("../resources/LeftButton.png")); + JButton UpButton = new JButton(new ImageIcon("../resources/UpButton.png")); + JButton DownButton = new JButton(new ImageIcon("../resources/DownButton.png")); JButton CreateTankBaseButton = new JButton("Создать (прост.)"); JButton CreateTankButton = new JButton("Создать (продв.)"); + SelectButton = new JButton ("Выбрать"); JButton StepButton = new JButton("Шаг"); JComboBox StrategyComboBox = new JComboBox(new String[]{ "Довести до центра", "Довести до края"}); @@ -48,6 +59,7 @@ public class FormTank { DownButton.setBounds(804,411,30,30); StrategyComboBox.setBounds(719,12,151,28); StepButton.setBounds(768, 46, 94, 29); + SelectButton.setBounds(383, 401, 180, 40); TankFrame.add(canv); TankFrame.add(CreateTankBaseButton); @@ -58,6 +70,7 @@ public class FormTank { TankFrame.add(DownButton); TankFrame.add(StrategyComboBox); TankFrame.add(StepButton); + TankFrame.add(SelectButton); StepButton.addActionListener( new ActionListener() { @@ -103,9 +116,12 @@ public class FormTank { CreateTankBaseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + Color color = Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)); + Color choosenColor = ChooseColor(TankFrame); + if (choosenColor != null) color = choosenColor; + DrawningTankBase = new DrawningTankBase(random.nextInt(100, 300), random.nextDouble(1000, 3000), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - pictureBoxWidth, pictureBoxHeight); + color, pictureBoxWidth, pictureBoxHeight); DrawningTankBase.SetPosition(random.nextInt(0, 100), random.nextInt(0, 100)); @@ -118,10 +134,17 @@ public class FormTank { CreateTankButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + Color bodyColor = Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)); + Color additionalColor = Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)); + + Color choosenColor = ChooseColor(TankFrame); + if (choosenColor != null) bodyColor = choosenColor; + + choosenColor = ChooseColor(TankFrame); + if (choosenColor != null) additionalColor = choosenColor; + DrawningTankBase = new DrawningTank(random.nextInt(100, 300), random.nextDouble(1000, 3000), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - random.nextBoolean(), random.nextBoolean(), pictureBoxWidth, pictureBoxHeight); + bodyColor, additionalColor, random.nextBoolean(), random.nextBoolean(), pictureBoxWidth, pictureBoxHeight); DrawningTankBase.SetPosition(random.nextInt(0, 100), random.nextInt(0, 100)); diff --git a/src/ProjectTankHard/FormTankCollection.java b/src/ProjectTankHard/FormTankCollection.java new file mode 100644 index 0000000..216da12 --- /dev/null +++ b/src/ProjectTankHard/FormTankCollection.java @@ -0,0 +1,128 @@ +package ProjectTankHard; + +import ProjectTankHard.DrawningObjects.DrawningTankBase; +import ProjectTankHard.Generics.TanksGenericCollection; +import ProjectTankHard.MovementStrategy.DrawningObjectTank; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + + +public class FormTankCollection { + private final TanksGenericCollection _tanks; + private int pictureBoxWidth = 605; + private int pictureBoxHeight = 426; + CollectionCanvas canv; + void Draw(){ + if(canv == null) return; + + canv.repaint(); + } + + public FormTankCollection(){ + _tanks = new TanksGenericCollection<>(pictureBoxWidth, pictureBoxHeight); + canv = new CollectionCanvas(); + canv.setBounds(12,12, pictureBoxWidth, pictureBoxHeight); + + JPanel toolBox = new JPanel(); + JFrame collectionFrame = new JFrame(); + + JButton addButton = new JButton("Добавить"); + JButton removeButton = new JButton("Удалить"); + JButton refreshButton = new JButton("Обновить"); + JTextField tankNumber = new JTextField(); + GridLayout gridLayout = new GridLayout(4,1); + + toolBox.setLayout(gridLayout); + toolBox.add(addButton); + toolBox.add(tankNumber); + toolBox.add(removeButton); + toolBox.add(refreshButton); + toolBox.setBounds(623, 12, 227, 426); + + collectionFrame.add(toolBox); + collectionFrame.add(canv); + collectionFrame.setVisible(true); + collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + collectionFrame.setSize(880,497); + + addButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_tanks == null) return; + + FormTank form = new FormTank(); + form.SelectButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_tanks.Insert(form.SelectedTank()) != -1) + { + JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + form.SelectedTank()._pictureWidth = pictureBoxWidth; + form.SelectedTank()._pictureHeight = pictureBoxHeight; + Draw(); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + + canv._tanks = _tanks; + form.TankFrame.dispose(); + Draw(); + } + }); + } + }); + + removeButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_tanks == null) return; + + String tmp = tankNumber.getText(); + int numb; + + try { + numb = Integer.parseInt(tmp); + } + catch(Exception ex){ + JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + + _tanks.Remove(numb); + _tanks.ShowTanks(); + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + Draw(); + } + }); + + refreshButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_tanks == null) return; + + _tanks.ShowTanks(); + Draw(); + } + }); + } +} + +class CollectionCanvas extends JComponent { + public TanksGenericCollection _tanks; + public CollectionCanvas() {} + + @Override + public void paintComponent (Graphics g){ + if (_tanks == null) return; + + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + g2d.drawImage(_tanks.ShowTanks(), 0, 0, this); + super.repaint(); + } +} diff --git a/src/ProjectTankHard/Generics/HardGeneric.java b/src/ProjectTankHard/Generics/HardGeneric.java new file mode 100644 index 0000000..e33ba40 --- /dev/null +++ b/src/ProjectTankHard/Generics/HardGeneric.java @@ -0,0 +1,53 @@ +package ProjectTankHard.Generics; + +import ProjectTankHard.DrawningObjects.DrawningTankBase; +import ProjectTankHard.DrawningObjects.IDraw; +import ProjectTankHard.Entities.EntityTankBase; + +import java.util.Random; + +public class HardGeneric { + T[] firstArray; + U[] secondArray; + private int currentSize; + private int CountFirst; + private int CountSecond; + private int pictureBoxWidth; + private int pictureBoxHeight; + public HardGeneric(int countFirst, int countSecond, int width, int height){ + currentSize = 0; + CountFirst = countFirst; + CountSecond = countSecond; + firstArray = (T[]) new EntityTankBase[CountFirst]; + secondArray = (U[]) new IDraw[CountSecond]; + pictureBoxHeight = height; + pictureBoxWidth = width; + } + public int InsertFirst(T entityTankBase){ + if(firstArray[CountFirst - 1] != null) return -1; + + for(int i = currentSize - 1; i >= 0; i--) { + firstArray[i + 1] = firstArray[i]; + secondArray[i + 1] = secondArray[i]; + } + + currentSize++; + firstArray[0] = entityTankBase; + + return 0; + } + public int InsertSecond(U inter){ + if(secondArray[CountSecond-1] != null) return -1; + + secondArray[0] = inter; + return 0; + } + public DrawningTankBase MakeObject(){ + Random rand = new Random(); + int indFirst = rand.nextInt(0, currentSize); + EntityTankBase entity = firstArray[indFirst]; + DrawningTankBase tank = new DrawningTankBase(entity.Speed(), entity.Weight(), entity.BodyColor(), + pictureBoxWidth, pictureBoxHeight); + return tank; + } +} diff --git a/src/ProjectTankHard/Generics/SetGeneric.java b/src/ProjectTankHard/Generics/SetGeneric.java new file mode 100644 index 0000000..90c1927 --- /dev/null +++ b/src/ProjectTankHard/Generics/SetGeneric.java @@ -0,0 +1,50 @@ +package ProjectTankHard.Generics; + +public class SetGeneric{ + private final Object[] _places; + public int Count; + public SetGeneric(int count){ + _places = new Object[count]; + Count = count; + } + public int Insert(T monorail){ + return Insert(monorail, 0); + } + public int Insert(T monorail, int position){ + if(!(position >= 0 && position < Count)) return -1; + + if (_places[position] == null) { + _places[position] = monorail; + } + + else { + int place = -1; + for (int i = position; i < Count; i++) { + if (_places[i] == null) { + place = i; + break; + } + } + + if (place == -1) return -1; + + for (int i = place - 1; i >= position; i--) { + _places[i+1] = _places[i]; + } + + _places[position] = monorail; + } + return position; + } + public boolean Remove(int position){ + if (!(position >= 0 && position < Count)) return false; + + _places[position] = null; + return true; + } + public T Get(int position){ + if (!(position >= 0 && position < Count)) return null; + + return (T)_places[position]; + } +} diff --git a/src/ProjectTankHard/Generics/TanksGenericCollection.java b/src/ProjectTankHard/Generics/TanksGenericCollection.java new file mode 100644 index 0000000..5e120ac --- /dev/null +++ b/src/ProjectTankHard/Generics/TanksGenericCollection.java @@ -0,0 +1,80 @@ +package ProjectTankHard.Generics; + +import ProjectTankHard.DrawningObjects.DrawningTankBase; +import ProjectTankHard.MovementStrategy.IMoveableObject; + +import java.awt.*; +import java.awt.image.BufferedImage; + +public class TanksGenericCollection { + private final int _pictureWidth; + private final int _pictureHeight; + private final int _placeSizeWidth = 150; + private final int _placeSizeHeight = 100; + private final SetGeneric _collection; + public TanksGenericCollection(int picWidth, int picHeight){ + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + + public int Insert(T obj){ + if (obj == null) return -1; + + return _collection.Insert(obj); + } + + public boolean Remove(int position){ + return _collection.Remove(position); + } + public U GetU(int pos){ + T ans = _collection.Get(pos); + if(ans == null) + return null; + return (U)ans.GetMoveableObject(); + } + + private void DrawBackground(Graphics g) + { + g.setColor(Color.BLACK); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, + i * _placeSizeWidth + _placeSizeWidth / 2, + j * _placeSizeHeight); + } + g.drawLine(i * _placeSizeWidth, 0, + i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + private void DrawObjects(Graphics g) + { + for (int i = 0; i < _collection.Count; i++) + { + DrawningTankBase tank = _collection.Get(i); + if (tank != null) + { + int columnsCount = _pictureHeight / _placeSizeHeight; + + int colIndex = i % columnsCount; + int rowIndex = i / columnsCount; + + tank.SetPosition(colIndex * _placeSizeWidth, rowIndex * _placeSizeHeight); + tank.DrawTank((Graphics2D) g); + } + } + } + public BufferedImage ShowTanks() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR); + Graphics gr = bmp.createGraphics(); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } +} \ No newline at end of file diff --git a/src/ProjectTankHard/Main.java b/src/ProjectTankHard/Main.java index 7e8873a..c70bad6 100644 --- a/src/ProjectTankHard/Main.java +++ b/src/ProjectTankHard/Main.java @@ -4,6 +4,6 @@ import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { - FormTank form = new FormTank(); + FormTankCollection form = new FormTankCollection(); } } \ No newline at end of file diff --git a/src/resources/DownButton.png b/src/resources/DownButton.png new file mode 100644 index 0000000..47ad2f8 Binary files /dev/null and b/src/resources/DownButton.png differ diff --git a/src/resources/LeftButton.png b/src/resources/LeftButton.png new file mode 100644 index 0000000..bf2c7bf Binary files /dev/null and b/src/resources/LeftButton.png differ diff --git a/src/resources/RightButton.png b/src/resources/RightButton.png new file mode 100644 index 0000000..4161f84 Binary files /dev/null and b/src/resources/RightButton.png differ diff --git a/src/resources/UpButton.png b/src/resources/UpButton.png new file mode 100644 index 0000000..7c0cae0 Binary files /dev/null and b/src/resources/UpButton.png differ