PIbd-11_Zubkova_S.V._LabWork03_Hard #3

Closed
sonyaz wants to merge 1 commits from LabWork03 into LabWork02
21 changed files with 768 additions and 109 deletions

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,54 @@
package CollectionGenericObjects;
import Drawings.DrawingTruck;
import java.awt.*;
public abstract class AbstractCompany {
/// Размер места (ширина)
protected int _placeSizeWidth = 190;
/// Размер места (высота)
protected int _placeSizeHeight = 100;
/// Ширина окна
protected int _pictureWidth;
/// Высота окна
protected int _pictureHeight;
/// Коллекция автомобилей
public ICollectionGenericObjects<DrawingTruck> _collection = null;
/// Вычисление максимального количества элементов, который можно разместить в окне
private int GetMaxCount(){
return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
}
/// Конструктор
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawingTruck> collection)
{
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = collection;
_collection.SetMaxCount(GetMaxCount(), (Class)DrawingTruck.class);
}
/// Получение случайного объекта из коллекции
public DrawingTruck GetRandomObject()
{
return _collection.Get((int)(Math.random()*GetMaxCount() + 0));
}
/// Вывод заднего фона
public abstract void DrawBackgound(Graphics g);
/// Расстановка объектов
protected abstract void SetObjectsPosition();
public void SetPosition()
{
SetObjectsPosition();
}
}

View File

@ -0,0 +1,61 @@
package CollectionGenericObjects;
import java.lang.reflect.Array;
import java.util.Random;
import Drawings.DrawingGasolineTanker;
import Drawings.DrawingTruck;
import Entities.EntityGasolineTanker;
import Entities.EntityTruck;
import Wheels.IDrawingWheels;
public class AdditionalCollection<T extends EntityTruck, U extends IDrawingWheels> {
public T[] _collectionEntity;
public U[] _collectionWheels;
public AdditionalCollection(int size, Class<T> type1, Class<T> type2) {
_collectionEntity = (T[]) Array.newInstance(type1, size);
_collectionWheels = (U[]) Array.newInstance(type2, size);
CountEntities = size;
CountWheels = size;
}
public int CountEntities;
public int CountWheels;
public int Insert(T entity) {
for (int i = 0; i < CountEntities; ++i) {
if (_collectionEntity[i] == null) {
_collectionEntity[i] = entity;
return i;
}
}
return -1;
}
public int Insert(U wheels) {
for (int i = 0; i < CountWheels; ++i) {
if (_collectionWheels[i] == null) {
_collectionWheels[i] = wheels;
return i;
}
}
return -1;
}
public DrawingTruck CreateAdditionalCollectionTruck() {
Random random = new Random();
if (_collectionEntity == null || _collectionWheels == null) {
return null;
}
T entity = _collectionEntity[random.nextInt(CountEntities)];
U wheels = _collectionWheels[random.nextInt(CountWheels)];
DrawingTruck drawingTruck = null;
if (entity instanceof EntityGasolineTanker) {
drawingTruck = new DrawingGasolineTanker((EntityGasolineTanker) entity, wheels);
} else {
drawingTruck = new DrawingTruck(entity, wheels);
}
return drawingTruck;
}
}

View File

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

View File

@ -0,0 +1,80 @@
package CollectionGenericObjects;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T> {
private T[] _collection;
private int Count;
public void SetMaxCount(int size, Class<T> type) {
if (size > 0) {
_collection = (T[]) Array.newInstance(type, size);
Count = size;
}
}
@Override
public int getCount() {
return Count;
}
@Override
public T Get(int position) {
if (position >= getCount() || position < 0) return null;
return (T) _collection[position];
}
@Override
public int Insert(T obj) {
int index = 0;
while (index < getCount())
{
if (_collection[index] == null)
{
_collection[index] = obj;
return index;
}
++index;
}
return -1;
}
@Override
public int Insert(T obj, int position) {
if (position >= getCount() || position < 0)
return -1;
if (_collection[position] == null) {
_collection[position] = obj;
return position;
}
int index = position + 1;
while (index < getCount())
{
if (_collection[index] == null)
{
_collection[index] = obj;
return index;
}
++index;
}
index = position - 1;
while (index >= 0)
{
if (_collection[index] == null)
{
_collection[index] = obj;
return index;
}
--index;
}
return -1;
}
@Override
public T Remove(int position) {
if (position >= getCount() || position < 0)
return null;
T obj = (T) _collection[position];
_collection[position] = null;
return obj;
}
}

