закончил labwork03

This commit is contained in:
BoiledMilk123 2024-06-04 23:53:41 +04:00
parent cc59420fbf
commit 506a4eb234
15 changed files with 772 additions and 60 deletions

View File

@ -1,10 +1,12 @@
package ProjectElectricLocomotive; package ProjectElectricLocomotive;
import ProjectElectricLocomotive.src.*; import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive;
import ProjectElectricLocomotive.src.Forms.FormElectricLocomotive;
import ProjectElectricLocomotive.src.Forms.FormLocomotiveCollection;
public class Main { public class Main {
public static void main(String[] args) public static void main(String[] args)
{ {
new FormElectricLocomotive(); new FormLocomotiveCollection();
} }
} }

View File

@ -0,0 +1,73 @@
package ProjectElectricLocomotive.src.CollectionGenericObjects;
import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.util.Optional;
import java.util.Random;
public abstract class AbstractCompany
{
protected final int _placeSizeWidth = 100;
protected final int _placeSizeHeight = 65;
protected int _pictureWidth;
protected int _pictureHeight;
protected ICollectionGenericObjects<DrawingLocomotive> _collection = null;
private int getMaxCount() {
return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
}
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawingLocomotive> collection)
{
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = collection;
_collection.setMaxCount(getMaxCount());
}
public int add(DrawingLocomotive ship) {
if (_collection == null) {
return -1;
}
return _collection.insert(ship);
}
public DrawingLocomotive remove(AbstractCompany company, int position) {
if(company._collection == null) return null;
return company._collection.remove(position);
}
public DrawingLocomotive GetRandomObjects()
{
if(_collection == null) return null;
Random rnd = new Random();
int maxCount = getMaxCount();
return _collection.get(rnd.nextInt(maxCount));
}
public BufferedImage show(JLabel pictureBox) {
BufferedImage bitmap = new BufferedImage(pictureBox.getWidth(), pictureBox.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = bitmap.createGraphics();
DrawBackground(graphics);
SetObjectsPosition();
for (int i = 0; i < (_collection != null ? _collection.getCount() : 0); ++i) {
if(_collection == null || _collection.get(i) == null) continue;
DrawingLocomotive obj = _collection.get(i);
obj.DrawTransport(graphics);
}
graphics.dispose();
return bitmap;
}
protected abstract void DrawBackground(Graphics graphics);
protected abstract void SetObjectsPosition();
}

View File

@ -0,0 +1,18 @@
package ProjectElectricLocomotive.src.CollectionGenericObjects;
import java.util.Optional;
public interface ICollectionGenericObjects <T extends Object>
{
int getCount();
void setMaxCount(int maxCount);
int insert(T obj);
int insert(T obj, int position);
T remove(int position);
T get(int position);
}

View File

@ -0,0 +1,57 @@
package ProjectElectricLocomotive.src.CollectionGenericObjects;
import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.AbstractMap;
public class LocomotiveDepotService extends AbstractCompany
{
private List<AbstractMap.SimpleEntry<Integer, Integer>> locCoord = new ArrayList<>();
private int countInRow;
public LocomotiveDepotService(int picWidth, int picHeight, ICollectionGenericObjects<DrawingLocomotive> collection)
{
super(picWidth, picHeight, collection);
}
@Override
protected void DrawBackground(Graphics g)
{
Color brown = new Color(52, 22, 22);
g.setColor(brown);
int x = 1, y = 0;
while (y + _placeSizeHeight <= _pictureHeight) {
int count = 0;
while (x + _placeSizeWidth <= _pictureWidth) {
count++;
g.drawLine(x, y, x + _placeSizeWidth, y);
g.drawLine(x, y, x, y + _placeSizeHeight);
g.drawLine(x, y + _placeSizeHeight, x + _placeSizeWidth, y + _placeSizeHeight);
locCoord.add(new AbstractMap.SimpleEntry<>(x, y));
x += _placeSizeWidth + 2;
}
countInRow = count;
x = 1;
y += _placeSizeHeight + 5;
}
}
@Override
protected void SetObjectsPosition() {
if (locCoord == null || _collection == null) return;
int row = 1, col = 1;
for (int i = 0; i < _collection.getCount(); i++, col++)
{
if(_collection.get(i) == null) continue;
_collection.get(i).SetPictureSize(_pictureWidth, _pictureHeight);
_collection.get(i).SetPosition(locCoord.get(row * countInRow - col).getKey() + 5, locCoord.get(row * countInRow - col).getValue() + 5);
if (col == countInRow)
{
col = 0;
row++;
}
}
}
}

