One More Commit

This commit is contained in:
gg12 darfren 2023-10-25 23:53:45 +04:00
parent d8f83dcb7d
commit bc5671ff30

View File

@ -0,0 +1,196 @@
package MonorailHard;
import MonorailHard.DrawningObjects.DrawningLocomotive;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.MovementStrategy.*;
import javax.swing.*;
import javax.swing.text.AbstractDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormMonorail{
private DrawningMonorail DrawningMonorail;
private AbstractStrategy _abstractStrategy;
Canvas canv;
static int pictureBoxWidth = 882;
static int pictureBoxHeight = 453;
public void Draw(){
canv.repaint();
}
public FormMonorail(){
JFrame MonorailFrame =new JFrame ();
JButton buttonCreate = new JButton("Создать");
JButton buttonCreateLocomotive = new JButton("Создать локомотив");
JButton buttonStep = new JButton("Шаг");
JComboBox comboBoxStrategy = new JComboBox(
new String[]{
"Довести до центра",
"Довести до края",
});
JButton UpButton = new JButton();
UpButton.setIcon(new ImageIcon("C:\\Users\\frenk\\IdeaProjects\\PIbd-13-Salin-O.A.-Monorail-Hard\\src\\UpButton.png"));
JButton DownButton = new JButton();
DownButton.setIcon(new ImageIcon("C:\\Users\\frenk\\IdeaProjects\\PIbd-13-Salin-O.A.-Monorail-Hard\\src\\DownButton.png"));
JButton LeftButton = new JButton();
LeftButton.setIcon(new ImageIcon("C:\\Users\\frenk\\IdeaProjects\\PIbd-13-Salin-O.A.-Monorail-Hard\\src\\LeftButton.png"));
JButton RightButton = new JButton();
RightButton.setIcon(new ImageIcon("C:\\Users\\frenk\\IdeaProjects\\PIbd-13-Salin-O.A.-Monorail-Hard\\src\\RightButton.png"));
buttonStep.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (DrawningMonorail == 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
DrawningObjectMonorail(DrawningMonorail), pictureBoxWidth,
pictureBoxHeight);
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
}
}
);
buttonCreate.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
DrawningMonorail = new DrawningMonorail(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
pictureBoxWidth, pictureBoxHeight);
canv.DrawningMonorail = DrawningMonorail;
comboBoxStrategy.enable(true);
Draw();
}
}
);
buttonCreateLocomotive.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Random random = new Random();
DrawningMonorail = new DrawningLocomotive(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
random.nextInt(1, 6),
pictureBoxWidth, pictureBoxHeight, random.nextBoolean(), random.nextBoolean(),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)));
canv.DrawningMonorail = DrawningMonorail;
comboBoxStrategy.enable(true);
Draw();
}
}
);
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningMonorail.EntityMonorail() == null) {
return;
}
DrawningMonorail.MoveTransport(DirectionType.Right);
Draw();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningMonorail.EntityMonorail() == null)
return;
DrawningMonorail.MoveTransport(DirectionType.Left);
Draw();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningMonorail.EntityMonorail() == null)
return;
DrawningMonorail.MoveTransport(DirectionType.Up);
Draw();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningMonorail.EntityMonorail() == null)
return;
DrawningMonorail.MoveTransport(DirectionType.Down);
Draw();
}
});
MonorailFrame.setSize (900, 500);
MonorailFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
MonorailFrame.setLayout(null);
canv = new Canvas();
canv.setSize(pictureBoxWidth, pictureBoxHeight);
buttonCreate.setBounds(198, 401, 180, 40);
buttonCreateLocomotive.setBounds(12, 401, 180, 40);
RightButton.setBounds(840,411,30,30);
LeftButton.setBounds(768,411,30,30);
UpButton.setBounds(804,375,30,30);
DownButton.setBounds(804,411,30,30);
comboBoxStrategy.setBounds(719,12,151,28);
buttonStep.setBounds(768, 46, 94, 29);
MonorailFrame.add(canv);
MonorailFrame.add(buttonCreate);
MonorailFrame.add(buttonCreateLocomotive);
MonorailFrame.add(UpButton);
MonorailFrame.add(DownButton);
MonorailFrame.add(LeftButton);
MonorailFrame.add(RightButton);
MonorailFrame.add(comboBoxStrategy);
MonorailFrame.add(buttonStep);
MonorailFrame.setVisible(true);
}
}
class Canvas extends JComponent{
public DrawningMonorail DrawningMonorail;
public Canvas(){
}
public void paintComponent (Graphics g){
if (DrawningMonorail == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
DrawningMonorail.DrawMonorail(g2d);
super.repaint();
}
}