PIbd-23. Ivanov V.N. Lab Work 05. Hard #5

Closed
Vyacheslav wants to merge 5 commits from LabWork5 into LabWork4
12 changed files with 533 additions and 13 deletions

View File

@ -0,0 +1,35 @@
package DoubleDeckerBus.DragDrop;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class ColorTransferable implements Transferable {
private Color color;
public 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, IOException {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}

View File

@ -0,0 +1,37 @@
package DoubleDeckerBus.DragDrop;
import DoubleDeckerBus.DrawningObjects.IDraw;
import javax.swing.*;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class IDrawComponent extends JComponent {
public IDraw obj;
public IDrawComponent(IDraw obj){
this.obj = obj;
this.addMouseListener(
new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
((IDrawComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawComponent)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 iDrawTransferable(((IDrawComponent)c).obj);
}
}
);
}
}

View File

@ -0,0 +1,12 @@
package DoubleDeckerBus.DragDrop;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class LabelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
}
}

View File

@ -0,0 +1,17 @@
package DoubleDeckerBus.DragDrop;
import javax.swing.*;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
public 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());
}
}

View File

@ -0,0 +1,12 @@
package DoubleDeckerBus.DragDrop;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PanelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
}
}

View File

@ -0,0 +1,16 @@
package DoubleDeckerBus.DragDrop;
import javax.swing.*;
import java.awt.datatransfer.Transferable;
public class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(((JPanel)c).getBackground());
}
}

View File

