Готовая лабораторная работа 3

This commit is contained in:
Никита Белянин 2023-11-12 16:09:58 +04:00
parent 3f27d54ee7
commit 12e650cdb7

View File

@ -10,19 +10,19 @@ public class FormTank {
} }
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
if (_drawingTank == null) { if (_drawingVehicle == null) {
return; return;
} }
super.paintComponents(g); super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
_drawingTank.DrawTransport(g2d); _drawingVehicle.DrawTransport(g2d);
super.repaint(); super.repaint();
} }
} }
public DrawingArmoVehicle SelectedVehicle; public DrawingArmoVehicle SelectedVehicle;
public boolean DialogResult = false; public boolean DialogResult = false;
public DrawingArmoVehicle _drawingTank; public DrawingArmoVehicle _drawingVehicle;
private AbstractStrategy abstractStrategy; private AbstractStrategy abstractStrategy;
Canvas canv; Canvas canv;
static int pictureBoxWidth = 980; static int pictureBoxWidth = 980;
@ -42,7 +42,7 @@ public class FormTank {
JButton buttonStrategysStep = new JButton("Шаг"); JButton buttonStrategysStep = new JButton("Шаг");
buttonSelectTank = new JButton("Добавить"); buttonSelectTank = new JButton("Добавить");
JComboBox<String> comboBoxStrategy = new JComboBox<String>( JComboBox<String> ComboBoxStrategy = new JComboBox<String>(
new String[]{ new String[]{
"к центру", "к центру",
"к краю", "к краю",
@ -71,11 +71,11 @@ public class FormTank {
buttonStrategysStep.addActionListener( buttonStrategysStep.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_drawingTank == null) { if (_drawingVehicle == null) {
return; return;
} }
if (comboBoxStrategy.isEnabled()) { if (ComboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) { switch (ComboBoxStrategy.getSelectedIndex()) {
case 0: case 0:
abstractStrategy = new MoveToCenter(); abstractStrategy = new MoveToCenter();
break; break;
@ -90,8 +90,8 @@ public class FormTank {
if (abstractStrategy == null) { if (abstractStrategy == null) {
return; return;
} }
abstractStrategy.SetData(new DrawingObjectTank(_drawingTank), pictureBoxWidth, pictureBoxHeight); abstractStrategy.SetData(new DrawingObjectTank(_drawingVehicle), pictureBoxWidth, pictureBoxHeight);
comboBoxStrategy.setEnabled(false); ComboBoxStrategy.setEnabled(false);
} }
if (abstractStrategy == null) { if (abstractStrategy == null) {
return; return;
@ -99,7 +99,7 @@ public class FormTank {
abstractStrategy.MakeStep(); abstractStrategy.MakeStep();
Draw(); Draw();
if (abstractStrategy.GetStatus() == Status.Finish) { if (abstractStrategy.GetStatus() == Status.Finish) {
comboBoxStrategy.setEnabled(true); ComboBoxStrategy.setEnabled(true);
abstractStrategy = null; abstractStrategy = null;
} }
} }
@ -121,7 +121,7 @@ public class FormTank {
if (selectedColor != null) { if (selectedColor != null) {
coloradditional = selectedColor; coloradditional = selectedColor;
} }
_drawingTank = new DrawingTank( _drawingVehicle = new DrawingTank(
random.nextInt(100, 300), random.nextInt(100, 300),
random.nextInt(1000, 3000), random.nextInt(1000, 3000),
color, color,
@ -134,7 +134,7 @@ public class FormTank {
pictureBoxHeight, pictureBoxHeight,
true true
); );
_drawingTank.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); _drawingVehicle.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw(); Draw();
} }
} }
@ -149,7 +149,7 @@ public class FormTank {
if (selectedColor != null) { if (selectedColor != null) {
color = selectedColor; color = selectedColor;
} }
_drawingTank = new DrawingArmoVehicle( _drawingVehicle = new DrawingArmoVehicle(
random.nextInt(100, 300), random.nextInt(100, 300),
random.nextInt(1000, 3000), random.nextInt(1000, 3000),
color, color,
@ -158,7 +158,7 @@ public class FormTank {
pictureBoxHeight pictureBoxHeight
); );
_drawingTank.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); _drawingVehicle.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw(); Draw();
} }
} }
@ -166,21 +166,21 @@ public class FormTank {
ActionListener actioListener = new ActionListener() { ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_drawingTank == null) { if (_drawingVehicle == null) {
return; return;
} }
switch (((JButton) (e.getSource())).getName()) { switch (((JButton) (e.getSource())).getName()) {
case "up": case "up":
_drawingTank.MoveTransport(Direction.Up); _drawingVehicle.MoveTransport(Direction.Up);
break; break;
case "down": case "down":
_drawingTank.MoveTransport(Direction.Down); _drawingVehicle.MoveTransport(Direction.Down);
break; break;
case "left": case "left":
_drawingTank.MoveTransport(Direction.Left); _drawingVehicle.MoveTransport(Direction.Left);
break; break;
case "right": case "right":
_drawingTank.MoveTransport(Direction.Right); _drawingVehicle.MoveTransport(Direction.Right);
break; break;
} }
Draw(); Draw();
@ -201,7 +201,7 @@ public class FormTank {
down.setBounds(900, 520, 40, 40); down.setBounds(900, 520, 40, 40);
left.setBounds(860, 520, 40, 40); left.setBounds(860, 520, 40, 40);
right.setBounds(940, 520, 40, 40); right.setBounds(940, 520, 40, 40);
comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20); ComboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20);
buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20); buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20);
buttonSelectTank.setBounds(pictureBoxWidth / 2, 530, 150, 30); buttonSelectTank.setBounds(pictureBoxWidth / 2, 530, 150, 30);
Frame.add(buttonSelectTank); Frame.add(buttonSelectTank);
@ -212,7 +212,7 @@ public class FormTank {
Frame.add(down); Frame.add(down);
Frame.add(left); Frame.add(left);
Frame.add(right); Frame.add(right);
Frame.add(comboBoxStrategy); Frame.add(ComboBoxStrategy);
Frame.add(buttonStrategysStep); Frame.add(buttonStrategysStep);
Frame.setVisible(true); Frame.setVisible(true);
} }