218 lines
8.8 KiB
Java
Raw Normal View History

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Set;
public class FormMap extends JFrame
{
public JPanel MainPanel;
private JButton ButtonCreate;
private JButton ButtonCreateModif;
private JButton ButtonLeft;
private JButton ButtonDown;
private JButton ButtonRight;
private JButton ButtonUp;
private JToolBar StatusStrip;
private JPanel PictureBoxPlane;
private JComboBox ComboBoxSelectorMap;
private JLabel LabelSpeed = new JLabel();
private JLabel LabelWeight = new JLabel();
private JLabel LabelColor = new JLabel();
protected DrawingPlane _plane;
private AbstractMap _abstractMap;
private Random rnd = new Random();
private BufferedImage bufferImg = null;
public void Draw()
{
PictureBoxPlane.removeAll();
bufferImg = new BufferedImage(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = bufferImg.getGraphics();
g.setColor(new Color(238, 238, 238));
g.fillRect(0, 0, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
if(_plane != null)
{
_plane.DrawTransport(g);
JLabel imageOfLogo = new JLabel();
imageOfLogo.setPreferredSize(PictureBoxPlane.getSize());
imageOfLogo.setMinimumSize(new Dimension(1, 1));
imageOfLogo.setIcon(new ImageIcon(bufferImg));
PictureBoxPlane.add(imageOfLogo, BorderLayout.CENTER);
}
validate();
}
@Override
public void paint(Graphics g)
{
super.paint(g);
Draw();
}
public void SetData(DrawingPlane _plane)
{
PictureBoxPlane.removeAll();
LabelSpeed.setText("Скорость: " + _plane.GetPlane().GetSpeed() + " ");
LabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " ");
LabelColor.setText("Цвет: r = " + _plane.GetPlane().GetColor().getRed() + " g = " + _plane.GetPlane().GetColor().getGreen() +
" b = " + _plane.GetPlane().GetColor().getBlue());
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.CreateMap(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), new DrawningObjectPlane((_plane)))));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
}
public FormMap()
{
_abstractMap = new SimpleMap();
//создание строки отображения скорости, веса и цвета объекта
Box LableBox = Box.createHorizontalBox();
LableBox.setMinimumSize(new Dimension(1, 20));
LableBox.add(LabelSpeed);
LableBox.add(LabelWeight);
LableBox.add(LabelColor);
StatusStrip.add(LableBox);
try
{
Image img = ImageIO.read(getClass().getResource("resourses/Up.png"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(getClass().getResource("resourses/Left.png"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(getClass().getResource("resourses/Down.png"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(getClass().getResource("resourses/Right.png"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex)
{
System.out.println(ex.getMessage());
}
_plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
ButtonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rnd = new Random();
var plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100),
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
SetData(plane);
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxPlane.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject((Direction.Up))));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
PictureBoxPlane.repaint();
}
});
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxPlane.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject((Direction.Left))));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
PictureBoxPlane.repaint();
}
});
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxPlane.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject((Direction.Down))));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
PictureBoxPlane.repaint();
}
});
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxPlane.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject((Direction.Right))));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
PictureBoxPlane.repaint();
}
});
ButtonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_plane = new DrawingAirbus(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextBoolean(), rnd.nextBoolean());
_plane.SetPosition(rnd.nextInt(100, 500), rnd.nextInt(10, 100),
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
SetData(_plane);
}
});
ComboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ComboBoxSelectorMap = (JComboBox)e.getSource();
String item = (String)ComboBoxSelectorMap.getSelectedItem();
switch(item)
{
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Буря в пустыне":
_abstractMap = new DesertStormMap();
break;
case "Звёздные войны":
_abstractMap = new StarWarsMap();
break;
}
}
});
}
}