некоторые новые измененеия и добавления

This commit is contained in:
Hells Hound 2022-10-21 09:13:03 +04:00
parent b88d9dd7f7
commit ba8046949c
10 changed files with 460 additions and 4 deletions

View File

@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/out" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

8
.idea/compiler.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="AircraftCarrier_Hard" options="--add-modules java.datatransfer,java.desktop --add-exports java.datatransfer/java.awt=ALL-UNNAMED" />
</option>
</component>
</project>

70
src/DrawingMap.java Normal file
View File

@ -0,0 +1,70 @@
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class DrawingMap extends JPanel{
private final FormMap Map;
private AbstractMap _abstractMap;
BufferedImage bufferedImage;
public DrawingMap(FormMap map){
Map=map;
_abstractMap = new SimpleMap();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bufferedImage,0,0,null);
}
private void SetData(DrawingWarship warship) {
Random rand=new Random();
warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
Map.SpeedLabel.setText("Скорость: "+warship.GetWarship().GetSpeed());
Map.WeightLabel.setText("Вес: "+warship.GetWarship().GetWeight());
Map.BodyColorLabel.setText("Цвет: "+Integer.toHexString(warship.GetWarship().GetBodyColor().getRGB()).substring(2));
bufferedImage = _abstractMap.CreateMap(700,550,new DrawingObjectWarship(warship));
}
public void CreateButtonAction(){
Random rand=new Random();
Color color1 = JColorChooser.showDialog(Map, "Выберите цвет тела корабля", null);
if(color1==null)
color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
DrawingWarship warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3));
SetData(warship);
}
public void CreateModifButtonAction(){
Random rand=new Random();
Color color1=JColorChooser.showDialog(Map, "Выберите цвет тела корабля",null);
Color color2=JColorChooser.showDialog(Map, "Выборите цвет модификаций корабля",null);
if(color1==null)
color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
if (color2==null)
color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
DrawingWarship warship = new DrawingAdvancedWarship(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1,
color2, rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3));
SetData(warship);
}
public void DirectionButtonAction(Direction side){
if(_abstractMap != null){
bufferedImage = _abstractMap.MoveObject(side);
}
}
public void ComboBoxSelectorMapAction(String name){
switch (name){
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Вторая карта":
_abstractMap = new SecondMap();
break;
}
}
public void DrawMap(Graphics g){
g.drawImage(bufferedImage,0,0,null);
}
}

View File

