225 lines
9.6 KiB
Java
225 lines
9.6 KiB
Java
package src;
|
||
import src.DrawningObjects.DrawningAirplane;
|
||
import src.MovementStrategy.*;
|
||
import src.DrawningObjects.DrawningAirplaneWithRadar;
|
||
import org.w3c.dom.ranges.DocumentRange;
|
||
|
||
import javax.swing.*;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
import java.util.Random;
|
||
|
||
public class FormAirplaneWithRadar {
|
||
private DrawningAirplane DrawningAirplane;
|
||
private AbstractStrategy _abstractStrategy;
|
||
Canvas canv;
|
||
static int pictureBoxWidth = 882;
|
||
static int pictureBoxHeight = 453;
|
||
public JButton buttonSelect;
|
||
public JFrame AirplaneFrame;
|
||
public void Draw(){
|
||
canv.repaint();
|
||
}
|
||
public Color ChooseColor(JFrame MonorailFrame){
|
||
JColorChooser dialog = new JColorChooser();
|
||
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.WHITE);
|
||
return res;
|
||
}
|
||
public DrawningAirplane SelectedAirplane(){
|
||
return DrawningAirplane;
|
||
}
|
||
public void ChangeAirplane(DrawningAirplane newAirplane){
|
||
newAirplane.SetPosition(0,0);
|
||
DrawningAirplane = newAirplane;
|
||
canv.DrawningAirplane = DrawningAirplane;
|
||
|
||
}
|
||
public FormAirplaneWithRadar(){
|
||
AirplaneFrame =new JFrame ();
|
||
JButton buttonCreate = new JButton("Создать");
|
||
JButton buttonCreateLocomotive = new JButton("Создать самолет с радаром");
|
||
JButton buttonStep = new JButton("Шаг");
|
||
buttonSelect = new JButton ("Выбрать");
|
||
JComboBox comboBoxStrategy = new JComboBox(
|
||
new String[]{
|
||
"Довести до центра",
|
||
"Довести до края",
|
||
});
|
||
JButton UpButton = new JButton();
|
||
UpButton.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\улгту\\2 курс\\РПП\\PIbd-23_Nasyrov_A_G_AirplaneWithRadar_hard\\src\\UpButton.jpg"));
|
||
JButton DownButton = new JButton();
|
||
DownButton.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\улгту\\2 курс\\РПП\\PIbd-23_Nasyrov_A_G_AirplaneWithRadar_hard\\src\\DownButton.jpg"));
|
||
JButton LeftButton = new JButton();
|
||
LeftButton.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\улгту\\2 курс\\РПП\\PIbd-23_Nasyrov_A_G_AirplaneWithRadar_hard\\src\\LeftButton.jpg"));
|
||
JButton RightButton = new JButton();
|
||
RightButton.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\улгту\\2 курс\\РПП\\PIbd-23_Nasyrov_A_G_AirplaneWithRadar_hard\\src\\RightButton.jpg"));
|
||
buttonStep.addActionListener(
|
||
new ActionListener() {
|
||
public void actionPerformed(ActionEvent e){
|
||
if (DrawningAirplane == 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
|
||
DrawningObjectAirplane(DrawningAirplane), 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();
|
||
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||
Color choosen = ChooseColor(AirplaneFrame);
|
||
if(choosen != null){
|
||
color = choosen;
|
||
}
|
||
DrawningAirplane = new DrawningAirplane(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||
color,
|
||
pictureBoxWidth, pictureBoxHeight);
|
||
canv.DrawningAirplane = DrawningAirplane;
|
||
comboBoxStrategy.enable(true);
|
||
Draw();
|
||
}
|
||
}
|
||
);
|
||
buttonCreateLocomotive.addActionListener(
|
||
new ActionListener() {
|
||
public void actionPerformed(ActionEvent e){
|
||
Random random = new Random();
|
||
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||
Color choosen = ChooseColor(AirplaneFrame);
|
||
if(choosen != null){
|
||
color = choosen;
|
||
}
|
||
choosen = ChooseColor(AirplaneFrame);
|
||
if(choosen != null){
|
||
additionalColor = choosen;
|
||
}
|
||
DrawningAirplane = new DrawningAirplaneWithRadar(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||
color,
|
||
additionalColor,
|
||
random.nextBoolean(), random.nextBoolean(),
|
||
pictureBoxWidth, pictureBoxHeight);
|
||
canv.DrawningAirplane = DrawningAirplane;
|
||
comboBoxStrategy.enable(true);
|
||
Draw();
|
||
}
|
||
}
|
||
);
|
||
RightButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(DrawningAirplane.EntityAirplane() == null) {
|
||
return;
|
||
}
|
||
DrawningAirplane.MoveTransport(DirectionType.Right);
|
||
Draw();
|
||
}
|
||
});
|
||
LeftButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(DrawningAirplane.EntityAirplane() == null)
|
||
return;
|
||
DrawningAirplane.MoveTransport(DirectionType.Left);
|
||
Draw();
|
||
}
|
||
});
|
||
UpButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(DrawningAirplane.EntityAirplane() == null)
|
||
return;
|
||
DrawningAirplane.MoveTransport(DirectionType.Up);
|
||
Draw();
|
||
}
|
||
});
|
||
DownButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(DrawningAirplane.EntityAirplane() == null)
|
||
return;
|
||
DrawningAirplane.MoveTransport(DirectionType.Down);
|
||
Draw();
|
||
}
|
||
});
|
||
|
||
AirplaneFrame.setSize (900, 500);
|
||
AirplaneFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
|
||
AirplaneFrame.setLayout(null);
|
||
canv = new Canvas();
|
||
canv.setSize(pictureBoxWidth, pictureBoxHeight);
|
||
buttonSelect.setBounds(383,401, 180, 40);
|
||
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);
|
||
AirplaneFrame.add(canv);
|
||
AirplaneFrame.add(buttonCreate);
|
||
AirplaneFrame.add(buttonCreateLocomotive);
|
||
AirplaneFrame.add(UpButton);
|
||
AirplaneFrame.add(DownButton);
|
||
AirplaneFrame.add(LeftButton);
|
||
AirplaneFrame.add(RightButton);
|
||
AirplaneFrame.add(comboBoxStrategy);
|
||
AirplaneFrame.add(buttonStep);
|
||
AirplaneFrame.add(buttonSelect);
|
||
AirplaneFrame.setVisible(true);
|
||
}
|
||
}
|
||
class Canvas extends JComponent{
|
||
public DrawningAirplane DrawningAirplane;
|
||
public Canvas(){
|
||
}
|
||
public void paintComponent (Graphics g){
|
||
if (DrawningAirplane == null){
|
||
return;
|
||
}
|
||
super.paintComponents (g) ;
|
||
Graphics2D g2d = (Graphics2D)g;
|
||
DrawningAirplane.DrawAirplane(g2d);
|
||
super.repaint();
|
||
}
|
||
}
|
||
|