some base works on 5 Laba

This commit is contained in:
Timourka 2023-11-05 01:55:46 +04:00
parent bdab79faad
commit b2efa293ab
2 changed files with 195 additions and 3 deletions

View File

@ -90,8 +90,8 @@ public class FormTrainCollecltion {
return;
}
FormTrain form = new FormTrain();
form.buttonSelectTrain.addActionListener(
FormTrainConfig form = new FormTrainConfig();
/*form.buttonSelectTrain.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (obj.Add(form._drawingTrain) != -1)
@ -108,7 +108,7 @@ public class FormTrainCollecltion {
form.w.dispose();
}
}
);
);*/
}
}
);

View File

@ -0,0 +1,192 @@
package laba1Loco;
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;
public class FormTrainConfig {
private class Canvas extends JComponent{
public Canvas(){
}
public void paintComponent (Graphics g){
if (_train == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_train.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 DrawingTrain _train = null;
public JButton buttonAdd;
public JFrame w;
public FormTrainConfig(){
Border border = BorderFactory.createLineBorder(Color.GRAY);
JLabel labelSpeed = new JLabel("Speed");
JLabel labelWeight = new JLabel("Weight");
JLabel labelWheelNum = new JLabel("Wheel");
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 checkBoxTube = new JCheckBox("Smoke tube");
JCheckBox checkBoxFuelTank = new JCheckBox("Fuel tank");
JCheckBox checkBoxLocoLine = new JCheckBox("Loco line");
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("Train");
labelTrain.setTransferHandler(new LabelTransferHandler());
labelTrain.addMouseListener(new LabelMouseAdapter());
labelTrain.setBorder(border);
labelTrain.setHorizontalAlignment(SwingConstants.CENTER);
labelTrain.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelLoco = new JLabel("Loco");
labelLoco.setTransferHandler(new LabelTransferHandler());
labelLoco.addMouseListener(new LabelMouseAdapter());
labelLoco.setBorder(border);
labelLoco.setHorizontalAlignment(SwingConstants.CENTER);
labelLoco.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelColor = new JLabel("Color");
labelColor.setBorder(border);
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
labelColor.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelAdditionalColor = new JLabel("Additional color");
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 "Train":
_train = new DrawingTrain((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, (int)numericWheelNum.getValue(), CanvasWidth,CanvasHeight);
break;
case "Loco":
_train = new DrawingLoco((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, (int)numericWheelNum.getValue(), Color.BLACK, checkBoxTube.isSelected(), checkBoxFuelTank.isSelected(), checkBoxLocoLine.isSelected(), CanvasWidth,CanvasHeight);
break;
}
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
buttonAdd = new JButton("Add");
JButton buttonCancel = new JButton("Cancel");
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);
checkBoxTube.setBounds(10,100,120,20);
checkBoxFuelTank.setBounds(10,130,120,20);
checkBoxLocoLine.setBounds(10,160,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(checkBoxTube);
w.add(checkBoxFuelTank);
w.add(checkBoxLocoLine);
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(
new ActionListener() {
public void actionPerformed(ActionEvent e){
w.dispose();
}
}
);
}
}