PIbd21.LyovushkinaA.A.Conta.../FormContainerShip.java

231 lines
9.0 KiB
Java
Raw Normal View History

2023-09-25 18:40:25 +04:00
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
2023-10-24 01:06:14 +04:00
public class FormContainerShip{
public DrawingShip _drawingShip;
2023-10-09 23:11:13 +04:00
private AbstractStrategy _abstractStrategy;
2023-09-25 18:40:25 +04:00
Canvas canv;
2023-10-24 01:06:14 +04:00
class Canvas extends JComponent{
public DrawingShip _drawingShip;
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingShip == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingShip.DrawShip(g2d);
super.repaint();
}
}
public DrawingShip SelectedShip;
public boolean DialogResult = false;
public JButton buttonSelectedShip;
public JFrame w;
2023-09-25 18:40:25 +04:00
public void Draw(){
canv.repaint();
}
2023-10-09 23:11:13 +04:00
public FormContainerShip(){
2023-10-24 01:06:14 +04:00
SelectedShip = null;
w = new JFrame ("ContainerShip");
2023-10-09 23:11:13 +04:00
JButton buttonCreateShip = new JButton("создать кораблик");
JButton buttonCreateContainerShip = new JButton("создать контейнеровоз");
JButton buttonStep = new JButton("шаг");
2023-10-24 01:06:14 +04:00
buttonSelectedShip = new JButton("выбрать");
2023-10-09 23:11:13 +04:00
String[] items = {
"MoveToCenter", "MoveToRight-Down"
};
JComboBox comboBoxStrategy = new JComboBox(items);
comboBoxStrategy.setEditable(false);
2023-09-25 18:40:25 +04:00
JButton up = new JButton();
up.setBorderPainted(false);
up.setFocusPainted(false);
up.setContentAreaFilled(false);
up.setName("up");
2023-10-07 21:42:37 +04:00
up.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo11.png"));
2023-09-25 18:40:25 +04:00
JButton down = new JButton();
down.setBorderPainted(false);
down.setFocusPainted(false);
down.setContentAreaFilled(false);
down.setName("down");
2023-10-07 21:42:37 +04:00
down.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo33.png"));
2023-09-25 18:40:25 +04:00
JButton left = new JButton();
left.setBorderPainted(false);
left.setFocusPainted(false);
left.setContentAreaFilled(false);
left.setName("left");
2023-10-07 21:42:37 +04:00
left.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo44.png"));
2023-09-25 18:40:25 +04:00
JButton right = new JButton();
right.setBorderPainted(false);
right.setFocusPainted(false);
right.setContentAreaFilled(false);
right.setName("right");
2023-10-07 21:42:37 +04:00
right.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo22.png"));
2023-10-09 23:11:13 +04:00
buttonCreateShip.addActionListener(
2023-09-25 18:40:25 +04:00
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
2023-10-24 01:06:14 +04:00
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE);
if(selectedColor != null){
color = selectedColor;
}
2023-10-09 23:11:13 +04:00
_drawingShip = new DrawingShip(random.nextInt(100, 300),
random.nextInt(1000, 3000),
2023-10-24 01:06:14 +04:00
color,960, 560);
2023-10-09 23:11:13 +04:00
_drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100));
canv._drawingShip = _drawingShip;
Draw();
}
}
);
buttonCreateContainerShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
2023-10-24 01:06:14 +04:00
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE);
if(selectedColor != null){
color = selectedColor;
}
Color color2 = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor2 = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE);
if(selectedColor2 != null){
color2 = selectedColor2;
}
2023-10-09 23:11:13 +04:00
_drawingShip = new DrawingContainerShip(random.nextInt(100, 300),
2023-09-25 18:40:25 +04:00
random.nextInt(1000, 3000),
2023-10-24 01:06:14 +04:00
color,
color2,
2023-10-09 23:11:13 +04:00
random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560);
2023-09-25 18:40:25 +04:00
_drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100));
canv._drawingShip = _drawingShip;
Draw();
}
}
);
2023-10-09 23:11:13 +04:00
buttonStep.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if (_drawingShip == 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
DrawningObjectShip(_drawingShip), 960,
560);
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
}
}
);
2023-09-25 18:40:25 +04:00
ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingShip == null)
{
return;
}
switch(((JButton)(e.getSource())).getName()){
case "up":
_drawingShip.MoveTransport(Direction.Up);
break;
case "down":
_drawingShip.MoveTransport(Direction.Down);
break;
case "left":
_drawingShip.MoveTransport(Direction.Left);
break;
case "right":
_drawingShip.MoveTransport(Direction.Right);
break;
}
Draw();
}
};
up.addActionListener(actioListener);
down.addActionListener(actioListener);
left.addActionListener(actioListener);
right.addActionListener(actioListener);
2023-10-09 23:11:13 +04:00
2023-09-25 18:40:25 +04:00
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 1000, 600);
2023-10-09 23:11:13 +04:00
buttonCreateShip.setBounds(2, 540, 200, 20);
buttonCreateContainerShip.setBounds(215, 540, 200, 20);
2023-10-24 01:06:14 +04:00
buttonSelectedShip.setBounds(550, 540, 150, 20);
2023-10-09 23:11:13 +04:00
comboBoxStrategy.setBounds(800, 10, 150, 30);
buttonStep.setBounds(800, 45, 100, 20);
2023-09-25 18:40:25 +04:00
up.setBounds(900, 480, 45, 45);
down.setBounds(900, 520, 45, 45);
left.setBounds(860, 520, 45, 45);
right.setBounds(940, 520, 45, 45);
w.add(canv);
2023-10-09 23:11:13 +04:00
w.add(buttonCreateShip);
w.add(buttonCreateContainerShip);
2023-10-24 01:06:14 +04:00
w.add(buttonSelectedShip);
2023-10-09 23:11:13 +04:00
w.add(comboBoxStrategy);
w.add(buttonStep);
2023-09-25 18:40:25 +04:00
w.add(up);
w.add(down);
w.add(left);
w.add(right);
w.setVisible (true);
}
}
class Canvas extends JComponent{
2023-10-09 23:11:13 +04:00
public DrawingShip _drawingShip;
public Canvas(){
2023-09-25 18:40:25 +04:00
}
public void paintComponent (Graphics g){
if (_drawingShip == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingShip.DrawShip(g2d);
super.repaint();
}
}