This commit is contained in:
LivelyPuer 2024-06-17 12:48:25 +04:00
parent feb5eccc7f
commit 1b5f6fc87a
16 changed files with 609 additions and 412 deletions

View File

@ -1,71 +0,0 @@
package Drawnings;
import Entities.EntityElectroTrans;
import java.awt.*;
public class DrawningElectroTrans extends DrawningTrans {
private EntityElectroTrans entityElectroTrans;
public DrawningElectroTrans(int speed, float weight, Color bodyColor, int wheelsType, Color additionalColor, boolean horns, boolean battery) {
super(speed, weight, bodyColor, wheelsType, 110, 60);
entityElectroTrans = new EntityElectroTrans(speed, weight, bodyColor, additionalColor, horns, battery);
}
public void drawTrans(Graphics g) {
if (entityElectroTrans == null || _startPosX == null || _startPosY == null) {
return;
}
super.drawTrans(g);
Graphics2D g2d = (Graphics2D) g;
Point[] electroTransHorns;
if (entityElectroTrans.getHorns()) {
electroTransHorns = new Point[]{
new Point(_startPosX + 40, _startPosY + 10),
new Point(_startPosX + 20, _startPosY),
new Point(_startPosX + 60, _startPosY),
};
} else {
electroTransHorns = new Point[]{
new Point(_startPosX + 40, _startPosY + 7),
new Point(_startPosX + 20, _startPosY + 7),
new Point(_startPosX + 60, _startPosY + 7),
};
}
Polygon electroTransHornsPolygon = new Polygon();
for (Point point : electroTransHorns)
electroTransHornsPolygon.addPoint(point.x, point.y);
g2d.setColor(entityElectroTrans.getAdditionalColor());
g2d.drawPolygon(electroTransHornsPolygon);
if (entityElectroTrans.getBattery()) {
Point[] electroTransBattery = new Point[]{
new Point(_startPosX + 25, _startPosY + 32),
new Point(_startPosX + 25, _startPosY + 36),
new Point(_startPosX + 22, _startPosY + 36),
new Point(_startPosX + 22, _startPosY + 40),
new Point(_startPosX + 25, _startPosY + 40),
new Point(_startPosX + 25, _startPosY + 46),
new Point(_startPosX + 58, _startPosY + 46),
new Point(_startPosX + 58, _startPosY + 32),
};
Polygon electroTransBatteryPolygon = new Polygon();
for (Point point : electroTransBattery)
electroTransBatteryPolygon.addPoint(point.x, point.y);
g2d.setColor(entityElectroTrans.getAdditionalColor());
g2d.fillPolygon(electroTransBatteryPolygon);
}
}
}

View File

@ -1,27 +0,0 @@
package Drawnings;
import java.awt.*;
public class DrawningRectWheels implements IDrawWheels {
private WheelsCount wheelsCount;
@Override
public void setNumber(int wheelCount) {
for (WheelsCount value : WheelsCount.values()) {
if (value.getEnumNumber() == wheelCount) {
wheelsCount = value;
return;
}
}
}
@Override
public void drawWheels(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int wheelDistance = 100 / wheelsCount.getEnumNumber();
for (int i = 0; i < wheelsCount.getEnumNumber(); i++) {
g2d.drawRect(_startX + 5 + i * wheelDistance, _startY + 46, 8, 8);
}
}
}

View File

