PIbd-23-Radaev-A.V.-Catamar.../FormCatamaran.java

178 lines
7.1 KiB
Java
Raw Normal View History

2023-12-01 20:37:58 +04:00
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
2023-12-10 11:59:44 +04:00
public class FormCatamaran {
Canvas canv;
private DrawningCatamaran DrawningCatamaran;
private AbstractStrategy _abstractStrategy;
2023-12-01 20:37:58 +04:00
private JButton UpButton;
private JButton LeftButton;
private JButton RightButton;
private JButton DownButton;
private JButton CreateButton;
2023-12-10 11:59:44 +04:00
private JButton CreateButtonPro;
final int pictureBoxWidth = 885;
final int pictureBoxHeight = 462;
public void Draw() {
canv.repaint();
2023-12-01 20:37:58 +04:00
}
2023-12-10 11:59:44 +04:00
public FormCatamaran() {
JFrame CatamaranFrame = new JFrame();
2023-12-01 20:37:58 +04:00
UpButton = new JButton("");
LeftButton = new JButton("");
RightButton = new JButton("");
DownButton = new JButton("");
CreateButton = new JButton("Создать");
2023-12-10 11:59:44 +04:00
CreateButtonPro = new JButton("Создать про");
JButton buttonStep = new JButton("Шаг");
JComboBox comboBoxStrategy = new JComboBox(
new String[] {"Довести до центра","Довести до края",
});
buttonStep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (DrawningCatamaran == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) {
case 0:
_abstractStrategy = new MoveToCenter();
break;
case 1:
_abstractStrategy = new MoveToBorder();
break;
default:
_abstractStrategy = null;
break;
}
;
if (_abstractStrategy == null) {
return;
}
_abstractStrategy.SetData(new DrawningObjectCatamaran(DrawningCatamaran), pictureBoxWidth,
pictureBoxHeight);
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null) {
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish) {
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
}
});
2023-12-01 20:37:58 +04:00
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2023-12-10 11:59:44 +04:00
Random random = new Random();
DrawningCatamaran = new DrawningCatamaran(random.nextInt(200,1000), random.nextDouble(1000,3000),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), pictureBoxWidth,
pictureBoxHeight, random.nextBoolean(), random.nextBoolean());
canv.DrawningCatamaran = DrawningCatamaran;
comboBoxStrategy.enable(true);
Draw();
}
});
CreateButtonPro.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random random = new Random();
DrawningCatamaran = new DrawningCatamaranPro(random.nextInt(200,1000), random.nextDouble(1000,3000),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
pictureBoxWidth, pictureBoxHeight,
random.nextBoolean(), random.nextBoolean(),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
random.nextBoolean(), random.nextBoolean());
canv.DrawningCatamaran = DrawningCatamaran;
comboBoxStrategy.enable(true);
Draw();
2023-12-01 20:37:58 +04:00
}
});
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2023-12-10 11:59:44 +04:00
if (DrawningCatamaran.EntityCatamaran() == null) {
2023-12-01 20:37:58 +04:00
return;
2023-12-10 11:59:44 +04:00
}
DrawningCatamaran.MoveTransport(DirectionType.Right);
Draw();
2023-12-01 20:37:58 +04:00
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2023-12-10 11:59:44 +04:00
if (DrawningCatamaran.EntityCatamaran() == null)
2023-12-01 20:37:58 +04:00
return;
2023-12-10 11:59:44 +04:00
DrawningCatamaran.MoveTransport(DirectionType.Left);
Draw();
2023-12-01 20:37:58 +04:00
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2023-12-10 11:59:44 +04:00
if (DrawningCatamaran.EntityCatamaran() == null)
2023-12-01 20:37:58 +04:00
return;
2023-12-10 11:59:44 +04:00
DrawningCatamaran.MoveTransport(DirectionType.Up);
Draw();
2023-12-01 20:37:58 +04:00
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2023-12-10 11:59:44 +04:00
if (DrawningCatamaran.EntityCatamaran() == null)
2023-12-01 20:37:58 +04:00
return;
2023-12-10 11:59:44 +04:00
DrawningCatamaran.MoveTransport(DirectionType.Down);
Draw();
2023-12-01 20:37:58 +04:00
}
});
2023-12-10 11:59:44 +04:00
CatamaranFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CatamaranFrame.setSize(900, 500);
CatamaranFrame.setLayout(null);
CatamaranFrame.setTitle("Form Catamaran");
CreateButtonPro.setBounds(120, 401, 120, 40);
CreateButton.setBounds(12, 401, 90, 40);
RightButton.setBounds(830, 391, 50, 50);
LeftButton.setBounds(718, 391, 50, 50);
UpButton.setBounds(774, 335, 50, 50);
DownButton.setBounds(774, 391, 50, 50);
comboBoxStrategy.setBounds(719, 12, 151, 28);
buttonStep.setBounds(768, 46, 94, 29);
canv = new Canvas();
canv.setSize(pictureBoxWidth, pictureBoxHeight);
CatamaranFrame.add(canv);
CatamaranFrame.add(comboBoxStrategy);
CatamaranFrame.add(UpButton);
CatamaranFrame.add(DownButton);
CatamaranFrame.add(LeftButton);
CatamaranFrame.add(RightButton);
CatamaranFrame.add(CreateButton);
CatamaranFrame.add(CreateButtonPro);
CatamaranFrame.add(buttonStep);
CatamaranFrame.setVisible(true);
}
}
class Canvas extends JComponent {
public DrawningCatamaran DrawningCatamaran;
public Canvas() {
}
public void paintComponent(Graphics g) {
if (DrawningCatamaran == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
DrawningCatamaran.DrawnCatamaran(g2d);
super.repaint();
2023-12-01 20:37:58 +04:00
}
}