This commit is contained in:
Yunusov_Niyaz 2023-12-07 20:36:56 +04:00
parent 4efda8d15b
commit a5e9097d61
6 changed files with 446 additions and 25 deletions

View File

@ -5,8 +5,8 @@ public class DrawingBus {
public EntityBus getEntityBus() {
return entityBus;
}
private int pictureWidth;
private int pictureHeight;
public int pictureWidth;
public int pictureHeight;
protected int _startPosX;
public int getPosX() {
return _startPosX;
@ -23,6 +23,7 @@ public class DrawingBus {
public int getHeight() {
return busHeight;
}
public int doorsNumber;
private IDrawDoors drawingDoors;
public DrawingBus(int speed, double weight, Color bodyColor, int width, int height, int doorsNumber, int doorsType) {
if (width < busWidth || height < busHeight)
@ -135,4 +136,13 @@ public class DrawingBus {
graphics2D.drawRect(_startPosX + 196, _startPosY + 91, 10, 20);
}
public IMoveableObject GetMoveableObject() { return new DrawingObjectBus(this);}
public void setDrawingDoors(IDrawDoors obj){
drawingDoors = obj;
}
public void setDoorsNum(int num){
drawingDoors.setNumber(num);
}
public void setBodyColor(Color color){
entityBus.bodyColor = color;
}
}

View File

@ -8,6 +8,9 @@ public class DrawingTrolleybus extends DrawingBus{
if (entityBus != null)
entityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, roga, battery);
}
public void setAdditionalColor(Color color){
((EntityTrolleybus)entityBus).additionalColor = color;
}
public void drawTransport(Graphics2D graphics2D) {
if (!(entityBus instanceof EntityTrolleybus))
return;
@ -35,4 +38,4 @@ public class DrawingTrolleybus extends DrawingBus{
graphics2D.drawLine(_startPosX + 189, _startPosY + 111, _startPosX + 183, _startPosY + 119);
}
}
}
}

View File

@ -10,7 +10,7 @@ public class EntityBus {
public double getWeight() {
return weight;
}
private Color bodyColor;
public Color bodyColor;
public Color getBodyColor() {
return bodyColor;
}
@ -20,4 +20,4 @@ public class EntityBus {
this.weight = weight;
this.bodyColor = bodyColor;
}
}
}

View File

@ -1,7 +1,7 @@
import java.awt.*;
public class EntityTrolleybus extends EntityBus{
private Color additionalColor;
public Color additionalColor;
public Color getAdditionalColor(){return additionalColor;}
private boolean roga;
public boolean getRoga() {
@ -18,4 +18,4 @@ public class EntityTrolleybus extends EntityBus{
this.roga = roga;
this.battery = battery;
}
}
}

View File

