WIP: PIbd-11 Karakozov_AK LabWork05 Hard #5

Closed
insideq wants to merge 1 commits from LabWork05 into LabWork04
6 changed files with 468 additions and 71 deletions
Showing only changes of commit d4678adf15 - Show all commits

View File

@ -1,8 +1,5 @@
package Drawings;
import DifferentRollers.DrawingRollersCross;
import DifferentRollers.DrawingRollersPlus;
import DifferentRollers.DrawingRollersStar;
import DifferentRollers.IDifferentRollers;
import Entities.EntityBulldozer;
@ -31,29 +28,9 @@ public class DrawingBulldozer extends JPanel {
_startPosY = null;
}
protected void SetTypeRollers(){
int rollersCount = (int)(Math.random()*4);
switch ((int)(Math.random()*3+1)){
case 1:
drawingRollers = new DrawingRollersCross();
break;
case 2:
drawingRollers = new DrawingRollersPlus();
break;
case 3:
drawingRollers = new DrawingRollersStar();
break;
default:
rollersCount = 0;
break;
}
drawingRollers.setRollersCount(rollersCount);
}
public DrawingBulldozer(int speed, double weight, Color bodyColor){
super();
EntityBulldozer = new EntityBulldozer(speed, weight, bodyColor);
SetTypeRollers();
}
protected DrawingBulldozer(int _drawingExcWidth, int _drawingExcHeight) {

View File

@ -9,7 +9,6 @@ public class DrawingExcavator extends DrawingBulldozer{
public DrawingExcavator(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){
EntityBulldozer = new EntityExcavator(speed, weight, bodyColor, additionalColor, prop, ladle);
SetTypeRollers();
}
public DrawingExcavator(EntityExcavator entity, IDifferentRollers rollers) {

View File

@ -4,12 +4,17 @@ import java.awt.*;
public class EntityBulldozer {
private Integer Speed;
public void setSpeed(int speed) {Speed = speed;}
private Double Weight;
public void setWeight(double weight) {Weight = weight;}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
public void setBodyColor(Color color) {BodyColor = color;}
public double Step;
public EntityBulldozer(int speed, double weight, Color bodyColor){
Speed = speed;

View File

@ -7,11 +7,15 @@ public class EntityExcavator extends EntityBulldozer{
public Color getAdditionalColor(){
return AdditionalColor;
}
public void setAdditionalColor(Color color) {AdditionalColor = color;}
public boolean Prop;
public boolean getProp(){
return Prop;
}
public void setProp(boolean prop) {Prop = prop;}
public boolean Ladle;
public void setLadle(boolean ladle) {Ladle = ladle;}
public boolean getLadle(){
return Ladle;
}

View File

@ -1,7 +1,6 @@
import CollectionGenericObjects.*;
import Drawings.CanvasFormBulldozerCollection;
import Drawings.DrawingBulldozer;
import Drawings.DrawingExcavator;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
@ -9,7 +8,6 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Random;
import java.util.LinkedList;
import static java.lang.Integer.parseInt;
@ -28,8 +26,7 @@ public class FormBulldozerCollection extends JFrame{
private JList listBoxCollection = new JList();
private JButton buttonRemoveCollection = new JButton("Удалить");
private JButton buttonCreateCompany = new JButton("Создать компанию");
private JButton CreateExcButton = new JButton("Создать экскаватор");;
private JButton CreateBullButton = new JButton("Создать бульдозер");
private JButton CreateButton = new JButton("Создать бульдозер");
private JButton RemoveButton = new JButton("Удалить");
private JButton GoToCheckButton = new JButton("Тест");
private JButton RandomButton = new JButton("Случайный объект");
@ -46,37 +43,7 @@ public class FormBulldozerCollection extends JFrame{
_canvasExcavator.SetCollectionToCanvas(_company);
_canvasExcavator.repaint();
}
private void CreateObject(String typeOfClass) {
if (_company == null) return;
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = getColor();
DrawingBulldozer drawingBulldozer;
switch (typeOfClass) {
case "DrawingBulldozer":
drawingBulldozer = new DrawingBulldozer(speed, weight, bodyColor);
break;
case "DrawingExcavator":
Color additionalColor = getColor();
boolean prop = new Random().nextBoolean();
boolean ladle = new Random().nextBoolean();
drawingBulldozer = new DrawingExcavator(speed, weight, bodyColor, additionalColor, prop, ladle);
break;
default: return;
}
if (_company._collection.Insert(drawingBulldozer) != -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);
@ -92,16 +59,14 @@ public class FormBulldozerCollection extends JFrame{
MaskedTextField = new JFormattedTextField(mask);
CreateBullButton.addActionListener(new ActionListener() {
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingBulldozer");
}
});
CreateExcButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingExcavator");
if (_company == null) return;
FormExcavatorConfig form = new FormExcavatorConfig("", new Dimension(700, 300));
form.setCompany(_company);
form.Init();
form.setLocationRelativeTo(null);
}
});
@ -255,6 +220,7 @@ public class FormBulldozerCollection extends JFrame{
}
FormExcavator form = new FormExcavator("Экскаватор", new Dimension(900,565));
form.Init(bulldozer);
form.setLocationRelativeTo(null);
}
});
@ -273,8 +239,7 @@ public class FormBulldozerCollection extends JFrame{
buttonRemoveCollection.setBounds(getWidth()-190, 210, 150, 20);
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
buttonCreateCompany.setBounds(getWidth()-190, 240, 150, 20);
CreateBullButton.setBounds(getWidth()-190, 280, 150, 30);
CreateExcButton.setBounds(getWidth()-190, 320, 150, 30);
CreateButton.setBounds(getWidth()-190, 280, 150, 30);
MaskedTextField.setBounds(getWidth()-190,360,150,30);
RemoveButton.setBounds(getWidth()-190, 400, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 440, 150, 30);
@ -294,8 +259,7 @@ public class FormBulldozerCollection extends JFrame{
add(buttonRemoveCollection);
add(ComboBoxCollections);
add(buttonCreateCompany);
add(CreateBullButton);
add(CreateExcButton);
add(CreateButton);
add(MaskedTextField);
add(RemoveButton);
add(GoToCheckButton);

View File

@ -0,0 +1,448 @@
import CollectionGenericObjects.AbstractCompany;
import DifferentRollers.DrawingRollersStar;
import DifferentRollers.DrawingRollersCross;
import DifferentRollers.DrawingRollersPlus;
import DifferentRollers.IDifferentRollers;
import Drawings.DrawingBulldozer;
import Drawings.DrawingExcavator;
import Entities.EntityExcavator;
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.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
public class FormExcavatorConfig extends JFrame {
private String title;
private Dimension dimension;
private DrawingBulldozer _bulldozer;
private AbstractCompany company = null;
private JLabel labelSpeed = new JLabel("Скорость");
private JLabel labelWeight = new JLabel("Вес");
private JLabel labelBulldozer = new JLabel("Бульдозер", SwingConstants.CENTER);
private JLabel labelExcavator = new JLabel("Экскаватор", SwingConstants.CENTER);
private JLabel labelColor = new JLabel("Цвет");
private JLabel labelBodyColor = new JLabel("Цвет кузова", SwingConstants.CENTER);
private JLabel labelRollersStar = new JLabel("Катки Звезда",SwingConstants.CENTER);
private JLabel labelRollersCross = new JLabel("Катки Крест", SwingConstants.CENTER);
private JLabel labelRollersPlus = new JLabel("Катки Плюс", SwingConstants.CENTER);
private JLabel labelAdditionalColor = new JLabel("Доп. цвет", SwingConstants.CENTER);
private JLabel labelNumberOfRollers = new JLabel("Кол-во катков");
private JSpinner spinnerSpeed = new JSpinner();
private JSpinner spinnerWeight = new JSpinner();
private JSpinner spinnerRollersCount = new JSpinner();
private JCheckBox checkBoxLadle = new JCheckBox("Добавить ковш");
private JCheckBox checkBoxProp = new JCheckBox("Добавить опоры");
private JComponent panelObject = new JPanel();
private JPanel panelColorRed = new JPanel();
private JPanel panelColorGreen = new JPanel();
private JPanel panelColorBlue = new JPanel();
private JPanel panelColorYellow = new JPanel();
private JPanel panelColorBlack = new JPanel();
private JPanel panelColorWhite = new JPanel();
private JPanel panelColorGray = new JPanel();
private JPanel panelColorPink = new JPanel();
private JButton buttonAdd = new JButton("Добавить");
private JButton buttonCansel = new JButton("Назад");
public FormExcavatorConfig(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init() {
SpinnerModel numSpeed = new SpinnerNumberModel(100, 100, 1000, 1);
SpinnerModel numWeight = new SpinnerNumberModel(100, 100, 1000, 1);
spinnerSpeed.setModel(numSpeed);
spinnerWeight.setModel(numWeight);
SpinnerModel numRollers = new SpinnerNumberModel(0, 0, 3, 1);
spinnerRollersCount.setModel(numRollers);
panelObject = new Canvas();
panelObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
spinnerSpeed.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (_bulldozer == null) return;
_bulldozer.EntityBulldozer.setSpeed((int)spinnerSpeed.getValue());
}
});
spinnerWeight.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (_bulldozer == null) return;
_bulldozer.EntityBulldozer.setWeight((int)spinnerWeight.getValue());
}
});
checkBoxLadle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_bulldozer == null) return;
if (_bulldozer.EntityBulldozer instanceof EntityExcavator excavator) {
excavator.setLadle(checkBoxLadle.isSelected());
panelObject.repaint();
}
}
});
checkBoxProp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_bulldozer == null) return;
if (_bulldozer.EntityBulldozer instanceof EntityExcavator excavator) {
excavator.setProp(checkBoxProp.isSelected());
panelObject.repaint();
}
}
});
labelBulldozer.setBackground(Color.WHITE);
labelBulldozer.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelExcavator.setBackground(Color.WHITE);
labelExcavator.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelBodyColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelAdditionalColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelRollersCross.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelRollersStar.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelRollersPlus.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
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());
}
};
labelBulldozer.addMouseListener(labelObjectsMouseDown);
labelBulldozer.setTransferHandler(labelObjectsTransferHandler);
labelExcavator.addMouseListener(labelObjectsMouseDown);
labelExcavator.setTransferHandler(labelObjectsTransferHandler);
MouseAdapter labelRollersMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
};
labelRollersCross.addMouseListener(labelRollersMouseDown);
labelRollersStar.addMouseListener(labelRollersMouseDown);
labelRollersPlus.addMouseListener(labelRollersMouseDown);
labelRollersCross.setTransferHandler(new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
return new RollersTransferable(new DrawingRollersCross());
}
});
labelRollersStar.setTransferHandler(new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
return new RollersTransferable(new DrawingRollersStar());
}
});
labelRollersPlus.setTransferHandler(new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
return new RollersTransferable(new DrawingRollersPlus());
}
});
panelObject.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor) || support.isDataFlavorSupported(RollersTransferable.rollersDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
if (canImport(support)) {
try {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "Бульдозер":
_bulldozer = new DrawingBulldozer((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.WHITE);
break;
case "Экскаватор":
_bulldozer = new DrawingExcavator((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.WHITE, Color.BLACK, checkBoxLadle.isSelected(), checkBoxProp.isSelected());
break;
}
if (_bulldozer != null) {
_bulldozer.SetPictureSize(155,155);
_bulldozer.SetPosition(5,10);
}
else return false;
}
catch (UnsupportedFlavorException | IOException e) {}
try {
IDifferentRollers rollers = (IDifferentRollers) support.getTransferable().getTransferData(RollersTransferable.rollersDataFlavor);
_bulldozer.drawingRollers = rollers;
_bulldozer.drawingRollers.setRollersCount((int) spinnerRollersCount.getValue());
}catch (UnsupportedFlavorException | IOException e) {}
panelObject.repaint();
return true;
}
return false;
}
});
JPanel[] colorPanels = {
panelColorRed,
panelColorGreen,
panelColorBlue,
panelColorYellow,
panelColorWhite,
panelColorBlack,
panelColorGray,
panelColorPink,
};
panelColorRed.setBackground(Color.RED);
panelColorGreen.setBackground(Color.GREEN);
panelColorBlue.setBackground(Color.BLUE);
panelColorYellow.setBackground(Color.YELLOW);
panelColorWhite.setBackground(Color.WHITE);
panelColorBlack.setBackground(Color.BLACK);
panelColorGray.setBackground(Color.GRAY);
panelColorPink.setBackground(Color.MAGENTA);
MouseAdapter colorMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
};
for (var panelColor : colorPanels) {
panelColor.addMouseListener(colorMouseDown);
panelColor.setTransferHandler(new ColorTransferHandler());
}
labelBodyColor.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_bulldozer == null) return false;
_bulldozer.EntityBulldozer.setBodyColor(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
labelAdditionalColor.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferSupport support) {
if (!(_bulldozer instanceof DrawingExcavator)) return false;
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_bulldozer == null) return false;
if (_bulldozer.EntityBulldozer instanceof EntityExcavator excavator) {
excavator.setAdditionalColor(color);
labelColor.setBackground(color);
return true;
}
return false;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_bulldozer == null) return;
DrawingBulldozer copyBulldozer;
if (_bulldozer instanceof DrawingExcavator)
copyBulldozer = new DrawingExcavator((EntityExcavator) _bulldozer.EntityBulldozer, _bulldozer.drawingRollers);
else
copyBulldozer = new DrawingBulldozer(_bulldozer.EntityBulldozer, _bulldozer.drawingRollers);
company._collection.Insert(copyBulldozer);
FormBulldozerCollection.canvasShow();
dispose();
}
});
buttonCansel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
labelSpeed.setBounds(10, 17, 50, 15);
labelWeight.setBounds(10,43, 50, 15);
labelNumberOfRollers.setBounds(10, 68, 50, 15);
labelBulldozer.setBounds(10,150,90,30);
labelExcavator.setBounds(10,190,90,30);
labelColor.setBounds(170,10,30,15);
labelBodyColor.setBounds(500,5,75, 40);
labelAdditionalColor.setBounds(580,5,75,40);
labelRollersCross.setBounds(120, 210, 100, 40);
labelRollersStar.setBounds(230, 210, 100,40);
labelRollersPlus.setBounds(340,210,100,40);
spinnerSpeed.setBounds(60,15, 60,20);
spinnerWeight.setBounds(60,40, 60,20);
spinnerRollersCount.setBounds(60, 65,60,20);
checkBoxLadle.setBounds(8,85,150,20);
checkBoxProp.setBounds(8,105,150,20);
panelObject.setBounds(500,50,160,150);
panelColorRed.setBounds(170, 30, 40, 40);
panelColorGreen.setBounds(220, 30, 40,40);
panelColorBlue.setBounds(270,30,40,40);
panelColorYellow.setBounds(320,30,40,40);
panelColorWhite.setBounds(170, 80,40,40);
panelColorBlack.setBounds(220,80,40,40);
panelColorGray.setBounds(270,80,40,40);
panelColorPink.setBounds(320,80,40,40);
buttonAdd.setBounds(500, 210, 75, 40);
buttonCansel.setBounds(590, 210, 70, 40);
setSize(dimension.width, dimension.height);
setLayout(null);
add(labelSpeed);
add(labelWeight);
add(labelNumberOfRollers);
add(labelBodyColor);
add(labelBulldozer);
add(labelExcavator);
add(labelColor);
add(labelBodyColor);
add(labelAdditionalColor);
add(labelRollersCross);
add(labelRollersStar);
add(labelRollersPlus);
add(spinnerSpeed);
add(spinnerWeight);
add(spinnerRollersCount);
add(checkBoxLadle);
add(checkBoxProp);
add(panelObject);
add(panelColorRed);
add(panelColorGreen);
add(panelColorBlue);
add(panelColorYellow);
add(panelColorWhite);
add(panelColorBlack);
add(panelColorGray);
add(panelColorPink);
add(buttonAdd);
add(buttonCansel);
setVisible(true);
}
public void setCompany(AbstractCompany company) {
this.company = company;
}
private class Canvas extends JComponent {
public Canvas() {
}
public void paintComponent(Graphics g) {
if (_bulldozer == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_bulldozer.DrawTransport(g2d);
super.repaint();
}
}
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 RollersTransferable implements Transferable {
private IDifferentRollers rollers;
private static final DataFlavor rollersDataFlavor = new DataFlavor(IDifferentRollers.class, "Rollers");
public RollersTransferable(IDifferentRollers rollers) {
this.rollers = rollers;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{rollersDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(rollersDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return rollers;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
}