2 Commits

Author SHA1 Message Date
7b7d2bb3a3 Лабораторная работа №3 2024-05-05 22:07:16 +04:00
4c5819779a Not Done 2024-05-05 21:09:02 +04:00
37 changed files with 747 additions and 56 deletions

View File

@@ -5,15 +5,16 @@
</component>
<component name="ChangeListManager">
<list default="true" id="39e7c520-e505-4e53-a269-c949d7c9f5d1" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/src/Scripts/Wheels/IDrawingWheels.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/CountWheels.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/DirectionType.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/DrawingField.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/DrawingModernMonorail.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/DrawingWheels.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/EntityModernMonorail.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/FormModernMonorail.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/Program.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/ProjectMonorail/Scripts/Wheels/DrawingWheels.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/ProjectMonorail/Scripts/Wheels/DrawingWheels.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Scripts/CollectionGenericObjects/AdditionalCollection.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Scripts/CollectionGenericObjects/AdditionalCollection.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Scripts/CollectionGenericObjects/DepotSharingService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Scripts/CollectionGenericObjects/DepotSharingService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Scripts/Drawing/DrawingModernMonorail.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Scripts/Drawing/DrawingModernMonorail.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Scripts/Drawing/DrawingMonorail.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Scripts/Drawing/DrawingMonorail.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Scripts/Forms/FormAdditionalCollection.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Scripts/Forms/FormAdditionalCollection.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Scripts/Forms/FormMonorailCollection.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Scripts/Forms/FormMonorailCollection.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Scripts/Wheels/DrawingWheels.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Scripts/Wheels/DrawingWheels.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -24,6 +25,7 @@
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
<option value="Interface" />
</list>
</option>
</component>
@@ -44,8 +46,9 @@
"Application.Program.executor": "Run",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "LabWorking__2",
"kotlin-language-version-configured": "true"
"git-widget-placeholder": "LabWorking__3",
"kotlin-language-version-configured": "true",
"last_opened_file_path": "D:/Учеба/PIbd-11_Tarasov_V.D._Hard/ProjectMonorail"
}
}]]></component>
<component name="RunManager">
@@ -57,14 +60,6 @@
</method>
</configuration>
</component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="jdk-21.0.2-openjdk-21.0.2-4caba194b151-4f524021" />
<option value="jdk-22-openjdk-22-3183f394aec4-bad0321e" />
</set>
</attachedChunks>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">

View File

@@ -0,0 +1,36 @@
package Scripts.CollectionGenericObjects;
import Scripts.Drawing.DrawingMonorail;
import java.awt.*;
public abstract class AbstractCompany {
protected int _placeSizeWidth = 220;
protected int _placeSizeHeight = 155;
protected int _pictureWidth;
protected int _pictureHeight;
public ICollectionGenericObjects<DrawingMonorail> _collection = null;
private int GetMaxCount() {
return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
}
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawingMonorail> collection)
{
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = collection;
System.out.println(_pictureHeight+" "+_pictureWidth+" "+_placeSizeHeight+" "+_placeSizeWidth);
_collection.SetMaxCount(GetMaxCount(), (Class) DrawingMonorail.class);
}
//Перегрузок нет
public DrawingMonorail GetRandomObject()
{
return _collection.Get((int)(Math.random()*GetMaxCount() + 0));
}
public void SetPosition()
{
SetObjectsPosition();
}
public abstract void DrawBackgound(Graphics graphics);
protected abstract void SetObjectsPosition();
}

View File

@@ -0,0 +1,62 @@
package Scripts.CollectionGenericObjects;
import Scripts.Drawing.DrawingModernMonorail;
import Scripts.Drawing.DrawingMonorail;
import Scripts.Entities.EntityModernMonorail;
import Scripts.Entities.EntityMonorail;
import Scripts.Wheels.DrawingWheels;
import Scripts.Wheels.IDrawingWheels;
import java.lang.reflect.Array;
import java.util.Random;
public class AdditionalCollection <T extends EntityMonorail, 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) {
int index = 0;
while (index < CountEntities) {
if (_collectionEntity[index] == null)
{
_collectionEntity[index] = entity;
return index;
}
++index;
}
return -1;
}
public int Insert(U decks) {
int index = 0;
while (index < CountWheels) {
if (_collectionWheels[index] == null)
{
_collectionWheels[index] = decks;
return index;
}
++index;
}
return -1;
}
public DrawingMonorail CreateAdditionalCollectionStormtrooper() {
Random random = new Random();
if (_collectionEntity == null || _collectionWheels == null) return null;
T entity = _collectionEntity[random.nextInt(CountEntities)];
U wheels = _collectionWheels[random.nextInt(CountWheels)];
DrawingMonorail drawingBaseStormtrooper = null;
if (entity instanceof EntityModernMonorail) {
drawingBaseStormtrooper = new DrawingModernMonorail((EntityModernMonorail)entity, wheels);
}
else {
drawingBaseStormtrooper = new DrawingMonorail(entity, wheels);
}
return drawingBaseStormtrooper;
}
}

