Лабораторная работа №3 (часть 1)

This commit is contained in:
DjonniStorm 2024-05-05 00:25:32 +04:00
parent 33272174d0
commit 654d2e36e0
10 changed files with 484 additions and 71 deletions

View File

@ -2,7 +2,10 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/ProjectCleaningCar/Resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/ProjectCleaningCar/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

@ -0,0 +1,48 @@
package CollectionGenericObjects;
import Drawnings.DrawningTruck;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class AbstractCompany {
protected final int _placeSizeWidth = 210;
protected final int _placeSizeHeight = 80;
protected int _pictureWidth;
protected int _pictureHeight;
public ICollectionGenericObjects<DrawningTruck> _collection = null;
private int GetMaxCount() {
return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
};
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningTruck> collection) {
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = collection;
_collection.SetMaxCount(GetMaxCount());
}
public DrawningTruck GetRandomObject() {
Random rnd = new Random();
return _collection.Get(rnd.nextInt(GetMaxCount()));
}
public JPanel Show(JPanel panel) {
BufferedImage bitmap = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = bitmap.createGraphics();
panel.paint(g);
DrawBackground(g);
SetObjectsPosition();
for (int i = 0; i < _collection.Count(); i++) {
DrawningTruck obj = _collection.Get(i);
if (obj != null) {
obj.DrawTransport(g);
}
}
return panel;
}
protected abstract void DrawBackground(Graphics g);
protected abstract void SetObjectsPosition();
}

View File

@ -0,0 +1,50 @@
package CollectionGenericObjects;
import Drawnings.DrawningTruck;
import java.awt.*;
public class AutoParkService extends AbstractCompany {
public AutoParkService(int picWidth, int picHeight, ICollectionGenericObjects<DrawningTruck> collection) {
super(picWidth, picHeight, collection);
}
@Override
protected void DrawBackground(Graphics g) {
// Pen pen = new Pen(Color.Black, 4f);
g.setColor(Color.black);
for (int i = 0; i < _pictureHeight / _placeSizeHeight / 2; i++)
{
g.drawLine(0, i * _placeSizeHeight * 2, _pictureWidth / _placeSizeWidth * _placeSizeWidth, i * _placeSizeHeight * 2);
for (int j = 0; j < _pictureWidth / _placeSizeWidth + 1; ++j)
{
g.drawLine(j * _placeSizeWidth, i * _placeSizeHeight * 2, j * _placeSizeWidth, i * _placeSizeHeight * 2 + _placeSizeHeight);
}
}
}
@Override
protected void SetObjectsPosition() {
int nowWidth = 0;
int nowHeight = 0;
for (int i = 0; i < _collection.Count(); i++)
{
if (nowHeight > _pictureHeight / _placeSizeHeight)
{
return;
}
if (_collection.Get(i) != null)
{
_collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
_collection.Get(i).SetPosition(_placeSizeWidth * nowWidth + 30, nowHeight * _placeSizeHeight * 2 + 20);
}
if (nowWidth < _pictureWidth / _placeSizeWidth - 1) nowWidth++;
else
{
nowWidth = 0;
nowHeight++;
}
}
}
}

View File

@ -0,0 +1,10 @@
package CollectionGenericObjects;
public interface ICollectionGenericObjects<T> {
int Count();
void SetMaxCount(int value);
int Insert(T obj);
int Insert(T obj, int position);
T Remove(int position);
T Get(int position);
}

View File

@ -0,0 +1,86 @@
package CollectionGenericObjects;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
private ArrayList<T> _collection;
@Override
public int Count() {
return _collection.size();
}
@Override
public void SetMaxCount(int value) {
if (value > 0) {
if (!_collection.isEmpty()) {
ArrayList<T> tmp = new ArrayList<>(value);
tmp.addAll(0, _collection);
_collection = tmp;
for (int i = 0; ; i++) {
_collection.add(null);
}
} else {
_collection = new ArrayList<>(value);
for (int i = 0; i < value; i++) {
_collection.add(null);
}
}
}
}
public MassiveGenericObjects() {
_collection = new ArrayList<>();
}
@Override
public int Insert(T obj) {
for (int i = 0; i < Count(); i++) {
if (_collection.get(i) == null) {
_collection.add(i, obj);
return i;
}
}
return -1;
}
@Override
public int Insert(T obj, int position) {
if (position >= Count() || position < 0) return -1;
if (_collection.get(position) == null) {
_collection.add(position, obj);
return position;
}
int tmp = position + 1;
while (tmp < Count()) {
if (_collection.get(tmp) == null) {
_collection.add(tmp, obj);
return tmp;
}
++tmp;
}
tmp = position - 1;
while (tmp >= 0) {
if (_collection.get(tmp) == null) {
_collection.add(tmp, obj);
return tmp;
}
--tmp;
}
return -1;
}
@Override
public T Remove(int position) {
if (position >= Count() || position < 0) return null;
T myObject = _collection.get(position);
_collection.add(position, null);
return myObject;
}
@Override
public T Get(int position) {
if (position < 0 || position >= Count()) return null;
return _collection.get(position);
}
}

