Добавление некоторых классов

This commit is contained in:
Hells Hound 2022-10-16 23:00:42 +04:00
parent 565bfd8dd7
commit b88d9dd7f7
7 changed files with 276 additions and 131 deletions

155
src/AbstractMap.java Normal file
View File

@ -0,0 +1,155 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class AbstractMap {
private IDrawingObject _drawingObject = null;
protected int[][] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected final Random _random = new Random();
protected final int _freeRoad = 0;
protected final int _barrier = 1;
public BufferedImage CreateMap(int width, int height, IDrawingObject drawningObject)
{
_width = width;
_height = height;
_drawingObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public BufferedImage MoveObject(Direction direction)
{
if (_drawingObject != null)
{
if (true)
{
_drawingObject.MoveObject(direction);
}
float[] cortege = _drawingObject.GetCurrentPosition();
if (Check(cortege[0],cortege[1],cortege[2],cortege[3])!= 0)
{
_drawingObject.MoveObject(GetOpositDirection(direction));
}
return DrawMapWithObject();
}
return null;
}
private Direction GetOpositDirection(Direction dir)
{
switch (dir)
{
case None:
return Direction.None;
case Up:
return Direction.Down;
case Down:
return Direction.Up;
case Left:
return Direction.Right;
case Right:
return Direction.Left;
}
return Direction.None;
}
private boolean SetObjectOnMap()
{
if (_drawingObject == null || _map == null)
{
return false;
}
int x = _random.nextInt(0, 10);
int y = _random.nextInt(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
float[] cortege = _drawingObject.GetCurrentPosition();
float nowX = cortege[0];
float nowY = cortege[1];
float lenX = cortege[2]-cortege[0];
float lenY = cortege[3] - cortege[1];
while (Check(nowX, nowY, nowX + lenX, nowY + lenY) != 2)
{
int result;
do
{
result = Check(nowX, nowY, nowX + lenX, nowY + lenY);
if (result == 0)
{
_drawingObject.SetObject((int)nowX, (int)nowY, _width, _height);
return true;
}
else
{
nowX += _size_x;
}
} while (result != 2);
nowX = x;
nowY += _size_y;
}
return false;
}
private int Check(float Left, float Right, float Top, float Bottom)
{
int startX = (int)(Left / _size_x);
int startY = (int)(Right / _size_y);
int endX = (int)(Top / _size_x);
int endY = (int)(Bottom / _size_y);
if (startX < 0 || startY < 0 || endX >= _map[0].length || endY >= _map.length)
{
return 2;
}
for (int i = startX; i <= endX; i++)
{
for (int j = startY; j <= endY; j++)
{
if (_map[i][j] == _barrier)
{
return 1;
}
}
}
return 0;
}
private BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width,_height,BufferedImage.TYPE_INT_RGB);
if (_drawingObject == null || _map == null)
{
return bmp;
}
Graphics gr = bmp.getGraphics();
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map.length; ++j)
{
if (_map[i][j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i][j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawingObject.DrawningObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}

View File

@ -1,4 +1,5 @@
public enum Direction {
None,
Up,
Down,
Left,

View File

@ -9,11 +9,12 @@ public class DrawingAircraftCarrier extends DrawingWarship{
}
@Override
public void DrawTransport(Graphics2D g2){
public void DrawTransport(Graphics gr){
if(!(Warship instanceof EntityAircraftCarrier))
{
return;
}
Graphics2D g2 = (Graphics2D) gr;
EntityAircraftCarrier aircraftCarrier = (EntityAircraftCarrier) Warship;
if (aircraftCarrier.GetBodyKit())
@ -33,7 +34,6 @@ public class DrawingAircraftCarrier extends DrawingWarship{
g2.fillPolygon(pointXLine, pointYLine, 4);
g2.setColor(Color.BLACK);
g2.drawPolygon(pointXLine, pointYLine, 4);
}
if (aircraftCarrier.GetSuperEngine())

View File

@ -1,22 +0,0 @@
import javax.swing.*;
import java.awt.*;
public class DrawingComponents extends JComponent {
public DrawingWarship warship;
public DrawingComponents(){
super();
}
public void SetDrawingWarship(DrawingWarship warship){
this.warship = warship;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(warship != null){
warship.DrawTransport(g2);
}
repaint();
}
}

View File

@ -77,11 +77,12 @@ public class DrawingWarship {
}
}
public void DrawTransport(Graphics2D g2){
public void DrawTransport(Graphics gr){
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
{
return;
}
Graphics2D g2 = (Graphics2D) gr;
//главная палуба
int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4};
int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40};
@ -144,11 +145,11 @@ public class DrawingWarship {
}
public float[] GetCurrentPosition(){
float[] pos = new float[4];
pos[0] = _startPosX;
pos[1] =_startPosY;
pos[2] = _startPosX + _warshipWidth;
pos[3] = _startPosY + _warshipHeight;
return pos;
float[] cortege = new float[4];
cortege[0] = _startPosX;
cortege[1] =_startPosY;
cortege[2] = _startPosX + _warshipWidth;
cortege[3] = _startPosY + _warshipHeight;
return cortege;
}
}

View File

@ -3,36 +3,96 @@
<grid id="27dc6" binding="mainPanel" 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>
<xy x="23" y="20" width="961" height="517"/>
<xy x="49" y="20" width="935" height="482"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="80b72" class="javax.swing.JButton" binding="buttonCreate">
<grid id="dfecd" binding="drawPanel" layout-manager="GridLayoutManager" row-count="3" 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="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="3" col-span="6" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Create"/>
</properties>
</component>
<hspacer id="3f3da">
<constraints>
<grid row="2" 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>
<component id="32714" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" 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>
<properties/>
<border type="none"/>
<children>
<component id="80b72" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="2" 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="667bb">
<constraints>
<grid row="2" 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="e8c93">
<constraints>
<grid row="0" 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="34645" class="javax.swing.JButton" binding="buttonCreateModif">
<constraints>
<grid row="2" 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="Modification"/>
</properties>
</component>
<component id="55933" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" 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="bb79e" class="javax.swing.JButton" binding="buttonDown">
<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>
<enabled value="true"/>
<text value=""/>
</properties>
</component>
<component id="82cc1" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" 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="32714" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" 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>
</children>
</grid>
<toolbar id="3697c" binding="toolBar">
<constraints>
<grid row="3" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
@ -62,59 +122,6 @@
</component>
</children>
</toolbar>
<component id="55933" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" 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="82cc1" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" 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="bb79e" class="javax.swing.JButton" binding="buttonDown">
<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>
<enabled value="true"/>
<text value=""/>
</properties>
</component>
<grid id="dfecd" binding="drawPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="6" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="34645" class="javax.swing.JButton" binding="buttonCreateModif">
<constraints>
<grid row="2" 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="Modification"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -4,12 +4,13 @@ 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;
public class FormWarship extends JFrame {
private DrawingWarship _warship;
public DrawingComponents drawingComponents;
private JPanel mainPanel;
private JPanel drawPanel;
private JButton buttonCreate;
private JButton buttonLeft;
private JButton buttonUp;
@ -19,30 +20,37 @@ public class FormWarship extends JFrame {
private JLabel toolBarLabelSpeed;
private JLabel toolBarLabelWieght;
private JLabel toolBarLabelColor;
private JPanel drawPanel;
private JButton buttonCreateModif;
public FormWarship(){
InitializeComponent();
}
private void Draw(){
drawingComponents.repaint();
Graphics2D graphics = (Graphics2D) drawPanel.getGraphics();
graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight());
drawPanel.paintComponents(graphics);
_warship.DrawTransport(graphics);
}
private void SetData(){
Random rnd = new Random();
_warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), drawPanel.getWidth(), drawPanel.getHeight());
_warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), mainPanel.getWidth(), mainPanel.getHeight());
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(){
private void InitializeComponent() {
setContentPane(mainPanel);
setTitle("Warship");
setSize(900,700);
setSize(900, 700);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
@ -55,18 +63,14 @@ public class FormWarship extends JFrame {
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
buttonRight.setIcon(iconRight);
drawingComponents = new DrawingComponents();
drawPanel.add(drawingComponents);
//кнопка добавления объекта
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rnd = new Random();
_warship = new DrawingWarship(rnd.nextInt(100,300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)));
_warship = new DrawingWarship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
SetData();
drawingComponents.SetDrawingWarship(_warship);
Draw();
}
});
@ -76,12 +80,11 @@ public class FormWarship extends JFrame {
@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)),
_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();
drawingComponents.SetDrawingWarship(_warship);
Draw();
}
});
@ -89,7 +92,7 @@ public class FormWarship extends JFrame {
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_warship != null) _warship.MoveTransport(Direction.Left);
if (_warship != null) _warship.MoveTransport(Direction.Left);
Draw();
}
});
@ -97,7 +100,7 @@ public class FormWarship extends JFrame {
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_warship != null) _warship.MoveTransport(Direction.Right);
if (_warship != null) _warship.MoveTransport(Direction.Right);
Draw();
}
});
@ -105,7 +108,7 @@ public class FormWarship extends JFrame {
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_warship != null) _warship.MoveTransport(Direction.Up);
if (_warship != null) _warship.MoveTransport(Direction.Up);
Draw();
}
});
@ -113,16 +116,16 @@ public class FormWarship extends JFrame {
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_warship != null) _warship.MoveTransport(Direction.Down);
if (_warship != null) _warship.MoveTransport(Direction.Down);
Draw();
}
});
drawPanel.addComponentListener(new ComponentAdapter() {
mainPanel.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
_warship.ChangeBorders(drawPanel.getWidth(),drawPanel.getHeight());
Draw();
super.componentResized(e);
resizeWindow();
}
});
}