Lab3 Java HARD Петрушин Егор ПИбд-22 #3

Closed
Egor_Petrushin wants to merge 5 commits from Lab3 into Lab2
12 changed files with 751 additions and 55 deletions

View File

@ -31,6 +31,80 @@ public class DrawningClamps implements IDrawable {
setNum(numRollers);
}
public DrawningClamps(){}
@Override
public void Draw(Graphics2D g2d, int startPosX, int startPosY, Color color){
this.color = color;
this.g2d = g2d;
this.startPosX = startPosX;
this.startPosY = startPosY;
g2d.setPaint(Color.BLACK);
Ellipse2D ellipse7 = new Ellipse2D.Double(startPosX + 10, startPosY + 55, 120, 20);
Ellipse2D ellipse1 = new Ellipse2D.Double(startPosX + 5, startPosY + 50, 20, 20);
Ellipse2D ellipse2 = new Ellipse2D.Double(startPosX + 30, startPosY + 50, 20, 20);
Ellipse2D ellipse3 = new Ellipse2D.Double(startPosX + 55, startPosY + 50, 20, 20);
Ellipse2D ellipse4 = new Ellipse2D.Double(startPosX + 80, startPosY + 50, 20, 20);
Ellipse2D ellipse5 = new Ellipse2D.Double(startPosX + 105, startPosY + 50, 20, 20);
Ellipse2D ellipse6 = new Ellipse2D.Double(startPosX + 125, startPosY + 60, 10, 10);
g2d.draw(ellipse7);
switch (_numbeRollers) {
case Min:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
break;
case Mid:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
g2d.fill(ellipse5);
break;
case Max:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
g2d.fill(ellipse5);
g2d.fill(ellipse6);
break;
}
Ellipse2D ellipse11 = new Ellipse2D.Double(startPosX + 10, startPosY + 55, 10, 10);
Ellipse2D ellipse12 = new Ellipse2D.Double(startPosX + 35, startPosY + 55, 10, 10);
Ellipse2D ellipse13 = new Ellipse2D.Double(startPosX + 60, startPosY + 55, 10, 10);
Ellipse2D ellipse14 = new Ellipse2D.Double(startPosX + 85, startPosY + 55, 10, 10);
Ellipse2D ellipse15 = new Ellipse2D.Double(startPosX + 110, startPosY + 55, 10, 10);
Ellipse2D ellipse16 = new Ellipse2D.Double(startPosX + 127, startPosY + 60, 5, 5);
g2d.setPaint(color);
switch (_numbeRollers) {
case Min:
g2d.fill(ellipse11);
g2d.fill(ellipse12);
g2d.fill(ellipse13);
g2d.fill(ellipse14);
break;
case Mid:
g2d.fill(ellipse11);
g2d.fill(ellipse12);
g2d.fill(ellipse13);
g2d.fill(ellipse14);
g2d.fill(ellipse15);
break;
case Max:
g2d.fill(ellipse11);
g2d.fill(ellipse12);
g2d.fill(ellipse13);
g2d.fill(ellipse14);
g2d.fill(ellipse15);
g2d.fill(ellipse16);
break;
}
}
@Override
public void Draw(){
g2d.setPaint(Color.BLACK);

View File

@ -31,6 +31,80 @@ public class DrawningNails implements IDrawable {
setNum(numRollers);
}
public DrawningNails(){}
@Override
public void Draw(Graphics2D g2d, int startPosX, int startPosY, Color color){
this.color = color;
this.g2d = g2d;
this.startPosX = startPosX;
this.startPosY = startPosY;
g2d.setPaint(Color.BLACK);
Ellipse2D ellipse7 = new Ellipse2D.Double(startPosX + 10, startPosY + 55, 120, 20);
Ellipse2D ellipse1 = new Ellipse2D.Double(startPosX + 5, startPosY + 50, 20, 20);
Ellipse2D ellipse2 = new Ellipse2D.Double(startPosX + 30, startPosY + 50, 20, 20);
Ellipse2D ellipse3 = new Ellipse2D.Double(startPosX + 55, startPosY + 50, 20, 20);
Ellipse2D ellipse4 = new Ellipse2D.Double(startPosX + 80, startPosY + 50, 20, 20);
Ellipse2D ellipse5 = new Ellipse2D.Double(startPosX + 105, startPosY + 50, 20, 20);
Ellipse2D ellipse6 = new Ellipse2D.Double(startPosX + 125, startPosY + 60, 10, 10);
g2d.draw(ellipse7);
switch (_numbeRollers) {
case Min:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
break;
case Mid:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
g2d.fill(ellipse5);
break;
case Max:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
g2d.fill(ellipse5);
g2d.fill(ellipse6);
break;
}
Ellipse2D ellipse11 = new Ellipse2D.Double(startPosX + 15, startPosY + 62, 2, 2);
Ellipse2D ellipse12 = new Ellipse2D.Double(startPosX + 40, startPosY + 62, 2, 2);
Ellipse2D ellipse13 = new Ellipse2D.Double(startPosX + 65, startPosY + 62, 2, 2);
Ellipse2D ellipse14 = new Ellipse2D.Double(startPosX + 90, startPosY + 62, 2, 2);
Ellipse2D ellipse15 = new Ellipse2D.Double(startPosX + 115, startPosY + 62, 2, 2);
Ellipse2D ellipse16 = new Ellipse2D.Double(startPosX + 130, startPosY + 65, 1, 1);
g2d.setPaint(color);
switch (_numbeRollers) {
case Min:
g2d.fill(ellipse11);
g2d.fill(ellipse12);
g2d.fill(ellipse13);
g2d.fill(ellipse14);
break;
case Mid:
g2d.fill(ellipse11);
g2d.fill(ellipse12);
g2d.fill(ellipse13);
g2d.fill(ellipse14);
g2d.fill(ellipse15);
break;
case Max:
g2d.fill(ellipse11);
g2d.fill(ellipse12);
g2d.fill(ellipse13);
g2d.fill(ellipse14);
g2d.fill(ellipse15);
g2d.fill(ellipse16);
break;
}
}
@Override
public void Draw(){

View File

@ -30,11 +30,52 @@ public class DrawningRollers implements IDrawable{
setNum(numRollers);
}
public DrawningRollers(){}
@Override
public void Draw(Graphics2D g2d, int startPosX, int startPosY, Color color){
this.color = color;
this.g2d = g2d;
this.startPosX = startPosX;
this.startPosY = startPosY;
g2d.setPaint(Color.BLACK);
Ellipse2D ellipse7 = new Ellipse2D.Double(startPosX + 10, startPosY + 55, 120, 20);
Ellipse2D ellipse1 = new Ellipse2D.Double(startPosX + 5, startPosY + 50, 20, 20);
Ellipse2D ellipse2 = new Ellipse2D.Double(startPosX + 30, startPosY + 50, 20, 20);
Ellipse2D ellipse3 = new Ellipse2D.Double(startPosX + 55, startPosY + 50, 20, 20);
Ellipse2D ellipse4 = new Ellipse2D.Double(startPosX + 80, startPosY + 50, 20, 20);
Ellipse2D ellipse5 = new Ellipse2D.Double(startPosX + 105, startPosY + 50, 20, 20);
Ellipse2D ellipse6 = new Ellipse2D.Double(startPosX + 125, startPosY + 60, 10, 10);
g2d.draw(ellipse7);
switch (_numbeRollers) {
case Min:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
break;
case Mid:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
g2d.fill(ellipse5);
break;
case Max:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
g2d.fill(ellipse5);
g2d.fill(ellipse6);
break;
}
}
@Override
public void Draw(){
g2d.setPaint(Color.BLACK);
Ellipse2D ellipse7 = new Ellipse2D.Double(startPosX + 10, startPosY + 55, 120, 20);
Ellipse2D ellipse1 = new Ellipse2D.Double(startPosX + 5, startPosY + 50, 20, 20);
Ellipse2D ellipse2 = new Ellipse2D.Double(startPosX + 30, startPosY + 50, 20, 20);
Ellipse2D ellipse3 = new Ellipse2D.Double(startPosX + 55, startPosY + 50, 20, 20);

View File

@ -4,6 +4,8 @@ import java.awt.geom.Path2D;
import SelfPropelledArtilleryUnit.DirectionType;
import SelfPropelledArtilleryUnit.Entities.EntitySPAU;
import SelfPropelledArtilleryUnit.MovementStrategy.DrawningObjectSPAU;
import SelfPropelledArtilleryUnit.MovementStrategy.IMoveableObject;
public class DrawningSPAU {
@ -16,6 +18,7 @@ public class DrawningSPAU {
private int rollVar;
private int carWidth = 135;
private int carHeight = 75;
private IDrawable Rollers;
protected void setEntitySPAU(EntitySPAU entitySPAU) {
this.EntitySPAU = entitySPAU;
@ -60,6 +63,24 @@ public class DrawningSPAU {
}
EntitySPAU = new EntitySPAU(speed, weight, bodyColor);
}
public DrawningSPAU(EntitySPAU sPAU, IDrawable roll, int width, int height)
{ if (this.carHeight >= height)
{
return;
}
if (this.carWidth >= width)
{
return;
}
startPosX = 0;
startPosY = 0;
pictureWidth = width;
pictureHeight = height;
EntitySPAU = sPAU;
Rollers = roll;
}
public void SetPosition(int x, int y)
{
@ -111,7 +132,8 @@ public class DrawningSPAU {
Path2D.Double path = new Path2D.Double();
//гусеницы
IDrawable Rollers = new DrawningRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers);
if (Rollers == null){
Rollers = new DrawningRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers);
switch (rollVar) {
case 2:
Rollers = new DrawningClamps(g2d, startPosX, startPosY, bodyColor, _numbeRollers);
@ -121,9 +143,12 @@ public class DrawningSPAU {
break;
default:
break;
}
Rollers.Draw();
}
else{
Rollers.Draw(g2d, startPosX, startPosY, bodyColor);
}
Rollers.Draw();
// пушка
path.moveTo(startPosX + 35, startPosY + 40);
@ -219,5 +244,10 @@ public class DrawningSPAU {
default:
return false;
}
}
public IMoveableObject GetMoveableObject() {
return new DrawningObjectSPAU(this);
}
}

View File

@ -1,6 +1,10 @@
package SelfPropelledArtilleryUnit.DrawningObjects;
import java.awt.Color;
import java.awt.Graphics2D;
public interface IDrawable {
public void setNum(int num);
public void Draw();
public void Draw(Graphics2D g2d, int startPosX, int startPosY, Color color);
}

View File

@ -0,0 +1,98 @@
package SelfPropelledArtilleryUnit;
import javax.swing.*;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningClamps;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningNails;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningRollers;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
import SelfPropelledArtilleryUnit.DrawningObjects.IDrawable;
import SelfPropelledArtilleryUnit.Entities.EntitySPAU;
import SelfPropelledArtilleryUnit.Generics.RandomParts;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormHard {
private JFrame frameMain;
private JPanel panelMain;
private JButton buttonCreate;
private RandomParts<EntitySPAU, IDrawable> generator;
public FormHard() {
InitializeComponent();
Random rand = new Random();
// макс. кол-во вариаций форм дверей/видов автобуса
int maxCnt = rand.nextInt(5, 11);
generator = new RandomParts<>(maxCnt, maxCnt, panelMain.getWidth(), panelMain.getHeight());
// добавление в массивы с дверьми/сущностями рандомные варианты
for (int i = 0; i < maxCnt; i++) {
generator.Add(createRandomEntitySPAU());
generator.Add(createRandomDop());
}
}
private void InitializeComponent() {
//Само окно
frameMain = new JFrame("Усложнённая лаб 3");
frameMain.setSize(900, 500);
frameMain.setLayout(new BorderLayout());
frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Панель, на которой отрисовывается
panelMain = new JPanel();
panelMain.setLayout(null);
buttonCreate = new JButton("Создать");
buttonCreate.setBounds(10,400,150,30);
buttonCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createRandomDrawingBus(e);
}
});
frameMain.add(panelMain, BorderLayout.CENTER);
panelMain.add(buttonCreate);
frameMain.setVisible(true);
}
private void Draw(DrawningSPAU drawingSPAU){
if (drawingSPAU == null) {
return;
}
Graphics g = panelMain.getGraphics();
// Очистка перед перерисовкой
panelMain.paint(g);
drawingSPAU.DrawTransport(g);
}
private void createRandomDrawingBus(ActionEvent e) {
DrawningSPAU drawingBus = generator.CreateObject();
drawingBus.SetPosition(50, 50);
Draw(drawingBus);
}
private EntitySPAU createRandomEntitySPAU() {
Random rand = new Random();
Color color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
EntitySPAU entityBus = new EntitySPAU(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), color);
return entityBus;
}
private IDrawable createRandomDop() {
IDrawable dops;
Random rand = new Random();
int shape = rand.nextInt(1, 3);
if (shape == 1) {
dops = new DrawningClamps();
}
else if (shape == 2) {
dops = new DrawningNails();
}
else {
dops = new DrawningRollers();
}
dops.setNum(rand.nextInt(4, 6));
return dops;
}
}