View File

@@ -0,0 +1,48 @@
package Scripts.CollectionGenericObjects;
import Scripts.Drawing.DrawingMonorail;
import java.awt.*;
public class DepotSharingService extends AbstractCompany {
public DepotSharingService (int picWidth, int picHeight, ICollectionGenericObjects<DrawingMonorail> collection) {
super(picWidth, picHeight, collection);
}
private int _offsetX = 30;
@Override
public void DrawBackgound(Graphics graphics) {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
graphics.setColor(Color.BLACK);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height + 1; ++j)
{
graphics.drawLine(i * _offsetX + i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth + i * _offsetX + i * _placeSizeWidth, j * _placeSizeHeight);
graphics.drawLine(i * _offsetX + i * _placeSizeWidth, j * _placeSizeHeight, i * _offsetX + i * _placeSizeWidth, _placeSizeHeight + j * _placeSizeHeight);
graphics.drawLine(i * _offsetX + i * _placeSizeWidth, _placeSizeHeight + j * _placeSizeHeight, _placeSizeWidth + i * _offsetX + i * _placeSizeWidth, _placeSizeHeight + j * _placeSizeHeight);
}
}
}
@Override
protected void SetObjectsPosition() {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int boarderOffsetX = 20;
int boarderOffsetY = 20;
int currnetIndex = 0;
for (int j = height; j >= 0; j--) {
for (int i = 0; i < width; i++, currnetIndex++) {
if (_collection.Get(currnetIndex) == null) continue;
_collection.Get(currnetIndex).SetPictureSize(_pictureWidth, _pictureHeight);
_collection.Get(currnetIndex).SetPosition(boarderOffsetX + i * _placeSizeWidth + i * _offsetX, boarderOffsetY + j * _placeSizeHeight);
}
}
}
}

View File

@@ -0,0 +1,11 @@
package Scripts.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,76 @@
package Scripts.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];
}
}

View File

