185 lines
6.9 KiB
Java
185 lines
6.9 KiB
Java
import javax.swing.*;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
import java.awt.event.ComponentEvent;
|
||
import java.awt.event.ComponentListener;
|
||
import java.awt.image.BufferedImage;
|
||
import java.util.Random;
|
||
public class FormMap extends JComponent {
|
||
private AbstractMap _abstractMap;
|
||
private DrawingStormtrooper _stormtrooper;
|
||
private BufferedImage bufferedImage;
|
||
public static void main(String[] args) {
|
||
FormMap formMap = new FormMap();
|
||
}
|
||
public FormMap() {
|
||
JFrame form = new JFrame("Карта");
|
||
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
form.setSize(1000, 500);
|
||
form.setVisible(true);
|
||
form.setLocationRelativeTo(null);
|
||
Panel statusPanel = new Panel();
|
||
statusPanel.setBackground(Color.WHITE);
|
||
statusPanel.setLayout(new GridBagLayout());
|
||
setLayout(new BorderLayout());
|
||
form.add(statusPanel, BorderLayout.SOUTH);
|
||
Label speedLabel = new Label("Скорость: ");
|
||
Label weightLabel = new Label("Вес: ");
|
||
Label colorLabel = new Label("Цвет: ");
|
||
|
||
String[] maps = {
|
||
"Простая карта",
|
||
"Ясное небо",
|
||
"Пасмурное небо"
|
||
};
|
||
JComboBox mapSelectComboBox = new JComboBox(maps);
|
||
mapSelectComboBox.setEditable(true);
|
||
mapSelectComboBox.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
String item = (String)mapSelectComboBox.getSelectedItem();
|
||
if (item == null) return;
|
||
switch (item) {
|
||
case ("Простая карта"):
|
||
_abstractMap = new SimpleMap();
|
||
break;
|
||
case ("Ясное небо"):
|
||
_abstractMap = new SecondMap();
|
||
break;
|
||
case ("Пасмурное небо"):
|
||
_abstractMap = new ThirdMap();
|
||
break;
|
||
|
||
}
|
||
}
|
||
});
|
||
JButton createButton = new JButton("Создать");
|
||
createButton.addActionListener(e -> {
|
||
|
||
Random rnd = new Random();
|
||
|
||
var stormtrop = new DrawingStormtrooper(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
|
||
rnd.nextInt(0, 256)));
|
||
speedLabel.setText("Скорость: " + stormtrop.Stormtrooper.getSpeed());
|
||
weightLabel.setText("Вес: " + (int)stormtrop.Stormtrooper.getWeight());
|
||
colorLabel.setText("Цвет: " + stormtrop.Stormtrooper.getBodyColor().getRed() + " " +
|
||
stormtrop.Stormtrooper.getBodyColor().getGreen() + " " + stormtrop.Stormtrooper.getBodyColor().getBlue() );
|
||
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(form.getWidth(), form.getHeight() - 80, new DrawningObjectStormtrooper(stormtrop));
|
||
repaint();
|
||
|
||
});
|
||
JButton modifiedButton = new JButton("Модификация");
|
||
modifiedButton.addActionListener(e -> {
|
||
Random rnd = new Random();
|
||
|
||
Color colorFirst = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
|
||
Color colorSecond = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
|
||
|
||
var militarystorm = new DrawingMilitaryStormtrooper(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
|
||
colorFirst,
|
||
colorSecond,
|
||
rnd.nextBoolean(),
|
||
rnd.nextBoolean());
|
||
militarystorm.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 80);
|
||
speedLabel.setText("Скорость: " + militarystorm.Stormtrooper.getSpeed());
|
||
weightLabel.setText("Вес: " + militarystorm.Stormtrooper.getWeight());
|
||
colorLabel.setText("Цвет: " + militarystorm.Stormtrooper.getBodyColor().getRed() + " " + militarystorm.Stormtrooper.getBodyColor().getGreen() + " " + militarystorm.Stormtrooper.getBodyColor().getBlue() );
|
||
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(form.getWidth(), form.getHeight() - 80, new DrawningObjectStormtrooper(militarystorm));
|
||
repaint();
|
||
});
|
||
GridBagConstraints c = new GridBagConstraints();//заполнение statuspanel
|
||
|
||
statusPanel.add(mapSelectComboBox);
|
||
statusPanel.add(createButton);
|
||
statusPanel.add(modifiedButton);
|
||
|
||
|
||
c.fill = GridBagConstraints.NONE;
|
||
c.anchor = GridBagConstraints.CENTER;
|
||
c.weightx = 0.5;
|
||
c.gridx = 0;
|
||
c.gridy = 1;
|
||
statusPanel.add(mapSelectComboBox,c);
|
||
|
||
c.gridx = 1;
|
||
c.gridy = 1;
|
||
statusPanel.add(createButton,c);
|
||
|
||
c.gridx = 2;
|
||
c.gridy = 1;
|
||
statusPanel.add(modifiedButton,c);
|
||
|
||
c.fill = GridBagConstraints.HORIZONTAL;
|
||
c.gridx = 3;
|
||
c.gridy = 1;
|
||
statusPanel.add(speedLabel,c);
|
||
|
||
c.gridx = 4;
|
||
c.gridy = 1;
|
||
statusPanel.add(weightLabel,c);
|
||
|
||
c.gridx = 5;
|
||
c.gridy = 1;
|
||
c.gridwidth = 1;
|
||
statusPanel.add(colorLabel,c);
|
||
JButton upButton = new JButton("↑");
|
||
JButton rightButton = new JButton("→");
|
||
JButton leftButton = new JButton("←");
|
||
JButton downButton = new JButton("↓");
|
||
upButton.addActionListener(e -> {
|
||
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.UP);
|
||
repaint();
|
||
});
|
||
|
||
|
||
rightButton.addActionListener(e -> {
|
||
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.RIGHT);
|
||
repaint();
|
||
});
|
||
|
||
|
||
leftButton.addActionListener(e -> {
|
||
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.LEFT);
|
||
repaint();
|
||
});
|
||
|
||
|
||
downButton.addActionListener(e -> {
|
||
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.DOWN);
|
||
repaint();
|
||
});
|
||
|
||
c.fill = GridBagConstraints.NONE;
|
||
c.gridx = 8;
|
||
c.gridy = 0;
|
||
c.gridwidth = 1;
|
||
statusPanel.add(upButton,c);
|
||
|
||
c.gridx = 7;
|
||
c.gridy = 1;
|
||
c.gridwidth = 1;
|
||
statusPanel.add(leftButton,c);
|
||
|
||
c.gridx = 9;
|
||
c.gridy = 1;
|
||
c.gridwidth = 1;
|
||
statusPanel.add(rightButton,c);
|
||
c.gridx = 8;
|
||
c.gridy = 1;
|
||
c.gridwidth = 1;
|
||
statusPanel.add(downButton,c);
|
||
|
||
form.getContentPane().add(this);
|
||
super.repaint();
|
||
}
|
||
protected void paintComponent(Graphics g) {
|
||
super.paintComponent(g);
|
||
Graphics2D g2 = (Graphics2D)g;
|
||
if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,1000,420,null);
|
||
super.repaint();
|
||
}
|
||
|
||
}
|