Лабораторная работа 3. Базовая часть
This commit is contained in:
parent
f1cd73f9c4
commit
797641613b
@ -13,9 +13,17 @@ public class FormAirFighter extends JFrame implements ComponentListener{
|
|||||||
private JButton buttonDown;
|
private JButton buttonDown;
|
||||||
private JButton buttonLeft;
|
private JButton buttonLeft;
|
||||||
private JButton buttonRight;
|
private JButton buttonRight;
|
||||||
|
private JLabel labelBodyColor;
|
||||||
|
private JLabel labelSpeed;
|
||||||
|
private JLabel labelWeight;
|
||||||
private String item = "2";
|
private String item = "2";
|
||||||
private String itemEngine = "Классический";
|
private String itemEngine = "Классический";
|
||||||
private DrawningAirFighter _airFighter;
|
private DrawningAirFighter _airFighter = null;
|
||||||
|
// Выбранный объект
|
||||||
|
private DrawningAirFighter SelectedAirFighter = null;
|
||||||
|
public DrawningAirFighter GetSelectedAirFighter(){
|
||||||
|
return SelectedAirFighter;
|
||||||
|
}
|
||||||
|
|
||||||
// Слушатель кнопки направления
|
// Слушатель кнопки направления
|
||||||
private ActionListener actionListenerDirection = new ActionListener() {
|
private ActionListener actionListenerDirection = new ActionListener() {
|
||||||
@ -36,6 +44,13 @@ public class FormAirFighter extends JFrame implements ComponentListener{
|
|||||||
panelForDrawing.repaint();
|
panelForDrawing.repaint();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
private void SetData(){
|
||||||
|
_airFighter.SetPosition((int)(Math.random() * 100 + 10), (int)(Math.random() * 100 + 10), panelForDrawing.getWidth(), panelForDrawing.getHeight());
|
||||||
|
labelSpeed.setText("Скорость: " + _airFighter.AirFighter.getSpeed());
|
||||||
|
labelWeight.setText("Вес: " + _airFighter.AirFighter.getWeight());
|
||||||
|
labelBodyColor.setText("Цвет: " + _airFighter.AirFighter.getBodyColor());
|
||||||
|
panelForDrawing.repaint();
|
||||||
|
}
|
||||||
public FormAirFighter(){
|
public FormAirFighter(){
|
||||||
super("Военный самолет");
|
super("Военный самолет");
|
||||||
// Panel для отрисовки
|
// Panel для отрисовки
|
||||||
@ -48,19 +63,19 @@ public class FormAirFighter extends JFrame implements ComponentListener{
|
|||||||
}catch (Exception e){}
|
}catch (Exception e){}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
panelForDrawing.setLayout(new BorderLayout());
|
panelForDrawing.setLayout(null);
|
||||||
panelForDrawing.setSize(getWidth(), getHeight());
|
panelForDrawing.setSize(getWidth(), getHeight());
|
||||||
setContentPane(panelForDrawing);
|
add(panelForDrawing);
|
||||||
|
|
||||||
// statusLabel
|
// statusLabel
|
||||||
JLabel labelSpeed = new JLabel("Скорость:");
|
labelSpeed = new JLabel("Скорость:");
|
||||||
JLabel labelWeight = new JLabel("Вес:");
|
labelWeight = new JLabel("Вес:");
|
||||||
JLabel labelBodyColor = new JLabel("Цвет:");
|
labelBodyColor = new JLabel("Цвет:");
|
||||||
statusLabel.add(labelSpeed);
|
statusLabel.add(labelSpeed);
|
||||||
statusLabel.add(labelWeight);
|
statusLabel.add(labelWeight);
|
||||||
statusLabel.add(labelBodyColor);
|
statusLabel.add(labelBodyColor);
|
||||||
statusLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
|
statusLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
|
||||||
statusLabel.setSize(new Dimension(getWidth(), 30));
|
statusLabel.setSize(new Dimension(800, 30));
|
||||||
// Кнопка создания
|
// Кнопка создания
|
||||||
JButton buttonCreate = new JButton("Создать");
|
JButton buttonCreate = new JButton("Создать");
|
||||||
buttonCreate.setSize(100, 25);
|
buttonCreate.setSize(100, 25);
|
||||||
@ -69,13 +84,37 @@ public class FormAirFighter extends JFrame implements ComponentListener{
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
_airFighter = new DrawningAirFighter((int)(Math.random() * 300 + 200),(int)(Math.random() * 3000 + 2000), new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)), Integer.parseInt(item), itemEngine);
|
_airFighter = new DrawningAirFighter((int)(Math.random() * 300 + 200),(int)(Math.random() * 3000 + 2000), new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)), Integer.parseInt(item), itemEngine);
|
||||||
_airFighter.SetPosition((int)(Math.random() * 100 + 10), (int)(Math.random() * 100 + 10), panelForDrawing.getWidth(), panelForDrawing.getHeight());
|
SetData();
|
||||||
labelSpeed.setText("Скорость: " + _airFighter.AirFighter.getSpeed());
|
|
||||||
labelWeight.setText("Вес: " + _airFighter.AirFighter.getWeight());
|
|
||||||
labelBodyColor.setText("Цвет: " + _airFighter.AirFighter.getBodyColor());
|
|
||||||
panelForDrawing.repaint();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// Кнопка модификации
|
||||||
|
JButton buttonModific = new JButton("Модифицировать");
|
||||||
|
buttonModific.setSize(150, 25);
|
||||||
|
buttonModific.setLayout(new FlowLayout(FlowLayout.LEFT));
|
||||||
|
buttonModific.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
_airFighter = new DrawningUpgradeAirFighter((int)(Math.random() * 300 + 200),(int)(Math.random() * 3000 + 2000), new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)),
|
||||||
|
new Color((int)(Math.random() * 255), (int)(Math.random()) * 255, (int)(Math.random() * 255)), ((int)(Math.random() * 2)) == 1, ((int)(Math.random() * 2)) == 1, Integer.parseInt(item), itemEngine);
|
||||||
|
SetData();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
buttonModific.setBounds(50, 0, 150, 25);
|
||||||
|
panelForDrawing.add(buttonModific);
|
||||||
|
// Кнопка выбора объекта
|
||||||
|
JButton buttonSelected = new JButton("Выбрать");
|
||||||
|
buttonSelected.setSize(100, 25);
|
||||||
|
buttonSelected.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
SelectedAirFighter = _airFighter;
|
||||||
|
dispatchEvent(new WindowEvent(FormAirFighter.this, WindowEvent.WINDOW_CLOSING));
|
||||||
|
dispose();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
buttonSelected.setBounds(200, 0, 100, 25);
|
||||||
|
panelForDrawing.add(buttonSelected);
|
||||||
|
|
||||||
// Контейнер кнопок и панели с информацией
|
// Контейнер кнопок и панели с информацией
|
||||||
Container container = new Container();
|
Container container = new Container();
|
||||||
container.setLayout(new GridBagLayout());
|
container.setLayout(new GridBagLayout());
|
||||||
@ -110,8 +149,8 @@ public class FormAirFighter extends JFrame implements ComponentListener{
|
|||||||
item = (String)box.getSelectedItem();
|
item = (String)box.getSelectedItem();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
editComboBox.setEditable(true);
|
editComboBox.setBounds(0, 0, 50, 25);
|
||||||
container.add(editComboBox, constraints);
|
panelForDrawing.add(editComboBox);
|
||||||
// Контейнер с кнопками движения
|
// Контейнер с кнопками движения
|
||||||
Container containerDirection = new Container();
|
Container containerDirection = new Container();
|
||||||
containerDirection.setPreferredSize(new Dimension(120, 70));
|
containerDirection.setPreferredSize(new Dimension(120, 70));
|
||||||
@ -183,7 +222,7 @@ public class FormAirFighter extends JFrame implements ComponentListener{
|
|||||||
}catch (IOException e){}
|
}catch (IOException e){}
|
||||||
|
|
||||||
constraints.anchor = GridBagConstraints.EAST;
|
constraints.anchor = GridBagConstraints.EAST;
|
||||||
constraints.gridx = 1;
|
constraints.gridx = 5;
|
||||||
constraints.gridy = 0;
|
constraints.gridy = 0;
|
||||||
container.add(containerDirection, constraints);
|
container.add(containerDirection, constraints);
|
||||||
|
|
||||||
|
@ -62,7 +62,6 @@ public class FormMap extends JFrame{
|
|||||||
public FormMap(){
|
public FormMap(){
|
||||||
super("Военный самолет");
|
super("Военный самолет");
|
||||||
//Panel для отрисовки
|
//Panel для отрисовки
|
||||||
//panelForDrawing.setLayout(new BorderLayout());
|
|
||||||
panelForDrawing.setLayout(null);
|
panelForDrawing.setLayout(null);
|
||||||
panelForDrawing.setPreferredSize(new Dimension(getWidth(), getHeight()));
|
panelForDrawing.setPreferredSize(new Dimension(getWidth(), getHeight()));
|
||||||
panelForDrawing.setBounds(0, 0, getWidth(), getHeight());
|
panelForDrawing.setBounds(0, 0, getWidth(), getHeight());
|
||||||
|
272
AirFighter/src/FormMapWithSetAirFighters.java
Normal file
272
AirFighter/src/FormMapWithSetAirFighters.java
Normal file
@ -0,0 +1,272 @@
|
|||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class FormMapWithSetAirFighters extends JFrame {
|
||||||
|
private JLabel panelForDrawing;
|
||||||
|
private JButton buttonUp;
|
||||||
|
private JButton buttonDown;
|
||||||
|
private JButton buttonLeft;
|
||||||
|
private JButton buttonRight;
|
||||||
|
private MapWithSetAirFightersGeneric<DrawningObjectAirFighter, AbstractMap> _mapAirFightersCollectionGeneric;
|
||||||
|
private ActionListener actionListenerDirection = new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JButton tempButton = (JButton)e.getSource();
|
||||||
|
Direction dir = null;
|
||||||
|
if (buttonUp.equals(tempButton)) {
|
||||||
|
dir = Direction.Up;
|
||||||
|
} else if (buttonDown.equals(tempButton)) {
|
||||||
|
dir = Direction.Down;
|
||||||
|
} else if (buttonLeft.equals(tempButton)) {
|
||||||
|
dir = Direction.Left;
|
||||||
|
} else if (buttonRight.equals(tempButton)) {
|
||||||
|
dir = Direction.Right;
|
||||||
|
}
|
||||||
|
panelForDrawing.setIcon(new ImageIcon(_mapAirFightersCollectionGeneric.MoveObject(dir)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private FormMapWithSetAirFighters(){
|
||||||
|
super("Хранилище объектов");
|
||||||
|
setLayout(null);
|
||||||
|
// Панелька для отрисовки
|
||||||
|
panelForDrawing = new JLabel();
|
||||||
|
panelForDrawing.setBounds(0, 0, 800, 700);
|
||||||
|
add(panelForDrawing);
|
||||||
|
// Панель инструментов
|
||||||
|
JPanel panelTools = new JPanel();
|
||||||
|
panelTools.setBounds(800, 0, 200, 700);
|
||||||
|
panelTools.setLayout(null);
|
||||||
|
// ComboBox выбора карты
|
||||||
|
String[] maps = {
|
||||||
|
"Простая карта",
|
||||||
|
"Карта шторма"
|
||||||
|
};
|
||||||
|
JComboBox mapComboBox = new JComboBox(maps);
|
||||||
|
mapComboBox.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JComboBox box = (JComboBox)e.getSource();
|
||||||
|
AbstractMap map = null;
|
||||||
|
switch ((String)box.getSelectedItem()){
|
||||||
|
case "Простая карта":
|
||||||
|
map = new SimpleMap();
|
||||||
|
break;
|
||||||
|
case "Карта шторма":
|
||||||
|
map = new StormMap();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
map = new SimpleMap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_mapAirFightersCollectionGeneric = new
|
||||||
|
MapWithSetAirFightersGeneric<DrawningObjectAirFighter, AbstractMap>(
|
||||||
|
panelForDrawing.getWidth(), panelForDrawing.getHeight(), map);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mapComboBox.setBounds(0, 10, 160, 30);
|
||||||
|
mapComboBox.setSelectedIndex(0);
|
||||||
|
panelTools.add(mapComboBox);
|
||||||
|
// Кнопка добавления
|
||||||
|
JButton buttonAdd = new JButton("Добавить объект");
|
||||||
|
buttonAdd.setBounds(0, 50, 160, 30);
|
||||||
|
buttonAdd.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_mapAirFightersCollectionGeneric == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FormAirFighter form = new FormAirFighter();
|
||||||
|
form.setSize(500, 500);
|
||||||
|
form.setVisible(true);
|
||||||
|
form.addWindowListener(new WindowListener() {
|
||||||
|
@Override
|
||||||
|
public void windowOpened(WindowEvent e) { }
|
||||||
|
@Override
|
||||||
|
public void windowClosing(WindowEvent e) {
|
||||||
|
int confirm = JOptionPane.showOptionDialog(form,
|
||||||
|
"Вы точно хотите выбрать этот элемент?",
|
||||||
|
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
|
||||||
|
JOptionPane.QUESTION_MESSAGE, null, null, null);
|
||||||
|
if (confirm == JOptionPane.YES_OPTION){
|
||||||
|
if(form.GetSelectedAirFighter() == null) {
|
||||||
|
JOptionPane.showMessageDialog(panelTools, "Объект не удалось добавить");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
DrawningObjectAirFighter airFighter = new DrawningObjectAirFighter(form.GetSelectedAirFighter());
|
||||||
|
if (_mapAirFightersCollectionGeneric.Add(airFighter) == 0)
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(panelTools, "Объект добавлен");
|
||||||
|
panelForDrawing.setIcon(new ImageIcon(_mapAirFightersCollectionGeneric.ShowSet()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void windowClosed(WindowEvent e) {}
|
||||||
|
@Override
|
||||||
|
public void windowIconified(WindowEvent e) {}
|
||||||
|
@Override
|
||||||
|
public void windowDeiconified(WindowEvent e) {}
|
||||||
|
@Override
|
||||||
|
public void windowActivated(WindowEvent e) {}
|
||||||
|
@Override
|
||||||
|
public void windowDeactivated(WindowEvent e) {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelTools.add(buttonAdd);
|
||||||
|
// Текстовое поля для получения индекса
|
||||||
|
JTextField textFieldIndex = new JTextField();
|
||||||
|
textFieldIndex.setBounds(0, 90, 160, 30);
|
||||||
|
panelTools.add(textFieldIndex);
|
||||||
|
// Кнопка удаления
|
||||||
|
// Кнопка добавления
|
||||||
|
JButton buttonRemove = new JButton("Удалить объект");
|
||||||
|
buttonRemove.setBounds(0, 130, 160, 30);
|
||||||
|
buttonRemove.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
try {
|
||||||
|
if (textFieldIndex.getText().isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int confirm = JOptionPane.showOptionDialog(FormMapWithSetAirFighters.this,
|
||||||
|
"Вы точно хотите удалить объект?",
|
||||||
|
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
|
||||||
|
JOptionPane.QUESTION_MESSAGE, null, null, null);
|
||||||
|
if (confirm == JOptionPane.NO_OPTION) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int pos = Integer.parseInt(textFieldIndex.getText());
|
||||||
|
if (_mapAirFightersCollectionGeneric.Remove(pos) != null) {
|
||||||
|
JOptionPane.showMessageDialog(panelTools, "Объект удален");
|
||||||
|
panelForDrawing.setIcon(new ImageIcon(_mapAirFightersCollectionGeneric.ShowSet()));
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(panelTools, "Не удалось удалить объект");
|
||||||
|
}
|
||||||
|
}catch (Exception ex){
|
||||||
|
JOptionPane.showMessageDialog(panelTools, "Неккоректный ввод");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelTools.add(buttonRemove);
|
||||||
|
// Кнопка перехода в хранилище
|
||||||
|
JButton buttonShowStorage = new JButton("Посмотреть хранилище");
|
||||||
|
buttonShowStorage.setBounds(0, 170, 160, 30);
|
||||||
|
buttonShowStorage.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_mapAirFightersCollectionGeneric == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
panelForDrawing.setIcon(new ImageIcon(_mapAirFightersCollectionGeneric.ShowSet()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelTools.add(buttonShowStorage);
|
||||||
|
// Кнопка перехода на карту
|
||||||
|
JButton buttonShowOnMap = new JButton("Посмотреть карту");
|
||||||
|
buttonShowOnMap.setBounds(0, 210, 160, 30);
|
||||||
|
buttonShowOnMap.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_mapAirFightersCollectionGeneric == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
panelForDrawing.setIcon(new ImageIcon(_mapAirFightersCollectionGeneric.ShowOnMap()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelTools.add(buttonShowOnMap);
|
||||||
|
// Контейнер с кнопками движения
|
||||||
|
Container containerDirection = new Container();
|
||||||
|
containerDirection.setPreferredSize(new Dimension(120, 70));
|
||||||
|
containerDirection.setLayout(new GridBagLayout());
|
||||||
|
GridBagConstraints constraintsDirection = new GridBagConstraints();
|
||||||
|
constraintsDirection.weightx = 40;
|
||||||
|
constraintsDirection.weighty = 40;
|
||||||
|
constraintsDirection.gridwidth = 2;
|
||||||
|
constraintsDirection.gridheight = 1;
|
||||||
|
// Кнопка Up
|
||||||
|
try {
|
||||||
|
BufferedImage bufferedImage = ImageIO.read(new File("AirFighter/Images/Up.png"));
|
||||||
|
Image imageUp = bufferedImage.getScaledInstance(30, 30, Image.SCALE_DEFAULT);
|
||||||
|
ImageIcon iconUp = new ImageIcon(imageUp);
|
||||||
|
buttonUp = new JButton(iconUp);
|
||||||
|
buttonUp.setPreferredSize(new Dimension(30, 30));
|
||||||
|
buttonUp.setContentAreaFilled(false);
|
||||||
|
buttonUp.setBorderPainted(false);
|
||||||
|
buttonUp.setActionCommand(Direction.Up.name());
|
||||||
|
buttonUp.addActionListener(actionListenerDirection);
|
||||||
|
constraintsDirection.gridx = 1;
|
||||||
|
constraintsDirection.gridy = 0;
|
||||||
|
containerDirection.add(buttonUp, constraintsDirection);
|
||||||
|
}catch (IOException e){}
|
||||||
|
// Кнопка Down
|
||||||
|
try {
|
||||||
|
BufferedImage bufferedImage = ImageIO.read(new File("AirFighter/Images/Down.png"));
|
||||||
|
Image imageDown = bufferedImage.getScaledInstance(30, 30, Image.SCALE_DEFAULT);
|
||||||
|
ImageIcon iconDown = new ImageIcon(imageDown);
|
||||||
|
buttonDown = new JButton(iconDown);
|
||||||
|
buttonDown.setActionCommand(Direction.Down.name());
|
||||||
|
buttonDown.addActionListener(actionListenerDirection);
|
||||||
|
buttonDown.setPreferredSize(new Dimension(30, 30));
|
||||||
|
buttonDown.setContentAreaFilled(false);
|
||||||
|
buttonDown.setBorderPainted(false);
|
||||||
|
constraintsDirection.gridx = 1;
|
||||||
|
constraintsDirection.gridy = 1;
|
||||||
|
containerDirection.add(buttonDown, constraintsDirection);
|
||||||
|
}catch (IOException e){}
|
||||||
|
// Кнопка Left
|
||||||
|
try {
|
||||||
|
BufferedImage bufferedImage = ImageIO.read(new File("AirFighter/Images/Left.png"));
|
||||||
|
Image imageLeft = bufferedImage.getScaledInstance(30, 30, Image.SCALE_DEFAULT);
|
||||||
|
ImageIcon iconLeft = new ImageIcon(imageLeft);
|
||||||
|
buttonLeft = new JButton(iconLeft);
|
||||||
|
buttonLeft.setActionCommand(Direction.Left.name());
|
||||||
|
buttonLeft.addActionListener(actionListenerDirection);
|
||||||
|
buttonLeft.setPreferredSize(new Dimension(30, 30));
|
||||||
|
buttonLeft.setContentAreaFilled(false);
|
||||||
|
buttonLeft.setBorderPainted(false);
|
||||||
|
constraintsDirection.fill = GridBagConstraints.WEST;
|
||||||
|
constraintsDirection.gridx = 0;
|
||||||
|
containerDirection.add(buttonLeft, constraintsDirection);
|
||||||
|
}catch (IOException e){}
|
||||||
|
// Кнопка Right
|
||||||
|
try {
|
||||||
|
BufferedImage bufferedImage = ImageIO.read(new File("AirFighter/Images/Right.png"));
|
||||||
|
Image imageRight = bufferedImage.getScaledInstance(30, 30, Image.SCALE_DEFAULT);
|
||||||
|
ImageIcon iconRight = new ImageIcon(imageRight);
|
||||||
|
buttonRight = new JButton(iconRight);
|
||||||
|
buttonRight.setActionCommand(Direction.Right.name());
|
||||||
|
buttonRight.addActionListener(actionListenerDirection);
|
||||||
|
buttonRight.setPreferredSize(new Dimension(30, 30));
|
||||||
|
buttonRight.setContentAreaFilled(false);
|
||||||
|
buttonRight.setBorderPainted(false);
|
||||||
|
constraintsDirection.fill = GridBagConstraints.EAST;
|
||||||
|
constraintsDirection.gridx = 2;
|
||||||
|
containerDirection.add(buttonRight, constraintsDirection);
|
||||||
|
}catch (IOException e){}
|
||||||
|
containerDirection.setBounds(20, 500, 110, 70);
|
||||||
|
panelTools.add(containerDirection);
|
||||||
|
|
||||||
|
add(panelTools);
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Создание формы
|
||||||
|
FormMapWithSetAirFighters _formMap = new FormMapWithSetAirFighters();
|
||||||
|
_formMap.setSize (1000,700);
|
||||||
|
_formMap.getRootPane().setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
|
||||||
|
_formMap.addWindowListener (new WindowAdapter() {
|
||||||
|
public void windowClosing (WindowEvent e) {
|
||||||
|
e.getWindow ().dispose ();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
_formMap.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
143
AirFighter/src/MapWithSetAirFightersGeneric.java
Normal file
143
AirFighter/src/MapWithSetAirFightersGeneric.java
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
public class MapWithSetAirFightersGeneric <T extends IDrawningObject, U extends AbstractMap>{
|
||||||
|
/// Ширина окна отрисовки
|
||||||
|
private final int _pictureWidth;
|
||||||
|
/// Высота окна отрисовки
|
||||||
|
private final int _pictureHeight;
|
||||||
|
/// Размер занимаемого объектом места (ширина)
|
||||||
|
private final int _placeSizeWidth = 210;
|
||||||
|
/// Размер занимаемого объектом места (высота)
|
||||||
|
private final int _placeSizeHeight = 90;
|
||||||
|
/// Набор объектов
|
||||||
|
private final SetAirFightersGeneric<T> _setAirFighters;
|
||||||
|
/// Карта
|
||||||
|
private final U _map;
|
||||||
|
/// Конструктор
|
||||||
|
public MapWithSetAirFightersGeneric(int picWidth, int picHeight, U map)
|
||||||
|
{
|
||||||
|
int width = picWidth / _placeSizeWidth;
|
||||||
|
int height = picHeight / _placeSizeHeight;
|
||||||
|
_setAirFighters = new SetAirFightersGeneric<T>(width * height);
|
||||||
|
_pictureWidth = picWidth;
|
||||||
|
_pictureHeight = picHeight;
|
||||||
|
_map = map;
|
||||||
|
}
|
||||||
|
/// Добавление в хранилище
|
||||||
|
public int Add(T airFighter)
|
||||||
|
{
|
||||||
|
return _setAirFighters.Insert(airFighter);
|
||||||
|
}
|
||||||
|
/// Удаление из хранилища
|
||||||
|
public T Remove(int position)
|
||||||
|
{
|
||||||
|
return _setAirFighters.Remove(position);
|
||||||
|
}
|
||||||
|
/// Вывод всего набора объектов
|
||||||
|
public BufferedImage ShowSet()
|
||||||
|
{
|
||||||
|
BufferedImage bimg = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics gr = bimg.createGraphics();
|
||||||
|
DrawBackground(gr);
|
||||||
|
DrawAirFighters(gr);
|
||||||
|
return bimg;
|
||||||
|
}
|
||||||
|
/// Просмотр объекта на карте
|
||||||
|
public BufferedImage ShowOnMap()
|
||||||
|
{
|
||||||
|
Shaking();
|
||||||
|
for (int i = 0; i < _setAirFighters.Count(); i++)
|
||||||
|
{
|
||||||
|
var airFighter = _setAirFighters.Get(i);
|
||||||
|
if (airFighter != null)
|
||||||
|
{
|
||||||
|
return _map.CreateMap(_pictureWidth, _pictureHeight, airFighter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
|
}
|
||||||
|
/// Перемещение объекта по крате
|
||||||
|
public BufferedImage MoveObject(Direction direction)
|
||||||
|
{
|
||||||
|
if (_map != null)
|
||||||
|
{
|
||||||
|
return _map.MoveObject(direction);
|
||||||
|
}
|
||||||
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
|
}
|
||||||
|
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
||||||
|
private void Shaking()
|
||||||
|
{
|
||||||
|
int j = _setAirFighters.Count() - 1;
|
||||||
|
for (int i = 0; i < _setAirFighters.Count(); i++)
|
||||||
|
{
|
||||||
|
if (_setAirFighters.Get(i) == null)
|
||||||
|
{
|
||||||
|
for (; j > i; j--)
|
||||||
|
{
|
||||||
|
var airFighter = _setAirFighters.Get(j);
|
||||||
|
if (airFighter != null)
|
||||||
|
{
|
||||||
|
_setAirFighters.Insert(airFighter, i);
|
||||||
|
_setAirFighters.Remove(j);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j <= i)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Метод отрисовки фона
|
||||||
|
private void DrawBackground(Graphics g)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||||
|
{//линия рамзетки места
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i *
|
||||||
|
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||||
|
|
||||||
|
int[] pointX = new int[]{i * _placeSizeWidth + _placeSizeWidth / 5,(i + 1) * _placeSizeWidth - _placeSizeWidth / 5,
|
||||||
|
(i + 1) * _placeSizeWidth - _placeSizeWidth / 5,i * _placeSizeWidth + _placeSizeWidth / 5};
|
||||||
|
int[] pointY = new int[]{j * _placeSizeHeight + _placeSizeHeight / 10, j * _placeSizeHeight + _placeSizeHeight / 10,
|
||||||
|
(j + 1) * _placeSizeHeight - _placeSizeHeight / 10, (j + 1) * _placeSizeHeight - _placeSizeHeight / 10};
|
||||||
|
g.setColor(Color.GRAY);
|
||||||
|
g.fillPolygon(pointX, pointY, 4);
|
||||||
|
}
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
|
||||||
|
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Метод прорисовки объектов
|
||||||
|
private void DrawAirFighters(Graphics g)
|
||||||
|
{
|
||||||
|
int currentWidth = _pictureWidth / _placeSizeWidth - 1;
|
||||||
|
int currentHeight = _pictureHeight / _placeSizeHeight - 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < _setAirFighters.Count(); i++)
|
||||||
|
{
|
||||||
|
if(_setAirFighters.Get(i) != null) {
|
||||||
|
_setAirFighters.Get(i).SetObject(currentWidth * _placeSizeWidth + 50, currentHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
||||||
|
_setAirFighters.Get(i).DrawningObject(g);
|
||||||
|
}
|
||||||
|
if(currentWidth > 0)
|
||||||
|
{
|
||||||
|
currentWidth -= 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(currentHeight > 0)
|
||||||
|
{
|
||||||
|
currentHeight -= 1;
|
||||||
|
currentWidth = _pictureWidth / _placeSizeWidth - 1;
|
||||||
|
}else return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
92
AirFighter/src/SetAirFightersGeneric.java
Normal file
92
AirFighter/src/SetAirFightersGeneric.java
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
public class SetAirFightersGeneric<T> {
|
||||||
|
// Массив объектов, которые храним
|
||||||
|
private T[] _places;
|
||||||
|
// Количество объектов в массиве
|
||||||
|
public int Count(){return _places.length;};
|
||||||
|
// Конструктор
|
||||||
|
public SetAirFightersGeneric(int count)
|
||||||
|
{
|
||||||
|
_places = (T[])new Object[count];
|
||||||
|
}
|
||||||
|
/// Проверка на наличие пустых мест
|
||||||
|
private boolean CheckNullPosition(int firstIndex)
|
||||||
|
{
|
||||||
|
if(firstIndex >= _places.length && firstIndex < 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(int i = firstIndex; i < _places.length; i++)
|
||||||
|
{
|
||||||
|
if(_places[i] == null)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/// Добавление объекта в набор
|
||||||
|
public int Insert(T airFighter)
|
||||||
|
{
|
||||||
|
return Insert(airFighter, 0);
|
||||||
|
}
|
||||||
|
/// Добавление объекта в набор на конкретную позицию
|
||||||
|
public int Insert(T airFighter, int position)
|
||||||
|
{
|
||||||
|
if(airFighter == null)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (_places[position] == null)
|
||||||
|
{
|
||||||
|
_places[position] = airFighter;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(CheckNullPosition(position + 1))
|
||||||
|
{
|
||||||
|
T tempMain = airFighter;
|
||||||
|
for (int i = position; i < _places.length; i++)
|
||||||
|
{
|
||||||
|
if (_places[i] == null)
|
||||||
|
{
|
||||||
|
_places[i] = tempMain;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
T temp2 = _places[i];
|
||||||
|
_places[i] = tempMain;
|
||||||
|
tempMain = temp2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
/// Удаление объекта из набора с конкретной позиции
|
||||||
|
public T Remove(int position)
|
||||||
|
{
|
||||||
|
if (position >= 0 && position < _places.length && _places[position] != null)
|
||||||
|
{
|
||||||
|
T temp = _places[position];
|
||||||
|
_places[position] = null;
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
/// Получение объекта из набора по позиции
|
||||||
|
public T Get(int position)
|
||||||
|
{
|
||||||
|
if (_places[position] != null)
|
||||||
|
{
|
||||||
|
return _places[position];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user