PIbd-12_Karamushko_M.K._Air.../formMap.java

120 lines
3.8 KiB
Java
Raw Normal View History

2022-11-10 16:44:45 +04:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class formMap implements Form {
private JPanel DrawPlace;
private JLabel weightLabel;
private JLabel speedLabel;
private JLabel colorLabel;
private JButton createButton;
private JButton downButton;
private JButton leftButton;
private JButton rightButton;
private JButton upButton;
private JComboBox comboBoxSelectorMap;
private JPanel mainPanel;
private JButton modifiedButton;
private Canvas canv = new Canvas(this);
private DrawingAircraft _aircraft;
private AbstractMap _abstractMap = new SimpleMap();
private JFrame jframe = getFrame();
public formMap() {
}
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setBounds(300, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
private void SetData(DrawingAircraft aircraft)
{
Color bodyColor = _aircraft.AirFighter.BodyColor;
String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")";
speedLabel.setText("Скорость: " + _aircraft.AirFighter.Speed + " ");
weightLabel.setText("Вес: " + _aircraft.AirFighter.Weight + " ");
colorLabel.setText("Цвет: " + colorString);
Dimension canvSize = canv.getSize();
_abstractMap.CreateMap(canvSize.width, canvSize.height, new DrawingObjectAircraft(aircraft));
}
public void run() {
jframe.add(mainPanel);
DrawPlace.add(canv);
comboBoxSelectorMap.addActionListener(e -> {
String selectedItem = comboBoxSelectorMap.getSelectedItem().toString();
switch(selectedItem) {
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Моя карта":
_abstractMap = new MyMap();
break;
}
});
createButton.addActionListener(e -> {
Random rnd = new Random();
_aircraft = new DrawingAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
SetData(_aircraft);
canv.repaint();
});
modifiedButton.addActionListener(e -> {
Random rnd = new Random();
_aircraft = new DrawingModernAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
rnd.nextInt(0, 2) == 1, rnd.nextInt(0, 2) == 1);
SetData(_aircraft);
canv.repaint();
});
downButton.addActionListener(e -> {
if(_abstractMap == null) return;
_abstractMap.MoveObject(Direction.Down);
canv.repaint();
});
upButton.addActionListener(e -> {
if(_abstractMap == null) return;
_abstractMap.MoveObject(Direction.Up);
canv.repaint();
});
leftButton.addActionListener(e -> {
if(_abstractMap == null) return;
_abstractMap.MoveObject(Direction.Left);
canv.repaint();
});
rightButton.addActionListener(e -> {
if(_abstractMap == null) return;
_abstractMap.MoveObject(Direction.Right);
canv.repaint();
});
}
@Override
public void Draw(Graphics2D g) {
_abstractMap.DrawMapWithObject(g);
}
}