Добавление новой формы Карта

This commit is contained in:
prodigygirl 2022-10-23 17:29:00 +04:00
parent b500486bd0
commit 7b254d18b9
3 changed files with 282 additions and 1 deletions

129
src/main/java/FormMap.form Normal file
View File

@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMap">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="564" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="7cc9f" binding="drawPanel" custom-create="true" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="1" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-1"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="e4749" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="30a94" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="e6875" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Right.png"/>
<text value=""/>
</properties>
</component>
<component id="81a7c" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<hideActionText value="true"/>
<icon value="Up.png"/>
<text value=""/>
</properties>
</component>
<component id="87a25" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Down.png"/>
<text value=""/>
</properties>
</component>
<component id="fee79" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Left.png"/>
<text value=""/>
<visible value="true"/>
</properties>
</component>
<component id="2d428" class="javax.swing.JButton" binding="buttonCreate_Modif">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
</children>
</grid>
<component id="54172" class="javax.swing.JLabel" binding="labelSpeed">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="725a5" class="javax.swing.JLabel" binding="labelWeight">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="46832" class="javax.swing.JLabel" binding="labelColor">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="625" class="javax.swing.JComboBox" binding="mapSelectorComboBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Карта 1"/>
<item value="Карта 2"/>
</model>
</properties>
</component>
<hspacer id="9af30">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</form>

151
src/main/java/FormMap.java Normal file
View File

@ -0,0 +1,151 @@
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.Objects;
import java.util.Random;
public class FormMap extends JFrame {
private JPanel drawPanel;
private JButton buttonCreate;
private JButton buttonRight;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonCreate_Modif;
private JLabel labelSpeed;
private JLabel labelWeight;
private JLabel labelColor;
private JComboBox mapSelectorComboBox;
private JPanel mainPanel;
private AbstractMap abstractMap;
private Image img;
public FormMap() {
super("Карта");
abstractMap = new SimpleMap();
setBounds(100, 100, 700, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
labelSpeed.setText("Скорость: ");
labelWeight.setText("Вес: ");
labelColor.setText("Цвет: ");
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Random rnd = new Random();
DrawingArmoredCar armoredCar = new DrawingArmoredCar(rnd.nextInt(100,300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
setData(armoredCar);
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (abstractMap != null) {
img = abstractMap.MoveObject(Direction.Up);
drawPanel.repaint();
}
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (abstractMap != null) {
img = abstractMap.MoveObject(Direction.Down);
drawPanel.repaint();
}
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (abstractMap != null) {
img = abstractMap.MoveObject(Direction.Left);
drawPanel.repaint();
}
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (abstractMap != null) {
img = abstractMap.MoveObject(Direction.Right);
drawPanel.repaint();
}
}
});
setContentPane(mainPanel);
setVisible(true);
buttonCreate_Modif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rnd = new Random();
DrawingTank tank = new DrawingTank(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)),
1==rnd.nextInt(0, 2), 1==rnd.nextInt(0, 2));
setData(tank);
}
});
mapSelectorComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = (String) mapSelectorComboBox.getSelectedItem();
switch (name)
{
case "Простая карта":
abstractMap = new SimpleMap();
break;
case "Карта 1":
abstractMap = new MyMapWooden();
break;
case "Карта 2":
//abstractMap = new MyMapWooden();
break;
}
//setData();
}
});
}
private void createUIComponents() {
drawPanel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
super.repaint();
}
};
}
private void setData(DrawingArmoredCar drawingArmoredCar)
{
if (abstractMap != null)
img = abstractMap.CreateMap(drawPanel.getWidth(), drawPanel.getHeight(), new DrawingObjectArmoredCar(drawingArmoredCar));
labelSpeed.setText("Скорость: " + drawingArmoredCar.getArmoredCar().getSpeed());
labelWeight.setText("Вес: " + drawingArmoredCar.getArmoredCar().getWeight());
labelColor.setText("Цвет: " + drawingArmoredCar.getArmoredCar().getBodyColor().getRGB());
}
}

View File

@ -1,5 +1,6 @@
public class Program {
public static void main(String[] args) {
new FormArmoredCar();
//new FormArmoredCar();
new FormMap();
}
}