@ -25,8 +25,8 @@ public class DrawingObjectWarship implements IDrawingObject {
}
@Override
public void DrawningObject(Graphics2D g2) {
_warship.DrawTransport(g2);
public void DrawningObject(Graphics g) {
_warship.DrawTransport(g);
}
@Override

137
src/FormMap.form Normal file
View File

@ -0,0 +1,137 @@
<?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="3" column-count="1" 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="897" height="504"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<toolbar id="ced8" binding="toolBar">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="7c17c" class="javax.swing.JLabel" binding="toolBarLabelSpeed">
<constraints/>
<properties>
<text value="Speed"/>
</properties>
</component>
<component id="a7229" class="javax.swing.JLabel" binding="toolBarLabelWieght">
<constraints/>
<properties>
<text value="Weight"/>
</properties>
</component>
<component id="7c70" class="javax.swing.JLabel" binding="toolBarLabelColor">
<constraints/>
<properties>
<text value="Color"/>
</properties>
</component>
</children>
</toolbar>
<grid id="3b625" binding="drawPanel" layout-manager="GridLayoutManager" row-count="4" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c54f8" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="3" 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="Create"/>
</properties>
</component>
<hspacer id="a1d54">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="4c365">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="aaec9" class="javax.swing.JButton" binding="buttonCreateModif">
<constraints>
<grid row="3" 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="Modificarion"/>
</properties>
</component>
<component id="c7660" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="3" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="e0470" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="a6de2" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="633e1" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="e1523" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="0" column="3" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Карта-Линии"/>
</model>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

144
src/FormMap.java Normal file
View File

@ -0,0 +1,144 @@
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.util.Objects;
import java.util.Random;
public class FormMap extends JFrame{
private AbstractMap _abstractMap;
private JPanel mainPanel;
private JToolBar toolBar;
private JLabel toolBarLabelSpeed;
private JLabel toolBarLabelWieght;
private JLabel toolBarLabelColor;
private JButton buttonCreate;
private JButton buttonCreateModif;
private JButton buttonRight;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JComboBox ComboBoxSelectorMap;
private JPanel drawPanel;
public FormMap(){
InitializeComponent();
_abstractMap = new SimpleMap();
}
private void SetData(DrawingWarship warship){
Graphics2D graphics = (Graphics2D) drawPanel.getGraphics();
graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight());
drawPanel.paintComponents(graphics);
graphics.drawImage(_abstractMap.CreateMap(drawPanel.getWidth(), drawPanel.getHeight(), new DrawingObjectWarship(warship)),0,0,null);
toolBarLabelSpeed.setText("Color: " + warship.GetWarship().GetSpeed() + " ");
toolBarLabelWieght.setText("Weight: " + warship.GetWarship().GetWeight() + " ");
toolBarLabelColor.setText("Color: " + warship.GetWarship().GetBodyColor().getRed() + " " +
warship.GetWarship().GetBodyColor().getGreen() + " " + warship.GetWarship().GetBodyColor().getBlue());
}
private void resizeWindow() {
_warship.ChangeBorders(drawPanel.getWidth(), drawPanel.getHeight());
Draw();
}
private void InitializeComponent() {
setContentPane(mainPanel);
setTitle("Warship");
setSize(900, 700);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");
buttonUp.setIcon(iconUp);
Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg");
buttonDown.setIcon(iconDown);
Icon iconLeft = new ImageIcon("src\\Images\\ArrowLeft.jpg");
buttonLeft.setIcon(iconLeft);
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
buttonRight.setIcon(iconRight);
ComboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String mapName = Objects.requireNonNull(ComboBoxSelectorMap.getSelectedItem()).toString();
switch (mapName){
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Вторая карта":
_abstractMap = new LineMap();
break;
}
}
});
//кнопка добавления объекта
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rnd = new Random();
var warship = new DrawingWarship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
SetData(warship);
Draw();
}
});
//добавление модифицированного объекта
/*buttonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rnd = new Random();
_warship = new DrawingAircraftCarrier(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(), rnd.nextBoolean());
SetData();
Draw();
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_warship != null) _warship.MoveTransport(Direction.Left);
Draw();
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_warship != null) _warship.MoveTransport(Direction.Right);
Draw();
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_warship != null) _warship.MoveTransport(Direction.Up);
Draw();
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_warship != null) _warship.MoveTransport(Direction.Down);
Draw();
}
});*/
mainPanel.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
resizeWindow();
}
});
}
}

View File

@ -8,7 +8,7 @@ public interface IDrawingObject {
// Изменение направления пермещения объекта
void MoveObject(Direction direction);
// Отрисовка объекта
void DrawningObject(Graphics2D g2);
void DrawningObject(Graphics g);
// Получение текущей позиции объекта
// /Left, Right, Top, Bottom)
float[] GetCurrentPosition();

53
src/LineMap.java Normal file
View File

@ -0,0 +1,53 @@
import java.awt.*;
public class LineMap extends AbstractMap{
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.gray);
g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void GenerateMap()
{
_map = new int[100][100];
_size_x = (float)_width / _map.length;
_size_y = (float)_height / _map[0].length;
int counter = 0;
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[i].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
boolean flag = true;
while (counter < 20)
{
int lineX = _random.nextInt(11, 89);
int lineY = _random.nextInt(11, 89);
if (flag)
{
for (int i = lineY; i <= lineY + 10; i++)
_map[lineX][i] = _barrier;
flag = false;
}
else
{
for (int i = lineX; i <= lineX + 10; i++)
_map[i][lineY] = _barrier;
flag = true;
}
counter++;
}
}
}

View File

@ -1,6 +1,6 @@
public class Main {
public static void main(String[] args) {
new FormWarship();
new FormMap();
}
}

43
src/SimpleMap.java Normal file
View File

@ -0,0 +1,43 @@
import java.awt.*;
public class SimpleMap extends AbstractMap{
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.gray);
g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void GenerateMap()
{
_map = new int[100][100];
_size_x = (float)_width / _map.length;
_size_y = (float)_height / _map[0].length;
int counter = 0;
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[i].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
while (counter < 50)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
}