Лабораторная работа №3
This commit is contained in:
parent
33b6eb7cc2
commit
6d2ced70f7
61
src/CollectionAdditionalObjects/AdditionalCollections.java
Normal file
61
src/CollectionAdditionalObjects/AdditionalCollections.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package CollectionAdditionalObjects;
|
||||||
|
|
||||||
|
import DifferentRollers.IDifferentRollers;
|
||||||
|
import Drawings.DrawingBulldozer;
|
||||||
|
import Drawings.DrawingExcavator;
|
||||||
|
import Entities.EntityBulldozer;
|
||||||
|
import Entities.EntityExcavator;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class AdditionalCollections <T extends EntityBulldozer, U extends IDifferentRollers>{
|
||||||
|
public T[] _collectionEntity;
|
||||||
|
public U[] _collectionRollers;
|
||||||
|
public AdditionalCollections(int size, Class<T> type1, Class<T> type2) {
|
||||||
|
_collectionEntity = (T[]) Array.newInstance(type1, size);
|
||||||
|
_collectionRollers = (U[]) Array.newInstance(type2, size);
|
||||||
|
CountEntities = size;
|
||||||
|
CountRollers = size;
|
||||||
|
}
|
||||||
|
public int CountEntities;
|
||||||
|
public int CountRollers;
|
||||||
|
public int Insert(T entity) {
|
||||||
|
int index = 0;
|
||||||
|
while (index < CountEntities) {
|
||||||
|
if (_collectionEntity[index] == null)
|
||||||
|
{
|
||||||
|
_collectionEntity[index] = entity;
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
public int Insert(U rollers) {
|
||||||
|
int index = 0;
|
||||||
|
while (index < CountRollers) {
|
||||||
|
if (_collectionRollers[index] == null)
|
||||||
|
{
|
||||||
|
_collectionRollers[index] = rollers;
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
public DrawingBulldozer CreateAdditionalCollectionBulldozer() {
|
||||||
|
Random random = new Random();
|
||||||
|
if (_collectionEntity == null || _collectionRollers == null) return null;
|
||||||
|
T entity = _collectionEntity[random.nextInt(CountEntities)];
|
||||||
|
U rollers = _collectionRollers[random.nextInt(CountRollers)];
|
||||||
|
DrawingBulldozer drawingBulldozer = null;
|
||||||
|
if (entity instanceof EntityExcavator) {
|
||||||
|
drawingBulldozer = new DrawingExcavator((EntityExcavator) entity, rollers);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
drawingBulldozer = new DrawingBulldozer(entity, rollers);
|
||||||
|
}
|
||||||
|
return drawingBulldozer;
|
||||||
|
}
|
||||||
|
}
|
34
src/CollectionGenericObjects/AbstractCompany.java
Normal file
34
src/CollectionGenericObjects/AbstractCompany.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package CollectionGenericObjects;
|
||||||
|
|
||||||
|
import Drawings.DrawingBulldozer;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public abstract class AbstractCompany {
|
||||||
|
protected int _placeSizeWidth = 195;
|
||||||
|
protected int _placeSizeHeight = 80;
|
||||||
|
protected int _pictureWidth;
|
||||||
|
protected int _pictureHeight;
|
||||||
|
public ICollectionGenericObjects<DrawingBulldozer> _collection = null;
|
||||||
|
private int GetMaxCount() {
|
||||||
|
return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawingBulldozer> collection)
|
||||||
|
{
|
||||||
|
_pictureWidth = picWidth;
|
||||||
|
_pictureHeight = picHeight;
|
||||||
|
_collection = collection;
|
||||||
|
_collection.SetMaxCount(GetMaxCount(), DrawingBulldozer.class);
|
||||||
|
}
|
||||||
|
//перегрузка операторов в джаве невозможна
|
||||||
|
public DrawingBulldozer GetRandomObject()
|
||||||
|
{
|
||||||
|
return _collection.Get((int)(Math.random()*GetMaxCount()));
|
||||||
|
}
|
||||||
|
public void SetPosition()
|
||||||
|
{
|
||||||
|
SetObjectsPosition();
|
||||||
|
}
|
||||||
|
public abstract void DrawBackground(Graphics graphics);
|
||||||
|
protected abstract void SetObjectsPosition();
|
||||||
|
}
|
49
src/CollectionGenericObjects/BulldozerSharingService.java
Normal file
49
src/CollectionGenericObjects/BulldozerSharingService.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package CollectionGenericObjects;
|
||||||
|
|
||||||
|
import Drawings.DrawingBulldozer;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class BulldozerSharingService extends AbstractCompany{
|
||||||
|
public BulldozerSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawingBulldozer> collection) {
|
||||||
|
super(picWidth, picHeight, collection);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void DrawBackground(Graphics g) {
|
||||||
|
int width = _pictureWidth / _placeSizeWidth;
|
||||||
|
int height = _pictureHeight / _placeSizeHeight;
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
for (int i = 0; i < width; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < height + 1; ++j)
|
||||||
|
{
|
||||||
|
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void SetObjectsPosition() {
|
||||||
|
int width = _pictureWidth / _placeSizeWidth;
|
||||||
|
int height = _pictureHeight / _placeSizeHeight;
|
||||||
|
|
||||||
|
int posWidth = width - 1;
|
||||||
|
int posHeight = 0;
|
||||||
|
for (int i = 0; i < (_collection.getCount()); i++) {
|
||||||
|
if (_collection.Get(i) != null) {
|
||||||
|
_collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
|
||||||
|
_collection.Get(i).SetPosition(_placeSizeWidth * posWidth + 4, posHeight * _placeSizeHeight + 4);
|
||||||
|
}
|
||||||
|
if (posWidth > 0)
|
||||||
|
posWidth--;
|
||||||
|
else {
|
||||||
|
posWidth = width - 1;
|
||||||
|
posHeight++;
|
||||||
|
}
|
||||||
|
if (posHeight > height) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
12
src/CollectionGenericObjects/ICollectionGenericObjects.java
Normal file
12
src/CollectionGenericObjects/ICollectionGenericObjects.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
75
src/CollectionGenericObjects/MassiveGenericObjects.java
Normal file
75
src/CollectionGenericObjects/MassiveGenericObjects.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package CollectionGenericObjects;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public T Get(int position) {
|
||||||
|
if (position >= getCount() || position < 0) return null;
|
||||||
|
return (T) _collection[position];
|
||||||
|
}
|
||||||
|
}
|
34
src/Drawings/CanvasFormBulldozerCollection.java
Normal file
34
src/Drawings/CanvasFormBulldozerCollection.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package Drawings;
|
||||||
|
|
||||||
|
import CollectionGenericObjects.AbstractCompany;
|
||||||
|
import CollectionGenericObjects.ICollectionGenericObjects;
|
||||||
|
import CollectionGenericObjects.MassiveGenericObjects;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class CanvasFormBulldozerCollection<T> extends JComponent
|
||||||
|
{
|
||||||
|
public AbstractCompany company = null;
|
||||||
|
public void SetCollectionToCanvas(AbstractCompany company) {
|
||||||
|
this.company = company;
|
||||||
|
}
|
||||||
|
public CanvasFormBulldozerCollection(){}
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
super.paintComponents(g);
|
||||||
|
if (company == null || company._collection == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
company.DrawBackground(g);
|
||||||
|
for (int i = 0; i < company._collection.getCount(); i++) {
|
||||||
|
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
T obj = (T)company._collection.Get(i);
|
||||||
|
if (obj instanceof DrawingBulldozer) {
|
||||||
|
((DrawingBulldozer) obj).DrawTransport(g2d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -56,6 +56,17 @@ public class DrawingBulldozer extends JPanel {
|
|||||||
SetTypeRollers();
|
SetTypeRollers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected DrawingBulldozer(int _drawingExcWidth, int _drawingExcHeight) {
|
||||||
|
super();
|
||||||
|
this._drawingExcWidth = _drawingExcWidth;
|
||||||
|
this._drawingExcHeight = _drawingExcHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingBulldozer(EntityBulldozer entity, IDifferentRollers rollers) {
|
||||||
|
EntityBulldozer = entity;
|
||||||
|
drawingRollers = rollers;
|
||||||
|
}
|
||||||
|
|
||||||
// Смена границ формы отрисовки
|
// Смена границ формы отрисовки
|
||||||
public void SetPictureSize(int width, int height)
|
public void SetPictureSize(int width, int height)
|
||||||
{
|
{
|
||||||
@ -171,7 +182,7 @@ public class DrawingBulldozer extends JPanel {
|
|||||||
g.fillOval(_startPosX + 50, _startPosY + 48, 5, 5);
|
g.fillOval(_startPosX + 50, _startPosY + 48, 5, 5);
|
||||||
|
|
||||||
int x = _startPosX;
|
int x = _startPosX;
|
||||||
if (drawingRollers.getRollersCount() != null){
|
if (drawingRollers != null && drawingRollers.getRollersCount() != null){
|
||||||
switch (drawingRollers.getRollersCount()){
|
switch (drawingRollers.getRollersCount()){
|
||||||
case OneRoller:
|
case OneRoller:
|
||||||
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityBulldozer.getBodyColor());
|
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityBulldozer.getBodyColor());
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package Drawings;
|
package Drawings;
|
||||||
|
|
||||||
|
import DifferentRollers.IDifferentRollers;
|
||||||
import Entities.EntityExcavator;
|
import Entities.EntityExcavator;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -11,6 +12,11 @@ public class DrawingExcavator extends DrawingBulldozer{
|
|||||||
SetTypeRollers();
|
SetTypeRollers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DrawingExcavator(EntityExcavator entity, IDifferentRollers rollers) {
|
||||||
|
EntityBulldozer = entity;
|
||||||
|
drawingRollers = rollers;
|
||||||
|
}
|
||||||
|
|
||||||
// Отрисовка Экскаватора
|
// Отрисовка Экскаватора
|
||||||
@Override
|
@Override
|
||||||
public void DrawTransport(Graphics2D g){
|
public void DrawTransport(Graphics2D g){
|
||||||
@ -18,7 +24,7 @@ public class DrawingExcavator extends DrawingBulldozer{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
super.DrawTransport(g);
|
super.DrawTransport(g);
|
||||||
if (excavator.Prop){
|
if (excavator.getProp()){
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
//Опоры
|
//Опоры
|
||||||
//справа
|
//справа
|
||||||
@ -43,7 +49,7 @@ public class DrawingExcavator extends DrawingBulldozer{
|
|||||||
g.fillRect(_startPosX + 2, _startPosY + 69, 10, 2);
|
g.fillRect(_startPosX + 2, _startPosY + 69, 10, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(excavator.Ladle){
|
if(excavator.getLadle()){
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
//Ковш
|
//Ковш
|
||||||
//ковш(стрела)
|
//ковш(стрела)
|
||||||
|
@ -8,7 +8,13 @@ public class EntityExcavator extends EntityBulldozer{
|
|||||||
return AdditionalColor;
|
return AdditionalColor;
|
||||||
}
|
}
|
||||||
public boolean Prop;
|
public boolean Prop;
|
||||||
|
public boolean getProp(){
|
||||||
|
return Prop;
|
||||||
|
}
|
||||||
public boolean Ladle;
|
public boolean Ladle;
|
||||||
|
public boolean getLadle(){
|
||||||
|
return Ladle;
|
||||||
|
}
|
||||||
public EntityExcavator(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){
|
public EntityExcavator(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){
|
||||||
super(speed, weight, bodyColor);
|
super(speed, weight, bodyColor);
|
||||||
AdditionalColor = additionalColor;
|
AdditionalColor = additionalColor;
|
||||||
|
137
src/FormAdditionalCollection.java
Normal file
137
src/FormAdditionalCollection.java
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
import CollectionAdditionalObjects.AdditionalCollections;
|
||||||
|
import CollectionGenericObjects.AbstractCompany;
|
||||||
|
import DifferentRollers.DrawingRollersCross;
|
||||||
|
import DifferentRollers.DrawingRollersPlus;
|
||||||
|
import DifferentRollers.DrawingRollersStar;
|
||||||
|
import DifferentRollers.IDifferentRollers;
|
||||||
|
import Drawings.DrawingBulldozer;
|
||||||
|
import Drawings.CanvasExcavator;
|
||||||
|
import Drawings.DrawingExcavator;
|
||||||
|
import Entities.EntityBulldozer;
|
||||||
|
import Entities.EntityExcavator;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class FormAdditionalCollection extends JFrame {
|
||||||
|
public DrawingBulldozer drawingBulldozer = null;
|
||||||
|
private AbstractCompany company = null;
|
||||||
|
private CanvasExcavator canvasExc = new CanvasExcavator();
|
||||||
|
private AdditionalCollections<EntityBulldozer, IDifferentRollers> additionalCollection = null;
|
||||||
|
private Random random = new Random();
|
||||||
|
private JButton buttonGenerate = new JButton("Создать");
|
||||||
|
private JList<String> listEntity = new JList<String>();
|
||||||
|
private JList<String> listRollers = new JList<String>();
|
||||||
|
public FormAdditionalCollection() {
|
||||||
|
setTitle("Случайный объект");
|
||||||
|
setMinimumSize(new Dimension(650,310));
|
||||||
|
additionalCollection = new AdditionalCollections<EntityBulldozer, IDifferentRollers>(3, (Class) EntityBulldozer.class, (Class) IDifferentRollers.class);
|
||||||
|
AddEntities();
|
||||||
|
AddRollers();
|
||||||
|
buttonGenerate.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
|
drawingBulldozer = additionalCollection.CreateAdditionalCollectionBulldozer();
|
||||||
|
drawingBulldozer.SetPictureSize(getWidth(), getHeight());
|
||||||
|
drawingBulldozer.SetPosition(50,50);
|
||||||
|
canvasExc._drawingBulldozer = drawingBulldozer;
|
||||||
|
canvasExc.repaint();
|
||||||
|
DrawingBulldozer copyBulldozer;
|
||||||
|
if (drawingBulldozer instanceof DrawingExcavator)
|
||||||
|
copyBulldozer = new DrawingExcavator((EntityExcavator) drawingBulldozer.EntityBulldozer, drawingBulldozer.drawingRollers);
|
||||||
|
else
|
||||||
|
copyBulldozer = new DrawingBulldozer(drawingBulldozer.EntityBulldozer, drawingBulldozer.drawingRollers);
|
||||||
|
company._collection.Insert(copyBulldozer);
|
||||||
|
FormBulldozerCollection.canvasShow();
|
||||||
|
|
||||||
|
String[] data1 = new String[additionalCollection.CountEntities];
|
||||||
|
for (int i = 0; i < additionalCollection.CountEntities; i++) {
|
||||||
|
EntityBulldozer entity = additionalCollection._collectionEntity[i];
|
||||||
|
data1[i] = ToString(entity);
|
||||||
|
}
|
||||||
|
String[] data2 = new String[additionalCollection.CountRollers];
|
||||||
|
for (int i = 0; i < additionalCollection.CountRollers; i++) {
|
||||||
|
IDifferentRollers rollers = additionalCollection._collectionRollers[i];
|
||||||
|
data2[i] = ToString(rollers);
|
||||||
|
}
|
||||||
|
listEntity.setListData(data1);
|
||||||
|
listRollers.setListData(data2);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
buttonGenerate.setBounds(450, 10, 100, 50);
|
||||||
|
add(buttonGenerate);
|
||||||
|
listEntity.setBounds(10,200,300,60);
|
||||||
|
listRollers.setBounds(320,200,300,60);
|
||||||
|
add(listEntity);
|
||||||
|
add(listRollers);
|
||||||
|
add(canvasExc);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
private String ToString(EntityBulldozer entity) {
|
||||||
|
String str = "";
|
||||||
|
if (entity instanceof EntityExcavator) str += "EntityExcavator ";
|
||||||
|
else str += "EntityBulldozer ";
|
||||||
|
str += entity.getBodyColor().toString();
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
private String ToString(IDifferentRollers rollers) {
|
||||||
|
if (rollers == null || rollers.getRollersCount() == null)
|
||||||
|
return "Dont have rollers";
|
||||||
|
String str = "Rollers ";
|
||||||
|
if (rollers instanceof DrawingRollersCross) str += "Type Cross ";
|
||||||
|
else if (rollers instanceof DrawingRollersPlus) str += "Type Plus ";
|
||||||
|
else str += "Type Star ";
|
||||||
|
str += rollers.getRollersCount().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);
|
||||||
|
Color bodycolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
|
||||||
|
EntityBulldozer entity;
|
||||||
|
if (random.nextBoolean()) {
|
||||||
|
entity = new EntityBulldozer(speed, weight, bodycolor);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Color additionalcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
|
||||||
|
boolean prop = random.nextBoolean();
|
||||||
|
boolean ladle = random.nextBoolean();
|
||||||
|
entity = new EntityExcavator(speed, weight, bodycolor,
|
||||||
|
additionalcolor, prop, ladle);
|
||||||
|
}
|
||||||
|
additionalCollection.Insert(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void AddRollers() {
|
||||||
|
for (int i = 0; i < additionalCollection.CountRollers; i++) {
|
||||||
|
random = new Random();
|
||||||
|
Integer numberOfRollers = random.nextInt(0, 4);
|
||||||
|
IDifferentRollers drawingRollers = null;
|
||||||
|
switch (random.nextInt(0,4)) {
|
||||||
|
case 1:
|
||||||
|
drawingRollers = new DrawingRollersCross();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
drawingRollers = new DrawingRollersPlus();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
drawingRollers = new DrawingRollersStar();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
numberOfRollers = null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (drawingRollers != null) drawingRollers.setRollersCount(numberOfRollers);
|
||||||
|
additionalCollection.Insert(drawingRollers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void setCompany(AbstractCompany company) {
|
||||||
|
this.company = company;
|
||||||
|
}
|
||||||
|
}
|
204
src/FormBulldozerCollection.java
Normal file
204
src/FormBulldozerCollection.java
Normal file
@ -0,0 +1,204 @@
|
|||||||
|
import CollectionGenericObjects.AbstractCompany;
|
||||||
|
import CollectionGenericObjects.BulldozerSharingService;
|
||||||
|
import CollectionGenericObjects.MassiveGenericObjects;
|
||||||
|
import Drawings.CanvasFormBulldozerCollection;
|
||||||
|
import Drawings.DrawingBulldozer;
|
||||||
|
import Drawings.DrawingExcavator;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.text.MaskFormatter;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static java.lang.Integer.parseInt;
|
||||||
|
|
||||||
|
public class FormBulldozerCollection extends JFrame{
|
||||||
|
private String title;
|
||||||
|
private Dimension dimension;
|
||||||
|
public static CanvasFormBulldozerCollection<DrawingBulldozer> _canvasExcavator = new CanvasFormBulldozerCollection<DrawingBulldozer>();
|
||||||
|
private static AbstractCompany _company = null;
|
||||||
|
private JButton CreateExcButton = new JButton("Создать экскаватор");
|
||||||
|
private JButton CreateBullButton = 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 MaskedTextField;
|
||||||
|
public FormBulldozerCollection(String title, Dimension dimension) {
|
||||||
|
this.title = title;
|
||||||
|
this.dimension = dimension;
|
||||||
|
}
|
||||||
|
public static void canvasShow() {
|
||||||
|
_company.SetPosition();
|
||||||
|
_canvasExcavator.SetCollectionToCanvas(_company);
|
||||||
|
_canvasExcavator.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();
|
||||||
|
DrawingBulldozer drawingBulldozer;
|
||||||
|
switch (typeOfClass) {
|
||||||
|
case "DrawingBulldozer":
|
||||||
|
drawingBulldozer = new DrawingBulldozer(speed, weight, bodyColor);
|
||||||
|
break;
|
||||||
|
case "DrawingExcavator":
|
||||||
|
Color additionalColor = getColor();
|
||||||
|
boolean prop = new Random().nextBoolean();
|
||||||
|
boolean ladle = new Random().nextBoolean();
|
||||||
|
drawingBulldozer = new DrawingExcavator(speed, weight, bodyColor, additionalColor, prop, ladle);
|
||||||
|
break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
if (_company._collection.Insert(drawingBulldozer, 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(title);
|
||||||
|
setMinimumSize(dimension);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
MaskFormatter mask = null;
|
||||||
|
try {
|
||||||
|
mask = new MaskFormatter("##");
|
||||||
|
mask.setPlaceholder("00");
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
MaskedTextField = new JFormattedTextField(mask);
|
||||||
|
|
||||||
|
ComboBoxCollections.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
switch (ComboBoxCollections.getSelectedItem().toString()) {
|
||||||
|
case "Хранилище":
|
||||||
|
_company = new BulldozerSharingService(getWidth()-200, getHeight()-70, new MassiveGenericObjects<DrawingBulldozer>());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
CreateBullButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
CreateObject("DrawingBulldozer");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
CreateExcButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
CreateObject("DrawingExcavator");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
RemoveButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_company == null || MaskedTextField.getText() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int pos = parseInt(MaskedTextField.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;
|
||||||
|
}
|
||||||
|
DrawingBulldozer bulldozer = null;
|
||||||
|
int counter = 100;
|
||||||
|
while (bulldozer == null)
|
||||||
|
{
|
||||||
|
bulldozer = _company.GetRandomObject();
|
||||||
|
counter--;
|
||||||
|
if (counter <= 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bulldozer == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FormExcavator form = new FormExcavator("Экскаватор", new Dimension(900,565));
|
||||||
|
form.Init(bulldozer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
_canvasExcavator.setBounds(0, 0, getWidth()-200, getHeight());
|
||||||
|
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
|
||||||
|
CreateBullButton.setBounds(getWidth()-190, 60, 150, 30);
|
||||||
|
CreateExcButton.setBounds(getWidth()-190, 100, 150, 30);
|
||||||
|
MaskedTextField.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);
|
||||||
|
|
||||||
|
setSize(dimension.width,dimension.height);
|
||||||
|
setLayout(null);
|
||||||
|
add(_canvasExcavator);
|
||||||
|
add(ComboBoxCollections);
|
||||||
|
add(CreateBullButton);
|
||||||
|
add(CreateExcButton);
|
||||||
|
add(MaskedTextField);
|
||||||
|
add(RemoveButton);
|
||||||
|
add(GoToCheckButton);
|
||||||
|
add(RandomButton);
|
||||||
|
add(RefreshButton);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
@ -1,24 +1,16 @@
|
|||||||
import Drawings.CanvasExcavator;
|
import Drawings.*;
|
||||||
import Drawings.DirectionType;
|
|
||||||
import Drawings.DrawingBulldozer;
|
|
||||||
import Drawings.DrawingExcavator;
|
|
||||||
import MovementStrategy.*;
|
import MovementStrategy.*;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.ComponentAdapter;
|
|
||||||
import java.awt.event.ComponentEvent;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class FormExcavator extends JFrame {
|
public class FormExcavator extends JFrame {
|
||||||
private String title;
|
private String title;
|
||||||
private Dimension dimension;
|
private Dimension dimension;
|
||||||
private int Width, Height;
|
private int Width, Height;
|
||||||
private CanvasExcavator canvasExcavator = new CanvasExcavator();
|
private CanvasExcavator canvasExcavator = new CanvasExcavator();
|
||||||
private JButton CreateButton = new JButton("Создать экскаватор");
|
|
||||||
private JButton CreateBullButton = new JButton("Создать бульдозер");
|
|
||||||
private JButton UpButton = new JButton();
|
private JButton UpButton = new JButton();
|
||||||
private JButton DownButton = new JButton();
|
private JButton DownButton = new JButton();
|
||||||
private JButton LeftButton = new JButton();
|
private JButton LeftButton = new JButton();
|
||||||
@ -31,43 +23,17 @@ public class FormExcavator extends JFrame {
|
|||||||
this.dimension = dimension;
|
this.dimension = dimension;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateObject(String typeOfClass) {
|
public void Init(DrawingBulldozer bulldozer) {
|
||||||
int StartPositionX = (int)(Math.random() * 90 + 10);
|
|
||||||
int StartPositionY = (int)(Math.random() * 90 + 10);
|
|
||||||
int speed = (int)(Math.random() * 300 + 100);
|
|
||||||
double weight = (double)(Math.random() * 3000 + 1000);
|
|
||||||
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
|
||||||
switch (typeOfClass) {
|
|
||||||
case "DrawingBulldozer":
|
|
||||||
canvasExcavator._drawingBulldozer = new DrawingBulldozer(speed, weight, bodyColor);
|
|
||||||
canvasExcavator._drawingBulldozer.SetPictureSize(Width, Height);
|
|
||||||
canvasExcavator._drawingBulldozer.SetPosition(StartPositionX, StartPositionY);
|
|
||||||
canvasExcavator.repaint();
|
|
||||||
break;
|
|
||||||
case "DrawingExcavator":
|
|
||||||
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
|
||||||
boolean prop = new Random().nextBoolean();
|
|
||||||
boolean ladle = new Random().nextBoolean();
|
|
||||||
canvasExcavator._drawingBulldozer = new DrawingExcavator(speed, weight, bodyColor, additionalColor, prop, ladle);
|
|
||||||
canvasExcavator._drawingBulldozer.SetPictureSize(Width, Height);
|
|
||||||
canvasExcavator._drawingBulldozer.SetPosition(StartPositionX, StartPositionY);
|
|
||||||
canvasExcavator.repaint();
|
|
||||||
break;
|
|
||||||
default: return;
|
|
||||||
}
|
|
||||||
_strategy = null;
|
|
||||||
ComboBoxStrategy.setEnabled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Init() {
|
|
||||||
setTitle(title);
|
setTitle(title);
|
||||||
setMinimumSize(dimension);
|
setMinimumSize(dimension);
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
Width = getWidth() - 15;
|
Width = getWidth() - 15;
|
||||||
Height = getHeight() - 35;
|
Height = getHeight() - 35;
|
||||||
|
ComboBoxStrategy.setEnabled(true);
|
||||||
_strategy = null;
|
_strategy = null;
|
||||||
|
canvasExcavator._drawingBulldozer = bulldozer;
|
||||||
|
|
||||||
CreateButton.setName("Create");
|
|
||||||
Icon iconUp = new ImageIcon("res/up.png");
|
Icon iconUp = new ImageIcon("res/up.png");
|
||||||
UpButton.setIcon(iconUp);
|
UpButton.setIcon(iconUp);
|
||||||
UpButton.setName("Up");
|
UpButton.setName("Up");
|
||||||
@ -81,20 +47,6 @@ public class FormExcavator extends JFrame {
|
|||||||
Icon iconRight = new ImageIcon("res/right.png");
|
Icon iconRight = new ImageIcon("res/right.png");
|
||||||
RightButton.setIcon(iconRight);
|
RightButton.setIcon(iconRight);
|
||||||
|
|
||||||
CreateButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
CreateObject("DrawingExcavator");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
CreateBullButton.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
CreateObject("DrawingBulldozer");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
ButtonStrategy.addActionListener(new ActionListener() {
|
ButtonStrategy.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@ -166,16 +118,12 @@ public class FormExcavator extends JFrame {
|
|||||||
setSize(dimension.width,dimension.height);
|
setSize(dimension.width,dimension.height);
|
||||||
setLayout(null);
|
setLayout(null);
|
||||||
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
|
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
|
||||||
CreateButton.setBounds(10, getHeight() - 85, 160, 35);
|
|
||||||
CreateBullButton.setBounds(180, getHeight() - 85, 160, 35);
|
|
||||||
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
|
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
|
||||||
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
|
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
|
||||||
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
|
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
|
||||||
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
|
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
|
||||||
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 25);
|
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 25);
|
||||||
ButtonStrategy.setBounds(getWidth() - 130, 45, 100, 25);
|
ButtonStrategy.setBounds(getWidth() - 130, 45, 100, 25);
|
||||||
add(CreateButton);
|
|
||||||
add(CreateBullButton);
|
|
||||||
add(UpButton);
|
add(UpButton);
|
||||||
add(DownButton);
|
add(DownButton);
|
||||||
add(RightButton);
|
add(RightButton);
|
||||||
|
@ -2,7 +2,7 @@ import java.awt.*;
|
|||||||
|
|
||||||
public class Program {
|
public class Program {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
FormExcavator form = new FormExcavator("Экскаватор", new Dimension(700, 500));
|
FormBulldozerCollection form = new FormBulldozerCollection("Коллекция экскаваторов", new Dimension(1100, 650));
|
||||||
form.Init();
|
form.Init();
|
||||||
form.setLocationRelativeTo(null);
|
form.setLocationRelativeTo(null);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user