@ -1,149 +0,0 @@
package Drawnings;
import Entities.*;
import MovementStrategy.*;
import java.awt.*;
import java.util.Random;
public class DrawningTrans {
private final EntityTrans entityTran;
public EntityTrans getEntityTrans() {
return entityTran;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
protected Integer _startPosX;
protected Integer _startPosY;
private int _drawingTransWidth = 110;
private int _drawingTransHeight = 60;
public int GetPosX(){return _startPosX;}
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _drawingTransWidth;}
public int GetHeight(){return _drawingTransHeight;}
private IDrawWheels drawWheels;
public DrawningTrans(int speed, float weight, Color bodyColor, int wheelsType) {
entityTran = new EntityTrans(speed, weight, bodyColor);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
_pictureHeight = null;
switch (wheelsType) {
case 0:
drawWheels = new DrawningWheels();
break;
case 1:
drawWheels = new DrawningTriangleWheels();
break;
case 2:
drawWheels = new DrawningRectWheels();
break;
}
Random random = new Random();
int wheelsCount = random.nextInt(1, 4);
System.out.print(wheelsCount);
drawWheels.setNumber(wheelsCount);
}
protected DrawningTrans(int speed, float weight, Color bodyColor, int wheelsType, int transWidth, int transHeight) {
this(speed, weight, bodyColor, wheelsType);
_drawingTransHeight = transHeight;
_drawingTransWidth = transWidth;
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;
_startPosX = x;
_startPosY = y;
if (_drawingTransWidth + x > _pictureWidth || x < 0) {
_startPosX = 0;
}
if (_drawingTransHeight + y > _pictureHeight || y < 0) {
_startPosY = 0;
}
}
public boolean setPictureSize(int width, int height) {
if (_drawingTransHeight > height || _drawingTransWidth > width)
return false;
_pictureHeight = height;
_pictureWidth = width;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX + _drawingTransWidth > width)
_startPosX = width - _drawingTransWidth;
if (_startPosY + _drawingTransHeight > height)
_startPosY = height - _drawingTransHeight;
}
return true;
}
public boolean moveTransport(MovementDirection direction) {
if (entityTran == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case MovementDirection.Left:
if (_startPosX - entityTran.getStep() > 0)
_startPosX -= (int) entityTran.getStep();
return true;
case MovementDirection.Up:
if (_startPosY - entityTran.getStep() > 0)
_startPosY -= (int) entityTran.getStep();
return true;
case MovementDirection.Right:
if (_startPosX + entityTran.getStep() < _pictureWidth - _drawingTransWidth)
_startPosX += (int) entityTran.getStep();
return true;
case MovementDirection.Down:
if (_startPosY + entityTran.getStep() < _pictureHeight - _drawingTransHeight)
_startPosY += (int) entityTran.getStep();
return true;
default:
return false;
}
}
public void drawTrans(Graphics g) {
if (entityTran == null || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
Point[] electroTransBorders = new Point[]{
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 10, _startPosY + 10),
new Point(_startPosX + 70, _startPosY + 10),
new Point(_startPosX + 80, _startPosY + 30),
new Point(_startPosX + 80, _startPosY + 50),
new Point(_startPosX, _startPosY + 50),
};
Polygon electroTransPolygon = new Polygon();
for (Point point : electroTransBorders)
electroTransPolygon.addPoint(point.x, point.y);
g2d.setColor(entityTran.getBodyColor());
g2d.drawPolygon(electroTransPolygon);
Point[] electroTransGlass = new Point[]{
new Point(_startPosX + 2, _startPosY + 30),
new Point(_startPosX + 10, _startPosY + 13),
new Point(_startPosX + 70, _startPosY + 13),
new Point(_startPosX + 78, _startPosY + 30),
};
Polygon electroTransGlassPolygon = new Polygon();
for (Point point : electroTransGlass)
electroTransGlassPolygon.addPoint(point.x, point.y);
g2d.setColor(entityTran.getBodyColor());
g2d.drawPolygon(electroTransGlassPolygon);
drawWheels.drawWheels(g2d, entityTran.getBodyColor(), _startPosX, _startPosY);
}
}

View File

@ -1,29 +0,0 @@
package Drawnings;
import java.awt.*;
public class DrawningTriangleWheels implements IDrawWheels {
private WheelsCount wheelsCount;
@Override
public void setNumber(int wheelCount) {
for (WheelsCount value : WheelsCount.values()) {
if (value.getEnumNumber() == wheelCount) {
wheelsCount = value;
return;
}
}
}
@Override
public void drawWheels(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int wheelDistance = 100 / wheelsCount.getEnumNumber();
for (int i = 0; i < wheelsCount.getEnumNumber(); i++) {
g2d.drawLine(_startX + 5 + i * wheelDistance - 4, (int) _startY + 44, _startX + 5 + i * wheelDistance + 4, (int) _startY + 44);
g2d.drawLine(_startX + 5 + i * wheelDistance + 4, (int) _startY + 44, _startX + 5 + i * wheelDistance, (int) _startY + 48);
g2d.drawLine(_startX + 5 + i * wheelDistance, (int) _startY + 48, _startX + 5 + i * wheelDistance - 4, (int) _startY + 44);
}
}
}

View File

@ -1,27 +0,0 @@
package Drawnings;
import java.awt.*;
public class DrawningWheels implements IDrawWheels {
private WheelsCount wheelsCount;
@Override
public void setNumber(int wheelCount) {
for (WheelsCount value : WheelsCount.values()) {
if (value.getEnumNumber() == wheelCount) {
wheelsCount = value;
return;
}
}
}
@Override
public void drawWheels(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int wheelDistance = 100 / wheelsCount.getEnumNumber();
for (int i = 0; i < wheelsCount.getEnumNumber(); i++) {
g2d.drawOval(_startX + 5 + i * wheelDistance, _startY + 46, 8, 8);
}
}
}

View File

@ -1,8 +0,0 @@
package Drawnings;
import java.awt.*;
public interface IDrawWheels {
void setNumber(int x);
void drawWheels(Graphics2D graphics2D, Color color, int _startX, int _startY);
}

View File

@ -1,16 +0,0 @@
package Drawnings;
public enum WheelsCount {
Two(2),
Three(3),
Four(4);
final private int EnumNumber;
WheelsCount(int enumNumber) {
EnumNumber = enumNumber;
}
public int getEnumNumber() {
return EnumNumber;
}
}

View File