View File

@ -0,0 +1,74 @@
package ProjectElectricLocomotive.src.CollectionGenericObjects;
import java.util.Optional;
public class MassiveGenericObjects<T extends Object> implements ICollectionGenericObjects<T>
{
private T[] _collection;
@Override
public int getCount(){
return _collection.length;
}
@Override
public void setMaxCount(int maxCount) {
if (maxCount > 0) {
_collection = (T[]) new Object[maxCount];
}
}
public MassiveGenericObjects() {
_collection = (T[]) new Object[0]; // Create an empty array of type T
}
@Override
public int insert(T obj) {
return insert(obj, 0);
}
@Override
public int insert(T obj, int position) {
for(int i = position; i < getCount(); i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
for(int i = position - 1; i >= 0; --i)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
return -1;
}
@Override
public T remove(int position) {
if (position < 0 || position >= getCount()) return null;
if (_collection[position] != null)
{
T obj = _collection[position];
_collection[position] = null;
return obj;
}
return null;
}
@Override
public T get(int position) {
if(position >= 0 && position < getCount())
{
return _collection[position];
}
else
{
return null;
}
}
}

View File

@ -0,0 +1,76 @@
package ProjectElectricLocomotive.src.CollectionGenericObjects;
import ProjectElectricLocomotive.src.Drawnings.*;
import ProjectElectricLocomotive.src.Entities.EntityElectricLocomotive;
import ProjectElectricLocomotive.src.Entities.EntityLocomotive;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class SpecialGenerateLocomotive <T extends EntityLocomotive, U extends IDrawingOrnament>
{
public List<T> entityArray = new ArrayList<T>();
public List<U> wheelsArray = new ArrayList<U>();
public void add(T element) {
entityArray.add(element);
}
public void add(U element) {
wheelsArray.add(element);
}
public String getEn(int ind) {
if(entityArray.get(ind) instanceof EntityElectricLocomotive) {
EntityElectricLocomotive entity = (EntityElectricLocomotive)entityArray.get(ind);
return "EntityWarmlyShip{" +
"speed=" + entity.GetSpeed() +
", weight=" + entity.GetWeight() +
", bodyColor=" + entity.GetBodyColor() +
", AdditionalColor=" + entity.GetAdditionalColor() +
", Horns=" + entity.GetElectricHorns() +
", BatteryPlacement=" + entity.GetBatteryPlacement() +
", step=" + entity.GetStep() +
'}';
}
else{
return "EntityShip{" +
"speed=" + entityArray.get(ind).GetSpeed() +
", weight=" + entityArray.get(ind).GetWeight() +
", bodyColor=" + entityArray.get(ind).GetBodyColor() +
", step=" + entityArray.get(ind).GetStep() +
'}';
}
}
public String getDec(int ind)
{
if(wheelsArray.get(ind) instanceof DrawingOrnamentWheels)
{
return "DrawingOrnamentWheels - " + ((DrawingOrnamentWheels) wheelsArray.get(ind))._wheels.GetCountWheels();
}
else if(wheelsArray.get(ind) instanceof DrawingWheels)
{
return "DrawingWheels - " + ((DrawingWheels) wheelsArray.get(ind))._wheels.GetCountWheels();
}
else
{
return "DrawingSecondOrnamentWheels - " + ((DrawningSecondOrnamentWheels) wheelsArray.get(ind))._wheels.GetCountWheels();
}
}
public DrawingLocomotive createDrawingObject() {
Random rnd = new Random();
T _objectFromArray = entityArray.get(rnd.nextInt(entityArray.size()));
DrawingLocomotive _locomotiveCreated;
if (_objectFromArray instanceof EntityElectricLocomotive) {
DrawingElectricLocomotive _electricLocomotive = new DrawingElectricLocomotive(_objectFromArray.GetSpeed(), _objectFromArray.GetWeight(), _objectFromArray.GetBodyColor(), ((EntityElectricLocomotive) _objectFromArray).GetAdditionalColor(), ((EntityElectricLocomotive) _objectFromArray).GetElectricHorns(), ((EntityElectricLocomotive) _objectFromArray).GetBatteryPlacement());
_electricLocomotive._ornament = wheelsArray.get(rnd.nextInt(wheelsArray.size()));
_locomotiveCreated = _electricLocomotive;
} else {
_locomotiveCreated = new DrawingLocomotive(_objectFromArray.GetSpeed(), _objectFromArray.GetWeight(), _objectFromArray.GetBodyColor());
}
return _locomotiveCreated;
}
}

View File

@ -6,7 +6,7 @@ import java.awt.*;
public class DrawingOrnamentWheels implements IDrawingOrnament public class DrawingOrnamentWheels implements IDrawingOrnament
{ {
private CountWheels _wheels; public CountWheels _wheels;
@Override @Override
public void SetCountWheels(int count) public void SetCountWheels(int count)
{ {

View File

@ -5,7 +5,7 @@ import ProjectElectricLocomotive.src.CountWheels;
import java.awt.*; import java.awt.*;
public class DrawingWheels implements IDrawingOrnament public class DrawingWheels implements IDrawingOrnament
{ {
private CountWheels _wheels; public CountWheels _wheels;
@Override @Override
public void SetCountWheels(int count) public void SetCountWheels(int count)
{ {
@ -19,6 +19,8 @@ public class DrawingWheels implements IDrawingOrnament
} }
} }
@Override @Override
public void DrawWheels(Graphics g,int _startPosX, int _startPosY) public void DrawWheels(Graphics g,int _startPosX, int _startPosY)
{ {

View File

@ -6,7 +6,7 @@ import java.awt.*;
public class DrawningSecondOrnamentWheels implements IDrawingOrnament public class DrawningSecondOrnamentWheels implements IDrawingOrnament
{ {
private CountWheels _wheels; public CountWheels _wheels;
@Override @Override
public void SetCountWheels(int count) public void SetCountWheels(int count)
{ {

View File

@ -1,14 +1,12 @@
package ProjectElectricLocomotive.src.Drawnings; package ProjectElectricLocomotive.src.Forms;
import ProjectElectricLocomotive.src.DirectionType; import ProjectElectricLocomotive.src.DirectionType;
import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive; import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive;
import ProjectElectricLocomotive.src.FormElectricLocomotive;
import ProjectElectricLocomotive.src.MovementStrategy.*; import ProjectElectricLocomotive.src.MovementStrategy.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Random;
public class DrawingField extends JPanel { public class DrawingField extends JPanel {
private final FormElectricLocomotive field; private final FormElectricLocomotive field;
@ -17,6 +15,14 @@ public class DrawingField extends JPanel {
public DrawingField(FormElectricLocomotive field) { public DrawingField(FormElectricLocomotive field) {
this.field = field; this.field = field;
} }
public void setLocomotive(DrawingLocomotive locomotive) {
_drawingLocomotive = locomotive;
_drawingLocomotive.SetPictureSize(getWidth(),getHeight());
field.comboBoxStrategy.setEnabled(true);
_strategy = null;
}
@Override @Override
protected void paintComponent(Graphics g) protected void paintComponent(Graphics g)
{ {
@ -43,36 +49,6 @@ public class DrawingField extends JPanel {
if (_drawingLocomotive == null) return; if (_drawingLocomotive == null) return;
_drawingLocomotive.MoveTransport(DirectionType.Left); _drawingLocomotive.MoveTransport(DirectionType.Left);
} }
public void CreateObject(String type){
Random rnd = new Random();
switch (type)
{
case "DrawningLocomotive":
_drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
break;
case "DrawningElectricLocomotive":
_drawingLocomotive = new DrawingElectricLocomotive(rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextBoolean(),
rnd.nextBoolean());
break;
default:
return;
}
_drawingLocomotive.SetPictureSize(getWidth(),getHeight());
_drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
}
public void CreateButtonElectricLocomotiveAction(){
CreateObject("DrawningElectricLocomotive");
}
public void CreateButtonLocomotiveAction(){
CreateObject("DrawningLocomotive");
}
public void ButtonStrategyStepAction() public void ButtonStrategyStepAction()
{ {

View File

@ -0,0 +1,84 @@
package ProjectElectricLocomotive.src.Forms;
import ProjectElectricLocomotive.src.CollectionGenericObjects.SpecialGenerateLocomotive;
import ProjectElectricLocomotive.src.Drawnings.*;
import ProjectElectricLocomotive.src.Entities.EntityElectricLocomotive;
import ProjectElectricLocomotive.src.Entities.EntityLocomotive;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingGenerateLocomotive extends JPanel
{
private final FormGenerateLocomotive field;
protected SpecialGenerateLocomotive _generateLocomotive;
private DrawingLocomotive _locomotive = null;
public void setGenerateLocomotive()
{
_generateLocomotive = new SpecialGenerateLocomotive();
for(int i = 0; i < 3; i++)
{
Random rnd = new Random();
EntityLocomotive _EntityLocomotive;
int shipNum = rnd.nextInt(2);
switch (shipNum){
case 1:
_EntityLocomotive = new EntityLocomotive(rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
break;
default:
_EntityLocomotive = new EntityElectricLocomotive(rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextBoolean(),
rnd.nextBoolean());
break;
}
_generateLocomotive.add(_EntityLocomotive);
IDrawingOrnament _wheels;
int varDecks = rnd.nextInt(3);
switch (varDecks)
{
case 0:
_wheels = new DrawingWheels();
break;
case 1:
_wheels = new DrawingOrnamentWheels();
break;
default:
_wheels = new DrawningSecondOrnamentWheels();
break;
}
_wheels.SetCountWheels(rnd.nextInt(1,4));
_generateLocomotive.add(_wheels);
}
}
@Override
protected void paintComponent(Graphics g)
{
if (_locomotive == null) return;
_locomotive.DrawTransport(g);
//repaint();
}
public DrawingGenerateLocomotive(FormGenerateLocomotive field) {
this.field = field;
setGenerateLocomotive();
}
public void createNewObject()
{
_locomotive = _generateLocomotive.createDrawingObject();
_locomotive.SetPictureSize(field.getWidth(), field.getHeight());
_locomotive.SetPosition(field.panelLocomotive.getWidth()/2 - 50, field.getHeight()/2 - 50);
}
public void insertCollection(){
if(_locomotive == null) return;
field._collectionForm.insertNewLocomotive(_locomotive);
//createNewObject();
}
}

View File

@ -0,0 +1,123 @@
package ProjectElectricLocomotive.src.Forms;
import ProjectElectricLocomotive.src.CollectionGenericObjects.AbstractCompany;
import ProjectElectricLocomotive.src.CollectionGenericObjects.LocomotiveDepotService;
import ProjectElectricLocomotive.src.CollectionGenericObjects.MassiveGenericObjects;
import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive;
import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingLocomotiveCollection extends JPanel
{
private final FormLocomotiveCollection field;
private AbstractCompany _company = null;
public DrawingLocomotiveCollection(FormLocomotiveCollection field) {
this.field = field;
}
void comboBoxSelectorCompany_SelectedIndexChanged(String text)
{
switch (text)
{
case "Хранилище":
_company = new LocomotiveDepotService(field.pictureBox.getWidth(), field.pictureBox.getHeight(), new MassiveGenericObjects<DrawingLocomotive>());
break;
}
}
public void insertNewLocomotive(DrawingLocomotive _drawingLocomotive)
{
if (_company == null) return;
if (_company.add(_drawingLocomotive) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен");
field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox)));
}
else
{
JOptionPane.showMessageDialog(null, "Объект не удалось добавить");
}
}
private void CreateObject(String type)
{
if (_company == null) return;
DrawingLocomotive _drawingLocomotive;
Random rnd = new Random();
switch (type)
{
case "DrawningLocomotive":
_drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
getColor(rnd));
break;
case "DrawningElectricLocomotive":
_drawingLocomotive = new DrawingElectricLocomotive(rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
getColor(rnd),
getColor(rnd),
rnd.nextBoolean(),
rnd.nextBoolean());
break;
default:
return;
}
insertNewLocomotive(_drawingLocomotive);
}
private static Color getColor(Random random) {
Color initialColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", initialColor);
if (selectedColor != null) {
return selectedColor;
} else {
return initialColor;
}
}
public void CreateButtonElectricLocomotiveAction(){
CreateObject("DrawningElectricLocomotive");
}
public void CreateButtonLocomotiveAction(){
CreateObject("DrawningLocomotive");
}
public void ButtonCreateSpecialLocomotive_Click()
{
FormGenerateLocomotive form = new FormGenerateLocomotive(this);
}
public void ButtonRemoveLocomotive_Click(JFormattedTextField maskedTextField)
{
if (maskedTextField.getText().isEmpty() || _company == null) return;
int pos = Integer.parseInt(maskedTextField.getText());
int choice = JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (choice == JOptionPane.NO_OPTION) return;
if (_company.remove(_company, pos) != null) {
JOptionPane.showMessageDialog(null, "Объект удален");
field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox)));
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
}
protected void ButtonGoToCheck_Click()
{
if (_company == null) return;
DrawingLocomotive locomotive = null;
int counter = 100;
while (locomotive == null)
{
locomotive = _company.GetRandomObjects();
counter--;
if(counter <= 0) break;
}
if (locomotive == null) return;
FormElectricLocomotive form = new FormElectricLocomotive(locomotive);
}
protected void ButtonRefresh_Click()
{
if (_company == null) return;
field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox)));
}
}

View File

@ -1,6 +1,7 @@
package ProjectElectricLocomotive.src; package ProjectElectricLocomotive.src.Forms;
import ProjectElectricLocomotive.src.Drawnings.DrawingField; import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive;
import ProjectElectricLocomotive.src.Forms.DrawingField;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -26,29 +27,26 @@ public class FormElectricLocomotive extends JFrame{
JButton ButtonStep = new JButton("Шаг"); JButton ButtonStep = new JButton("Шаг");
JButton ButtonCreateElectricLocomotive=new JButton("CreateElectricLocomotive");
JButton ButtonCreateLocomotive=new JButton("CreateLocomotive");
public JComboBox<String> comboBoxStrategy = new JComboBox<>(); public JComboBox<String> comboBoxStrategy = new JComboBox<>();
JButton ButtonUp=new JButton("Up"); JButton ButtonUp=new JButton();
JButton ButtonDown=new JButton("Down"); JButton ButtonDown=new JButton();
JButton ButtonRight=new JButton("Right"); JButton ButtonRight=new JButton("Right");
JButton ButtonLeft=new JButton("Left"); JButton ButtonLeft=new JButton("Left");
public FormElectricLocomotive(){ public FormElectricLocomotive(DrawingLocomotive _drawingLocomotive){
super("Electric Locomotive"); super("Тесты");
setSize(900,500); setSize(900,500);
Width = getWidth(); Width = getWidth();
Height = getHeight(); Height = getHeight();
setBackground(Color.WHITE); setBackground(Color.WHITE);
ShowWindow(); ShowWindow();
field.setLocomotive(_drawingLocomotive);
RefreshWindow(); RefreshWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); setVisible(true);
} }
@ -103,17 +101,6 @@ public class FormElectricLocomotive extends JFrame{
CreatePanel.setLayout(new FlowLayout()); CreatePanel.setLayout(new FlowLayout());
CreatePanel.setBackground(new Color(0,0,0,0)); CreatePanel.setBackground(new Color(0,0,0,0));
CreatePanel.add(ButtonCreateLocomotive);
ButtonCreateLocomotive.addActionListener(e->{
field.CreateButtonLocomotiveAction();
repaint();
});
CreatePanel.add(ButtonCreateElectricLocomotive);
ButtonCreateElectricLocomotive.addActionListener(e->{
field.CreateButtonElectricLocomotiveAction();
repaint();
});
comboBoxStrategy.setPreferredSize(new Dimension(80, 40)); comboBoxStrategy.setPreferredSize(new Dimension(80, 40));
comboBoxStrategy.addItem("К центру"); comboBoxStrategy.addItem("К центру");
comboBoxStrategy.addItem("К краю"); comboBoxStrategy.addItem("К краю");

View File

@ -0,0 +1,111 @@
package ProjectElectricLocomotive.src.Forms;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormGenerateLocomotive extends JFrame
{
protected int Width;
private int Height;
protected JPanel panelLocomotive;
private JPanel panelInfoLocomotive;
private JPanel panelButtonManage;
private JLabel labelInfoLocomotive;
private JPanel panelInfoWheels;
private JLabel labelInfoWheels;
private JButton createNewObject;
private JButton sendObject;
protected DrawingLocomotiveCollection _collectionForm;
DrawingGenerateLocomotive field = new DrawingGenerateLocomotive(this);
public FormGenerateLocomotive(DrawingLocomotiveCollection _collection){
super("Коллекция локомотивов");
_collectionForm = _collection;
setSize(900,500);
Width = getWidth();
Height = getHeight();
initComponents();
RefreshWindow();
setVisible(true);
field.createNewObject();
}
private void initComponents()
{
panelLocomotive = new JPanel();
panelInfoLocomotive = new JPanel();
panelInfoWheels = new JPanel();
labelInfoLocomotive = new JLabel();
labelInfoWheels = new JLabel();
panelButtonManage = new JPanel();
createNewObject = new JButton("Сгенерировать");
sendObject = new JButton("Отправить");
panelLocomotive.setBorder(BorderFactory.createTitledBorder("Объект"));
panelInfoLocomotive.setBorder(BorderFactory.createTitledBorder("Информация о кораблях"));
panelInfoWheels.setBorder(BorderFactory.createTitledBorder("Информация о палубах"));
panelButtonManage.setLayout(new GridLayout(1, 2));
labelInfoLocomotive.setVerticalAlignment(SwingConstants.TOP);
labelInfoLocomotive.setPreferredSize(new Dimension(Width/3, Height - 40));
labelInfoWheels.setVerticalAlignment(SwingConstants.TOP);
createNewObject.setPreferredSize(new Dimension(130, 30));
sendObject.setPreferredSize(new Dimension(130, 30));
StringBuilder sbE = new StringBuilder();
StringBuilder sbD = new StringBuilder();
for(int i = 0; i < 3; i++){
sbE.append(field._generateLocomotive.getEn(i)).append("<br>").append("<br>");
sbD.append(field._generateLocomotive.getDec(i)).append("<br>").append("<br>");
}
labelInfoLocomotive.setText("<html>" + sbE.toString() + "</html>");
labelInfoWheels.setText("<html>" + sbD.toString() + "</html>");
createNewObject.addActionListener(e->{
field.createNewObject();
repaint();
});
sendObject.addActionListener(e->{
field.insertCollection();
//repaint();
});
panelLocomotive.add(panelButtonManage);
panelInfoLocomotive.add(labelInfoLocomotive);
panelInfoWheels.add(labelInfoWheels);
panelButtonManage.add(createNewObject);
panelButtonManage.add(sendObject);
add(panelLocomotive);
add(panelInfoLocomotive);
add(panelInfoWheels);
add(field);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
Width = getWidth();
Height = getHeight();
repaint();
RefreshWindow();
}
});
}
public void RefreshWindow(){
field.setLayout(new FlowLayout());
panelLocomotive.setBounds(0, 0, Width/3, Height/3 - 40);
panelInfoLocomotive.setBounds(Width/3, 0, Width/3, Height - 40);
panelInfoWheels.setBounds(2 * Width/3, 0, Width/3, Height - 40);
panelButtonManage.setBounds(10,10,panelLocomotive.getWidth() - 10, panelLocomotive.getHeight() / 6);
}
}

View File

@ -0,0 +1,129 @@
package ProjectElectricLocomotive.src.Forms;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.MaskFormatter;
import java.text.ParseException;
public class FormLocomotiveCollection extends JFrame {
protected int Width;
private int Height;
private JPanel groupBoxTools;
private JButton buttonAddElectricLocomotive;
private JButton buttonAddLocomotive;
private JComboBox<String> comboBoxSelectorCompany;
private JButton buttonRefresh;
private JButton buttonGoToCheck;
private JButton buttonRemoveLocomotive;
private JButton buttonCreateSpecialLocomotive;
private JFormattedTextField maskedTextBoxPosition;
protected JLabel pictureBox;
DrawingLocomotiveCollection field = new DrawingLocomotiveCollection(this);
public FormLocomotiveCollection(){
super("Коллекция локомотивов");
setSize(900,500);
Width = getWidth();
Height = getHeight();
initComponents();
RefreshWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void initComponents() {
groupBoxTools = new JPanel();
buttonAddElectricLocomotive = new JButton("Добавление электровоза");
buttonAddLocomotive = new JButton("Добавление локомотива");
comboBoxSelectorCompany = new JComboBox<>();
buttonRefresh = new JButton("Обновить");
buttonGoToCheck = new JButton("Передать на тесты");
buttonRemoveLocomotive = new JButton("Удалить локомотив");
buttonCreateSpecialLocomotive = new JButton("Сгенерировать локомотив");
maskedTextBoxPosition = createMaskedTextField();
pictureBox = new JLabel();
comboBoxSelectorCompany.addActionListener(e -> {
field.comboBoxSelectorCompany_SelectedIndexChanged((String) comboBoxSelectorCompany.getSelectedItem());
});
comboBoxSelectorCompany.addItem("");
comboBoxSelectorCompany.addItem("Хранилище");
buttonAddLocomotive.addActionListener(e->{
field.CreateButtonLocomotiveAction();
repaint();
});
buttonAddElectricLocomotive.addActionListener(e->{
field.CreateButtonElectricLocomotiveAction();
repaint();
});
buttonRemoveLocomotive.addActionListener(e->{
field.ButtonRemoveLocomotive_Click(maskedTextBoxPosition);
repaint();
});
buttonGoToCheck.addActionListener(e->{
field.ButtonGoToCheck_Click();
repaint();
});
buttonRefresh.addActionListener(e->{
field.ButtonRefresh_Click();
repaint();
});
buttonCreateSpecialLocomotive.addActionListener(e->{
field.ButtonCreateSpecialLocomotive_Click();
repaint();
});
groupBoxTools.setBorder(BorderFactory.createTitledBorder("Инструменты"));
groupBoxTools.setLayout(new GridLayout(8, 1));
groupBoxTools.add(comboBoxSelectorCompany);
groupBoxTools.add(buttonAddLocomotive);
groupBoxTools.add(buttonAddElectricLocomotive);
groupBoxTools.add(maskedTextBoxPosition);
groupBoxTools.add(buttonRemoveLocomotive);
groupBoxTools.add(buttonGoToCheck);
groupBoxTools.add(buttonCreateSpecialLocomotive);
groupBoxTools.add(buttonRefresh);
add(groupBoxTools);
add(pictureBox);
add(field);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
Width = getWidth();
Height = getHeight();
repaint();
RefreshWindow();
}
});
}
private JFormattedTextField createMaskedTextField() {
try {
MaskFormatter formatter = new MaskFormatter("##");
formatter.setPlaceholderCharacter('_');
JFormattedTextField maskedTextField = new JFormattedTextField(formatter);
maskedTextField.setFocusLostBehavior(JFormattedTextField.PERSIST);
return maskedTextField;
} catch (ParseException e) {
e.printStackTrace();
return new JFormattedTextField();
}
}
public void RefreshWindow(){
field.setLayout(new FlowLayout());
groupBoxTools.setBounds(Width - 210, 0, 190, Height - 40);
pictureBox.setBounds(0,0,Width - 210, Height - 40);
}
}