PIbd-21_Kouvshinoff_T._A._W.../laba1Loco/FormTrain.java

204 lines
8.1 KiB
Java

package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class FormTrain{
private DrawingTrain _drawingTrain;
private AbstractStrategy abstractStrategy;
Canvas canv;
static int pictureBoxWidth = 980;
static int pictureBoxHeight = 580;
public void Draw(){
canv.repaint();
}
public FormTrain(){
JFrame w=new JFrame ("Loco");
JButton buttonCreate = new JButton("create");
JButton buttonCreateLocomotive = new JButton("createLocomotive");
JButton buttonStrategysStep = new JButton("strategys step");
JComboBox comboBoxStrategy = new JComboBox(
new String[]{
"к центру",
"к краю",
});
JButton up = new JButton();
up.setBorderPainted(false);
up.setFocusPainted(false);
up.setContentAreaFilled(false);
up.setName("up");
up.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowUp340x259.png"));
JButton down = new JButton();
down.setBorderPainted(false);
down.setFocusPainted(false);
down.setContentAreaFilled(false);
down.setName("down");
down.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowDown340x259.png"));
JButton left = new JButton();
left.setBorderPainted(false);
left.setFocusPainted(false);
left.setContentAreaFilled(false);
left.setName("left");
left.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowL340x259.png"));
JButton right = new JButton();
right.setBorderPainted(false);
right.setFocusPainted(false);
right.setContentAreaFilled(false);
right.setName("right");
right.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowR340x259.png"));
buttonStrategysStep.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (_drawingTrain == 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
DrawningObjectTrain(_drawingTrain), pictureBoxWidth,
pictureBoxHeight);
comboBoxStrategy.setEnabled(false);
}
if (abstractStrategy == null)
{
return;
}
abstractStrategy.MakeStep();
Draw();
if (abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
}
}
);
buttonCreateLocomotive.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingTrain = new DrawingLoco(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextInt(2, 5),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextInt(0, 2) == 1,
random.nextInt(0, 2) == 1,
random.nextInt(0, 2) == 1,
pictureBoxWidth,
pictureBoxHeight);
_drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingTrain = _drawingTrain;
Draw();
}
}
);
buttonCreate.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingTrain = new DrawingTrain(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextInt(2, 5),
pictureBoxWidth,
pictureBoxHeight);
_drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingTrain = _drawingTrain;
Draw();
}
}
);
ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingTrain == null)
{
return;
}
switch(((JButton)(e.getSource())).getName()){
case "up":
_drawingTrain.MoveTransport(Direction.Up);
break;
case "down":
_drawingTrain.MoveTransport(Direction.Down);
break;
case "left":
_drawingTrain.MoveTransport(Direction.Left);
break;
case "right":
_drawingTrain.MoveTransport(Direction.Right);
break;
}
Draw();
}
};
up.addActionListener(actioListener);
down.addActionListener(actioListener);
left.addActionListener(actioListener);
right.addActionListener(actioListener);
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
buttonCreate.setBounds(2, 540, 100, 20);
buttonCreateLocomotive.setBounds(105, 540, 150, 20);
up.setBounds(900, 480, 40, 40);
down.setBounds(900, 520, 40, 40);
left.setBounds(860, 520, 40, 40);
right.setBounds(940, 520, 40, 40);
comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20);
buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20);
w.add(canv);
w.add(buttonCreate);
w.add(buttonCreateLocomotive);
w.add(up);
w.add(down);
w.add(left);
w.add(right);
w.add(comboBoxStrategy);
w.add(buttonStrategysStep);
}
}
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();
}
}