PIbd-21 Potapov N.S. LabWork05 Hard #5
189
ProjectStormtrooper/FormPlaneConfig.java
Normal file
189
ProjectStormtrooper/FormPlaneConfig.java
Normal file
@ -0,0 +1,189 @@
|
||||
package ProjectStormtrooper;
|
||||
|
||||
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 javax.swing.border.Border;
|
||||
|
||||
import java.awt.event.*;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FormPlaneConfig {
|
||||
private class Canvas extends JComponent {
|
||||
public Canvas() {
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
if (_plane == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_plane.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
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 LabelMouseAdapter extends MouseAdapter {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
final int WindowHeight = 700;
|
||||
final int WindowWidth = 1000;
|
||||
final int CanvasHeight = 600;
|
||||
final int CanvasWidth = 600;
|
||||
public DrawingPlane _plane = null;
|
||||
public JButton buttonAdd;
|
||||
public JFrame w;
|
||||
|
||||
public FormPlaneConfig() {
|
||||
Border border = BorderFactory.createLineBorder(Color.GRAY);
|
||||
JLabel labelSpeed = new JLabel("Скорость");
|
||||
JLabel labelWeight = new JLabel("Вес");
|
||||
JLabel labelWheelNum = new JLabel("Двигатели");
|
||||
SpinnerModel spinnerModel = new SpinnerNumberModel(100, 100, 1000, 1);
|
||||
JSpinner numericSpeed = new JSpinner(spinnerModel);
|
||||
SpinnerModel spinnerModel2 = new SpinnerNumberModel(100, 100, 1000, 1);
|
||||
JSpinner numericWeight = new JSpinner(spinnerModel2);
|
||||
SpinnerModel spinnerModel3 = new SpinnerNumberModel(2, 2, 4, 1);
|
||||
JSpinner numericWheelNum = new JSpinner(spinnerModel3);
|
||||
JCheckBox checkBoxRockets = new JCheckBox("Ракеты");
|
||||
JCheckBox checkBoxBombs = new JCheckBox("Бомбы");
|
||||
JPanel[] colorPanels = {
|
||||
new JPanel(), new JPanel(), new JPanel(), new JPanel(),
|
||||
new JPanel(), new JPanel(), new JPanel(), new JPanel(),
|
||||
};
|
||||
colorPanels[0].setBackground(Color.BLACK);
|
||||
colorPanels[1].setBackground(Color.BLUE);
|
||||
colorPanels[2].setBackground(Color.GRAY);
|
||||
colorPanels[3].setBackground(Color.YELLOW);
|
||||
colorPanels[4].setBackground(Color.RED);
|
||||
colorPanels[5].setBackground(Color.GREEN);
|
||||
colorPanels[6].setBackground(Color.ORANGE);
|
||||
colorPanels[7].setBackground(Color.WHITE);
|
||||
|
||||
JLabel labelTrain = new JLabel("Самолет");
|
||||
labelTrain.setTransferHandler(new LabelTransferHandler());
|
||||
labelTrain.addMouseListener(new LabelMouseAdapter());
|
||||
labelTrain.setBorder(border);
|
||||
labelTrain.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
labelTrain.setVerticalAlignment(SwingConstants.CENTER);
|
||||
|
||||
JLabel labelLoco = new JLabel("Штурмовик");
|
||||
labelLoco.setTransferHandler(new LabelTransferHandler());
|
||||
labelLoco.addMouseListener(new LabelMouseAdapter());
|
||||
labelLoco.setBorder(border);
|
||||
labelLoco.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
labelLoco.setVerticalAlignment(SwingConstants.CENTER);
|
||||
|
||||
JLabel labelColor = new JLabel("Цвет");
|
||||
labelColor.setBorder(border);
|
||||
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
labelColor.setVerticalAlignment(SwingConstants.CENTER);
|
||||
JLabel labelAdditionalColor = new JLabel("Доп. цвет");
|
||||
labelAdditionalColor.setBorder(border);
|
||||
labelAdditionalColor.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
labelAdditionalColor.setVerticalAlignment(SwingConstants.CENTER);
|
||||
|
||||
Canvas canvas = new Canvas();
|
||||
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 {
|
||||
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
|
||||
switch (data) {
|
||||
case "Самолет":
|
||||
_plane = new DrawingPlane((int)numericSpeed.getValue(), (double)numericWeight.getValue(), Color.WHITE, CanvasWidth, CanvasHeight);
|
||||
break;
|
||||
case "Штурмовик":
|
||||
_plane = new DrawingStormtrooper((int)numericSpeed.getValue(), (double)numericWeight.getValue(), Color.WHITE, Color.WHITE, checkBoxRockets.isSelected(), checkBoxBombs.isSelected(), CanvasWidth, CanvasHeight);
|
||||
break;
|
||||
}
|
||||
canvas.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
buttonAdd = new JButton("Добавить");
|
||||
JButton buttonCancel = new JButton("Отмена");
|
||||
|
||||
labelSpeed.setBounds(10, 10, 40, 20);
|
||||
labelWeight.setBounds(10, 40, 40, 20);
|
||||
labelWheelNum.setBounds(10, 70, 40, 20);
|
||||
numericSpeed.setBounds(55, 10, 80, 20);
|
||||
numericWeight.setBounds(55, 40, 80, 20);
|
||||
numericWheelNum.setBounds(55, 70, 80, 20);
|
||||
checkBoxRockets.setBounds(10, 100, 120, 20);
|
||||
checkBoxBombs.setBounds(10, 130, 120, 20);
|
||||
for (int i = 0; i < colorPanels.length; i += 2) {
|
||||
colorPanels[i].setBounds(10, 200 + i / 2 * 60, 50, 50);
|
||||
colorPanels[i + 1].setBounds(70, 200 + i / 2 * 60, 50, 50);
|
||||
}
|
||||
labelTrain.setBounds(10, 500, 50, 30);
|
||||
labelLoco.setBounds(70, 500, 50, 30);
|
||||
|
||||
labelColor.setBounds(WindowWidth - CanvasWidth, 10, CanvasWidth / 3, 30);
|
||||
labelAdditionalColor.setBounds(WindowWidth - CanvasWidth + CanvasWidth * 2 / 3, 10, CanvasWidth / 3, 30);
|
||||
canvas.setBounds(WindowWidth - CanvasWidth, 50, CanvasWidth, CanvasHeight);
|
||||
buttonAdd.setBounds(WindowWidth - CanvasWidth, CanvasHeight + 60, CanvasWidth / 3, 30);
|
||||
buttonCancel.setBounds(WindowWidth - CanvasWidth + CanvasWidth * 2 / 3, CanvasHeight + 60, CanvasWidth / 3, 30);
|
||||
|
||||
w = new JFrame();
|
||||
w.setSize(WindowWidth + 20, WindowHeight + 40);
|
||||
w.setLayout(null);
|
||||
w.add(labelSpeed);
|
||||
w.add(labelWeight);
|
||||
w.add(labelWheelNum);
|
||||
w.add(numericSpeed);
|
||||
w.add(numericWeight);
|
||||
w.add(numericWheelNum);
|
||||
w.add(checkBoxRockets);
|
||||
w.add(checkBoxBombs);
|
||||
for (var it : colorPanels)
|
||||
w.add(it);
|
||||
w.add(labelTrain);
|
||||
w.add(labelLoco);
|
||||
w.add(labelColor);
|
||||
w.add(labelAdditionalColor);
|
||||
w.add(canvas);
|
||||
w.add(buttonAdd);
|
||||
w.add(buttonCancel);
|
||||
|
||||
w.setVisible(true);
|
||||
|
||||
buttonCancel.addActionListener(
|
||||
e -> w.dispose()
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user