@@ -1,4 +1,4 @@
package Scripts;
package Scripts.Drawing;
public enum DirectionType {
Up,

View File

@@ -1,4 +1,6 @@
package Scripts;
package Scripts.Drawing;
import Scripts.Forms.FormModernMonorail;
import javax.swing.*;
import java.awt.*;
@@ -12,6 +14,7 @@ public class DrawingField extends JPanel {
}
public DrawingMonorail getDrawingMonorail() {return _drawingMonorail;}
public void setDrawingMonorail(DrawingMonorail drawingMonorail) {_drawingMonorail = drawingMonorail;}
@Override
protected void paintComponent(Graphics g) {
@@ -50,14 +53,14 @@ public class DrawingField extends JPanel {
Random rnd=new Random();
_drawingMonorail = new DrawingMonorail(rnd.nextInt(50)+10,rnd.nextInt(100)+500,new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)), null, null);
_drawingMonorail.SetPictureSize(getWidth(),getHeight());
_drawingMonorail.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10,getWidth(),getHeight());
_drawingMonorail.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10);
}
public void CreateButtonAction_ModernMonorail(){
Random rnd=new Random();
_drawingMonorail = new DrawingModernMonorail(rnd.nextInt(50)+10,rnd.nextInt(100)+500,new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)), new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)), rnd.nextBoolean(), true);
_drawingMonorail.SetPictureSize(getWidth(),getHeight());
_drawingMonorail.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10,getWidth(),getHeight());
_drawingMonorail.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10);
}
public void ResizeField(){

View File

@@ -1,6 +1,9 @@
package Scripts;
package Scripts.Drawing;
import Scripts.Entities.EntityModernMonorail;
import Scripts.Entities.EntityMonorail;
import Scripts.Wheels.DrawingWheels;
import Scripts.Wheels.IDrawingWheels;
import java.awt.*;
import java.util.Random;
@@ -17,6 +20,17 @@ public class DrawingModernMonorail extends DrawingMonorail {
_wheelsSeed = rnd.nextInt(0, 2);
}
public DrawingModernMonorail(EntityModernMonorail entityModernMonorail, IDrawingWheels wheels)
{
super((EntityMonorail)entityModernMonorail, 190, 80, wheels);
_entityMonorail = new EntityModernMonorail(entityModernMonorail.getSpeed(), entityModernMonorail.getWeight(), entityModernMonorail.getBodyColor(), entityModernMonorail.getAdditionalColor(), entityModernMonorail.getMonorailTrack(), entityModernMonorail.getMonorailTrack());
_wheels = new DrawingWheels();
Random rnd = new Random();
_wheels.SetCountWheels(2 + rnd.nextInt(0, 3));
_wheelsSeed = rnd.nextInt(0, 2);
}
@Override
public void DrawTransport(Graphics g)
{

View File

@@ -1,13 +1,15 @@
package Scripts;
package Scripts.Drawing;
import Scripts.Entities.EntityMonorail;
import Scripts.Wheels.DrawingWheels;
import Scripts.Wheels.IDrawingWheels;
import java.awt.*;
import java.util.Random;
public class DrawingMonorail {
protected EntityMonorail _entityMonorail;
protected DrawingWheels _wheels;
protected IDrawingWheels _wheels;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
@@ -19,7 +21,7 @@ public class DrawingMonorail {
private int _drawningMonorailHeight = 80;
public EntityMonorail getMonorail() {return _entityMonorail;}
public DrawingWheels getWheels() {return _wheels;}
public IDrawingWheels getWheels() {return _wheels;}
protected int _wheelsSeed;
public int GetPositionX() { return _startPositionX;}
@@ -41,6 +43,45 @@ public class DrawingMonorail {
_wheelsSeed = rnd.nextInt(0, 2);
}
public DrawingMonorail(int speed, float weight, Color bodyColor)
{
this(100, 40);
_entityMonorail = new EntityMonorail(speed, weight, bodyColor);
_wheels = new DrawingWheels();
Random rnd = new Random();
_wheels.SetCountWheels(2 + rnd.nextInt(0, 3));
_wheelsSeed = rnd.nextInt(0, 2);
}
public DrawingMonorail(EntityMonorail entityMonorail, Integer drawningMonorailWidth, Integer drawningMonorailHeight, IDrawingWheels wheels)
{
this(drawningMonorailWidth, drawningMonorailHeight);
_wheels = new DrawingWheels();
_wheels.SetCountWheels(wheels.getCountWheels());
_entityMonorail = new EntityMonorail(entityMonorail.getSpeed(), entityMonorail.getWeight(), entityMonorail.getBodyColor());
_wheels = new DrawingWheels();
Random rnd = new Random();
_wheels.SetCountWheels(2 + rnd.nextInt(0, 3));
_wheelsSeed = rnd.nextInt(0, 2);
}
public DrawingMonorail(EntityMonorail entityMonorail, IDrawingWheels wheels)
{
this(100, 40);
_wheels = new DrawingWheels();
_wheels.SetCountWheels(wheels.getCountWheels());
_entityMonorail = new EntityMonorail(entityMonorail.getSpeed(), entityMonorail.getWeight(), entityMonorail.getBodyColor());
_wheels = new DrawingWheels();
Random rnd = new Random();
_wheels.SetCountWheels(2 + rnd.nextInt(0, 3));
_wheelsSeed = rnd.nextInt(0, 2);
}
protected DrawingMonorail(Integer drawningMonorailWidth, Integer drawningMonorailHeight) {
if(drawningMonorailWidth == null || drawningMonorailHeight == null) return;
@@ -69,7 +110,7 @@ public class DrawingMonorail {
return true;
}
public void SetPosition(int x, int y, int width, int height)
public void SetPosition(int x, int y)
{
if (_pictureHeight == null || _pictureWidth == null) return;

View File

@@ -1,4 +1,4 @@
package Scripts;
package Scripts.Entities;
import java.awt.*;
import java.util.Random;

View File

@@ -1,4 +1,4 @@
package Scripts;
package Scripts.Entities;
import java.awt.*;
import java.util.Random;

View File

@@ -0,0 +1,32 @@
package Scripts.Forms;
import Scripts.CollectionGenericObjects.AbstractCompany;
import Scripts.Drawing.DrawingMonorail;
import javax.swing.*;
import java.awt.*;
public class CanvasFormMonorailCollection<T> extends JComponent {
public AbstractCompany company = null;
public void SetCollectionToCanvas(AbstractCompany company) {
this.company = company;
}
public CanvasFormMonorailCollection() {};
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 DrawingMonorail) {
((DrawingMonorail) obj).DrawTransport(g2d);
}
}
super.repaint();
}
}

View File

@@ -0,0 +1,20 @@
package Scripts.Forms;
import Scripts.Drawing.DrawingMonorail;
import javax.swing.*;
import java.awt.*;
public class CanvasMonorail extends JComponent {
public DrawingMonorail _drawingMonorail;
public CanvasMonorail(){}
public void paintComponent(Graphics g) {
if (_drawingMonorail == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingMonorail.DrawTransport(g2d);
super.repaint();
}
}

View File

@@ -0,0 +1,133 @@
package Scripts.Forms;
import Scripts.CollectionGenericObjects.AbstractCompany;
import Scripts.CollectionGenericObjects.AdditionalCollection;
import Scripts.Drawing.DrawingModernMonorail;
import Scripts.Drawing.DrawingMonorail;
import Scripts.Entities.EntityModernMonorail;
import Scripts.Entities.EntityMonorail;
import Scripts.Wheels.DrawingWheels;
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 DrawingMonorail _drawingMonorail = null;
private DrawingMonorail _copyMonorail = null;
private AbstractCompany _company = null;
private CanvasMonorail _canvasMonorail = new CanvasMonorail();
private AdditionalCollection<EntityMonorail, DrawingWheels> _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> listEngines = new JList<String>();
public FormAdditionalCollection() {
setTitle("Случайные монорельсы");
setMinimumSize(new Dimension(970,310));
_additionalCollection = new AdditionalCollection<EntityMonorail, DrawingWheels>(3,(Class)EntityMonorail.class,(Class)DrawingWheels.class);
AddEntities();
AddWheels();
buttonGoToCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_drawingMonorail !=null){
_company._collection.Insert(_copyMonorail);
FormMonorailCollection.canvasShow();
dispose();
}
}
});
buttonGenerate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawingMonorail = _additionalCollection.CreateAdditionalCollectionStormtrooper();
_drawingMonorail.SetPictureSize(getWidth(), getHeight());
_drawingMonorail.SetPosition(360,30);
_canvasMonorail._drawingMonorail = _drawingMonorail;
_canvasMonorail.repaint();
if (_drawingMonorail instanceof DrawingModernMonorail)
_copyMonorail = new DrawingModernMonorail((EntityModernMonorail) _drawingMonorail.getMonorail(), _drawingMonorail.getWheels());
else
_copyMonorail = new DrawingMonorail(_drawingMonorail.getMonorail(), _drawingMonorail.getWheels());
}
});
buttonGoToCollection.setBounds(830,200,120,60);
buttonGenerate.setBounds(830, 130, 120, 60);
listEntity.setBounds(10,200,400,60);
listEngines.setBounds(420,200,400,60);
add(buttonGenerate);
add(buttonGoToCollection);
add(listEntity);
add(listEngines);
add(_canvasMonorail);
setVisible(true);
}
private String ToString(EntityMonorail entity) {
String str = "";
if (entity instanceof EntityModernMonorail) str += "EntityModernMonorail ";
else str += "EntityMonorail ";
str += entity.getBodyColor().toString();
return str;
}
private String ToString(DrawingWheels wheels) {
if (wheels == null || wheels.getCountWheels() == 0)
return "Не имеет колес";
String str = ""+wheels.getCountWheels();
str+=" колес";
return str;
}
public void AddEntities() {
for (int i = 0; i < _additionalCollection.CountEntities; i++) {
random = new Random();
int speed = random.nextInt(100, 300);
float weight = random.nextInt(1000, 3000);
Color bodycolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
EntityMonorail entityMonorail;
if (random.nextBoolean()) {
entityMonorail = new EntityMonorail(speed, weight, bodycolor);
}
else {
Color additionalcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
boolean monorailTrack = random.nextBoolean();
boolean cabin = random.nextBoolean();
entityMonorail = new EntityModernMonorail(speed, weight, bodycolor, additionalcolor, monorailTrack, cabin);
}
_additionalCollection.Insert(entityMonorail);
}
}
public void AddWheels() {
for (int i = 0; i < _additionalCollection.CountWheels; i++) {
random = new Random();
EntityMonorail entity = _additionalCollection._collectionEntity[i];
DrawingWheels drawingWheels = null;
drawingWheels = new DrawingWheels();
drawingWheels.SetCountWheels((int) ((Math.random() * 3) + 1) * 2);
if(drawingWheels!=null){
_additionalCollection.Insert(drawingWheels);
}
}
}
void setCompany(AbstractCompany company) {
this._company = company;
String[] data1 = new String[_additionalCollection.CountEntities];
for (int i = 0; i < _additionalCollection.CountEntities; i++) {
EntityMonorail entity = _additionalCollection._collectionEntity[i];
data1[i] = ToString(entity);
}
String[] data2 = new String[_additionalCollection.CountWheels];
for (int i = 0; i < _additionalCollection.CountWheels; i++) {
DrawingWheels wheels = _additionalCollection._collectionWheels[i];
data2[i] = ToString(wheels);
}
listEntity.setListData(data1);
listEngines.setListData(data2);
}
}