View File

@ -13,16 +13,16 @@ import SelfPropelledArtilleryUnit.MovementStrategy.Status;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Form extends JFrame {
public class FormSPAU extends JFrame {
private JPanel mainPanel;
private JButton buttonCreate;
private JButton buttonCreateParent;
private JButton buttonStep;
private JButton buttonSelectSPAU;
private JComboBox<String> comboBoxStrategy;
private JComboBox<String> comboBoxRoll;
private JButton buttonUp;
@ -35,7 +35,13 @@ public class Form extends JFrame {
private JTextField wheelsTextField;
private JLabel wheelsLabel;
public Form() {
private DrawningSPAU SelectedSPAU;
public DrawningSPAU GetSelectedSPAU() {
return SelectedSPAU;
}
public FormSPAU() {
initComponents();
}
@ -43,7 +49,8 @@ public class Form extends JFrame {
if (drawningSPAU == null) {
return;
}
BufferedImage bmp = new BufferedImage(pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight(), BufferedImage.TYPE_INT_ARGB);
BufferedImage bmp = new BufferedImage(pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
drawningSPAU.DrawTransport(gr);
ImageIcon imageIcon = new ImageIcon(bmp);
@ -99,18 +106,25 @@ public class Form extends JFrame {
mainPanel.add(buttonStep);
buttonStep.setBounds(794, 55, 76, 30);
buttonStep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonStep_Click(e);
}
public void actionPerformed(ActionEvent e) {
buttonStep_Click(e);
}
});
// ButtonSelectSPAU
buttonSelectSPAU = new JButton("Выбрать");
mainPanel.add(buttonSelectSPAU);
buttonSelectSPAU.setLocation(new Point(759, 90));
buttonSelectSPAU.setBounds(794, 85, 76, 30);
buttonSelectSPAU.addActionListener(e -> buttonSelectSPAU_Click(e));
// comboBoxRoll
comboBoxRoll = new JComboBox<>();
mainPanel.add(comboBoxRoll);
comboBoxRoll.setModel(new DefaultComboBoxModel<>(new String[] { "1", "2", "3" }));
comboBoxRoll.setBounds(767, 232, 103, 28);
// comboBoxStrategy
comboBoxStrategy = new JComboBox<>();
mainPanel.add(comboBoxStrategy);
@ -119,7 +133,7 @@ public class Form extends JFrame {
buttonUp.setBounds(804, 336, 30, 30);
mainPanel.add(buttonUp);
ImageIcon iconUp = new ImageIcon("Arrows/upper-arrow.png");
ImageIcon iconUp = new ImageIcon("/Arrows/upper-arrow.png");
buttonUp.setIcon(iconUp);
buttonUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
@ -129,7 +143,7 @@ public class Form extends JFrame {
buttonDown.setBounds(804, 409, 30, 30);
mainPanel.add(buttonDown);
ImageIcon iconDown = new ImageIcon("Arrows/down-arrow.png");
ImageIcon iconDown = new ImageIcon("/Arrows/down-arrow.png");
buttonDown.setIcon(iconDown);
buttonDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
@ -139,7 +153,7 @@ public class Form extends JFrame {
buttonLeft.setBounds(768, 372, 30, 30);
mainPanel.add(buttonLeft);
ImageIcon iconLeft = new ImageIcon("Arrows/left-arrow.png");
ImageIcon iconLeft = new ImageIcon("/Arrows/left-arrow.png");
buttonLeft.setIcon(iconLeft);
buttonLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
@ -149,7 +163,7 @@ public class Form extends JFrame {
buttonRight.setBounds(840, 372, 30, 30);
mainPanel.add(buttonRight);
ImageIcon iconRight = new ImageIcon("Arrows/right-arrow.png");
ImageIcon iconRight = new ImageIcon("/Arrows/right-arrow.png");
buttonRight.setIcon(iconRight);
buttonRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
@ -175,12 +189,25 @@ public class Form extends JFrame {
if (wheelCount > 0) {
Random random = new Random();
drawningSPAU = new DrawningSPAUchild(wheelCount, Integer.parseInt((String) comboBoxRoll.getSelectedItem()), random.nextInt(200) + 100,
(double) 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)),
true, true,
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
Color dopColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", color);
if (selectedColor != null) {
color = selectedColor;
}
Color selectedDopColor = JColorChooser.showDialog(null, "Выберите дополнительный цвет", dopColor);
if (selectedDopColor != null) {
dopColor = selectedDopColor;
}
drawningSPAU = new DrawningSPAUchild(wheelCount, Integer.parseInt((String) comboBoxRoll.getSelectedItem()),
random.nextInt(200) + 100,
(double) random.nextInt(2000) + 1000,
color,
dopColor,
true, true,
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
drawningSPAU.SetPosition(random.nextInt(90) + 20, random.nextInt(90) + 20);
Draw();
}
@ -196,47 +223,63 @@ public class Form extends JFrame {
if (wheelCount > 0) {
Random random = new Random();
drawningSPAU = new DrawningSPAU(wheelCount, Integer.parseInt((String) comboBoxRoll.getSelectedItem()), random.nextInt(200) + 100,
(double) random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", color);
if (selectedColor != null) {
color = selectedColor;
}
drawningSPAU = new DrawningSPAU(wheelCount, Integer.parseInt((String) comboBoxRoll.getSelectedItem()),
random.nextInt(200) + 100,
(double) random.nextInt(2000) + 1000,
color,
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
drawningSPAU.SetPosition(random.nextInt(90) + 20, random.nextInt(90) + 20);
Draw();
}
}
protected void buttonStep_Click (ActionEvent e) {
if (drawningSPAU == null) {
return;
private void buttonSelectSPAU_Click(ActionEvent e) {
SelectedSPAU = drawningSPAU;
int result = JOptionPane.showConfirmDialog(null, "Добавить?", "Add SPAU", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.OK_OPTION) {
dispose();
}
}
if (comboBoxStrategy.isEnabled()) {
int selectedIndex = comboBoxStrategy.getSelectedIndex();
switch (selectedIndex) {
case 0:
abstractStrategy = new MoveToCenter();
break;
case 1:
abstractStrategy = new MoveToBorder();
break;
default:
abstractStrategy = null;
break;
protected void buttonStep_Click(ActionEvent e) {
if (drawningSPAU == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
int selectedIndex = comboBoxStrategy.getSelectedIndex();
switch (selectedIndex) {
case 0:
abstractStrategy = new MoveToCenter();
break;
case 1:
abstractStrategy = new MoveToBorder();
break;
default:
abstractStrategy = null;
break;
}
if (abstractStrategy == null) {
return;
}
abstractStrategy.setData(new DrawningObjectSPAU(drawningSPAU), pictureBoxSPAU.getWidth(),
pictureBoxSPAU.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (abstractStrategy == null) {
return;
}
abstractStrategy.setData(new DrawningObjectSPAU(drawningSPAU), pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (abstractStrategy == null) {
return;
}
abstractStrategy.makeStep();
Draw();
if (abstractStrategy.getStatus() == Status.Finish) {
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
abstractStrategy.makeStep();
Draw();
if (abstractStrategy.getStatus() == Status.Finish) {
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
}
protected void buttonMoveActionPerformed(Object sender, ActionEvent e) {

View File

@ -0,0 +1,124 @@
package SelfPropelledArtilleryUnit;
import javax.swing.*;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
import SelfPropelledArtilleryUnit.Generics.SPAUGenericCollection;
import SelfPropelledArtilleryUnit.MovementStrategy.DrawningObjectSPAU;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FormSPAUCollection extends JFrame {
private JPanel contentPane;
private JLabel pictureBoxCollection;
private JFormattedTextField maskedTextBoxNumber;
private JButton ButtonAddSPAU;
private JButton ButtonRemoveSPAU;
private JButton ButtonRefreshCollection;
private final int countPlaces = 12;
private final SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU> _SPAUs;
public FormSPAUCollection() {
initComponents();
_SPAUs = new SPAUGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
}
private void initComponents() {
contentPane = new JPanel();
pictureBoxCollection = new JLabel();
maskedTextBoxNumber = new JFormattedTextField();
ButtonAddSPAU = new JButton();
ButtonRemoveSPAU = new JButton();
ButtonRefreshCollection = new JButton();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 899, 456);
contentPane.setLayout(null);
pictureBoxCollection.setBounds(12, 12, 680, 432);
contentPane.add(pictureBoxCollection);
maskedTextBoxNumber.setBounds(729, 51, 143, 27);
contentPane.add(maskedTextBoxNumber);
ButtonAddSPAU.setBounds(748, 102, 102, 33);
ButtonAddSPAU.setText("Добавить");
ButtonAddSPAU.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonAddSPAU_Click(e);
}
});
contentPane.add(ButtonAddSPAU);
ButtonRemoveSPAU.setBounds(750, 141, 100, 37);
ButtonRemoveSPAU.setText("Удалить");
ButtonRemoveSPAU.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonRemoveSPAU_Click(e);
}
});
contentPane.add(ButtonRemoveSPAU);
ButtonRefreshCollection.setBounds(750, 195, 100, 35);
ButtonRefreshCollection.setText("Обновить");
ButtonRefreshCollection.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonRefreshCollection_Click(e);
}
});
contentPane.add(ButtonRefreshCollection);
setContentPane(contentPane);
}
private void ButtonAddSPAU_Click(ActionEvent e) {
FormSPAU form = new FormSPAU();
form.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
int addedIndex = _SPAUs.add(form.GetSelectedSPAU());
if (addedIndex != -1 && addedIndex <= countPlaces) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
ImageIcon iconSPAU = new ImageIcon(_SPAUs.showSPAUs());
pictureBoxCollection.setIcon(iconSPAU);
} else {
JOptionPane.showMessageDialog(contentPane, "Не удалось добавить объект");
}
form.dispose();
}
});
int result = JOptionPane.showConfirmDialog(null, "Add SPAU?", "Add SPAU", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
form.setSize(900, 500);
form.setVisible(true);
}
}
private void ButtonRemoveSPAU_Click(ActionEvent e) {
if (JOptionPane.showConfirmDialog(this, "Удалить объект?", "Удаление",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
int pos;
try {
pos = Integer.parseInt(maskedTextBoxNumber.getText());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
return;
}
if (_SPAUs.remove(pos)) {
JOptionPane.showMessageDialog(this, "Объект удален");
pictureBoxCollection.setIcon(new ImageIcon(_SPAUs.showSPAUs()));
} else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
}
}
}
private void ButtonRefreshCollection_Click(ActionEvent e) {
pictureBoxCollection.setIcon(new ImageIcon(_SPAUs.showSPAUs()));
}
}

View File

@ -0,0 +1,60 @@
package SelfPropelledArtilleryUnit.Generics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
import SelfPropelledArtilleryUnit.DrawningObjects.IDrawable;
import SelfPropelledArtilleryUnit.Entities.EntitySPAU;
public class RandomParts<T extends EntitySPAU, I extends IDrawable> {
private ArrayList<T> entities;
private ArrayList<I> interfaceDops;
private int CntOfEnt;
private int MaxCntOfEnt;
private int CntOfDops;
private int MaxCntOfDops;
private int WidthOfPanel;
private int HeightOfPanel;
public RandomParts(int maxCountOfBuses, int maxCountOfDoors, int width, int height) {
MaxCntOfEnt = maxCountOfBuses;
MaxCntOfDops = maxCountOfDoors;
entities = new ArrayList<T>(MaxCntOfEnt);
interfaceDops = new ArrayList<I>(MaxCntOfDops);
CntOfEnt = 0;
CntOfDops = 0;
WidthOfPanel = width;
HeightOfPanel = height;
}
//Полиморфные методы добавления в массив
public boolean Add(T bus) {
if (bus == null || CntOfEnt >= MaxCntOfEnt) {
return false;
}
entities.add(CntOfEnt++, bus);
return true;
}
public boolean Add(I door) {
if (door == null || CntOfDops >= MaxCntOfDops) {
return false;
}
interfaceDops.add(CntOfDops++, door);
return true;
}
public DrawningSPAU CreateObject() {
if (CntOfEnt == 0 || CntOfDops == 0) {
return null;
}
Random rand = new Random();
int indexOfEntity = rand.nextInt(0, CntOfEnt);
int indexOfDop = rand.nextInt(0, CntOfDops);
T ent = entities.get(indexOfEntity);
I dop = interfaceDops.get(indexOfDop);
return new DrawningSPAU(ent, dop, WidthOfPanel, HeightOfPanel);
}
}

View File

@ -0,0 +1,84 @@
package SelfPropelledArtilleryUnit.Generics;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
import SelfPropelledArtilleryUnit.MovementStrategy.IMoveableObject;
public class SPAUGenericCollection<T extends DrawningSPAU, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 210;
private final int _placeSizeHeight = 90;
private final SetGeneric<T> _collection;
public SPAUGenericCollection(int picWidth, int picHeight) {
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = 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) {
if (pos < 0 || pos >= _collection.size()) {
return false;
}
_collection.remove(pos);
return true;
}
public U getU(int pos) {
if (pos < 0 || pos >= _collection.size()) {
return null;
}
return (U) _collection.get(pos).GetMoveableObject();
}
public BufferedImage showSPAUs() {
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = bmp.getGraphics();
drawBackground(g);
drawObjects(g);
return bmp;
}
private void drawBackground(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, _pictureWidth, _pictureHeight); // заполняем весь прямоугольник белым цветом
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) {
int j = 0;
int stringCount = 0;
DrawningSPAU current;
for (int i = 0; i < _collection.size(); i++) {
current = _collection.get(i);
current.SetPosition(stringCount * 200, 280 - j * 100);
stringCount++;
if (stringCount >= 3) {
j++;
stringCount = 0;
}
current.DrawTransport(g);
System.out.print(i + " ");
}
}
}

View File

@ -0,0 +1,63 @@
package SelfPropelledArtilleryUnit.Generics;
public class SetGeneric<T> {
private final T[] places;
public int count;
public int size = 0;
public SetGeneric(int count) {
this.count = count;
places = (T[]) new Object[count];
}
public int insert(T spau) {
return insert(spau, 0);
}
public int size(){
return size;
}
public int insert(T spau, int position) {
if (position < 0 || spau == null) {
return -1;
}
if (position >= count) {
return -1;
}
int positionNull = -1;
for (int i = position; i < count; i++) {
if (places[i] == null) {
positionNull = i;
break;
}
}
if (positionNull == -1 && places[count - 1] != null) {
return -1;
} else if (positionNull == -1) {
positionNull = count - 1;
}
for (int i = positionNull; i > position; i--) {
places[i] = places[i - 1];
}
places[position] = spau;
size++;
return position;
}
public boolean remove(int position) {
if (position < 0 || position >= count)
return false;
places[position] = null;
size--;
return true;
}
public T get(int position) {
if (position < 0 || position >= count)
return null;
return places[position];
}
}

View File

@ -4,7 +4,8 @@ import javax.swing.*;
public class SelfPropelledArtilleryUnit {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Form form = new Form();
FormSPAUCollection form = new FormSPAUCollection();
FormHard formHard = new FormHard();
form.setSize(900, 500);
form.setVisible(true);
});