View File

@ -0,0 +1,49 @@
package CollectionGenericObjects;
import Drawings.DrawingTruck;
import java.awt.*;
public class TruckSharingService extends AbstractCompany {
public TruckSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawingTruck> collection) {
super(picWidth, picHeight, collection);
}
@Override
public void DrawBackgound(Graphics g) {
int count_width = _pictureWidth / _placeSizeWidth; // кол-во мест в ширину
int count_height = _pictureHeight / _placeSizeHeight;
g.setColor(Color.BLACK);
for (int i = 0; i < count_width; i++) {
for (int j = 0; j < count_height+1; ++j) {
g.drawLine(i * _placeSizeWidth + 10, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth - 50, j * _placeSizeHeight); // вертикаль
g.drawLine(i * _placeSizeWidth + 10, j * _placeSizeHeight, i * _placeSizeWidth + 10, j * _placeSizeHeight + _placeSizeHeight);
}
}
}
@Override
protected void SetObjectsPosition() {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int positionWidth = 0;
int positionHeight = height - 1;
for (int i = 0; i < (_collection.getCount()); i++) {
if (_collection.Get(i) != null) {
_collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
_collection.Get(i).SetPosition(_placeSizeWidth * positionWidth + 20, positionHeight * _placeSizeHeight + 5);
}
if (positionWidth < width - 1) {
positionWidth++;
}
else {
positionWidth = 0;
positionHeight--;
}
if (positionHeight < 0) {
return;
}
}
}
}

View File

@ -0,0 +1,31 @@
package Drawings;
import javax.swing.*;
import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.ICollectionGenericObjects;
import CollectionGenericObjects.MassiveGenericObjects;
import Drawings.DrawingTruck;
import java.awt.*;
public class CanvasFormTruckCollection<T> extends JComponent {
public AbstractCompany company = null;
public void SetCollectionToCanvas(AbstractCompany company) {
this.company = company;
}
public CanvasFormTruckCollection () {};
public void paintComponent(Graphics g) {
super.paintComponents(g);
if (company == null || company._collection == null) {
return;
}
company.DrawBackgound(g);
for (int i = 0; i < company._collection.getCount(); i++) {
Graphics2D g2d = (Graphics2D) g;
T obj = (T) company._collection.Get(i);
if (obj instanceof DrawingTruck) {
((DrawingTruck) obj).DrawTransport(g2d);
}
}
super.repaint();
}
}

View File