View File

@@ -1,5 +1,7 @@
package Scripts;
package Scripts.Forms;
import Scripts.Drawing.DrawingField;
import Scripts.Drawing.DrawingMonorail;
import Scripts.MovementStratagy.*;
import javax.swing.*;
@@ -20,9 +22,6 @@ public class FormModernMonorail extends JFrame{
private DrawingField field = new DrawingField(this);
private JButton ButtonCreateMonorail = new JButton("Create Monorail");
private JButton ButtonCreateModernMonorail = new JButton("Create Modern Monorail");
private Icon _iconUp = new ImageIcon("Resource\\Arrows\\ArrowUp.png");
private Icon _iconDown = new ImageIcon("Resource\\Arrows\\ArrowDown.png");
private Icon _iconRight = new ImageIcon("Resource\\Arrows\\ArrowRight.png");
@@ -49,7 +48,6 @@ public class FormModernMonorail extends JFrame{
Height=getHeight();
ShowWindow();
RefreshWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@@ -103,18 +101,6 @@ public class FormModernMonorail extends JFrame{
CreatePanel.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
CreatePanel.setBackground(new Color(0,0,0,0));
CreatePanel.add(ButtonCreateMonorail);
CreatePanel.add(ButtonCreateModernMonorail);
ButtonCreateModernMonorail.addActionListener(e->{
field.CreateButtonAction_ModernMonorail();
repaint();
});
ButtonCreateMonorail.addActionListener(e->{
field.CreateButtonAction_Monorail();
repaint();
});
StrategyPanel.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
StrategyPanel.setBackground(new Color(0,0,0,0));
@@ -181,4 +167,8 @@ public class FormModernMonorail extends JFrame{
BottomAndCreatePanel.setBounds(-220,Height-110,Width,80);
DimentionPanel.setBounds(Width-170,Height-170,190,140);
}
public void Init(DrawingMonorail drawingMonorail) {
field.setDrawingMonorail(drawingMonorail);
}
}

View File

@@ -0,0 +1,211 @@
package Scripts.Forms;
import Scripts.CollectionGenericObjects.AbstractCompany;
import Scripts.CollectionGenericObjects.DepotSharingService;
import Scripts.CollectionGenericObjects.MassiveGenericObjects;
import Scripts.Drawing.DrawingModernMonorail;
import Scripts.Drawing.DrawingMonorail;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import static java.lang.Integer.parseInt;
public class FormMonorailCollection extends JFrame{
private String title;
private Dimension dimension;
public static CanvasFormMonorailCollection<DrawingMonorail> _canvasMonorail = new CanvasFormMonorailCollection<DrawingMonorail>();
private static AbstractCompany _company = null;
private JButton CreateButton = new JButton("Создать современный монорельс");;
private JButton CreateShipButton = 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 FormMonorailCollection(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public static void canvasShow() {
_company.SetPosition();
_canvasMonorail.SetCollectionToCanvas(_company);
_canvasMonorail.repaint();
}
private void CreateObject(String typeOfClass){
if (_company == null) return;
int speed = (int)(Math.random() * 300 + 100);
float weight = (float)(Math.random() * 3000 + 1000);
Color bodyColor = getColor();
DrawingMonorail drawingMonorail;
switch (typeOfClass) {
case "DrawingMonorail":
drawingMonorail = new DrawingMonorail(speed, weight, bodyColor);
break;
case "DrawingModernMonorail":
Color additionalColor = getColor();
boolean monorailTrack = new Random().nextBoolean();
boolean cabin = true;
drawingMonorail = new DrawingModernMonorail(speed, weight, bodyColor, additionalColor, monorailTrack, cabin);
break;
default: return;
}
if (_company._collection.Insert(drawingMonorail, 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);
TextField = new JFormattedTextField();
ComboBoxCollections.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (ComboBoxCollections.getSelectedItem().toString()) {
case "Хранилище":
_company = new DepotSharingService(getWidth()-200, getHeight()-110, new MassiveGenericObjects<DrawingMonorail>());
break;
}
}
});
CreateShipButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingMonorail");
}
});
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingModernMonorail");
}
});
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) {
System.out.println(pos);
JOptionPane.showMessageDialog(null, "Объект удален");
canvasShow();
}
else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
}
});
GoToCheckButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
DrawingMonorail drawingMonorail = null;
int counter = 100;
while (drawingMonorail == null)
{
drawingMonorail = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
}
if (drawingMonorail == null)
{
return;
}
FormModernMonorail form = new FormModernMonorail();
form.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
canvasShow();
super.windowClosing(e);
}
});
form.Init(drawingMonorail);
}
});
RefreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
canvasShow();
}
});
RandomButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_company==null){
return;
}
FormAdditionalCollection form = new FormAdditionalCollection();
form.setCompany(_company);
}
});
_canvasMonorail.setBounds(0, 0, getWidth()-200, getHeight());
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
CreateShipButton.setBounds(getWidth()-190, 60, 150, 30);
CreateButton.setBounds(getWidth()-190, 100, 150, 30);
RandomButton.setBounds(getWidth()-190, 140, 150, 30);
TextField.setBounds(getWidth()-190,200,150,30);
RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
setSize(dimension.width,dimension.height);
setLayout(null);
add(_canvasMonorail);
add(ComboBoxCollections);
add(CreateShipButton);
add(CreateButton);
add(TextField);
add(RemoveButton);
add(GoToCheckButton);
add(RandomButton);
add(RefreshButton);
setVisible(true);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
_canvasMonorail.setBounds(0, 0, getWidth()-200, getHeight()-70);
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
CreateShipButton.setBounds(getWidth()-190, 60, 150, 30);
CreateButton.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, 140, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
}
});
}
}

