Лаба 5 - В С Ё! 2.0
This commit is contained in:
parent
fc549cc992
commit
3a5b2d28bb
452
src/FormAirplaneConfig.java
Normal file
452
src/FormAirplaneConfig.java
Normal file
@ -0,0 +1,452 @@
|
||||
package src;
|
||||
|
||||
import src.DrawningObjects.*;
|
||||
|
||||
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;
|
||||
|
||||
import static java.lang.Integer.parseInt;
|
||||
|
||||
public class FormAirplaneConfig {
|
||||
private 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 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, IOException {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return color;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class iDrawTransferable implements Transferable {
|
||||
private IDraw iDrawObject;
|
||||
private 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private 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());
|
||||
}
|
||||
}
|
||||
|
||||
private class LabelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
private class PanelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
private 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);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public JFrame frameConfig;
|
||||
|
||||
public JButton addButton;
|
||||
public JButton cancelButton;
|
||||
public DrawningAirplane _airplane;
|
||||
int _pictureBoxWidth = 218;
|
||||
int _pictureBoxHeight = 190;
|
||||
|
||||
public FormAirplaneConfig(){
|
||||
frameConfig = new JFrame();
|
||||
addButton = new JButton("Добавить");
|
||||
cancelButton = new JButton("Отмена");
|
||||
addButton.setBounds(555,262,94,29);
|
||||
cancelButton.setBounds(679,262,94,29);
|
||||
Canvas canv = new Canvas();
|
||||
canv.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
canv.setBounds(555, 65, _pictureBoxWidth, _pictureBoxHeight);
|
||||
JLabel iDrawLabel = new JLabel("Форма");
|
||||
JLabel colorLabel = new JLabel("Цвет");
|
||||
DrawningAirplane toGetSize = new DrawningAirplane(0,0, null, 0, 0);
|
||||
IDrawComponent usualComp = new IDrawComponent(new DrawningIlluminators(toGetSize._airplaneWidth - toGetSize._airplaneWidth / 10,
|
||||
toGetSize._airplaneHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._airplaneWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._airplaneHeight / 2));
|
||||
IDrawComponent circComp = new IDrawComponent(new DrawningIlluminatorsCirc(toGetSize._airplaneWidth - toGetSize._airplaneWidth / 10,
|
||||
toGetSize._airplaneHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._airplaneWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._airplaneHeight / 2));
|
||||
IDrawComponent quadComp = new IDrawComponent(new DrawningIlluminatorsQuad(toGetSize._airplaneWidth - toGetSize._airplaneWidth / 10,
|
||||
toGetSize._airplaneHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._airplaneWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._airplaneHeight / 2));
|
||||
usualComp.setLayout(new GridLayout(1,1));
|
||||
circComp.setLayout(new GridLayout(1,1));
|
||||
quadComp.setLayout(new GridLayout(1,1));
|
||||
iDrawLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
iDrawLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
JLabel cartLabel = new JLabel("Круглые");
|
||||
JLabel ornLabel = new JLabel("Прозрачные");
|
||||
JLabel baseLabel = new JLabel("Квадратные");
|
||||
cartLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
cartLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
ornLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
ornLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
baseLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
baseLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
|
||||
usualComp.add(cartLabel);
|
||||
circComp.add(ornLabel);
|
||||
quadComp.add(baseLabel);
|
||||
usualComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
circComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
quadComp.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 checkRadar = new JCheckBox("Радар");
|
||||
JCheckBox checkDopBak = new JCheckBox("Дополнительный бак");
|
||||
checkRadar.setBounds(6, 132, 159, 24);
|
||||
checkDopBak.setBounds(6, 162, 145, 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);
|
||||
usualComp.setBounds(171,229, 120, 50);
|
||||
circComp.setBounds(297,229, 120, 50);
|
||||
quadComp.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 illuminatorNumbSpinnerModel = new SpinnerNumberModel(10, 10, 30, 10);
|
||||
JLabel illuminatorLabel = new JLabel ("Количество иллюминаторов");
|
||||
JSpinner illuminatorNumbSpinner = new JSpinner(illuminatorNumbSpinnerModel);
|
||||
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);
|
||||
illuminatorLabel.setBounds(6, 200, 150, 27);
|
||||
illuminatorNumbSpinner.setBounds(6, 230, 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(illuminatorLabel);
|
||||
frameConfig.add(weightSpinner);
|
||||
frameConfig.add(simpleLabel);
|
||||
frameConfig.add(advancedLabel);
|
||||
frameConfig.add(checkRadar);
|
||||
frameConfig.add(checkDopBak);
|
||||
frameConfig.add(canv);
|
||||
frameConfig.add(addButton);
|
||||
frameConfig.add(cancelButton);
|
||||
frameConfig.add(illuminatorNumbSpinner);
|
||||
|
||||
frameConfig.setVisible(true);
|
||||
frameConfig.add(colorPanel);
|
||||
frameConfig.add(usualComp);
|
||||
frameConfig.add(circComp);
|
||||
frameConfig.add(quadComp);
|
||||
colorPanel.setVisible(true);
|
||||
usualComp.setVisible(true);
|
||||
circComp.setVisible(true);
|
||||
quadComp.setVisible(true);
|
||||
|
||||
canv.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(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 illuminatorNumbObj = illuminatorNumbSpinner.getValue();
|
||||
Number illuminatorNumbNumb = (Number) illuminatorNumbObj;
|
||||
int illuminatorNumb = illuminatorNumbNumb.intValue();
|
||||
|
||||
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
|
||||
switch (data) {
|
||||
case "Простой":
|
||||
_airplane = new DrawningAirplane(speed, weight, Color.WHITE, _pictureBoxWidth, _pictureBoxHeight);
|
||||
_airplane.ChangeIlluminatorNumb(illuminatorNumb);
|
||||
break;
|
||||
case "Продвинутый":
|
||||
_airplane = new DrawningAirplaneWithRadar(speed, weight, Color.WHITE, Color.GRAY, checkRadar.isSelected(), checkDopBak.isSelected(), _pictureBoxWidth, _pictureBoxHeight);
|
||||
_airplane.ChangeIlluminatorNumb(illuminatorNumb);
|
||||
break;
|
||||
}
|
||||
_airplane.SetPosition(_pictureBoxWidth / 2 - _airplane.GetWidth()/2, _pictureBoxHeight / 2 - _airplane.GetHeight()/2);
|
||||
canv.DrawningAirplane = _airplane;
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
iDrawLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(iDrawTransferable.iDrawDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
IDraw obj = (IDraw) support.getTransferable().getTransferData(iDrawTransferable.iDrawDataFlavor);
|
||||
if (_airplane == null)
|
||||
return false;
|
||||
_airplane.ChangeIDraw(obj);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
colorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (_airplane == null)
|
||||
return false;
|
||||
_airplane.ChangeColor(color);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
addColorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (_airplane == null || !(_airplane instanceof DrawningAirplaneWithRadar))
|
||||
return false;
|
||||
((DrawningAirplaneWithRadar)_airplane).ChangeAddColor(color);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user