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

This commit is contained in:
DjonniStorm 2024-05-06 01:12:23 +04:00
parent 654d2e36e0
commit 88daf53b25
12 changed files with 255 additions and 83 deletions

View File

@ -14,24 +14,21 @@ public abstract class AbstractCompany {
protected int _pictureHeight;
public ICollectionGenericObjects<DrawningTruck> _collection = null;
private int GetMaxCount() {
return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
return _pictureWidth / _placeSizeWidth * (_pictureHeight / _placeSizeHeight / 2) - 1;
};
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningTruck> collection) {
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = collection;
_collection.SetMaxCount(GetMaxCount());
_collection.SetMaxCount(GetMaxCount(), (Class) DrawningTruck.class);
}
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);
public void Show(Graphics g) {
DrawBackground(g);
SetObjectsPosition();
for (int i = 0; i < _collection.Count(); i++) {
@ -40,7 +37,6 @@ public abstract class AbstractCompany {
obj.DrawTransport(g);
}
}
return panel;
}
protected abstract void DrawBackground(Graphics g);

View File

@ -0,0 +1,63 @@
package CollectionGenericObjects;
import Drawnings.CountWheels;
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import Drawnings.IDrawningWheels;
import Entities.EntityCleaningCar;
import Entities.EntityTruck;
import java.lang.reflect.Array;
import java.util.Random;
public class AdditionalCollection <T extends EntityTruck, U extends IDrawningWheels>{
private T[] _collectionEntity;
private U[] _collectionWheels;
public AdditionalCollection(int value, Class<T> type1, Class<T> type2) {
_collectionEntity = (T[]) Array.newInstance(type1, value);
_collectionWheels = (U[]) Array.newInstance(type2, value);
}
public int GetCountEntities() {
return _collectionEntity.length;
}
public int GetCountWheels() {
return _collectionWheels.length;
}
public int Insert(T entity) {
for (int i = 0; i < GetCountEntities(); i++)
{
if (_collectionEntity[i] == null)
{
_collectionEntity[i] = entity;
return i;
}
}
return -1;
}
public int Insert(U decks) {
for (int i = 0; i < GetCountWheels(); i++)
{
if (_collectionWheels[i] == null)
{
_collectionWheels[i] = decks;
return i;
}
}
return -1;
}
public DrawningTruck CreateFromAdditionalCollection() {
Random random = new Random();
if (_collectionEntity == null || _collectionWheels == null) return null;
T entity = _collectionEntity[random.nextInt(GetCountEntities())];
U wheels = _collectionWheels[random.nextInt(GetCountWheels())];
DrawningTruck drawingTruck = null;
if (entity instanceof EntityCleaningCar) {
drawingTruck = new DrawningCleaningCar((EntityCleaningCar) entity, wheels);
}
else {
drawingTruck = new DrawningTruck(entity, wheels);
}
return drawingTruck;
}
}

View File

@ -10,16 +10,17 @@ public class AutoParkService extends AbstractCompany {
}
@Override
protected void DrawBackground(Graphics g) {
// Pen pen = new Pen(Color.Black, 4f);
g.setColor(Color.black);
((Graphics2D) g).setStroke(new BasicStroke(4));
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);
g.drawLine(j * _placeSizeWidth, i * _placeSizeHeight * 2, j * _placeSizeWidth, i * _placeSizeHeight * 2 + _placeSizeHeight + 20);
}
}
((Graphics2D) g).setStroke(new BasicStroke(1));
}
@Override

View File