View File

@@ -1,6 +1,6 @@
package Scripts.MovementStratagy;
import Scripts.DirectionType;
import Scripts.Drawing.DirectionType;
public abstract class AbstractStrategy {
private IMoveableObject moveableObject;

View File

@@ -1,6 +1,6 @@
package Scripts.MovementStratagy;
import Scripts.DirectionType;
import Scripts.Drawing.DirectionType;
public interface IMoveableObject{
ObjectParameters GetObjectPosition();

View File

@@ -1,7 +1,7 @@
package Scripts.MovementStratagy;
import Scripts.DirectionType;
import Scripts.DrawingMonorail;
import Scripts.Drawing.DirectionType;
import Scripts.Drawing.DrawingMonorail;
public class MoveableMonorail implements IMoveableObject {
private DrawingMonorail _drawingMonorail = null;

View File

@@ -1,7 +1,12 @@
package Scripts;
import Scripts.Forms.FormMonorailCollection;
import java.awt.*;
public class Program {
public static void main(String[] args){
new FormModernMonorail();
FormMonorailCollection form = new FormMonorailCollection("Монорельс", new Dimension(800, 800));
form.Init();
}
}

View File

@@ -5,26 +5,31 @@ import Scripts.Wheels.CountWheels;
import java.awt.*;
import java.util.Random;
public class DrawingWheels {
public class DrawingWheels implements IDrawingWheels{
private CountWheels _wheels;
private IOrnament _ornament;
private int _seedOrnament;
public void SetCountWheels(int Count){
for (CountWheels temp: CountWheels.values())
if (temp.getCountWheels() == Count){
Random rnd = new Random();
_seedOrnament = rnd.nextInt(0, 3);
_wheels=temp;
return;
}
}
public int getCountWheels() {
return _wheels.getCountWheels();
if (_wheels != null) return _wheels.getCountWheels();
else {
SetCountWheels(2);
return _wheels.getCountWheels();
}
}
public void DrawWheels(Graphics2D g,int startPosX[], int startPosY, Color color) {
Random rnd = new Random();
int ranOrnament = rnd.nextInt(0, 3);
switch (ranOrnament) {
switch (_seedOrnament) {
case 0:
_ornament = new DrawOrnament();
break;

View File

@@ -0,0 +1,9 @@
package Scripts.Wheels;
import java.awt.*;
public interface IDrawingWheels {
public void SetCountWheels(int Count);
public int getCountWheels();
public void DrawWheels(Graphics2D g, int startPosX[], int startPosY, Color color);
}