@ -3,6 +3,7 @@ package Drawnings;
import CollectionGenericObjects.*;
import Forms.FormConstructor;
import Forms.FormElectroTrans;
import Forms.FormTransConfig;
import javax.swing.*;
import java.awt.*;
@ -12,6 +13,8 @@ import java.util.Stack;
public class DrawningAbstractCompany extends JComponent {
private AbstractCompany _company = null;
DrawningTrans _drawningTrans;
// public void collectionComboBox_SelectedIndexChanged(JComboBox<String> obj, int width, int height) {
private final StorageCollection<DrawningTrans> storageCollection = new StorageCollection<>();
private Stack<DrawningTrans> rubbishBinStack = new Stack<>();
@ -20,8 +23,7 @@ public class DrawningAbstractCompany extends JComponent {
switch (obj.getSelectedIndex()) {
case 1:
if (collectionList.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(frame,
"Коллекция не выбрана");
JOptionPane.showMessageDialog(frame, "Коллекция не выбрана");
return false;
}
_company = new TransSharingService(width, height, storageCollection.getCollection(collectionList.getSelectedValue()));
@ -31,31 +33,24 @@ public class DrawningAbstractCompany extends JComponent {
}
}
public void createObject(int type, JFrame obj) {
public void createObject(JFrame obj) {
if (_company == null) {
return;
}
DrawningTrans _drawningTrans;
Random random = new Random();
switch (type) {
case 0:
_drawningTrans = new DrawningTrans(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
getColorR(obj, random), random.nextInt(3));
break;
case 1:
_drawningTrans = new DrawningElectroTrans(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
getColorR(obj, random), random.nextInt(3),
getColorR(obj, random),
random.nextBoolean(), random.nextBoolean());
break;
default:
return;
}
FormTransConfig formTransConfig = new FormTransConfig();
formTransConfig.OpenFrame();
formTransConfig.getButtonAdd().addActionListener(e -> {
if (formTransConfig.getDrawningConfig().trans != null) {
_drawningTrans = formTransConfig.getDrawningConfig().trans;
if (AbstractCompany.add(_company, _drawningTrans) != -1) {
JOptionPane.showMessageDialog(obj, "Объект добавлен");
obj.repaint();
} else {
JOptionPane.showMessageDialog(obj, "Не удалось добавить объект");
}
formTransConfig.getjFrameTransConfig().dispatchEvent(new WindowEvent(formTransConfig.getjFrameTransConfig(), WindowEvent.WINDOW_CLOSING));
}
});
}
Color getColorR(JFrame obj, Random rnd) {
@ -72,11 +67,7 @@ public class DrawningAbstractCompany extends JComponent {
if (_company == null) {
return false;
}
int result = JOptionPane.showConfirmDialog(
obj,
"Удалить объект?",
"Подтвердение",
JOptionPane.YES_NO_OPTION);
int result = JOptionPane.showConfirmDialog(obj, "Удалить объект?", "Подтвердение", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
DrawningTrans deletableTrans = AbstractCompany.remove(_company, val);
if (deletableTrans != null) {
@ -144,26 +135,22 @@ public class DrawningAbstractCompany extends JComponent {
});
}
public StorageCollection<DrawningTrans> addCollectionButtonAction(JFrame jFrameCollectionBoats, JTextField textFieldSetCollectionName,
JRadioButton massiveRadioButton, JRadioButton listRadioButton) {
public StorageCollection<DrawningTrans> addCollectionButtonAction(JFrame jFrameCollectionBoats, JTextField textFieldSetCollectionName, JRadioButton massiveRadioButton, JRadioButton listRadioButton) {
if (textFieldSetCollectionName.getText().isEmpty() || (!massiveRadioButton.isSelected() && !listRadioButton.isSelected())) {
JOptionPane.showMessageDialog(jFrameCollectionBoats, "Не все элементы заполнены", "ERROR", JOptionPane.ERROR_MESSAGE);
return null;
}
CollectionType collectionType = CollectionType.None;
if (massiveRadioButton.isSelected())
collectionType = CollectionType.Massive;
else if (listRadioButton.isSelected())
collectionType = CollectionType.List;
if (massiveRadioButton.isSelected()) collectionType = CollectionType.Massive;
else if (listRadioButton.isSelected()) collectionType = CollectionType.List;
storageCollection.addCollection(textFieldSetCollectionName.getText(), collectionType);
return storageCollection;
}
public StorageCollection<DrawningTrans> deleteCollectionButtonAction(JFrame jFrameCollectionLocomotive, JList<String> keysList) {
if (keysList.getSelectedIndex() != -1) {
int result = JOptionPane.showConfirmDialog(jFrameCollectionLocomotive, "Удалить объект?",
"Подтверждение", JOptionPane.YES_NO_OPTION);
int result = JOptionPane.showConfirmDialog(jFrameCollectionLocomotive, "Удалить объект?", "Подтверждение", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
storageCollection.delCollection(keysList.getSelectedValue());
return storageCollection;

View File

@ -7,7 +7,7 @@ import java.awt.*;
public class DrawningElectroTrans extends DrawningTrans {
public DrawningElectroTrans(int speed, float weight, Color bodyColor, int wheelsType, Color additionalColor, boolean sail, boolean floaters) {
super(speed, weight, bodyColor, wheelsType, 110, 80);
entityTran = new EntityElectroTrans(speed, weight, bodyColor, additionalColor, floaters, sail);
entityTrans = new EntityElectroTrans(speed, weight, bodyColor, additionalColor, floaters, sail);
}
public DrawningElectroTrans(EntityElectroTrans entity, IDrawWheels wheels) {
super(entity, wheels);
@ -35,7 +35,7 @@ public class DrawningElectroTrans extends DrawningTrans {
@Override
public void drawTrans(Graphics g) {
if (entityTran == null || !(entityTran instanceof EntityElectroTrans entityElectroTrans) || _startPosX == null || _startPosY == null) {
if (entityTrans == null || !(entityTrans instanceof EntityElectroTrans entityElectroTrans) || _startPosX == null || _startPosY == null) {
return;
}
super.drawTrans(g);

View File

@ -2,28 +2,52 @@ package Drawnings;
import Entities.EntityTrans;
import MovementStrategy.*;
import java.awt.*;
import java.util.Random;
public class DrawningTrans {
public EntityTrans entityTran;
public EntityTrans getEntityTran() {
return entityTran;
public EntityTrans entityTrans;
public EntityTrans getEntityTrans() {
return entityTrans;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
protected Integer _startPosX;
protected Integer _startPosY;
private int _drawingBoatWidth = 107;
private int _drawingBoatHeight = 80;
public Integer GetPosX(){return _startPosX;}
public Integer GetPosY(){return _startPosY;}
public int GetWidth(){return _drawingBoatWidth;}
public int GetHeight(){return _drawingBoatHeight;}
public IDrawWheels drawWheels;
public Integer GetPosX() {
return _startPosX;
}
public Integer GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _drawingBoatWidth;
}
public int GetHeight() {
return _drawingBoatHeight;
}
protected IDrawWheels drawWheels;
public IDrawWheels getDrawWheels() {
return drawWheels;
}
public void setDrawWheels(IDrawWheels drawWheels) {
this.drawWheels = drawWheels;
}
public DrawningTrans(int speed, float weight, Color bodyColor, int wheelsType) {
entityTran = new EntityTrans(speed, weight, bodyColor);
entityTrans = new EntityTrans(speed, weight, bodyColor);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
@ -40,7 +64,7 @@ public class DrawningTrans {
break;
}
Random random = new Random();
int wheelsCount = random.nextInt(2,4);
int wheelsCount = random.nextInt(2, 4);
drawWheels.setNumber(wheelsCount);
}
@ -51,10 +75,12 @@ public class DrawningTrans {
_drawingBoatWidth = boatWidth;
}
public DrawningTrans(EntityTrans _entityBoat, IDrawWheels _drawPaddles) {
entityTran = _entityBoat;
drawWheels = _drawPaddles;
public DrawningTrans(EntityTrans _entityBoat, IDrawWheels _drawWheels) {
entityTrans = _entityBoat;
drawWheels = _drawWheels;
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;
@ -68,6 +94,7 @@ public class DrawningTrans {
_startPosY = 0;
}
}
public boolean setPictureSize(int width, int height) {
if (_drawingBoatHeight > height || _drawingBoatWidth > width)
@ -75,8 +102,7 @@ public class DrawningTrans {
_pictureHeight = height;
_pictureWidth = width;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX != null && _startPosY != null) {
if (_startPosX + _drawingBoatWidth > width)
_startPosX = width - _drawingBoatWidth;
if (_startPosY + _drawingBoatHeight > height)
@ -86,31 +112,32 @@ public class DrawningTrans {
}
public boolean moveTransport(MovementDirection direction) {
if (entityTran == null || _pictureWidth == null || _pictureHeight == null)
if (entityTrans == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case MovementDirection.Left:
if (_startPosX - entityTran.getStep() > 0)
_startPosX -= (int) entityTran.getStep();
if (_startPosX - entityTrans.getStep() > 0)
_startPosX -= (int) entityTrans.getStep();
return true;
case MovementDirection.Up:
if (_startPosY - entityTran.getStep() > 0)
_startPosY -= (int) entityTran.getStep();
if (_startPosY - entityTrans.getStep() > 0)
_startPosY -= (int) entityTrans.getStep();
return true;
case MovementDirection.Right:
if (_startPosX + entityTran.getStep() < _pictureWidth - _drawingBoatWidth)
_startPosX += (int) entityTran.getStep();
if (_startPosX + entityTrans.getStep() < _pictureWidth - _drawingBoatWidth)
_startPosX += (int) entityTrans.getStep();
return true;
case MovementDirection.Down:
if (_startPosY + entityTran.getStep() < _pictureHeight - _drawingBoatHeight)
_startPosY += (int) entityTran.getStep();
if (_startPosY + entityTrans.getStep() < _pictureHeight - _drawingBoatHeight)
_startPosY += (int) entityTrans.getStep();
return true;
default:
return false;
}
}
public void drawTrans(Graphics g) {
if (entityTran == null || _startPosX == null || _startPosY == null) {
if (entityTrans == null || _startPosX == null || _startPosY == null) {
return;
}
@ -128,7 +155,7 @@ public class DrawningTrans {
for (Point point : electroTransBorders)
electroTransPolygon.addPoint(point.x, point.y);
g2d.setColor(entityTran.getBodyColor());
g2d.setColor(entityTrans.getBodyColor());
g2d.drawPolygon(electroTransPolygon);
Point[] electroTransGlass = new Point[]{
@ -142,9 +169,9 @@ public class DrawningTrans {
for (Point point : electroTransGlass)
electroTransGlassPolygon.addPoint(point.x, point.y);
g2d.setColor(entityTran.getBodyColor());
g2d.setColor(entityTrans.getBodyColor());
g2d.drawPolygon(electroTransGlassPolygon);
drawWheels.drawWheels(g2d, entityTran.getBodyColor(), _startPosX, _startPosY);
drawWheels.drawWheels(g2d, entityTrans.getBodyColor(), _startPosX, _startPosY);
}
}

View File

@ -0,0 +1,20 @@
package Drawnings;
import javax.swing.*;
import java.awt.*;
public class DrawningTransConfig extends JComponent{
public DrawningTrans trans = null;
@Override
public void paintComponent(Graphics g) {
if (trans == null) {
return;
}
super.paintComponent(g);
trans.setPosition(10, 10);
trans.setPictureSize(200, 200);
trans.drawTrans(g);
super.repaint();
}
}

View File

@ -4,14 +4,23 @@ import java.awt.*;
public class EntityElectroTrans extends EntityTrans {
private Color AdditionalColor;
public void setAdditionalColor(Color additionalColor) {
AdditionalColor = additionalColor;
}
public Color getAdditionalColor() {
return AdditionalColor;
}
private boolean Horns;
public void setHorns(boolean horns) {
Horns = horns;
}
public boolean getHorns() {
return Horns;
}
private boolean Battery;
public void setBattery(boolean battery) {
Battery = battery;
}
public boolean getBattery() {
return Battery;
}

View File

@ -9,14 +9,26 @@ public class EntityTrans {
return Speed;
}
public void setSpeed(int speed) {
Speed = speed;
}
private double Weight;
public void setWeight(double weight) {
Weight = weight;
}
public double getWeight() {
return Weight;
}
private Color BodyColor;
public void setBodyColor(Color bodyColor) {
BodyColor = bodyColor;
}
public Color getBodyColor() {
return BodyColor;
}

View File

@ -1,6 +1,5 @@
package Forms;
import CollectionGenericObjects.CollectionType;
import CollectionGenericObjects.StorageCollection;
import Drawnings.DrawningAbstractCompany;
import Drawnings.DrawningTrans;
@ -12,7 +11,6 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
public class FormTransCollection extends JFrame {
final private JFrame jFrameCollectionTranss = new JFrame();
@ -30,9 +28,7 @@ public class FormTransCollection extends JFrame {
final private JLabel toolsNameLabel = new JLabel("Инструменты");
final private JPanel labelPanel = new JPanel();
final private JButton buttonAddTrans = new JButton("Добавить поезд");
final private JButton buttonAddElectroTrans = new JButton("Добавить Электропоезд");
final private JPanel addTransPanel = new JPanel();
final private JPanel addElectroTransPanel = new JPanel();
final private JButton buttonRemove = new JButton("Удалить поезд");
final private JPanel removePanel = new JPanel();
final private JButton goToCheckButton = new JButton("Отправит на тесты");
@ -89,10 +85,6 @@ public class FormTransCollection extends JFrame {
addTransPanel.setSize(170, 25);
addTransPanel.add(buttonAddTrans, BorderLayout.CENTER);
addElectroTransPanel.setLayout(new BorderLayout());
addElectroTransPanel.setSize(170, 25);
addElectroTransPanel.add(buttonAddElectroTrans, BorderLayout.CENTER);
removePanel.setLayout(new BorderLayout());
removePanel.setSize(170, 25);
removePanel.add(buttonRemove, BorderLayout.CENTER);
@ -168,7 +160,6 @@ public class FormTransCollection extends JFrame {
jFrameCollectionTranss.add(comboBoxPanel);
jFrameCollectionTranss.add(createCompanyPanel);
jFrameCollectionTranss.add(addTransPanel);
jFrameCollectionTranss.add(addElectroTransPanel);
jFrameCollectionTranss.add(textBoxPanel);
jFrameCollectionTranss.add(removePanel);
jFrameCollectionTranss.add(goToCheckPanel);
@ -178,7 +169,6 @@ public class FormTransCollection extends JFrame {
jFrameCollectionTranss.add(_company);
listOfDownPanel.add(buttonAddTrans);
listOfDownPanel.add(buttonAddElectroTrans);
listOfDownPanel.add(addFromConstructorButton);
listOfDownPanel.add(textBoxPosition);
listOfDownPanel.add(buttonRemove);
@ -204,7 +194,6 @@ public class FormTransCollection extends JFrame {
goGoToCheckFromRubbishBinPanel.setLocation(jFrameCollectionTranss.getWidth() - 200,
jFrameCollectionTranss.getHeight() - 295);
addTransPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 241);
addElectroTransPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 214);
addFromConstructorPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 187);
textBoxPanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 146);
removePanel.setLocation(jFrameCollectionTranss.getWidth() - 200, jFrameCollectionTranss.getHeight() - 119);
@ -217,11 +206,7 @@ public class FormTransCollection extends JFrame {
});
buttonAddTrans.addActionListener(e -> {
_company.createObject(0, jFrameCollectionTranss);
jFrameCollectionTranss.repaint();
});
buttonAddElectroTrans.addActionListener(e -> {
_company.createObject(1, jFrameCollectionTranss);
_company.createObject(jFrameCollectionTranss);
jFrameCollectionTranss.repaint();
});

View File

@ -0,0 +1,484 @@
package Forms;
import Drawnings.*;
import Entities.EntityTrans;
import Entities.EntityElectroTrans;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.io.IOException;
public class FormTransConfig extends JFrame {
final private JFrame jFrameTransConfig = new JFrame();
public JFrame getjFrameTransConfig() {
return jFrameTransConfig;
}
final private DrawningTransConfig drawningConfig = new DrawningTransConfig();
public DrawningTransConfig getDrawningConfig() {
return drawningConfig;
}
final private JSpinner spinnerSpeed = new JSpinner(new SpinnerNumberModel(100, 100, 1000, 100));
final private JSpinner spinnerWeight = new JSpinner(new SpinnerNumberModel(100, 100, 1000, 100));
final private JSpinner spinnerWheels = new JSpinner(new SpinnerNumberModel(2, 2, 4, 1));
final private JCheckBox checkBoxSail = new JCheckBox();
final private JCheckBox checkBoxFloaters = new JCheckBox();
final private JPanel CheckBoxesPanel = new JPanel();
final private JPanel SpinnersPanel = new JPanel();
final private JPanel PicturePanel = new JPanel();
final private JLabel labelBodyColor = new JLabel("Основной цвет");
final private JLabel labelAdditionalColor = new JLabel("Доп цвет");
final private JPanel TransColorsPanel = new JPanel();
final private JLabel labelTrans = new JLabel("Простой");
final private JLabel labelCatamaran = new JLabel("Продвинутый");
final private JPanel TypeTransPanel = new JPanel();
final private JPanel ColorsPanel = new JPanel();
final private JPanel panelRed = new JPanel();
final private JPanel panelGreen = new JPanel();
final private JPanel panelBlue = new JPanel();
final private JPanel panelYellow = new JPanel();
final private JPanel panelWhite = new JPanel();
final private JPanel panelBlack = new JPanel();
final private JPanel panelGray = new JPanel();
final private JPanel panelPink = new JPanel();
final private JLabel labelColor = new JLabel("Цвет");
final private JPanel labelColorPanel = new JPanel();
final private JLabel labelDefaultWheels = new JLabel("Обычные");
final private JLabel labelOvalWheels = new JLabel("Квадратные");
final private JLabel labelRectanglesWheels = new JLabel("Треугольные");
final private JPanel WheelsPanel = new JPanel();
final private JLabel labelSpeed = new JLabel("Скорость:");
final private JLabel labelWeight = new JLabel("Вес:");
final private JLabel labelWheels = new JLabel("Колеса:");
final private JLabel labelSail = new JLabel("Рога:");
final private JLabel labelFloaters = new JLabel("Батареи:");
final private JButton buttonAdd = new JButton("Создать");
final private JPanel buttonAddPanel = new JPanel();
public JButton getButtonAdd() {
return buttonAdd;
}
final private JButton buttonCancel = new JButton("Отмена");
final private JPanel buttonCancelPanel = new JPanel();
public void OpenFrame() {
jFrameTransConfig.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Toolkit tolls = Toolkit.getDefaultToolkit();
Dimension dimension = tolls.getScreenSize();
jFrameTransConfig.setBounds(dimension.width / 2 - 250, dimension.height / 2 - 250,
700, 330);
jFrameTransConfig.setTitle("Создание объекта");
SpinnersPanel.setLayout(new GridLayout(3, 2));
labelSpeed.setSize(new Dimension(50, 30));
spinnerSpeed.setSize(new Dimension(120, 30));
labelWeight.setSize(new Dimension(50, 30));
spinnerWeight.setSize(new Dimension(120, 30));
labelWheels.setSize(new Dimension(50, 30));
spinnerWheels.setSize(new Dimension(120, 30));
SpinnersPanel.setSize(new Dimension(170, 90));
SpinnersPanel.add(labelSpeed);
SpinnersPanel.add(spinnerSpeed);
SpinnersPanel.add(labelWeight);
SpinnersPanel.add(spinnerWeight);
SpinnersPanel.add(labelWheels);
SpinnersPanel.add(spinnerWheels);
CheckBoxesPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
CheckBoxesPanel.setSize(new Dimension(100, 90));
CheckBoxesPanel.add(labelSail);
CheckBoxesPanel.add(checkBoxSail);
CheckBoxesPanel.add(labelFloaters);
CheckBoxesPanel.add(checkBoxFloaters);
labelTrans.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelCatamaran.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
TypeTransPanel.setLayout(new BorderLayout());
TypeTransPanel.setSize(new Dimension(140, 42));
TypeTransPanel.add(labelTrans, BorderLayout.WEST);
TypeTransPanel.add(labelCatamaran, BorderLayout.EAST);
PicturePanel.setSize(new Dimension(220, 130));
PicturePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelBodyColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelAdditionalColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
TransColorsPanel.setLayout(new BorderLayout());
TransColorsPanel.setSize(new Dimension(150, 50));
TransColorsPanel.add(labelBodyColor, BorderLayout.WEST);
TransColorsPanel.add(labelAdditionalColor, BorderLayout.EAST);
labelDefaultWheels.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelOvalWheels.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelRectanglesWheels.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
WheelsPanel.setLayout(new BorderLayout(5, 5));
WheelsPanel.setSize(new Dimension(210, 42));
WheelsPanel.add(labelDefaultWheels, BorderLayout.WEST);
WheelsPanel.add(labelOvalWheels, BorderLayout.CENTER);
WheelsPanel.add(labelRectanglesWheels, BorderLayout.EAST);
panelRed.setBackground(Color.RED);
panelGreen.setBackground(Color.GREEN);
panelBlue.setBackground(Color.BLUE);
panelYellow.setBackground(Color.YELLOW);
panelWhite.setBackground(Color.WHITE);
panelBlack.setBackground(Color.BLACK);
panelGray.setBackground(Color.GRAY);
panelPink.setBackground(Color.PINK);
ColorsPanel.setLayout(new GridLayout(2, 4, 5, 5));
ColorsPanel.setSize(new Dimension(150, 75));
ColorsPanel.add(panelRed);
ColorsPanel.add(panelGreen);
ColorsPanel.add(panelBlue);
ColorsPanel.add(panelYellow);
ColorsPanel.add(panelWhite);
ColorsPanel.add(panelBlack);
ColorsPanel.add(panelGray);
ColorsPanel.add(panelPink);
labelColorPanel.setLayout(new BorderLayout());
labelColorPanel.setSize(new Dimension(70, 30));
labelColorPanel.add(labelColor, BorderLayout.CENTER);
buttonAddPanel.setLayout(new BorderLayout());
buttonAddPanel.setSize(new Dimension(90, 40));
buttonAddPanel.add(buttonAdd, BorderLayout.CENTER);
buttonCancelPanel.setLayout(new BorderLayout());
buttonCancelPanel.setSize(new Dimension(90, 40));
buttonCancelPanel.add(buttonCancel, BorderLayout.CENTER);
jFrameTransConfig.setLayout(null);
jFrameTransConfig.add(buttonCancelPanel);
jFrameTransConfig.add(buttonAddPanel);
jFrameTransConfig.add(WheelsPanel);
jFrameTransConfig.add(CheckBoxesPanel);
jFrameTransConfig.add(TypeTransPanel);
jFrameTransConfig.add(labelColorPanel);
jFrameTransConfig.add(ColorsPanel);
jFrameTransConfig.add(PicturePanel);
jFrameTransConfig.add(TransColorsPanel);
jFrameTransConfig.add(SpinnersPanel);
jFrameTransConfig.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
SpinnersPanel.setLocation(jFrameTransConfig.getWidth()-650, 10);
CheckBoxesPanel.setLocation(jFrameTransConfig.getWidth()-650, 115);
TypeTransPanel.setLocation(jFrameTransConfig.getWidth()-670, 235);
TransColorsPanel.setLocation(jFrameTransConfig.getWidth()-237, 35);
labelColorPanel.setLocation(jFrameTransConfig.getWidth()-390, 5);
PicturePanel.setLocation(jFrameTransConfig.getWidth()-270, 95);
ColorsPanel.setLocation(jFrameTransConfig.getWidth()-440, 35);
WheelsPanel.setLocation(jFrameTransConfig.getWidth()-500, 235);
buttonAddPanel.setLocation(jFrameTransConfig.getWidth()-225, 235);
buttonCancelPanel.setLocation(jFrameTransConfig.getWidth()-125, 235);
jFrameTransConfig.repaint();
}
});
MouseAdapter labelObjectsMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
};
TransferHandler labelObjectsTransferHandler = new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel) c).getText());
}
};
MouseAdapter labelWheelsMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
};
labelTrans.addMouseListener(labelObjectsMouseDown);
labelTrans.setTransferHandler(labelObjectsTransferHandler);
labelCatamaran.addMouseListener(labelObjectsMouseDown);
labelCatamaran.setTransferHandler(labelObjectsTransferHandler);
labelDefaultWheels.addMouseListener(labelWheelsMouseDown);
labelRectanglesWheels.addMouseListener(labelWheelsMouseDown);
labelOvalWheels.addMouseListener(labelWheelsMouseDown);
labelDefaultWheels.setTransferHandler(new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
return new WheelsTransferable(new DrawningWheels());
}
});
labelOvalWheels.setTransferHandler(new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
return new WheelsTransferable(new DrawningRectWheels());
}
});
labelRectanglesWheels.setTransferHandler(new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
return new WheelsTransferable(new DrawningRectangleWheels());
}
});
PicturePanel.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor)
|| support.isDataFlavorSupported(WheelsTransferable.wheelsDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
try {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
IDrawWheels wheels = new DrawningWheels();
wheels.setNumber((int) spinnerWheels.getValue());
switch (data) {
case "Простой":
drawningConfig.trans = new DrawningTrans(new EntityTrans((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(), Color.WHITE), wheels);
PicturePanel.repaint();
return true;
case "Продвинутый":
drawningConfig.trans = new DrawningElectroTrans(new EntityElectroTrans((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.WHITE, Color.BLACK, checkBoxSail.isSelected(), checkBoxFloaters.isSelected()), wheels);
PicturePanel.repaint();
return true;
}
}catch (UnsupportedFlavorException | IOException e) {}
try {
IDrawWheels drawWheels =
(IDrawWheels) support.getTransferable().getTransferData(WheelsTransferable.wheelsDataFlavor);
drawningConfig.trans.setDrawWheels(drawWheels);
drawningConfig.trans.getDrawWheels().setNumber((int) spinnerWheels.getValue());
}catch (UnsupportedFlavorException | IOException e) {}
PicturePanel.repaint();
return false;
}
});
PicturePanel.setLayout(new BorderLayout());
PicturePanel.add(drawningConfig, BorderLayout.CENTER);
MouseAdapter colorMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
};
panelBlack.addMouseListener(colorMouseDown);
panelBlack.setTransferHandler(new ColorTransferHandler());
panelBlue.addMouseListener(colorMouseDown);
panelBlue.setTransferHandler(new ColorTransferHandler());
panelGreen.addMouseListener(colorMouseDown);
panelGreen.setTransferHandler(new ColorTransferHandler());
panelGray.addMouseListener(colorMouseDown);
panelGray.setTransferHandler(new ColorTransferHandler());
panelPink.addMouseListener(colorMouseDown);
panelPink.setTransferHandler(new ColorTransferHandler());
panelRed.addMouseListener(colorMouseDown);
panelRed.setTransferHandler(new ColorTransferHandler());
panelYellow.addMouseListener(colorMouseDown);
panelYellow.setTransferHandler(new ColorTransferHandler());
panelWhite.addMouseListener(colorMouseDown);
panelWhite.setTransferHandler(new ColorTransferHandler());
labelBodyColor.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (drawningConfig.trans == null) return false;
drawningConfig.trans.entityTrans.setBodyColor(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
labelAdditionalColor.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
if (!(drawningConfig.trans instanceof DrawningElectroTrans)) return false;
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (drawningConfig.trans == null) return false;
if (drawningConfig.trans.entityTrans instanceof EntityElectroTrans catamaran) {
catamaran.setAdditionalColor(color);
return true;
}
return false;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
spinnerWheels.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.trans == null) {
return;
}
drawningConfig.trans.getDrawWheels().setNumber((int) spinnerWheels.getValue());
PicturePanel.repaint();
}
});
spinnerWeight.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.trans == null) {
return;
}
drawningConfig.trans.entityTrans.setWeight((int) spinnerWeight.getValue());
PicturePanel.repaint();
}
});
spinnerSpeed.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.trans == null) {
return;
}
drawningConfig.trans.entityTrans.setSpeed((int) spinnerSpeed.getValue());
PicturePanel.repaint();
}
});
checkBoxSail.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.trans == null) {
return;
}
if (drawningConfig.trans.entityTrans instanceof EntityElectroTrans) {
((EntityElectroTrans) drawningConfig.trans.entityTrans).setHorns(checkBoxSail.isSelected());
PicturePanel.repaint();
}
}
});
checkBoxFloaters.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (drawningConfig.trans == null) {
return;
}
if (drawningConfig.trans.entityTrans instanceof EntityElectroTrans) {
((EntityElectroTrans) drawningConfig.trans.entityTrans).setBattery(checkBoxFloaters.isSelected());
PicturePanel.repaint();
}
}
});
buttonCancel.addActionListener(e -> {
jFrameTransConfig.dispose();
}
);
jFrameTransConfig.setVisible(true);
}
private class ColorTransferable implements Transferable {
private Color color;
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
public ColorTransferable(Color color) {
this.color = color;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{colorDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return colorDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private class ColorTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(c.getBackground());
}
}
private class WheelsTransferable implements Transferable {
private IDrawWheels wheels;
private static final DataFlavor wheelsDataFlavor = new DataFlavor(IDrawWheels.class, "Wheels");
public WheelsTransferable(IDrawWheels wheels) {
this.wheels = wheels;
this.wheels.setNumber((int) spinnerWheels.getValue());
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{wheelsDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(wheelsDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return wheels;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
}

View File

@ -10,14 +10,14 @@ public class MoveableTrans implements IMoveableObject {
public ObjectParameters GetObjectPosition()
{
if (_trans == null || _trans.getEntityTran() == null)
if (_trans == null || _trans.getEntityTrans() == null)
{
return null;
}
return new ObjectParameters(_trans.GetPosX(), _trans.GetPosY(), _trans.GetWidth(), _trans.GetHeight());
}
public int GetStep() { return (int) _trans.getEntityTran().getStep(); }
public int GetStep() { return (int) _trans.getEntityTrans().getStep(); }
public boolean TryMoveObject(MovementDirection direction) { return _trans.moveTransport(direction); }
public void MoveObject(MovementDirection direction) { _trans.moveTransport(direction); }
}