PIbd-21. Makarov D.V. Lab work 03 Hard #3

Closed
DavidMakarov wants to merge 1 commits from lab3 into lab2
12 changed files with 575 additions and 22 deletions
Showing only changes of commit 78234afc84 - Show all commits

View File

@ -1,7 +1,7 @@
package AirplaneWithRadar;
import AirplaneWithRadar.MovementStrategy.Movement;
import AirplaneWithRadar.MovementStrategy.*;
import AirplaneWithRadar.MovementStrategy.Movement;
import AirplaneWithRadar.PaintObjects.PaintAirplane;
import AirplaneWithRadar.PaintObjects.PaintAirplaneWithRadar;
@ -14,6 +14,8 @@ import java.util.Random;
public class AirplaneWithRadarForm extends JFrame {
private PaintAirplane PaintPlanes;
private AbstractStrategy abstractStrategy;
public PaintAirplane SelectedAiplane;
public PaintAirplane GetSelectedAirplane() { return PaintPlanes; }
private void Draw()
{
@ -33,6 +35,8 @@ public class AirplaneWithRadarForm extends JFrame {
public AirplaneWithRadarForm()
{
InitializeComponent();
abstractStrategy = null;
SelectedAiplane = null;
}
private void InitializeComponent()
@ -46,6 +50,7 @@ public class AirplaneWithRadarForm extends JFrame {
ButtonUp = new JButton();
MovementStrategyComboBox = new JComboBox<String>();
ButtonPerformMove = new JButton();
SelectAiplaneButton = new JButton();
AirplanePictureBox.setBounds(0, 0, 884, 461);
//
// ButtonCreateBomberBase
@ -124,6 +129,15 @@ public class AirplaneWithRadarForm extends JFrame {
MovementStrategyComboBox.addItem("Перемещать в центр");
MovementStrategyComboBox.addItem("Перемещать вправо вниз");
//
// SelectBomberButton
//
SelectAiplaneButton.setBounds(354, 407, 159, 42);
SelectAiplaneButton.setName("SelectBomberButton");
SelectAiplaneButton.setText("Выбрать бомбардировщик");
SelectAiplaneButton.setBackground(new Color(225, 225, 225));
SelectAiplaneButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
SelectAiplaneButton.setFocusable(false);
//
// AirplaneWithRadarForm
//
setTitle("Самолет с радаром");
@ -141,6 +155,7 @@ public class AirplaneWithRadarForm extends JFrame {
add(ButtonPerformMove);
add(MovementStrategyComboBox);
add(AirplanePictureBox);
add(SelectAiplaneButton);
}
private JLabel AirplanePictureBox;
private JButton ButtonCreateAirplane;
@ -151,6 +166,7 @@ public class AirplaneWithRadarForm extends JFrame {
private JButton ButtonUp;
private JButton ButtonPerformMove;
private JComboBox<String> MovementStrategyComboBox;
public JButton SelectAiplaneButton;
private void ButtonMove_Click(ActionEvent e)
{
@ -184,11 +200,28 @@ public class AirplaneWithRadarForm extends JFrame {
{
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color additColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
JColorChooser BodyColorPicker = new JColorChooser(Color.BLACK);
if (JOptionPane.showConfirmDialog(null, BodyColorPicker, "Выберите цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)
== JOptionPane.OK_OPTION)
{
bodyColor = BodyColorPicker.getColor();
}
JColorChooser AdditionalColorPicker = new JColorChooser(Color.BLACK);
if (JOptionPane.showConfirmDialog(null, AdditionalColorPicker, "Выберите цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)
== JOptionPane.OK_OPTION)
{
additColor = AdditionalColorPicker.getColor();
}
PaintPlanes = new PaintAirplaneWithRadar(
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)),
bodyColor,
additColor,
true,
true,
AirplanePictureBox.getWidth(),
@ -203,10 +236,19 @@ public class AirplaneWithRadarForm extends JFrame {
{
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
JColorChooser BodyColorPicker = new JColorChooser(Color.BLACK);
if (JOptionPane.showConfirmDialog(null, BodyColorPicker, "Выберите цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)
== JOptionPane.OK_OPTION)
{
bodyColor = BodyColorPicker.getColor();
}
PaintPlanes = new PaintAirplane(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
bodyColor,
AirplanePictureBox.getWidth(),
AirplanePictureBox.getHeight()
);

View File

@ -0,0 +1,146 @@
package AirplaneWithRadar;
import AirplaneWithRadar.Generics.AirplanesGenericCollection;
import AirplaneWithRadar.MovementStrategy.PaintObjectAirplane;
import AirplaneWithRadar.PaintObjects.PaintAirplane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class FormAirplaneCollection extends JFrame {
private final AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane> aiplanes;
public FormAirplaneCollection()
{
InitializeComponent();
aiplanes = new AirplanesGenericCollection<PaintAirplane, PaintObjectAirplane>(
CollectionPictureBox.getWidth(), CollectionPictureBox.getHeight()
);
}
public void ButtonAddEntity_Click(ActionEvent e)
{
AirplaneWithRadarForm Form = new AirplaneWithRadarForm();
Form.SelectAiplaneButton.addActionListener(e2 -> {
if (aiplanes.Add(Form.GetSelectedAirplane()) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен");
CollectionPictureBox.setIcon(new ImageIcon(aiplanes.ShowAirplanes()));
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
Form.dispose();
});
}
public void ButtonRemoveEntity_Click(ActionEvent e)
{
int Pos;
try
{
Pos = Integer.parseInt(NumberMaskedTextBox.getText());
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Некорректный ввод");
return;
}
if (aiplanes.Remove(Pos) == true)
{
JOptionPane.showMessageDialog(null, "Объект удален");
CollectionPictureBox.setIcon(new ImageIcon(aiplanes.ShowAirplanes()));
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
}
public void ButtonRefreshCollection_Click(ActionEvent e)
{
CollectionPictureBox.setIcon(new ImageIcon(aiplanes.ShowAirplanes()));
}
private void InitializeComponent()
{
CollectionPictureBox = new JLabel();
ToolGroupBox = new JPanel();
RefreshCollectionButton = new JButton();
RemoveBomberButton = new JButton();
NumberMaskedTextBox = new JTextField();
AddBomberButton = new JButton();
//
// CollectionPictureBox
//
CollectionPictureBox.setBounds(0, 0, 812, 603);
//
// ToolGroupBox
//
ToolGroupBox.add(RefreshCollectionButton);
ToolGroupBox.add(RemoveBomberButton);
ToolGroupBox.add(NumberMaskedTextBox);
ToolGroupBox.add(AddBomberButton);
ToolGroupBox.setBounds(818, 12, 210, 545);
ToolGroupBox.setName("ToolGroupBox");
ToolGroupBox.setBorder(BorderFactory.createTitledBorder("Инструменты"));
ToolGroupBox.setLayout(null);
//
// RefreshCollectionButton
//
RefreshCollectionButton.setBounds(10, 190, 190, 30);
RefreshCollectionButton.setName("RefreshCollectionButton");
RefreshCollectionButton.setText("Обновить коллекцию");
RefreshCollectionButton.setBackground(new Color(225, 225, 225));
RefreshCollectionButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
RefreshCollectionButton.setFocusable(false);
RefreshCollectionButton.addActionListener(e -> ButtonRefreshCollection_Click(e));
//
// RemoveBomberButton
//
RemoveBomberButton.setBounds(10, 141, 190, 30);
RemoveBomberButton.setName("RemoveBomberButton");
RemoveBomberButton.setText("Удалить бомбардировщик");
RemoveBomberButton.setBackground(new Color(225, 225, 225));
RemoveBomberButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
RemoveBomberButton.setFocusable(false);
RemoveBomberButton.addActionListener(e -> ButtonRemoveEntity_Click(e));
//
// NumberMaskedTextBox
//
NumberMaskedTextBox.setBounds(10, 102, 190, 23);
NumberMaskedTextBox.setName("NumberMaskedTextBox");
//
// AddBomberButton
//
AddBomberButton.setBounds(10, 32, 190, 43);
AddBomberButton.setName("AddBomberButton");
AddBomberButton.setText("Добавить бомбардировщик");
AddBomberButton.setBackground(new Color(225, 225, 225));
AddBomberButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
AddBomberButton.setFocusable(false);
AddBomberButton.addActionListener(e -> ButtonAddEntity_Click(e));
//
// FormEntityCollection
//
setTitle("Набор самолётов");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1050, 603);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
add(ToolGroupBox);
add(CollectionPictureBox);
}
private JLabel CollectionPictureBox;
private JPanel ToolGroupBox;
private JButton RefreshCollectionButton;
private JButton RemoveBomberButton;
private JTextField NumberMaskedTextBox;
private JButton AddBomberButton;
}

View File

@ -0,0 +1,113 @@
package AirplaneWithRadar;
import AirplaneWithRadar.Entities.AirplaneEntity;
import AirplaneWithRadar.Generics.ArraysGeneric;
import AirplaneWithRadar.PaintObjects.*;
import AirplaneWithRadar.PaintObjects.IPaintIlluminators;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
public class FormWithArrays extends JFrame {
private ArraysGeneric<AirplaneEntity, IPaintIlluminators> arrays;
public FormWithArrays()
{
InitializeComponent();
Random random = new Random();
int MaxCount = random.nextInt(2, 4);
arrays = new ArraysGeneric<>(MaxCount, MaxCount, PictureBox.getWidth(), PictureBox.getHeight());
for (int i = 0; i < MaxCount; i++)
{
arrays.Add(new AirplaneEntity(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))
));
IPaintIlluminators paintIllums = null;
int paintIllumsVariant = random.nextInt(1, 4);
switch (paintIllumsVariant) {
case 1:
paintIllums = new PaintIlluminators();
break;
case 2:
paintIllums = new PaintRectangleIlluminators();
break;
case 3:
paintIllums = new PaintRoundedRectIlluminator();
break;
default:
break;
}
paintIllums.setCount(random.nextInt(6, 35));
arrays.Add(paintIllums);
}
}
private void Draw(PaintAirplane PaintPlanes)
{
BufferedImage bmp = new BufferedImage(
PictureBox.getWidth(),
PictureBox.getHeight(),
BufferedImage.TYPE_INT_ARGB
);
Graphics2D g = bmp.createGraphics();
PaintPlanes.DrawTransport(g);
PictureBox.setIcon(new ImageIcon(bmp));
}
private void ButtonCreate_Click(ActionEvent e)
{
PaintAirplane PaintPlanes = arrays.CreateObject();
PaintPlanes.SetPosition(100, 100);
Draw(PaintPlanes);
}
private void InitializeComponent()
{
PictureBox = new JLabel();
ButtonCreate = new JButton();
//
// ButtonCreate
//
ButtonCreate.setName("ButtonCreate");
ButtonCreate.setBounds(12, 419, 80, 30);
ButtonCreate.setText("Создать");
ButtonCreate.setBackground(new Color(225, 225, 225));
ButtonCreate.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonCreate.setFocusable(false);
ButtonCreate.addActionListener(e -> ButtonCreate_Click(e));
//
// PictureBox
//
PictureBox.setBounds(0, 0, 884, 461);
//
// BomberForm
//
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Усложнение");
setSize(900, 500);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
add(PictureBox);
add(ButtonCreate);
}
private JButton ButtonCreate;
private JLabel PictureBox;
}

View File

@ -0,0 +1,95 @@
package AirplaneWithRadar.Generics;
import AirplaneWithRadar.PaintObjects.PaintAirplane;
import AirplaneWithRadar.MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class AirplanesGenericCollection<T extends PaintAirplane, U extends IMoveableObject> {
private int pictWidth;
private int pictHeight;
private final int placeSizeWidth = 200;
private final int placeSizeHeight = 70;
private SetGeneric<T> collection;
public AirplanesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / placeSizeWidth;
int height = picHeight / placeSizeHeight;
pictWidth = picWidth;
pictHeight = picHeight;
collection = new SetGeneric<T>(width * height);
}
public int Add(T obj)
{
if (obj == null)
{
return -1;
}
return collection.Insert(obj);
}
public boolean Remove(int pos)
{
T obj = collection.Get(pos);
if (obj == null)
{
return false;
}
return collection.Remove(pos);
}
public U GetU(int pos)
{
return (U)collection.Get(pos).GetMoveableObject();
}
public BufferedImage ShowAirplanes()
{
BufferedImage bmp = new BufferedImage(pictWidth, pictHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
private void DrawBackground(Graphics2D g)
{
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(3));
for (int i = 0; i < pictWidth / placeSizeWidth; i++)
{
for (int j = 0; j < pictHeight / placeSizeHeight + 1; ++j)
{
g.drawLine(i * placeSizeWidth, j * placeSizeHeight, i * placeSizeWidth + placeSizeWidth / 2, j * placeSizeHeight);
}
g.drawLine(i * placeSizeWidth, 0, i * placeSizeWidth, pictHeight / placeSizeHeight * placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
int width = pictWidth / placeSizeWidth;
int height = pictHeight / placeSizeHeight;
int j = 3;
int k = 0;
for (int i = 0; i < collection.Count(); i++)
{
PaintAirplane airplane = collection.Get(i);
if (j < 0)
{
j += 4;
k++;
}
if (airplane != null)
{
airplane.SetPosition(placeSizeWidth * j, placeSizeHeight * k);
airplane.DrawTransport(g);
}
j--;
}
}
}

View File

@ -0,0 +1,66 @@
package AirplaneWithRadar.Generics;
import AirplaneWithRadar.Entities.AirplaneEntity;
import AirplaneWithRadar.PaintObjects.IPaintIlluminators;
import AirplaneWithRadar.PaintObjects.PaintAirplane;
import java.util.ArrayList;
import java.util.Random;
public class ArraysGeneric<T extends AirplaneEntity, U extends IPaintIlluminators> {
private ArrayList<T> airplanes;
private ArrayList<U> paintIllums;
private int airplanesCount = 0;
private int paintIllumsCount = 0;
private final int aiplanesMaxCount;
private final int paintIllumsMaxCount;
private final int pictWidth;
private final int pictHeight;
public ArraysGeneric(int AiplanesMaxCount, int PaintIllumsMaxCount, int Width, int Height)
{
aiplanesMaxCount = AiplanesMaxCount;
paintIllumsMaxCount = PaintIllumsMaxCount;
airplanes = new ArrayList<T>(AiplanesMaxCount);
paintIllums = new ArrayList<U>(PaintIllumsMaxCount);
pictWidth = Width;
pictHeight = Height;
}
public boolean Add(T Entity)
{
if (Entity == null || airplanesCount >= aiplanesMaxCount)
return false;
airplanes.add(airplanesCount++, Entity);
return true;
}
public boolean Add(U Renderer)
{
if (Renderer == null || paintIllumsCount >= paintIllumsMaxCount)
return false;
paintIllums.add(paintIllumsCount++, Renderer);
return true;
}
public PaintAirplane CreateObject()
{
if (airplanesCount == 0 || paintIllumsCount == 0)
return null;
Random Random = new Random();
int airplaneIndex = Random.nextInt(airplanesCount);
int paintIllumsIndex = Random.nextInt(paintIllumsCount);
T airplane = airplanes.get(airplaneIndex);
U paintIllums = this.paintIllums.get(paintIllumsIndex);
return new PaintAirplane(airplane, paintIllums, pictWidth, pictHeight);
}
}

View File

@ -0,0 +1,62 @@
package AirplaneWithRadar.Generics;
public class SetGeneric<T extends Object> {
private final T[] places;
public int Count() { return places.length; }
@SuppressWarnings("unchecked")
public SetGeneric(int Count)
{
places = (T[]) new Object[Count];
}
public int Insert(T Entity)
{
return Insert(Entity, 0);
}
public int Insert(T Entity, int Pos)
{
if (Pos >= Count() || Pos < 0)
return -1;
if (places[Pos] == null)
{
places[Pos] = Entity;
return Pos;
}
int EmptyPos = -1;
for (int i = Pos + 1; i < Count(); i++)
if (places[i] == null)
{
EmptyPos = i;
break;
}
if (EmptyPos == -1)
return -1;
places[EmptyPos] = Entity;
return EmptyPos;
}
public boolean Remove(int Pos)
{
if (Pos >= Count())
return false;
places[Pos] = null;
return true;
}
public T Get(int Pos)
{
if (Pos >= Count())
return null;
return places[Pos];
}
}

View File

@ -1,9 +1,11 @@
package AirplaneWithRadar;
import AirplaneWithRadar.PaintObjects.IPaintIlluminators;
import java.awt.Graphics;
import java.awt.Color;
public class PaintIlluminators {
public class PaintIlluminators implements IPaintIlluminators {
private IlluminatorsCount count;
public void setCount(int count) {
if (count <= 10)

View File

@ -1,7 +1,9 @@
package AirplaneWithRadar.PaintObjects;
import AirplaneWithRadar.Entities.AirplaneEntity;
import AirplaneWithRadar.MovementStrategy.IMoveableObject;
import AirplaneWithRadar.MovementStrategy.Movement;
import AirplaneWithRadar.MovementStrategy.PaintObjectAirplane;
import java.awt.*;
import java.util.Random;
@ -10,7 +12,7 @@ public class PaintAirplane {
public AirplaneEntity getAirplaneEntity() {
return airplaneEntity;
}
public IMoveableObject GetMoveableObject() {return new PaintObjectAirplane(this);}
private void setAirplaneEntity(AirplaneEntity airplaneEntity) {
this.airplaneEntity = airplaneEntity;
}
@ -65,20 +67,24 @@ public class PaintAirplane {
Random Random = new Random();
int paintIlluminatorsVariant = Random.nextInt(1, 4);
switch (paintIlluminatorsVariant) {
int EngineRendererVariant = Random.nextInt(1, 4);
switch (EngineRendererVariant) {
case 1:
paintIlluminators = new PaintIlluminators();
break;
case 2:
paintIlluminators = new PaintRectangleIlluminators();
break;
case 3:
paintIlluminators = new PaintRoundedRectIlluminator();
break;
default:
break;
}
paintIlluminators.setCount(Random.nextInt(6, 35));
}
@ -97,6 +103,21 @@ public class PaintAirplane {
Random rnd = new Random();
paintIlluminators.setCount(rnd.nextInt(6, 35));
}
public PaintAirplane(AirplaneEntity airplane, IPaintIlluminators paintIllums, int Width, int Height)
{
if (Width < planeWidth || Height < planeHeight)
return;
startPosX = 0;
startPosY = 0;
pictWidth = Width;
pictHeight = Height;
this.airplaneEntity = airplane;
this.paintIlluminators = paintIllums;
}
public void SetPosition(int x, int y) {
if (x + planeWidth < pictWidth && y + planeHeight < pictHeight) {
startPosX = x;

View File

@ -36,4 +36,10 @@ public class PaintAirplaneWithRadar extends PaintAirplane {
g.fillRoundRect(startPosX, startPosY + planeHeight - 2*planeHeight / 5, planeWidth / 5, planeHeight / 6, 5, 5);
}
}
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);
}
}

View File

@ -22,8 +22,8 @@ public class PaintIlluminators implements IPaintIlluminators{
int x = startPosX;
for (int i = 0; i < 10; i++) {
g.fillOval(x + 34, startPosY + 30, 7, 7);
x += 8;
g.fillOval(x + 34, startPosY + 30, 5, 5);
x += 7;
}
if (count == IlluminatorsCount.Ten) return;
@ -31,8 +31,8 @@ public class PaintIlluminators implements IPaintIlluminators{
x = startPosX;
for (int i = 0; i < 10; i++) {
g.fillOval(x + 34, startPosY + 38, 7, 7);
x += 8;
g.fillOval(x + 34, startPosY + 38, 5, 5);
x += 7;
}
if (count == IlluminatorsCount.Twenty) return;
@ -40,8 +40,8 @@ public class PaintIlluminators implements IPaintIlluminators{
x = startPosX;
for (int i = 0; i < 10; i++) {
g.fillOval(x + 34, startPosY + 46, 7, 7);
x += 8;
g.fillOval(x + 34, startPosY + 46, 5, 5);
x += 7;
}
}
}

View File

@ -21,8 +21,8 @@ public class PaintRoundedRectIlluminator implements IPaintIlluminators {
int x = startPosX;
for (int i = 0; i < 10; i++) {
g.fillRoundRect(x + 34, startPosY + 30, 6, 6,2,2);
x += 8;
g.fillRoundRect(x + 34, startPosY + 30, 5, 5,3,3);
x += 7;
}
if (count == IlluminatorsCount.Ten) return;
@ -30,8 +30,8 @@ public class PaintRoundedRectIlluminator implements IPaintIlluminators {
x = startPosX;
for (int i = 0; i < 10; i++) {
g.fillRoundRect(x + 34, startPosY + 38, 6, 6,2,2);
x += 8;
g.fillRoundRect(x + 34, startPosY + 38, 5, 5,3,3);
x += 7;
}
if (count == IlluminatorsCount.Twenty) return;
@ -39,8 +39,8 @@ public class PaintRoundedRectIlluminator implements IPaintIlluminators {
x = startPosX;
for (int i = 0; i < 10; i++) {
g.fillRoundRect(x + 34, startPosY + 46, 6, 6,2,2);
x += 8;
g.fillRoundRect(x + 34, startPosY + 46, 5, 5,3,3);
x += 7;
}
}
}

View File

@ -1,7 +1,7 @@
import AirplaneWithRadar.AirplaneWithRadarForm;
import AirplaneWithRadar.FormWithArrays;
public class Main {
public static void main(String[] args) {
new AirplaneWithRadarForm();
new FormWithArrays();
}
}