@ -1,6 +1,8 @@
import javax.swing.*;
import javax.swing.border.StrokeBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
@ -100,27 +102,33 @@ public class FrameBusCollection extends JFrame {
add(pictureBoxCollection,BorderLayout.CENTER);
}
private void buttonAddBusClick(){
if (listBoxStorages.getSelectedIndex() == -1)
if(listBoxStorages.getSelectedIndex() == -1) {
return;
var obj = storage.getCollection(listBoxStorages.getSelectedValue());
if (obj == null)
return;
FrameTrolleybus form;
try{
form = new FrameTrolleybus();
}
catch (IOException e){
throw new RuntimeException();
}
form.selectBusButton.addActionListener(e -> {
form.selectBus();
if (obj.insert(form.getSelectedBus())) {
JOptionPane.showMessageDialog(this, "Объект добавлен");
pictureBoxCollection.repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось добавить объект");
BusesGenericCollection<DrawingBus, DrawingObjectBus> drawingBus = storage.getCollection(listBoxStorages.getSelectedValue());
FrameBusConfig frameBusConfig = new FrameBusConfig();
frameBusConfig.addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (drawingBus.insert(frameBusConfig.drawingBus))
{
frameBusConfig.dispose();
frameBusConfig.drawingBus.pictureWidth = pictureBoxCollection.getWidth();
frameBusConfig.drawingBus.pictureHeight = pictureBoxCollection.getHeight();
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
pictureBoxCollection.repaint();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
}
});
frameBusConfig.cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frameBusConfig.dispose();
}
form.dispose();
});
}
private void buttonRemoveBusClick() {

400
src/FrameBusConfig.java Normal file
View File

@ -0,0 +1,400 @@
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
public class FrameBusConfig extends JFrame {
private static class LabelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel) c).getText());
}
}
private record ColorTransferable(Color color) implements Transferable {
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "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 record IDrawDoorsTransferable(IDrawDoors IDrawDoorsObject) implements Transferable {
private static final DataFlavor IDrawDoorsDataFlavor = new DataFlavor(IDrawDoors.class, "IDrawDoors");
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{IDrawDoorsDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return IDrawDoorsDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return IDrawDoorsObject;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private static class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(c.getBackground());
}
}
private static class LabelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
}
private static class PanelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
}
private static class IDrawDoorsComponent extends JComponent {
public IDrawDoors obj;
public IDrawDoorsComponent(IDrawDoors obj) {
this.obj = obj;
this.addMouseListener(
new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((IDrawDoorsComponent) e.getComponent()).getTransferHandler().exportAsDrag(((IDrawDoorsComponent) e.getComponent()), e, TransferHandler.COPY);
}
}
);
this.setTransferHandler(
new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new IDrawDoorsTransferable(((IDrawDoorsComponent) c).obj);
}
}
);
}
}
private final JComponent pictureBox = new JComponent() {
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingBus != null) drawingBus.drawTransport(graphics2D);
super.repaint();
}
};
public final JButton addButton = new JButton("Добавить");
public final JButton cancelButton = new JButton("Отмена");
public DrawingBus drawingBus;
private final int pictureBoxWidth = 218;
private final int pictureBoxHeight = 178;
public FrameBusConfig() {
super("Создание объекта");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGui();
setVisible(true);
}
private void createGui() {
pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
IDrawDoorsComponent doors = new IDrawDoorsComponent(new DrawingDoors());
IDrawDoorsComponent roundUpDoors = new IDrawDoorsComponent(new DrawingDoorsRoundedUp());
IDrawDoorsComponent roundUpAndDownDoors = new IDrawDoorsComponent(new DrawingDoorsRoundedUpAndDown());
JLabel rectangularLabel = new JLabel("Прямоугольные");
rectangularLabel.setHorizontalAlignment(SwingConstants.CENTER);
rectangularLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel roundUpLabel = new JLabel("Круглые сверху");
roundUpLabel.setHorizontalAlignment(SwingConstants.CENTER);
roundUpLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel roundUpAndDownLabel = new JLabel("Овальные");
roundUpAndDownLabel.setHorizontalAlignment(SwingConstants.CENTER);
roundUpAndDownLabel.setVerticalAlignment(SwingConstants.CENTER);
doors.setLayout(new GridLayout(1, 1));
roundUpDoors.setLayout(new GridLayout(1, 1));
roundUpAndDownDoors.setLayout(new GridLayout(1, 1));
doors.add(rectangularLabel);
roundUpDoors.add(roundUpLabel);
roundUpAndDownDoors.add(roundUpAndDownLabel);
doors.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
roundUpDoors.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
roundUpAndDownDoors.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel colorLabel = new JLabel("Цвет");
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel additionalColorLabel = new JLabel("Доп цвет");
additionalColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
additionalColorLabel.setVerticalAlignment(SwingConstants.CENTER);
additionalColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel IDrawDoorsLabel = new JLabel("Двери");
IDrawDoorsLabel.setHorizontalAlignment(SwingConstants.CENTER);
IDrawDoorsLabel.setVerticalAlignment(SwingConstants.CENTER);
IDrawDoorsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JCheckBox checkBoxRoga = new JCheckBox("Наличие рогов");
JCheckBox checkBoxBattery = new JCheckBox("Наличие батареи");
JLabel simpleLabel = new JLabel("Простой");
simpleLabel.setTransferHandler(new LabelTransferHandler());
simpleLabel.addMouseListener(new LabelMouseAdapter());
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel advancedLabel = new JLabel("Продвинутый");
advancedLabel.setTransferHandler(new LabelTransferHandler());
advancedLabel.addMouseListener(new LabelMouseAdapter());
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel speedLabel = new JLabel("Скорость");
JLabel weightLabel = new JLabel("Вес");
SpinnerNumberModel speedSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
SpinnerNumberModel weightSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
SpinnerNumberModel doorsNumberSpinnerModel = new SpinnerNumberModel(3, 3, 5, 1.0);
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
JSpinner doorsNumberSpinner = new JSpinner(doorsNumberSpinnerModel);
JPanel colorPanel = new JPanel();
JPanel redPanel = new JPanel();
JPanel greenPanel = new JPanel();
JPanel bluePanel = new JPanel();
JPanel yellowPanel = new JPanel();
JPanel whitePanel = new JPanel();
JPanel grayPanel = new JPanel();
JPanel blackPanel = new JPanel();
JPanel purplePanel = new JPanel();
redPanel.setTransferHandler(new PanelTransferHandler());
greenPanel.setTransferHandler(new PanelTransferHandler());
bluePanel.setTransferHandler(new PanelTransferHandler());
yellowPanel.setTransferHandler(new PanelTransferHandler());
whitePanel.setTransferHandler(new PanelTransferHandler());
grayPanel.setTransferHandler(new PanelTransferHandler());
blackPanel.setTransferHandler(new PanelTransferHandler());
purplePanel.setTransferHandler(new PanelTransferHandler());
redPanel.addMouseListener(new PanelMouseAdapter());
greenPanel.addMouseListener(new PanelMouseAdapter());
bluePanel.addMouseListener(new PanelMouseAdapter());
yellowPanel.addMouseListener(new PanelMouseAdapter());
whitePanel.addMouseListener(new PanelMouseAdapter());
grayPanel.addMouseListener(new PanelMouseAdapter());
blackPanel.addMouseListener(new PanelMouseAdapter());
purplePanel.addMouseListener(new PanelMouseAdapter());
redPanel.setName("Красный");
greenPanel.setName("Зелёный");
bluePanel.setName("Синий");
yellowPanel.setName("Жёлтый");
whitePanel.setName("Белый");
grayPanel.setName("Серый");
blackPanel.setName("Чёрный");
purplePanel.setName("Фиолетовый");
pictureBox.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
int speed = ((Number) speedSpinner.getValue()).intValue();
int weight = ((Number) weightSpinner.getValue()).intValue();
int blocksNumber = ((Number) doorsNumberSpinner.getValue()).intValue();
switch ((String) support.getTransferable().getTransferData(DataFlavor.stringFlavor)) {
case "Простой" -> {
drawingBus = new DrawingBus(speed, weight, Color.WHITE,
pictureBoxWidth, pictureBoxHeight, 0, blocksNumber);
drawingBus.setDoorsNum(blocksNumber);
}
case "Продвинутый" -> {
drawingBus = new DrawingTrolleybus(speed, weight, Color.WHITE, Color.BLACK,
checkBoxRoga.isSelected(), checkBoxBattery.isSelected(),
pictureBoxWidth, pictureBoxHeight, 0, blocksNumber);
drawingBus.setDoorsNum(blocksNumber);
}
}
drawingBus.setPosition(pictureBoxWidth / 2 - drawingBus.getWidth() / 2,
pictureBoxHeight / 2 - drawingBus.getHeight() / 2);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
IDrawDoorsLabel.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(IDrawDoorsTransferable.IDrawDoorsDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDrawDoors obj = (IDrawDoors) support.getTransferable().getTransferData(IDrawDoorsTransferable.IDrawDoorsDataFlavor);
obj.setNumber(((Number) doorsNumberSpinner.getValue()).intValue());
if (drawingBus == null)
return false;
drawingBus.setDrawingDoors(obj);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
colorLabel.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (drawingBus == null)
return false;
drawingBus.setBodyColor(color);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
additionalColorLabel.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (drawingBus == null || !(drawingBus instanceof DrawingTrolleybus))
return false;
((DrawingTrolleybus) drawingBus).setAdditionalColor(color);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
addButton.setBounds(555, 250, 94, 29);
cancelButton.setBounds(679, 250, 94, 29);
pictureBox.setBounds(555, 65, pictureBoxWidth, pictureBoxHeight);
colorLabel.setBounds(555, 20, 70, 33);
additionalColorLabel.setBounds(629, 20, 70, 33);
IDrawDoorsLabel.setBounds(703, 20, 70, 33);
checkBoxRoga.setBounds(6, 132, 159, 24);
checkBoxBattery.setBounds(6, 162, 145, 24);
simpleLabel.setBounds(171, 169, 120, 50);
advancedLabel.setBounds(297, 169, 120, 50);
doors.setBounds(171, 229, 120, 50);
roundUpDoors.setBounds(297, 229, 120, 50);
roundUpAndDownDoors.setBounds(423, 229, 120, 50);
colorPanel.setBounds(171, 23, 372, 143);
speedSpinner.setBounds(6, 46, 150, 27);
speedLabel.setBounds(6, 23, 73, 20);
weightSpinner.setBounds(6, 99, 150, 27);
weightLabel.setBounds(6, 76, 33, 20);
doorsNumberSpinner.setBounds(6, 200, 150, 27);
redPanel.setBackground(Color.RED);
greenPanel.setBackground(Color.GREEN);
bluePanel.setBackground(Color.BLUE);
yellowPanel.setBackground(Color.YELLOW);
whitePanel.setBackground(Color.WHITE);
grayPanel.setBackground(Color.GRAY);
blackPanel.setBackground(Color.BLACK);
purplePanel.setBackground(Color.MAGENTA);
colorPanel.setLayout(new GridLayout(2, 4, 26, 10));
colorPanel.add(redPanel);
colorPanel.add(greenPanel);
colorPanel.add(bluePanel);
colorPanel.add(yellowPanel);
colorPanel.add(whitePanel);
colorPanel.add(grayPanel);
colorPanel.add(blackPanel);
colorPanel.add(purplePanel);
add(colorLabel);
add(additionalColorLabel);
add(IDrawDoorsLabel);
setLayout(null);
setSize(818, 350);
add(speedLabel);
add(speedSpinner);
add(weightLabel);
add(weightSpinner);
add(simpleLabel);
add(advancedLabel);
add(checkBoxRoga);
add(checkBoxBattery);
add(pictureBox);
add(addButton);
add(cancelButton);
add(doorsNumberSpinner);
add(colorPanel);
add(doors);
add(roundUpDoors);
add(roundUpAndDownDoors);
}
}