This commit is contained in:
Timourka 2023-10-23 18:05:55 +04:00
parent 8562c126a2
commit d94c613ddf
4 changed files with 154 additions and 30 deletions

View File

@ -7,13 +7,29 @@ import javax.swing.Timer;
import java.awt.event.*;
public class FormTrain{
class Canvas extends JComponent{
public DrawingTrain _drawingTrain;
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingTrain == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingTrain.DrawTransport(g2d);
super.repaint();
}
}
public DrawingTrain SelectedTrain;
public boolean DialogResult = false;
private DrawingTrain _drawingTrain;
public DrawingTrain _drawingTrain;
private AbstractStrategy abstractStrategy;
Canvas canv;
static int pictureBoxWidth = 980;
static int pictureBoxHeight = 580;
public JButton buttonSelectTrain;
public JFrame w;
public void Draw(){
canv.repaint();
@ -21,11 +37,11 @@ public class FormTrain{
public FormTrain(){
SelectedTrain = null;
JFrame w=new JFrame ("Loco");
w = new JFrame ("Loco");
JButton buttonCreate = new JButton("create");
JButton buttonCreateLocomotive = new JButton("create locomotive");
JButton buttonStrategysStep = new JButton("strategys step");
JButton buttonSelectTrain = new JButton("select train");
buttonSelectTrain = new JButton("select train");
JComboBox<String> comboBoxStrategy = new JComboBox<String>(
new String[]{
@ -56,15 +72,7 @@ public class FormTrain{
right.setContentAreaFilled(false);
right.setName("right");
right.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowR340x259.png"));
buttonSelectTrain.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
SelectedTrain = _drawingTrain;
DialogResult = true;
w.dispose();
}
}
);
buttonStrategysStep.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
@ -226,17 +234,3 @@ public class FormTrain{
w.setVisible(true);
}
}
class Canvas extends JComponent{
public DrawingTrain _drawingTrain;
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingTrain == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingTrain.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,130 @@
package laba1Loco;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class FormTrainCollecltion {
class Canvas extends JComponent{
public TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains;
public Canvas(){
}
public void paintComponent (Graphics g){
super.paintComponent(g);
if (_trains.ShowTrains() != null) {
g.drawImage(_trains.ShowTrains(), 0, 0, this);
}
super.repaint();
}
}
Canvas canv;
static int pictureBoxWidth = 860;
static int pictureBoxHeight = 580;
private TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains;
public void Draw(){
canv.repaint();
}
FormTrainCollecltion(){
canv = new Canvas();
JFrame w = new JFrame ("TrainCollecltion");
_trains = new TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>(pictureBoxWidth, pictureBoxHeight);
canv._trains = _trains;
JButton ButtonAddTrain = new JButton("ButtonAddTrain");
ButtonAddTrain.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
FormTrain form = new FormTrain();
form.buttonSelectTrain.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (_trains.Add(form._drawingTrain) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект добавлен");
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось добавить объект");
}
form.w.dispose();
}
}
);
}
}
);
JTextField TextBoxNumber = new JTextField();
JButton ButtonRemoveTrain = new JButton("ButtonRemoveTrain");
ButtonRemoveTrain.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
for (char it : TextBoxNumber.getText().toCharArray())
if (it < '0' || it > '9')
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось удалить объект");
return;
}
if (TextBoxNumber.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось удалить объект");
return;
}
int pos = Integer.parseInt(TextBoxNumber.getText());
if (_trains.remove(pos) != null)
{
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект удален");
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось удалить объект");
}
}
}
);
JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection");
ButtonRefreshCollection.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Draw();
}
}
);
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
ButtonAddTrain.setBounds(pictureBoxWidth, 0, 120, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 30, 120, 20);
ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 120, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 120, 20);
w.add(canv);
w.add(ButtonAddTrain);
w.add(ButtonRemoveTrain);
w.add(ButtonRefreshCollection);
w.add(TextBoxNumber);
w.setVisible(true);
}
}

View File

@ -2,6 +2,6 @@ package laba1Loco;
public class Main{
public static void main(String[] args) {
FormTrain formTrain = new FormTrain();
FormTrainCollecltion formTrainCollecltion = new FormTrainCollecltion();
}
}

View File

@ -44,13 +44,13 @@ public class TrainsGenericCollection<T extends DrawingTrain, U extends IMoveable
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public void Add(T obj)
public int Add(T obj)
{
if (obj == null)
{
return ;
return -1;
}
_collection.Insert(obj);
return _collection.Insert(obj);
}
/// <summary>
/// Перегрузка оператора вычитания