@ -0,0 +1,36 @@
package DoubleDeckerBus.DragDrop;
import DoubleDeckerBus.DrawningObjects.IDraw;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class iDrawTransferable implements Transferable {
private IDraw iDrawObject;
public static final DataFlavor iDrawDataFlavor = new DataFlavor(IDraw.class, "IDraw");
public iDrawTransferable(IDraw iDrawObject) {
this.iDrawObject = iDrawObject;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{iDrawDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return iDrawDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return iDrawObject;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}

View File

@ -14,8 +14,9 @@ public class DrawningBus {
public int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected int _busWidth = 110;
protected int _busHeight = 70;
public int _busWidth = 110;
public int _busHeight = 70;
public int _doorNumb;
protected IDraw DrawningDoor;
public EntityBus EntityBus() {
@ -69,6 +70,18 @@ public class DrawningBus {
DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6));
}
public void ChangeIDraw(IDraw obj) {
DrawningDoor = obj;
obj.ChangeDoorsNumber(_doorNumb);
obj.ChangeX(_startPosX);
obj.ChangeY(_startPosY);
}
public void ChangeDoorsNumb(int numb) {
_doorNumb = numb;
DrawningDoor.ChangeDoorsNumber(numb);
}
public void SetPosition(int x, int y){
if (EntityBus == null) {
return;
@ -159,6 +172,10 @@ public class DrawningBus {
DrawningDoor.ChangeY(_startPosY);
}
public void ChangeColor(Color col){
EntityBus.BodyColor = col;
}
public void DrawTransport(Graphics2D g2d) {
if (EntityBus == null) {
return;

View File

@ -15,6 +15,10 @@ public class DrawningDoubleDeckerBus extends DrawningBus {
}
}
public void ChangeAddColor(Color col) {
((EntityDoubleDeckerBus)EntityBus).AdditionalColor = col;
}
@Override
public void DrawTransport(Graphics2D g2d) {

View File

@ -124,22 +124,29 @@ public class FormBusCollection {
return;
}
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus = _storage.Get(listBoxStorages.getSelectedValue());
FormDoubleDeckerBus form = new FormDoubleDeckerBus();
form.buttonSelect.addActionListener(new ActionListener() {
FormBusConfig form = new FormBusConfig();
form.addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_bus.Insert(form.SelectedBus()))
if (_bus.Insert(form._bus))
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form.SelectedBus()._pictureWidth = pictureBoxWidth;
form.SelectedBus()._pictureHeight = pictureBoxHeight;
form._bus._pictureWidth = pictureBoxWidth;
form._bus._pictureHeight = pictureBoxHeight;
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.BusFrame.dispose();
form.frameConfig.dispose();
Draw();
}
});
form.cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
form.frameConfig.dispose();
Draw();
}
});

View File

@ -0,0 +1,327 @@
package DoubleDeckerBus;
import DoubleDeckerBus.DragDrop.*;
import DoubleDeckerBus.DrawningObjects.*;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;
public class FormBusConfig {
public JFrame frameConfig;
public JButton addButton;
public JButton cancelButton;
public DrawningBus _bus;
int _pictureBoxWidth = 218;
int _pictureBoxHeight = 190;
public FormBusConfig(){
frameConfig = new JFrame();
addButton = new JButton("Добавить");
cancelButton = new JButton("Отмена");
addButton.setBounds(555,262,94,29);
cancelButton.setBounds(679,262,94,29);
Canvas canvas = new Canvas();
canvas.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
canvas.setBounds(555, 65, _pictureBoxWidth, _pictureBoxHeight);
JLabel iDrawLabel = new JLabel("Двери");
JLabel colorLabel = new JLabel("Цвет");
DrawningBus toGetSize = new DrawningBus(0,0, null, 0, 0);
IDrawComponent baseDoor = new IDrawComponent(new DrawningDoor(toGetSize._busWidth - toGetSize._busWidth / 10,
toGetSize._busHeight,
_pictureBoxWidth / 2 -toGetSize._busWidth / 2,
_pictureBoxHeight / 2 -toGetSize._busHeight / 2));
IDrawComponent ovalDoor = new IDrawComponent(new DrawningDoorOval(toGetSize._busWidth - toGetSize._busWidth / 10,
toGetSize._busHeight,
_pictureBoxWidth / 2 -toGetSize._busWidth / 2,
_pictureBoxHeight / 2 -toGetSize._busHeight / 2));
IDrawComponent triangleDoor = new IDrawComponent(new DrawningDoorTriangle(toGetSize._busWidth - toGetSize._busWidth / 10,
toGetSize._busHeight,
_pictureBoxWidth / 2 -toGetSize._busWidth / 2,
_pictureBoxHeight / 2 -toGetSize._busHeight / 2));
baseDoor.setLayout(new GridLayout(1,1));
ovalDoor.setLayout(new GridLayout(1,1));
triangleDoor.setLayout(new GridLayout(1,1));
iDrawLabel.setHorizontalAlignment(SwingConstants.CENTER);
iDrawLabel.setVerticalAlignment(SwingConstants.CENTER);
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel baseLabel = new JLabel("Прямоугольные");
JLabel ovalLabel = new JLabel("Овальные");
JLabel triangleLabel = new JLabel("Треугольные");
baseLabel.setHorizontalAlignment(SwingConstants.CENTER);
baseLabel.setVerticalAlignment(SwingConstants.CENTER);
ovalLabel.setHorizontalAlignment(SwingConstants.CENTER);
ovalLabel.setVerticalAlignment(SwingConstants.CENTER);
triangleLabel.setHorizontalAlignment(SwingConstants.CENTER);
triangleLabel.setVerticalAlignment(SwingConstants.CENTER);
baseDoor.add(baseLabel);
ovalDoor.add(ovalLabel);
triangleDoor.add(triangleLabel);
baseDoor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
ovalDoor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
triangleDoor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel addColorLabel = new JLabel("Доп цвет");
addColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
addColorLabel.setVerticalAlignment(SwingConstants.CENTER);
colorLabel.setBounds(555, 20, 70, 33);
addColorLabel.setBounds(629, 20, 70, 33);
iDrawLabel.setBounds(703, 20, 70, 33);
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
addColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
iDrawLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JCheckBox checkSecondFloor = new JCheckBox("Наличие 2 этажа");
JCheckBox checkLadder = new JCheckBox("Наличие лестницы");
JCheckBox checkLineBetweenFloor = new JCheckBox("Наличие разделителя");
checkSecondFloor.setBounds(6, 122, 159, 24);
checkLadder.setBounds(6, 152, 145, 24);
checkLineBetweenFloor.setBounds(6, 182, 162, 24);
JLabel simpleLabel = new JLabel("Простой");
JLabel advancedLabel = new JLabel("Продвинутый");
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
simpleLabel.setBounds(171,169, 120, 50);
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
advancedLabel.setBounds(297,169, 120, 50);
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
baseDoor.setBounds(171,229, 120, 50);
ovalDoor.setBounds(297,229, 120, 50);
triangleDoor.setBounds(423,229, 120, 50);
JLabel speedLabel = new JLabel ("Скорость");
JLabel weightLabel = new JLabel ("Вес");
JPanel colorPanel = new JPanel();
colorPanel.setBounds(171, 23, 234,143);
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 doorNumbSpinnerModel = new SpinnerNumberModel(3, 3, 5, 1.0);
JSpinner doorNumbSpinner = new JSpinner(doorNumbSpinnerModel);
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
speedSpinner.setBounds(6, 46, 150, 27);
speedLabel.setBounds(6, 23, 73, 20);
weightSpinner.setBounds(6, 99, 150, 27);
weightLabel.setBounds(6, 76, 33, 20);
doorNumbSpinner.setBounds(6, 220, 150, 27);
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("Фиолетовый");
simpleLabel.setTransferHandler(new LabelTransferHandler());
simpleLabel.addMouseListener(new LabelMouseAdapter());
advancedLabel.setTransferHandler(new LabelTransferHandler());
advancedLabel.addMouseListener(new LabelMouseAdapter());
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.PINK);
colorPanel.setLayout(new GridLayout(2, 4));
colorPanel.add(redPanel);
colorPanel.add(greenPanel);
colorPanel.add(bluePanel);
colorPanel.add(yellowPanel);
colorPanel.add(whitePanel);
colorPanel.add(grayPanel);
colorPanel.add(blackPanel);
colorPanel.add(purplePanel);
frameConfig.add(colorLabel);
frameConfig.add(addColorLabel);
frameConfig.add(iDrawLabel);
frameConfig.setLayout(null);
frameConfig.setSize(818, 350);
frameConfig.add(speedLabel);
frameConfig.add(speedSpinner);
frameConfig.add(weightLabel);
frameConfig.add(weightSpinner);
frameConfig.add(simpleLabel);
frameConfig.add(advancedLabel);
frameConfig.add(checkSecondFloor);
frameConfig.add(checkLadder);
frameConfig.add(checkLineBetweenFloor);
frameConfig.add(canvas);
frameConfig.add(addButton);
frameConfig.add(cancelButton);
frameConfig.add(doorNumbSpinner);
frameConfig.setVisible(true);
frameConfig.add(colorPanel);
frameConfig.add(baseDoor);
frameConfig.add(ovalDoor);
frameConfig.add(triangleDoor);
colorPanel.setVisible(true);
baseDoor.setVisible(true);
ovalDoor.setVisible(true);
triangleDoor.setVisible(true);
canvas.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 {
Object speedObj = speedSpinner.getValue();
Number speedNumb = (Number) speedObj;
int speed = speedNumb.intValue();
Object weightObj = weightSpinner.getValue();
Number weightNumb = (Number) weightObj;
int weight = weightNumb.intValue();
Object doorNumbObj = doorNumbSpinner.getValue();
Number doorNumb = (Number) doorNumbObj;
int doorsNumb = doorNumb.intValue();
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "Простой":
_bus = new DrawningBus(speed, weight, Color.WHITE, _pictureBoxWidth, _pictureBoxHeight);
_bus.ChangeDoorsNumb(doorsNumb);
break;
case "Продвинутый":
_bus = new DrawningDoubleDeckerBus(speed, weight, Color.WHITE, Color.BLACK,
doorsNumb, _pictureBoxWidth, _pictureBoxHeight,
checkSecondFloor.isSelected(), checkLadder.isSelected(), checkLineBetweenFloor.isSelected());
_bus.ChangeDoorsNumb(doorsNumb);
break;
}
_bus.SetPosition(_pictureBoxWidth / 2 -_bus._busWidth / 2,
_pictureBoxHeight / 2 -_bus._busHeight / 2);
canvas.DrawningBus = _bus;
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
iDrawLabel.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(iDrawTransferable.iDrawDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDraw obj = (IDraw) support.getTransferable().getTransferData(iDrawTransferable.iDrawDataFlavor);
if (_bus == null)
return false;
_bus.ChangeIDraw(obj);
canvas.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 (_bus == null)
return false;
_bus.ChangeColor(color);
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
addColorLabel.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 (_bus == null || !(_bus instanceof DrawningDoubleDeckerBus))
return false;
((DrawningDoubleDeckerBus)_bus).ChangeAddColor(color);
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
}
}

View File

@ -66,12 +66,12 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
private void DrawBackground(Graphics g) {
g.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight,
i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
for (int j = 0; j < _pictureHeight / (_placeSizeHeight * 2) + 1; ++j) {
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight * 2,
i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight * 2);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
_pictureHeight / _placeSizeHeight * _placeSizeHeight);
_pictureHeight / (_placeSizeHeight * 2) * _placeSizeHeight * 2);
}
}
@ -80,7 +80,7 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
DrawningBus bus = _collection.Get(i);
if (bus != null) {
int inRow = _pictureWidth / _placeSizeWidth;
bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight);
bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight * 2);
bus.DrawTransport((Graphics2D) g);
}
}