View File

@ -29,17 +29,6 @@
<grid row="0" column="0" row-span="3" col-span="5" vsize-policy="7" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="3587c" class="javax.swing.JButton" binding="buttonCreateCleaningCar">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="100" height="60"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<text value="Создать уборочную машину"/>
</properties>
</component>
<component id="1fedd" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="6" row-span="1" col-span="2" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
@ -61,17 +50,6 @@
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="7d444" class="javax.swing.JButton" binding="buttonCreateTruck">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="120" height="60"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<text value="Создать грузовик"/>
</properties>
</component>
<hspacer id="121e4">
<constraints>
<grid row="3" column="2" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">

View File

@ -13,38 +13,37 @@ import java.util.LinkedList;
public class FormCleaningCar extends JFrame {
protected DrawningTruck _drawningTruck;
JPanel pictureBox;
private JButton buttonCreateCleaningCar;
private JPanel pictureBox;
private JButton buttonRight;
private JButton buttonLeft;
private JButton buttonDown;
private JButton buttonUp;
private JButton buttonCreateTruck;
private JComboBox comboBoxStrategy;
private JButton buttonStrategyStep;
private List<JComponent> controls;
private AbstractStrategy _strategy;
public void SetTruck(DrawningTruck truck) {
_drawningTruck = truck;
_drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight());
comboBoxStrategy.setEnabled(true);
_strategy = null;
Draw();
}
public FormCleaningCar() {
setSize(1000, 600);
setLocationRelativeTo(null);
setTitle("Project Cleaning Car");
add(pictureBox);
setVisible(true);
pictureBox.setSize(getWidth(), getHeight());
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
comboBoxStrategy.setEnabled(false);
InitializeControlsRepaintList();
buttonCreateCleaningCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawningCleaningCar");
}
});
buttonCreateTruck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawningTruck");
}
});
ActionListener buttonMoveClickedListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -120,28 +119,6 @@ public class FormCleaningCar extends JFrame {
comboBoxStrategy.addItem("К центру");
comboBoxStrategy.addItem("К краю");
}
private void CreateObject(String type) {
Random random = new Random();
switch (type) {
case "DrawningTruck":
_drawningTruck = new DrawningTruck(random.nextInt(100, 300), random.nextDouble(100, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
break;
case "DrawningCleaningCar":
_drawningTruck = new DrawningCleaningCar(random.nextInt(100, 300), random.nextDouble(100, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(), random.nextBoolean());
break;
default: return;
}
_drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight());
_drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(0, 100));
_strategy = null;
comboBoxStrategy.setEnabled(true);
Draw();
}
private void Draw() {
if (_drawningTruck.getEntityTruck() == null ||
pictureBox.getWidth() == 0 || pictureBox.getHeight() == 0)
@ -150,7 +127,6 @@ public class FormCleaningCar extends JFrame {
g.setColor(pictureBox.getBackground());
g.fillRect(0,0, pictureBox.getWidth(), pictureBox.getHeight());
_drawningTruck.DrawTransport(g);
RepaintControls();
}
@ -161,8 +137,6 @@ public class FormCleaningCar extends JFrame {
}
private void InitializeControlsRepaintList() {
controls = new LinkedList<>();
controls.add(buttonCreateCleaningCar);
controls.add(buttonCreateTruck);
controls.add(buttonUp);
controls.add(buttonDown);

View File

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormCleaningCarCollection">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="964" height="661"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="ed6cf" binding="tools" layout-manager="GridLayoutManager" row-count="8" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="3599f" class="javax.swing.JLabel" binding="toolsNameLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<maximum-size width="-1" height="20"/>
</grid>
</constraints>
<properties>
<text value="Инструменты"/>
</properties>
</component>
<component id="112e" class="javax.swing.JButton" binding="buttonAddCleaningCar">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавление уборочной машины"/>
</properties>
</component>
<component id="8fa47" class="javax.swing.JFormattedTextField" binding="maskedTextBox">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="50" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="af87b" class="javax.swing.JButton" binding="buttonDel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить грузовик"/>
</properties>
</component>
<component id="5a95" class="javax.swing.JButton" binding="buttonAddTruck">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавление грузовика"/>
</properties>
</component>
<component id="f051c" class="javax.swing.JComboBox" binding="comboBoxSelectorCompany">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<maximum-size width="-1" height="70"/>
</grid>
</constraints>
<properties>
<toolTipText value="" noi18n="true"/>
</properties>
</component>
<component id="7c328" class="javax.swing.JButton" binding="buttonGoToCheck">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Передать на тесты"/>
</properties>
</component>
<component id="44aef" class="javax.swing.JButton" binding="buttonRefresh">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Обновить"/>
</properties>
</component>
</children>
</grid>
<grid id="abc4c" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="700" height="-1"/>
<preferred-size width="310" height="24"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,157 @@
import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.AutoParkService;
import CollectionGenericObjects.MassiveGenericObjects;
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.text.ParseException;
import java.util.Random;
import static java.lang.Integer.parseInt;
public class FormCleaningCarCollection extends JFrame{
private AbstractCompany _company = null;
private JPanel panel;
private JPanel tools;
private JLabel toolsNameLabel;
private JComboBox comboBoxSelectorCompany;
private JButton buttonAddTruck;
private JButton buttonAddCleaningCar;
private JPanel pictureBox;
private JFormattedTextField maskedTextBox;
private JButton buttonDel;
private JButton buttonGoToCheck;
private JButton buttonRefresh;
public FormCleaningCarCollection() {
setTitle("Коллекция уборочных машин");
setSize(1000, 600);
setLocation(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(panel);
tools.setBackground(new Color(220, 220, 220));
comboBoxSelectorCompany.addItem(new String(""));
comboBoxSelectorCompany.addItem(new String("Автопарк"));
//mask
MaskFormatter maskFormatter = null;
try {
maskFormatter = new MaskFormatter("##");
maskFormatter.setPlaceholder("00");
//это intellij
} catch (ParseException e) {
throw new RuntimeException(e);
}
MaskFormatter finalMaskFormatter = maskFormatter;
maskedTextBox.setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() {
@Override
public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
return finalMaskFormatter;
}
});
Init();
}
private void Init() {
//ComboBoxSelectorCompany_SelectedIndexChanged
comboBoxSelectorCompany.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (comboBoxSelectorCompany.getSelectedItem().toString()) {
case "Автопарк":
_company = new AutoParkService(pictureBox.getWidth(), pictureBox.getHeight(), new MassiveGenericObjects<DrawningTruck>());
System.out.println(pictureBox + "/" + pictureBox.getWidth() + "/" + pictureBox.getHeight());
break;
}
System.out.println(comboBoxSelectorCompany.getSelectedItem().toString());
}
});
//ButtonAddTruck_Click
buttonAddTruck.addActionListener(e -> CreateObject("DrawningTruck"));
//ButtonAddCleaningCar_Click
buttonAddCleaningCar.addActionListener(e -> CreateObject("DrawningCleaningCar"));
//ButtonDelTruck_Click
buttonDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null || maskedTextBox.getText() == null) return;
switch (JOptionPane.showConfirmDialog(null, "Удалить?", "Удаление", JOptionPane.YES_NO_OPTION)) {
case 0:
int pos = parseInt(maskedTextBox.getText());
if (_company._collection.Remove(pos) != null) {
JOptionPane.showMessageDialog(null, "Объект удалён");
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
break;
case 1:
return;
}
}
});
//ButtonGoToCheck_Click
buttonGoToCheck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null) return;
DrawningTruck truck = _company.GetRandomObject();
int counter = 100;
while (truck == null) {
truck = _company.GetRandomObject();
counter--;
if (counter <= 0) break;
}
if (truck == null) return;
FormCleaningCar formCleaningCar = new FormCleaningCar();
formCleaningCar.SetTruck(truck);
}
});
//ButtonRefresh_Click
buttonRefresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null) return;
}
});
}
private void CreateObject(String type) {
if (_company == null) return;
Random random = new Random();
DrawningTruck drawningTruck;
switch (type) {
case "DrawningTruck":
drawningTruck = new DrawningTruck(random.nextInt(100, 300), random.nextDouble(100, 3000),
GetColor(random));
break;
case "DrawningCleaningCar":
drawningTruck = new DrawningCleaningCar(random.nextInt(100, 300), random.nextDouble(100, 3000),
GetColor(random), GetColor(random),
random.nextBoolean(), random.nextBoolean(), random.nextBoolean());
break;
default: return;
}
if (_company._collection.Insert(drawningTruck, 0) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
pictureBox = _company.Show(pictureBox);
repaint();
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
private Color GetColor(Random random) {
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color colorChooser = JColorChooser.showDialog(this, "Выберите цвет", color);
return colorChooser;
}
}

View File

@ -1,13 +1,17 @@
import javax.swing.*;
import java.awt.*;
class Main {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(false);
JFrame frame = new JFrame("Project Cleaning Car");
frame.setContentPane(new FormCleaningCar().pictureBox);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(200, 200);
frame.pack();
frame.setSize(1000, 600);
frame.setVisible(true);
// JFrame.setDefaultLookAndFeelDecorated(false);
// JFrame frame = new JFrame("Project Cleaning Car");
// frame.setContentPane(new FormCleaningCarCollection().pictureBox);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setLocation(200, 200);
// frame.pack();
// frame.setSize(1000, 600);
// frame.setVisible(true);
FormCleaningCarCollection form = new FormCleaningCarCollection();
form.setVisible(true);
}
}