@ -1,18 +1,23 @@
package Drawings;
import Entities.EntityGasolineTanker;
import Entities.EntityTruck;
import Wheels.IDrawingWheels;
import java.awt.*;
public class DrawingGasolineTanker extends DrawingTruck{
private IDrawingWheels drawingWheels;
public DrawingGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean gasTank, boolean signalBeacon, int number) {
super(105,70);
EntityTruck = new EntityGasolineTanker(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon, number);
public DrawingGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean gasTank, boolean signalBeacon) {
//super(105,70);
EntityTruck = new EntityGasolineTanker(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon);
DrawWheels();
}
@Override
public DrawingGasolineTanker(EntityGasolineTanker entity, IDrawingWheels wheels) {
EntityTruck = entity;
drawingWheels = wheels;
}
@Override
public void DrawTransport(Graphics2D g) {
if (EntityTruck == null || !(EntityTruck instanceof EntityGasolineTanker gasolineTanker) || _startPosX == null || _startPosY == null) {
return;

View File

@ -4,12 +4,13 @@ import Wheels.DrawingOrnamentSquare;
import Wheels.DrawingOrnamentHeart;
import Wheels.DrawingWheels;
import Wheels.IDrawingWheels;
import javax.swing.*;
import java.awt.*;
public class DrawingTruck extends JPanel{
private IDrawingWheels drawingWheels;
public IDrawingWheels drawingWheels;
public Entities.EntityTruck EntityTruck;
// Ширина окна
private Integer _pictureWidth;
@ -34,9 +35,10 @@ public class DrawingTruck extends JPanel{
public Integer GetWidth() {return _drawingTruckWidth;}
public Integer GetHeight() {return _drawingTruckHeight;}
protected void DrawWheels() {
int number = (int)(Math.random() * 4 + 0);
public void DrawWheels() {
int numWheels = (int)(Math.random() * 4 + 2);
switch ((int)(Math.random() * 3 + 1)) {
case 1:
drawingWheels = new DrawingWheels();
break;
@ -47,10 +49,10 @@ public class DrawingTruck extends JPanel{
drawingWheels = new DrawingOrnamentHeart();
break;
default:
number = 0;
drawingWheels = new DrawingOrnamentSquare();
break;
}
drawingWheels.setNumWheels(number);
drawingWheels.setNumWheels(numWheels);
}
protected DrawingTruck(){
_pictureWidth = null;
@ -59,17 +61,24 @@ public class DrawingTruck extends JPanel{
_startPosY = null;
}
public DrawingTruck(int speed, double weight, Color bodyColor, int number){
public DrawingTruck(int speed, double weight, Color bodyColor){
super();
EntityTruck = new EntityTruck(speed, weight, bodyColor);
DrawWheels();
}
public DrawingTruck(EntityTruck entity, IDrawingWheels wheels) {
EntityTruck = entity;
drawingWheels = wheels;
}
protected DrawingTruck(int drawingTruckWidth, int drawingTruckHeight)
{
_drawingTruckWidth = drawingTruckWidth;
_drawingTruckHeight = drawingTruckHeight;
super();
this._drawingTruckWidth = drawingTruckWidth;
this._drawingTruckHeight = drawingTruckHeight;
}
@ -188,8 +197,15 @@ public class DrawingTruck extends JPanel{
{
return;
}
// колеса
drawingWheels.drawWheels(g, Color.BLACK, _startPosX, _startPosY);
if (drawingWheels == null) {
g.setColor(Color.BLACK);
g.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g.fillOval(_startPosX + 25, _startPosY + 50, 20, 20);
g.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
}
else drawingWheels.drawWheels(g, Color.BLACK, _startPosX, _startPosY);
// нижняя платформа
g.setColor(EntityTruck.getBodyColor());

View File

@ -15,7 +15,7 @@ public class EntityGasolineTanker extends EntityTruck{
public boolean SignalBeacon;
public boolean getSignalBeacon() {return SignalBeacon;}
public EntityGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean signalBeacon, boolean gasTank, int numWheels){
public EntityGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean signalBeacon, boolean gasTank){
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
GasTank = gasTank;

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormAdditionalCollection">
<grid id="27dc6" row-count="1" column-count="1" layout-manager="GridLayoutManager">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
<grid row="0" column="0" row-span="1" 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>
</form>

View File

@ -0,0 +1,177 @@
import CollectionGenericObjects.AbstractCompany;
import Drawings.DrawingGasolineTanker;
import Drawings.DrawingTruck;
import Drawings.CanvasGasolineTanker;
import Entities.EntityGasolineTanker;
import Entities.EntityTruck;
import Wheels.DrawingOrnamentHeart;
import Wheels.DrawingOrnamentSquare;
import Wheels.DrawingWheels;
import Wheels.IDrawingWheels;
import CollectionGenericObjects.AdditionalCollection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.*;
import javax.swing.*;
public class FormAdditionalCollection extends JFrame{
private AbstractCompany company = null;
public DrawingTruck drawingTruck = null;
private DrawingTruck copyTruck = null;
private CanvasGasolineTanker canvasTruck = new CanvasGasolineTanker();
private AdditionalCollection<EntityTruck, IDrawingWheels> additionalCollection = null;
private Random random = new Random();
private JButton buttonGenerate = new JButton("Создать");
private JButton buttonGoToCollection = new JButton("Добавить в коллекцию");
private JList<String> listEntity = new JList<String>();
private JList<String> listWheels = new JList<String>();
public FormAdditionalCollection() {
setTitle("Случайные грузовики");
setSize(650, 400);
additionalCollection = new AdditionalCollection<EntityTruck, IDrawingWheels>(3, (Class) EntityTruck.class, (Class) IDrawingWheels.class);
AddEntities();
AddWheels();
buttonGoToCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(drawingTruck !=null){
company._collection.Insert(copyTruck);
FormTruckCollection.canvasShow();
}
}
});
buttonGenerate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
drawingTruck = additionalCollection.CreateAdditionalCollectionTruck();
drawingTruck.SetPictureSize(getWidth(), getHeight());
drawingTruck.SetPosition(50, 50);
canvasTruck._drawingTruck = drawingTruck;
canvasTruck.repaint();
if (drawingTruck instanceof DrawingGasolineTanker)
copyTruck = new DrawingGasolineTanker((EntityGasolineTanker) drawingTruck.EntityTruck, drawingTruck.drawingWheels);
else
copyTruck = new DrawingTruck(drawingTruck.EntityTruck, drawingTruck.drawingWheels);
String[] data1 = new String[additionalCollection.CountEntities];
for (int i = 0; i < additionalCollection.CountEntities; i++) {
EntityTruck entity = additionalCollection._collectionEntity[i];
data1[i] = ToString(entity);
}
String[] data2 = new String[additionalCollection.CountWheels];
for (int i = 0; i < additionalCollection.CountWheels; i++) {
IDrawingWheels wheels = additionalCollection._collectionWheels[i];
data2[i] = ToString(wheels);
}
listEntity.setListData(data1);
listWheels.setListData(data2);
}
});
buttonGoToCollection.setBounds(300,300,150,40);
buttonGenerate.setBounds(130, 300, 150, 40);
add(buttonGenerate);
add(buttonGoToCollection);
listEntity.setBounds(10, 200, 300, 60);
listWheels.setBounds(320, 200, 300, 60);
add(listEntity);
add(listWheels);
add(canvasTruck);
setVisible(true);
}
private String ToString(EntityTruck entity) {
String str = "";
if (entity instanceof EntityGasolineTanker) str += "EntityGasolineTanker ";
else str += "EntityTruck ";
str += entity.getBodyColor().toString();
return str;
}
private String ToString(IDrawingWheels wheels) {
if (wheels == null || wheels.getNumWheels() == null)
return "Simple Wheels";
String str = "Wheels ";
if (wheels instanceof DrawingWheels) str += "Simple Wheels ";
else if (wheels instanceof DrawingOrnamentHeart) str += "OrnamentHeart ";
else if (wheels instanceof DrawingOrnamentSquare) str += "OrnamentSquare ";;
str += wheels.getNumWheels().toString();
return str;
}
public void AddEntities() {
for (int i = 0; i < additionalCollection.CountEntities; i++) {
random = new Random();
int speed = random.nextInt(100, 300);
double weight = random.nextInt(1000, 3000);
//int number = random.nextInt(0,4);
Color bodyColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
EntityTruck entityTruck;
if (random.nextBoolean()) {
entityTruck = new EntityTruck(speed, weight, bodyColor);
}
else {
Color additionalColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
boolean gasTank = random.nextBoolean();
boolean signalBeacon = random.nextBoolean();
entityTruck = new EntityGasolineTanker(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon);
}
additionalCollection.Insert(entityTruck);
}
}
public void AddWheels() {
for (int i = 0; i < additionalCollection.CountWheels; i++) {
random = new Random();
EntityTruck entity = additionalCollection._collectionEntity[i];
Integer number = random.nextInt(0, 4);
IDrawingWheels drawingWheels = null;
switch (random.nextInt(0,4)) {
case 1:
drawingWheels = new DrawingWheels();
break;
case 2:
drawingWheels = new DrawingOrnamentSquare();
break;
case 3:
drawingWheels = new DrawingOrnamentHeart();
break;
default:
number = null;
break;
}
if (drawingWheels != null) drawingWheels.setNumWheels(number);
additionalCollection.Insert(drawingWheels);
}
}
void setCompany(AbstractCompany company){
this.company = company;
String[] data1 = new String[additionalCollection.CountEntities];
for (int i = 0; i < additionalCollection.CountEntities; i++) {
EntityTruck entity = additionalCollection._collectionEntity[i];
data1[i] = ToString(entity);
}
String[] data2 = new String[additionalCollection.CountWheels];
for (int i = 0; i < additionalCollection.CountWheels; i++) {
IDrawingWheels wheels = additionalCollection._collectionWheels[i];
data2[i] = ToString(wheels);
}
listEntity.setListData(data1);
listWheels.setListData(data2);
}
}

View File

@ -8,14 +8,6 @@
<properties/>
<border type="none"/>
<children>
<component id="6cce4" class="javax.swing.JButton" binding="buttonCreateTruck">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать грузовик"/>
</properties>
</component>
<vspacer id="b7eca">
<constraints>
<grid row="0" column="0" row-span="2" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
@ -57,14 +49,6 @@
<text value=""/>
</properties>
</component>
<component id="62c53" class="javax.swing.JButton" binding="buttonCreateGasolineTanker">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать бензовоз"/>
</properties>
</component>
<component id="fabd7" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="9" fill="1" indent="0" use-parent-layout="false"/>

View File

@ -14,46 +14,16 @@ public class FormGasolineTanker extends JFrame{
private int Width;
private int Height;
private CanvasGasolineTanker canvasGasolineTanker = new CanvasGasolineTanker();
private JButton buttonCreateTruck = new JButton("Создать грузовик");
private JButton buttonRight = new JButton();
private JButton buttonLeft = new JButton();
private JButton buttonDown = new JButton();
private JButton buttonUp = new JButton();
private AbstractStrategy _strategy;
private JButton buttonCreateGasolineTanker = new JButton("Создать бензовоз");
private JComboBox comboBoxStrategy = new JComboBox(new String[] {"К центру", "К краю"});
private JButton buttonStrategy = new JButton("Шаг");
private void CreateObject(String typeOfClass){
int StartPositionX = 10 + (int)(Math.random() *((100-10) +1));
int StartPositionY = 10 + (int)(Math.random() *((100-10) +1));
int speed = 10 + (int)(Math.random() *((100-10) +1));
double weight = 1000 + (double)(Math.random() *((5000-1000) +1));
Color bodyColor = new Color((int) (Math.random() * ((256) + 1)), (int) (Math.random() * ((256) + 1)), (int) (Math.random() * ((256) + 1)));
int number = new Random().nextInt(3)+1;
switch (typeOfClass){
case "DrawingTruck":
canvasGasolineTanker._drawingTruck = new DrawingTruck(speed, weight, bodyColor, number);
canvasGasolineTanker._drawingTruck.SetPictureSize(Width,Height);
canvasGasolineTanker._drawingTruck.SetPosition(StartPositionX, StartPositionY);
canvasGasolineTanker.repaint();
break;
case "DrawingGasolineTanker":
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
boolean gasTank = new Random().nextBoolean();
boolean signalBeacon = new Random().nextBoolean();
canvasGasolineTanker._drawingTruck = new DrawingGasolineTanker(speed, weight, bodyColor, additionalColor, signalBeacon, gasTank, number);
canvasGasolineTanker._drawingTruck.SetPictureSize(Width,Height);
canvasGasolineTanker._drawingTruck.SetPosition(StartPositionX, StartPositionY);
canvasGasolineTanker.repaint();
break;
default: return;
}
_strategy = null;
comboBoxStrategy.setEnabled(true);
}
public void Init(){
public void Init(DrawingTruck truck){
setTitle("Бензовоз");
setSize(900,600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
@ -62,8 +32,7 @@ public class FormGasolineTanker extends JFrame{
Height = getHeight();
_strategy = null;
buttonCreateTruck.setName("CREATETRUCK");
buttonCreateGasolineTanker.setName("CREATEGASOLINETANKER");
canvasGasolineTanker._drawingTruck = truck;
buttonUp.setName("buttonUp");
Icon iconUp = new ImageIcon("src\\src\\resources\\Up.jpg");
@ -81,19 +50,6 @@ public class FormGasolineTanker extends JFrame{
Icon iconRight = new ImageIcon("src\\src\\resources\\Right.jpg");
buttonRight.setIcon(iconRight);
buttonCreateTruck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingTruck");
}
});
buttonCreateGasolineTanker.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingGasolineTanker");
}
});
buttonStrategy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -161,9 +117,6 @@ public class FormGasolineTanker extends JFrame{
setLayout(null);
canvasGasolineTanker.setBounds(0,0, getWidth(), getHeight());
buttonCreateTruck.setBounds(10, getHeight() - 90, 150, 40);
buttonCreateGasolineTanker.setBounds(170, getHeight() - 90, 150, 40);
buttonUp.setBounds(getWidth() - 200, getHeight() - 160, 45, 45);
buttonDown.setBounds(getWidth() - 140, getHeight() - 100, 45, 45);
buttonRight.setBounds(getWidth() - 80, getHeight() - 100, 45, 45);
@ -171,8 +124,7 @@ public class FormGasolineTanker extends JFrame{
comboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
buttonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
add(buttonCreateTruck);
add(buttonCreateGasolineTanker);
add(buttonUp);
add(buttonDown);
add(buttonRight);
@ -189,19 +141,14 @@ public class FormGasolineTanker extends JFrame{
if (canvasGasolineTanker._drawingTruck != null)
canvasGasolineTanker._drawingTruck.SetPictureSize(Width, Height);
canvasGasolineTanker.setBounds(0,0, getWidth(), getHeight());
buttonCreateTruck.setBounds(10, getHeight() - 90, 150, 40);
buttonCreateGasolineTanker.setBounds(170, getHeight() - 90, 150, 40);
buttonUp.setBounds(getWidth() - 140, getHeight() - 160, 45, 45);
buttonDown.setBounds(getWidth() - 140, getHeight() - 100, 45, 45);
buttonRight.setBounds(getWidth() - 80, getHeight() - 100, 45, 45);
buttonLeft.setBounds(getWidth() - 200, getHeight() - 100, 45, 45);
comboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
buttonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
}
});
}
}

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormTruckCollection">
<grid id="27dc6" layout-manager="GridLayoutManager" row-count="2" 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>
<xy x="20" y="20" width="788" height="470"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="4ce89">
<constraints>
<grid row="1" column="0" 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="f77b4" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Инструменты"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,219 @@
import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.MassiveGenericObjects;
import CollectionGenericObjects.TruckSharingService;
import Drawings.CanvasFormTruckCollection;
import Drawings.DrawingTruck;
import Drawings.DrawingGasolineTanker;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.text.ParseException;
import java.util.Random;
import static java.lang.Integer.parseInt;
public class FormTruckCollection extends JFrame{
public static CanvasFormTruckCollection<DrawingTruck> _canvasGasolineTanker = new CanvasFormTruckCollection<DrawingTruck>();
private static AbstractCompany _company = null;
private JButton ButtonCreateGasolineTanker = new JButton("Создать бензовоз");;
private JButton ButtonCreateTruck = new JButton("Создать грузовик");
private JButton RemoveButton = new JButton("Удалить");
private JButton GoToCheckButton = new JButton("Передать на тесты");
private JButton RandomButton = new JButton("Случайный");
private JButton RefreshButton = new JButton("Обновить");
private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"});
private JFormattedTextField TextField;
public static void canvasShow() {
_company.SetPosition();
_canvasGasolineTanker.SetCollectionToCanvas(_company);
_canvasGasolineTanker.repaint();
}
private void CreateObject(String typeOfClass) {
if (_company == null) return;
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = getColor();
DrawingTruck drawingTruck;
switch (typeOfClass) {
case "DrawingTruck":
drawingTruck = new DrawingTruck(speed, weight, bodyColor);
break;
case "DrawingGasolineTanker":
Color additionalColor = getColor();
boolean gasTank = new Random().nextBoolean();
boolean signalBeacon = new Random().nextBoolean();;
drawingTruck = new DrawingGasolineTanker(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon);
break;
default: return;
}
if (_company._collection.Insert(drawingTruck, 0) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
canvasShow();
}
else {
JOptionPane.showMessageDialog(null, "Объект не удалось добавить");
}
}
public Color getColor() {
Color initializator = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
Color color = JColorChooser.showDialog(this, "Цвет", initializator);
return color;
}
public void Init() {
setTitle("Коллекция грузовиков");
setSize(920,572);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MaskFormatter mask = null;
try {
mask = new MaskFormatter("##");
mask.setPlaceholder("00");
} catch (ParseException e) {
throw new RuntimeException(e);
}
TextField = new JFormattedTextField(mask);
ComboBoxCollections.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (ComboBoxCollections.getSelectedItem().toString()) {
case "Хранилище":
_company = new TruckSharingService(getWidth(), getHeight(), new MassiveGenericObjects<DrawingTruck>());
break;
}
}
});
ButtonCreateTruck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingTruck");
}
});
ButtonCreateGasolineTanker.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingGasolineTanker");
}
});
RemoveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null || TextField.getText() == null) {
return;
}
int pos = parseInt(TextField.getText());
int resultConfirmDialog = JOptionPane.showConfirmDialog(null,
"Удалить", "Удаление",
JOptionPane.YES_NO_OPTION);
if (resultConfirmDialog == JOptionPane.NO_OPTION) return;
if (_company._collection.Remove(pos) != null) {
JOptionPane.showMessageDialog(null, "Объект удален");
canvasShow();
}
else {
JOptionPane.showMessageDialog(null, "Объект не удалось удалить");
}
}
});
GoToCheckButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
DrawingTruck truck = null;
int counter = 100;
while (truck == null)
{
truck = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
}
if (truck == null)
{
return;
}
FormGasolineTanker formGasolineTanker = new FormGasolineTanker();
formGasolineTanker.Init(truck);
}
});
RandomButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
FormAdditionalCollection form = new FormAdditionalCollection();
form.setCompany(_company);
}
});
RefreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
canvasShow();
}
});
_canvasGasolineTanker.setBounds(0, 0, getWidth()-200, getHeight());
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
ButtonCreateTruck.setBounds(getWidth()-190, 60, 150, 30);
ButtonCreateGasolineTanker.setBounds(getWidth()-190, 100, 150, 30);
TextField.setBounds(getWidth()-190,200,150,30);
RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
RandomButton.setBounds(getWidth()-190, 320, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
setLayout(null);
add(_canvasGasolineTanker);
add(ComboBoxCollections);
add(ButtonCreateTruck);
add(ButtonCreateGasolineTanker);
add(TextField);
add(RemoveButton);
add(GoToCheckButton);
add(RandomButton);
add(RefreshButton);
setVisible(true);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
_canvasGasolineTanker.setBounds(0, 0, getWidth()-200, getHeight()-70);
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
ButtonCreateTruck.setBounds(getWidth()-190, 60, 150, 30);
ButtonCreateGasolineTanker.setBounds(getWidth()-190, 100, 150, 30);
TextField.setBounds(getWidth()-190,200,150,30);
RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
RandomButton.setBounds(getWidth()-190, 320, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
}
});
}
}

View File

@ -1,7 +1,7 @@
import java.awt.*;
public class Main{
public class Main{
public static void main(String[] args){
FormGasolineTanker formGasolineTanker = new FormGasolineTanker();
formGasolineTanker.Init();
FormTruckCollection formTruckCollection = new FormTruckCollection();
formTruckCollection.Init();
}
}

View File

@ -59,5 +59,4 @@ public abstract class AbstractStrategy {
}
return false;
}
}

View File

@ -1,5 +1,5 @@
package MovementStrategy;
// только объявляются методы
public interface IMoveableObjects {
ObjectParameters GetObjectPosition();
int GetStep();

View File

@ -3,7 +3,6 @@ import java.awt.*;
public class DrawingWheels implements IDrawingWheels{
private NumWheels numWheels;
@Override
public NumWheels getNumWheels() {
return numWheels;

View File

@ -3,5 +3,5 @@ import java.awt.*;
public interface IDrawingWheels {
void setNumWheels(int number);
NumWheels getNumWheels();
void drawWheels(Graphics2D g2d, Color color, int _startPosX, int _startPosY); //
void drawWheels(Graphics2D g2d, Color color, int _startPosX, int _startPosY);
}