@ -2,7 +2,7 @@ package CollectionGenericObjects;
public interface ICollectionGenericObjects<T> {
int Count();
void SetMaxCount(int value);
void SetMaxCount(int value, Class<T> type);
int Insert(T obj);
int Insert(T obj, int position);
T Remove(int position);

View File

@ -1,86 +1,71 @@
package CollectionGenericObjects;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
private ArrayList<T> _collection;
private T[] _collection;
@Override
public int Count() {
return _collection.size();
}
@Override
public void SetMaxCount(int value) {
public void SetMaxCount(int value, Class<T> type) {
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);
}
}
_collection = (T[]) Array.newInstance(type, value);
}
}
public MassiveGenericObjects() {
_collection = new ArrayList<>();
@Override
public int Count() {
return _collection.length;
}
@Override
public int Insert(T obj) {
for (int i = 0; i < Count(); i++) {
if (_collection.get(i) == null) {
_collection.add(i, obj);
for (int i = 0; i < Count(); i++)
{
if (_collection[i] == null)
{
_collection[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);
if (_collection[position] == null)
{
_collection[position] = obj;
return position;
}
int tmp = position + 1;
while (tmp < Count()) {
if (_collection.get(tmp) == null) {
_collection.add(tmp, obj);
return tmp;
int temp = position + 1;
while(temp < Count())
{
if (_collection[temp] == null)
{
_collection[temp] = obj;
return temp;
}
++tmp;
++temp;
}
tmp = position - 1;
while (tmp >= 0) {
if (_collection.get(tmp) == null) {
_collection.add(tmp, obj);
return tmp;
temp = position - 1;
while(temp >= 0)
{
if (_collection[temp] == null)
{
_collection[temp] = obj;
return temp;
}
--tmp;
--temp;
}
return -1;
}
@Override
public T Remove(int position) {
if (position >= Count() || position < 0) return null;
T myObject = _collection.get(position);
_collection.add(position, null);
T myObject = (T) _collection[position];
_collection[position] = null;
return myObject;
}
@Override
public T Get(int position) {
if (position < 0 || position >= Count()) return null;
return _collection.get(position);
return (T) _collection[position];
}
}

View File

@ -12,6 +12,11 @@ public class DrawningCleaningCar extends DrawningTruck {
super.SetWheelsType();
}
public DrawningCleaningCar(EntityCleaningCar cleaningCar, IDrawningWheels drawningWheels) {
EntityTruck = cleaningCar;
this.drawningWheels = drawningWheels;
}
@Override
public void DrawTransport(Graphics g) {
if (EntityTruck == null || !(EntityTruck instanceof EntityCleaningCar cleaningCar) || _startPosX == null || _startPosY == null) {

View File

@ -22,13 +22,17 @@ public class DrawningTruck {
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _drawningCarWidth;}
public int GetHeight(){return _drawningCarHeight;}
private DrawningTruck() {
protected DrawningTruck() {
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public DrawningTruck(EntityTruck truck, IDrawningWheels drawningWheels) {
EntityTruck = truck;
this.drawningWheels = drawningWheels;
}
protected void SetWheelsType() {
Random random = new Random();
switch (random.nextInt(1, 4)) {

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormAdditionalCollection">
<grid id="27dc6" binding="defaultPanel" layout-manager="GridLayoutManager" row-count="3" column-count="3" 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="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="6ae24">
<constraints>
<grid row="2" column="1" 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="d0d65" class="javax.swing.JList" binding="listEntity">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
<maximum-size width="-1" height="200"/>
</grid>
</constraints>
<properties/>
</component>
<component id="3d508" class="javax.swing.JList" binding="listWheels">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
<maximum-size width="-1" height="200"/>
</grid>
</constraints>
<properties/>
</component>
<component id="6d472" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="100"/>
<maximum-size width="200" height="100"/>
</grid>
</constraints>
<properties>
<font name="Roboto Light" size="28" style="1"/>
<text value="Создать"/>
</properties>
</component>
<grid id="d47fe" 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"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<vspacer id="ff720">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>

View File

@ -0,0 +1,27 @@
import CollectionGenericObjects.AdditionalCollection;
import Drawnings.IDrawningWheels;
import Entities.EntityTruck;
import javax.swing.*;
public class FormAdditionalCollection extends JFrame {
private JPanel defaultPanel;
private JButton buttonCreate;
private JList listEntity;
private JList listWheels;
private AdditionalCollection<EntityTruck, IDrawningWheels> additionalCollection;
public FormAdditionalCollection() {
setTitle("Создать форму, где использовался бы экземпляр от описанного выше " +
"параметризованного класса и выдавал сгенерированные экземпляры " +
"класса прорисовки с выводом результата на форму");
setLocation(200, 100);
pack();
setVisible(true);
add(defaultPanel);
setSize(1000, 600);
additionalCollection = new AdditionalCollection<>(3, (Class) EntityTruck.class, (Class) IDrawningWheels.class);
}
}

View File

@ -23,13 +23,24 @@ public class FormCleaningCar extends JFrame {
private List<JComponent> controls;
private AbstractStrategy _strategy;
@Override
public void paint(Graphics g) {
super.paint(g);
if (_drawningTruck.getEntityTruck() == null ||
pictureBox.getWidth() == 0 || pictureBox.getHeight() == 0)
return;
Graphics e = pictureBox.getGraphics();
e.setColor(pictureBox.getBackground());
e.fillRect(0,0, pictureBox.getWidth(), pictureBox.getHeight());
_drawningTruck.DrawTransport(e);
RepaintControls();
}
public void SetTruck(DrawningTruck truck) {
_drawningTruck = truck;
_drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight());
comboBoxStrategy.setEnabled(true);
_strategy = null;
Draw();
repaint(); //по-идее вызовет paint()
}
public FormCleaningCar() {
setSize(1000, 600);
@ -37,7 +48,6 @@ public class FormCleaningCar extends JFrame {
setTitle("Project Cleaning Car");
add(pictureBox);
setVisible(true);
pictureBox.setSize(getWidth(), getHeight());
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");

View File

@ -16,19 +16,9 @@
<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"/>
<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="Добавление уборочной машины"/>
@ -36,7 +26,7 @@
</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">
<grid row="3" 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>
@ -44,7 +34,7 @@
</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"/>
<grid row="4" 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="Удалить грузовик"/>
@ -52,7 +42,7 @@
</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"/>
<grid row="1" 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="Добавление грузовика"/>
@ -60,7 +50,7 @@
</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">
<grid row="0" 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>
@ -70,7 +60,7 @@
</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"/>
<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="Передать на тесты"/>
@ -78,12 +68,20 @@
</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"/>
<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="5f2f8" class="javax.swing.JButton" binding="buttonAdditionalCollection">
<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">

View File

@ -9,7 +9,6 @@ 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;
@ -19,7 +18,6 @@ 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;
@ -28,6 +26,8 @@ public class FormCleaningCarCollection extends JFrame{
private JButton buttonDel;
private JButton buttonGoToCheck;
private JButton buttonRefresh;
private JButton buttonAdditionalCollection;
public FormCleaningCarCollection() {
setTitle("Коллекция уборочных машин");
setSize(1000, 600);
@ -38,6 +38,7 @@ public class FormCleaningCarCollection extends JFrame{
tools.setBackground(new Color(220, 220, 220));
tools.setBorder(BorderFactory.createTitledBorder("Инструменты"));
comboBoxSelectorCompany.addItem(new String(""));
comboBoxSelectorCompany.addItem(new String("Автопарк"));
//mask
@ -88,13 +89,14 @@ public class FormCleaningCarCollection extends JFrame{
int pos = parseInt(maskedTextBox.getText());
if (_company._collection.Remove(pos) != null) {
JOptionPane.showMessageDialog(null, "Объект удалён");
repaint();
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
break;
case 1:
default:
return;
}
}
@ -121,6 +123,17 @@ public class FormCleaningCarCollection extends JFrame{
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null) return;
repaint();
}
});
buttonAdditionalCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
FormAdditionalCollection formAdditionalCollection = new FormAdditionalCollection();
}
});
@ -143,7 +156,6 @@ public class FormCleaningCarCollection extends JFrame{
}
if (_company._collection.Insert(drawningTruck, 0) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
pictureBox = _company.Show(pictureBox);
repaint();
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
@ -154,4 +166,13 @@ public class FormCleaningCarCollection extends JFrame{
Color colorChooser = JColorChooser.showDialog(this, "Выберите цвет", color);
return colorChooser;
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_company == null) return;
Graphics e = pictureBox.getGraphics();
e.setColor(pictureBox.getBackground());
e.fillRect(0,0, pictureBox.getWidth(), pictureBox.getHeight());
_company.Show(e);
}
}