ПИбд-23 Кислица Егор Дмитриевич Лабораторная работа №5 (усложненная) #5

Closed
Egor wants to merge 1 commits from Laba5 into Laba4
6 changed files with 513 additions and 15 deletions

View File

@ -12,6 +12,9 @@ public class DrawningAirFighter extends DrawningAirplane {
EntityAirplane = new EntityAirFighter(speed, weight, bodyColor, additionalColor, dopwings, rackets);
}
}
public void ChangeAddColor(Color col){
((EntityAirFighter)EntityAirplane).AdditionalColor = col;
}
@Override
public void DrawAirplane(Graphics2D g2d) {

View File

@ -13,8 +13,9 @@ public class DrawningAirplane {
public int _pictureHeight;
protected int _startPosX = 0;
protected int _startPosY = 0;
private int _airplaneWidth = 160;
private int _airplaneHeight = 160;
public int _airplaneWidth = 160;
public int _airplaneHeight = 160;
public int _engineNumb;
private int _airplanekorpusHeight = 90;
protected IDraw DrawningEngines;
@ -46,6 +47,17 @@ public class DrawningAirplane {
DrawningEngines.ChangeEnginesNumb(rand.nextInt(1, 5));
}
public void ChangeIDraw(IDraw obj){
DrawningEngines = obj;
obj.ChangeEnginesNumb(_engineNumb);
obj.ChangeEnginesNumb(_engineNumb);
obj.ChangeX(_startPosX);
obj.ChangeY(_startPosY);
}
public void ChangeEnginesNumb(int numb){
_engineNumb = numb;
DrawningEngines.ChangeEnginesNumb(numb);
}
public void SetPosition(int x, int y){
if(EntityAirplane == null)
return;
@ -120,6 +132,10 @@ public class DrawningAirplane {
DrawningEngines.ChangeX(_startPosX);
DrawningEngines.ChangeY(_startPosY);
}
public void ChangeColor(Color col){
EntityAirplane.BodyColor = col;
}
public void DrawAirplane(Graphics2D g2d){
if (EntityAirplane == null)

View File

@ -2,21 +2,19 @@ package src.Entities;
import java.awt.*;
public class EntityAirFighter extends EntityAirplane {
private Color BodyColor;
private Color AdditionalColor;
public Color AdditionalColor;
private boolean DopWing;
private boolean Racket;
public Color AdditionalColor(){return AdditionalColor;}
public Color BodyColor(){
return BodyColor;
}
public boolean Racket(){return Racket;}
public boolean DopWing(){return DopWing;}
public EntityAirFighter(int speed, double weight, Color bodyColor, Color additionalColor,
boolean racket, boolean dopwing){
super(speed, weight, bodyColor);
BodyColor = bodyColor;
AdditionalColor=additionalColor;
Racket = racket;
DopWing= dopwing;

View File

@ -4,7 +4,7 @@ import java.awt.*;
public class EntityAirplane {
private int Speed;
private double Weight, Step;
private Color BodyColor;
public Color BodyColor;
public int Speed(){
return Speed;
}

View File

@ -119,15 +119,15 @@ public class FormAirFighterCollection {
}
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue());
FormAirFighter form = new FormAirFighter();
form.buttonSelect.addActionListener(new ActionListener() {
FormAirFighterConfig form = new FormAirFighterConfig();
form.addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_airplanes.Insert(form.SelectedAirplane()))
if (_airplanes.Insert(form._airplane))
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form.SelectedAirplane()._pictureWidth = pictureBoxWidth;
form.SelectedAirplane()._pictureHeight = pictureBoxHeight;
form._airplane._pictureWidth = pictureBoxWidth;
form._airplane._pictureHeight = pictureBoxHeight;
Draw();
}
else
@ -135,7 +135,14 @@ public class FormAirFighterCollection {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.AirplaneFrame.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,474 @@
package src;
import src.DrawningObjects.*;
import src.DrawningObjects.DrawningAirplane;
import src.DrawningObjects.DrawningEnginesQuad;
import src.DrawningObjects.DrawningEnginesOval;
import src.DrawningObjects.DrawningEngines;
import src.DrawningObjects.DrawningAirFighter;
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 FormAirFighterConfig {
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 FormAirFighterConfig(){
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 cartComp = new IDrawComponent(new DrawningEnginesOval(toGetSize._airplaneWidth - toGetSize._airplaneWidth / 10,
toGetSize._airplaneHeight,
_pictureBoxWidth / 2 -toGetSize._airplaneWidth / 2,
_pictureBoxHeight / 2 -toGetSize._airplaneHeight / 2));
IDrawComponent ornComp = new IDrawComponent(new DrawningEnginesQuad(toGetSize._airplaneWidth - toGetSize._airplaneWidth / 10,
toGetSize._airplaneHeight,
_pictureBoxWidth / 2 -toGetSize._airplaneWidth / 2,
_pictureBoxHeight / 2 -toGetSize._airplaneHeight / 2));
IDrawComponent baseComp = new IDrawComponent(new DrawningEngines(toGetSize._airplaneWidth - toGetSize._airplaneWidth / 10,
toGetSize._airplaneHeight,
_pictureBoxWidth / 2 -toGetSize._airplaneWidth / 2,
_pictureBoxHeight / 2 -toGetSize._airplaneHeight / 2));
cartComp.setLayout(new GridLayout(1,1));
ornComp.setLayout(new GridLayout(1,1));
baseComp.setLayout(new GridLayout(1,1));
iDrawLabel.setHorizontalAlignment(SwingConstants.CENTER);
iDrawLabel.setVerticalAlignment(SwingConstants.CENTER);
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel ovalLabel = new JLabel("Не закрашенные");
JLabel quadLabel = new JLabel("Прямоугольные");
JLabel baseLabel = new JLabel("Овальные");
ovalLabel.setHorizontalAlignment(SwingConstants.CENTER);
ovalLabel.setVerticalAlignment(SwingConstants.CENTER);
quadLabel.setHorizontalAlignment(SwingConstants.CENTER);
quadLabel.setVerticalAlignment(SwingConstants.CENTER);
baseLabel.setHorizontalAlignment(SwingConstants.CENTER);
baseLabel.setVerticalAlignment(SwingConstants.CENTER);
cartComp.add(ovalLabel);
ornComp.add(quadLabel);
baseComp.add(baseLabel);
cartComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
ornComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
baseComp.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 checkRacket = new JCheckBox("Наличие доп. крыльев");
JCheckBox checkDopWing = new JCheckBox("Наличие ракет");
checkRacket.setBounds(6, 132, 159, 24);
checkDopWing.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);
cartComp.setBounds(171,229, 120, 50);
ornComp.setBounds(297,229, 120, 50);
baseComp.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 engineNumbSpinnerModel = new SpinnerNumberModel(1, 1, 3, 1.0);
JSpinner NumbSpinner = new JSpinner(engineNumbSpinnerModel);
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);
NumbSpinner.setBounds(6, 200, 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(checkRacket);
frameConfig.add(checkDopWing);
frameConfig.add(canv);
frameConfig.add(addButton);
frameConfig.add(cancelButton);
frameConfig.add(NumbSpinner);
frameConfig.setVisible(true);
frameConfig.add(colorPanel);
frameConfig.add(cartComp);
frameConfig.add(ornComp);
frameConfig.add(baseComp);
colorPanel.setVisible(true);
cartComp.setVisible(true);
ornComp.setVisible(true);
baseComp.setVisible(true);
canv.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 engineNumbObj = NumbSpinner.getValue();
Number engineNumbNumb = (Number) engineNumbObj;
int engineNumb = engineNumbNumb.intValue();
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "Простой":
_airplane = new DrawningAirplane(speed, weight, Color.WHITE,
_pictureBoxWidth, _pictureBoxHeight);
_airplane.ChangeEnginesNumb(engineNumb);
break;
case "Продвинутый":
_airplane = new DrawningAirFighter(speed, weight, Color.WHITE, Color.GRAY,
checkDopWing.isSelected(), checkRacket.isSelected(), _pictureBoxWidth, _pictureBoxHeight);
_airplane.ChangeEnginesNumb(engineNumb);
break;
}
_airplane.SetPosition(_pictureBoxWidth / 3 -75,
_pictureBoxHeight / 2 -_airplane._airplaneHeight / 6);
canv.DrawningAirplane = _airplane;
canv.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 (_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(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 (_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(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 (_airplane == null || !(_airplane instanceof DrawningAirFighter))
return false;
((DrawningAirFighter)_airplane).ChangeAddColor(color);
canv.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
}
}