PIbd-21_KozyrevSS_GasolineT.../ConfigFrame.java

430 lines
16 KiB
Java
Raw Permalink Normal View History

2023-11-28 18:11:06 +04:00
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.*;
import java.io.IOException;
class DrawConfig extends JComponent {
public DrawTanker _tanker;
public DrawConfig()
{
_tanker = null;
}
private boolean IntToBool(int n)
{
return n > 0;
}
public void paintComponent(Graphics g)
{
if (_tanker == null)
return;
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
_tanker.DrawTransport(g);
super.repaint();
}
}
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());
}
}
class ColorTransferable implements Transferable {
private Color color;
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);
}
}
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());
}
}
class LabelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
}
}
class PanelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
}
}
class WheelTransferable implements Transferable {
private IWheelDraw wheelDraw;
static final DataFlavor wheelDrawingDataFlavor = new DataFlavor(IWheelDraw.class, "Wheel Drawing");
public WheelTransferable(IWheelDraw wheelDrawing) {
wheelDraw = wheelDrawing;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{wheelDrawingDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(wheelDrawingDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return wheelDraw;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
class ComponentWheel extends JComponent{
public IWheelDraw wheelDraw;
public ComponentWheel(IWheelDraw _wheelDrawing){
wheelDraw = _wheelDrawing;
this.addMouseListener(
new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
((ComponentWheel)e.getComponent()).getTransferHandler().exportAsDrag(((ComponentWheel)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 WheelTransferable(((ComponentWheel)c).wheelDraw);
}
}
);
}
@Override
public void paintComponent (Graphics g){
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
wheelDraw.DrawWheels(20,-40,Color.BLACK,g2d);
super.repaint();
}
}
public class ConfigFrame extends JFrame {
public ConfigFrame()
{
initUI();
}
public JButton buttonAdd;
protected static final int Width = 1000;
protected static final int Height = 400;
DrawConfig Gasoline;
private void initUI()
{
Border border = BorderFactory.createLineBorder(Color.GRAY);
JLabel labelSpeed = new JLabel("Speed");
labelSpeed.setBounds(50, 10, 50, 50);
add(labelSpeed);
JLabel labelWeight = new JLabel("Weight");
labelWeight.setBounds(50, 60, 50, 50);
add(labelWeight);
JLabel labelWheel = new JLabel("Wheel");
labelWheel.setBounds(50, 110, 50, 50);
add(labelWheel);
SpinnerModel spinnerModel = new SpinnerNumberModel(100, 100, 1000, 1);
SpinnerModel spinnerModel2 = new SpinnerNumberModel(100, 100, 1000, 1);
SpinnerModel spinnerModel3 = new SpinnerNumberModel(2, 2, 4, 1);
JSpinner numericSpeed = new JSpinner(spinnerModel);
numericSpeed.setBounds(100, 20, 50, 30);
add(numericSpeed);
JSpinner numericWeight = new JSpinner(spinnerModel2);
numericWeight.setBounds(100, 70, 50, 30);
add(numericWeight);
JSpinner numericWheels = new JSpinner(spinnerModel3);
numericWheels.setBounds(100, 120, 50, 30);
add(numericWheels);
JCheckBox checkBoxLight = new JCheckBox("Light");
checkBoxLight.setBounds(50, 200, 100, 50);
add(checkBoxLight);
JCheckBox checkBoxFuel = new JCheckBox("Fuel");
checkBoxFuel.setBounds(50, 250, 100, 50);
add(checkBoxFuel);
JCheckBox checkBoxSpeedLine = new JCheckBox("Speed Line");
checkBoxSpeedLine.setBounds(50, 300, 100, 50);
add(checkBoxSpeedLine);
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.WHITE);
colorPanels[7].setBackground(Color.MAGENTA);
for (var i : colorPanels)
{
i.setTransferHandler(new PanelTransferHandler());
i.addMouseListener(new PanelMouseAdapter());
}
for (int i = 0; i < 8; i++)
{
colorPanels[i].setBounds(300 + 50 * (i % 4), 50 + i / 4 * 50, 50, 50);
add(colorPanels[i]);
}
JLabel labelBase = new JLabel("Base");
labelBase.setTransferHandler(new LabelTransferHandler());
labelBase.addMouseListener(new LabelMouseAdapter());
labelBase.setBorder(border);
labelBase.setHorizontalAlignment(SwingConstants.CENTER);
labelBase.setVerticalAlignment(SwingConstants.CENTER);
labelBase.setBounds(300, 200, 100, 50);
add(labelBase);
JLabel labelUpdate = new JLabel("Updated");
labelUpdate.setTransferHandler(new LabelTransferHandler());
labelUpdate.addMouseListener(new LabelMouseAdapter());
labelUpdate.setBorder(border);
labelUpdate.setHorizontalAlignment(SwingConstants.CENTER);
labelUpdate.setVerticalAlignment(SwingConstants.CENTER);
labelUpdate.setBounds(400, 200, 100, 50);
add(labelUpdate);
JLabel labelColor = new JLabel("BaseColor");
labelColor.setBorder(border);
labelColor.setVerticalAlignment(SwingConstants.CENTER);
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
labelColor.setBounds(600, 50, 100, 50);
add(labelColor);
labelColor.setTransferHandler(new TransferHandler(){
@Override
public boolean canImport(TransferHandler.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 (Gasoline._tanker == null)
return false;
Gasoline._tanker.GasolineTanker.setBodyColor(color);
Gasoline.repaint();
return true;
}
catch (UnsupportedFlavorException | IOException e)
{
e.printStackTrace();
}
}
return false;
}
});
JLabel labelAddColor = new JLabel("AddColor");
labelAddColor.setBorder(border);
labelAddColor.setHorizontalAlignment(SwingConstants.CENTER);
labelAddColor.setVerticalAlignment(SwingConstants.CENTER);
labelAddColor.setBounds(700, 50, 100, 50);
add(labelAddColor);
labelAddColor.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 (Gasoline._tanker == null)
return false;
if (!(Gasoline._tanker instanceof DrawGasolineTanker))
return false;
((GasolineTanker)Gasoline._tanker.GasolineTanker).setAdditionalColor(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
JLabel labelWheels = new JLabel("Wheels");
labelWheels.setBorder(border);
labelWheels.setHorizontalAlignment(SwingConstants.CENTER);
labelWheels.setVerticalAlignment(SwingConstants.CENTER);
labelWheels.setBounds(800, 50, 100, 50);
add(labelWheels);
labelWheels.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(WheelTransferable.wheelDrawingDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IWheelDraw wheelDrawing = (IWheelDraw) support.getTransferable().getTransferData(WheelTransferable.wheelDrawingDataFlavor);
if (Gasoline._tanker == null)
return false;
wheelDrawing.setWheelCount(Gasoline._tanker.wheelsDrawing.getWheelCount());
Gasoline._tanker.wheelsDrawing = wheelDrawing;
Gasoline.repaint();
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
setSize(Width, Height);
setTitle("TankGasoline");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
Gasoline = new DrawConfig();
Gasoline.setBounds(600, 100, 300, 200);
add(Gasoline);
Gasoline.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 "Base":
Gasoline._tanker = new DrawTanker((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, Width,Height, (int)numericWheels.getValue());
break;
case "Updated":
Gasoline._tanker = new DrawGasolineTanker((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, Color.BLACK, checkBoxFuel.isSelected(), checkBoxLight.isSelected(), checkBoxSpeedLine.isSelected(), Width, Height, (int)numericWheels.getValue());
break;
}
Gasoline.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
ComponentWheel componentWheelCircle = new ComponentWheel(new DrawWheelCircle());
ComponentWheel componentWheelClassic = new ComponentWheel(new DrawWheelClassic());
ComponentWheel componentWheelSquare = new ComponentWheel(new DrawWheelSquare());
componentWheelCircle.setBounds(300, 250, 50, 50);
add(componentWheelCircle);
componentWheelClassic.setBounds(350, 250, 50, 50);
add(componentWheelClassic);
componentWheelSquare.setBounds(400, 250, 50, 50);
add(componentWheelSquare);
buttonAdd = new JButton("Add");
buttonAdd.setBounds(600, 300, 100, 50);
add(buttonAdd);
JButton buttonCancel = new JButton("Cancel");
buttonCancel.setBounds(700, 300, 100, 50);
add(buttonCancel);
buttonCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}