Исправление проблемы с отображением выбранной карты на форме.

This commit is contained in:
Programmist73 2022-11-05 01:24:27 +04:00
parent 90ceb3ed94
commit 5adccc16a3
13 changed files with 124 additions and 98 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -54,53 +54,94 @@ public abstract class AbstractMap
public BufferedImage MoveObject(Direction direction)
{
position = _drawingObject.GetCurrentPosition();
for (int i = 0; i < _map.length; ++i)
float[] cortege = _drawingObject.GetCurrentPosition();
int leftCell = (int)(cortege[0] / _size_x);
int upCell = (int)(cortege[1] / _size_y);
int downCell = (int)(cortege[3] / _size_y);
int rightCell = (int)(cortege[2] / _size_x);
int step = (int)_drawingObject.Step();
boolean canMove = true;
switch (direction)
{
for (int j = 0; j < _map[i].length; ++j)
{
if (_map[i][j] == _barrier)
case Left:
for (int i = leftCell - (int)(step / _size_x) - 1 >= 0 ? leftCell - (int)(step / _size_x) - 1 : leftCell; i < leftCell; i++)
{
switch (direction)
for (int j = upCell; j < downCell; j++)
{
case Up:
if(_size_y * (j+1) >= position[0] - _drawingObject.Step() && _size_y * (j+1) < position[0] && _size_x * (i + 1) > position[3]
&& _size_x * (i + 1) <= position[1])
{
return DrawMapWithObject();
}
break;
case Down:
if (_size_y * j <= position[2] + _drawingObject.Step() && _size_y * j > position[2] && _size_x * (i+1) > position[3]
&& _size_x * (i+1) <= position[1])
{
return DrawMapWithObject();
}
break;
case Left:
if (_size_x * (i+1) >= position[3] - _drawingObject.Step() && _size_x * (i + 1) < position[3] && _size_y * (j + 1) < position[2]
&& _size_y * (j + 1) >= position[0])
{
return DrawMapWithObject();
}
break;
case Right:
if (_size_x * i <= position[1] + _drawingObject.Step() && _size_x * i > position[3] && _size_y * (j + 1) < position[2]
&& _size_y * (j + 1) >= position[0])
{
return DrawMapWithObject();
}
break;
if (_map[i][j] == _barrier)
{
canMove = false;
}
}
}
}
break;
case Up:
for (int i = leftCell; i <= rightCell; i++)
{
for (int j = upCell - (int)(step / _size_x) - 1 >= 0 ? upCell - (int)(step / _size_x) - 1 : downCell - (int)(step / _size_x); j < downCell - (int)(step / _size_x); j++)
{
if (_map[i][j] == _barrier)
{
canMove = false;
}
}
}
break;
case Down:
for (int i = leftCell; i <= rightCell; i++)
{
for (int j = downCell + (int)(step / _size_x) + 1 <= _map.length - 1 ? downCell + (int)(step / _size_x) + 1 : upCell; j > upCell; j--)
{
if (_map[i][j] == _barrier)
{
canMove = false;
}
}
}
break;
case Right:
for (int i = rightCell + (int)(step / _size_x) + 1 <= _map.length - 1 ? rightCell + (int)(step / _size_x) + 1 : rightCell; i > rightCell; i--)
{
for (int j = upCell; j < downCell; j++)
{
if (_map[i][j] == _barrier)
{
canMove = false;
}
}
}
break;
}
if (canMove)
{
_drawingObject.MoveObject(direction);
}
_drawingObject.MoveObject(direction);
return DrawMapWithObject();
}
/*
private Direction MoveObjectBack(Direction direction)
{
switch (direction)
{
case Up:
return Direction.Up;
break;
case Down:
return Direction.Down;
break;
case Left:
return Direction.Left;
break;
case Right:
return Direction.Right;
break;
}
return Direction.None;
}
*/
private boolean SetObjectOnMap()
{
if(_drawingObject == null || _map == null)

View File

@ -10,7 +10,8 @@ public enum Additional_Enum {
this.addEnum = i;
}
public int GetAddEnum(){
public int GetAddEnum()
{
return addEnum;
}
}

View File

@ -39,13 +39,13 @@ public class DesertStormMap extends AbstractMap{
protected void DrawRoadPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(roadColor);
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(_size_x), (int)(_size_y));
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j *(_size_y + 1)));
}
@Override
protected void DrawBarrierPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(barriedColor);
g.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(_size_x), (int)(_size_y));
g.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j *(_size_y + 1)));
}
}

View File

@ -5,7 +5,7 @@ public class DrawingAirbus extends DrawingPlane
//Инициализаци свойств
public DrawingAirbus(int speed, int weight, Color corpusColor, Color addColor, boolean addCompartment, boolean addEngine)
{
super(speed, weight, corpusColor, 110, 60);
super(speed, weight, corpusColor, 70, 30);
Plane = new EntityAirbus(speed, weight, corpusColor, addColor, addCompartment, addEngine);
}

View File

@ -135,8 +135,9 @@ public class DrawingPlane extends JPanel
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, _pictureWidth, _pictureHeight);
//корпус
g2d.setColor(Plane.GetColor());
g2d.fillRect((int)_startPosX, (int)_startPosY + 10, 40, 10);
//заливает фигуру
g2d.setPaint(Color.BLACK);
@ -157,10 +158,6 @@ public class DrawingPlane extends JPanel
g2d.fillRect((int)_startPosX + 35, (int)_startPosY + 21, 2, 2);
g2d.fillRect((int)_startPosX + 35, (int)_startPosY + 23, 4, 4);
g2d.setColor(Color.BLACK); //рисует контур
//корпус
g2d.drawRect((int)_startPosX, (int)_startPosY + 10, 40, 10);
//хвостовое крыло
g2d.drawLine((int)_startPosX, (int)_startPosY + 10, (int)_startPosX, (int)_startPosY);
g2d.drawLine((int)_startPosX, (int)_startPosY, (int)_startPosX + 10, (int)_startPosY + 10);
@ -200,7 +197,6 @@ public class DrawingPlane extends JPanel
public float[] GetCurrentPosition()
{
return new float[] {_startPosX, _startPosX + _airbusWidth,
_startPosY + _airbusHeight, _startPosX};
return new float[]{_startPosX, _startPosY, _startPosX + _airbusWidth, _startPosY + _airbusHeight};
}
}

View File

@ -52,4 +52,3 @@ public class DrawingRectAirplaneWindow extends JComponent implements IAdditional
}
}
}

View File

@ -2,23 +2,28 @@ import java.awt.*;
public class EntityPlane
{
public int Speed; //скорость
//скорость
public int Speed;
public int GetSpeed(){
return Speed;
}
public float Weight; //вес
//вес
public float Weight;
public float GetWeight(){
return Weight;
}
public Color CorpusColor; //цвет корпуса
public Color GetColor(){
//цвет корпуса
public Color CorpusColor;
public Color GetColor()
{
return CorpusColor;
}
//шаг перемещения самолёта
public float GetStep(){
public float GetStep()
{
return Speed * 100 / Weight;
}

View File

@ -93,7 +93,9 @@
<constraints>
<grid row="1" column="0" row-span="1" col-span="8" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<properties>
<foreground color="-4473925"/>
</properties>
<border type="none"/>
<children/>
</grid>
@ -105,7 +107,7 @@
<model>
<item value="Простая карта"/>
<item value="Буря в пустыне"/>
<item value="Звёздные Войны"/>
<item value="Звёздные войны"/>
</model>
<toolTipText value="" noi18n="true"/>
</properties>

View File

@ -3,11 +3,8 @@ 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
{
@ -32,31 +29,11 @@ public class FormMap extends JFrame
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);
if (bufferImg == null) {
return;
}
validate();
}
@Override
public void paint(Graphics g)
{
super.paint(g);
Draw();
PictureBoxPlane.getGraphics().drawImage(bufferImg, 0, 0, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), null);
}
public void SetData(DrawingPlane _plane)
@ -68,17 +45,19 @@ public class FormMap extends JFrame
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);
if (_abstractMap != null)
{
bufferImg = _abstractMap.CreateMap(PictureBoxPlane.getWidth(),
PictureBoxPlane.getHeight(), new DrawningObjectPlane(_plane));
}
PictureBoxPlane.revalidate();
}
public FormMap()
{
_abstractMap = new SimpleMap();
//создание строки отображения скорости, веса и цвета объекта
Box LableBox = Box.createHorizontalBox();
LableBox.setMinimumSize(new Dimension(1, 20));
@ -102,8 +81,6 @@ public class FormMap extends JFrame
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) {
@ -113,6 +90,7 @@ public class FormMap extends JFrame
plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100),
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
SetData(plane);
Draw();
}
});
@ -129,6 +107,7 @@ public class FormMap extends JFrame
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
PictureBoxPlane.repaint();
Draw();
}
});
@ -145,6 +124,7 @@ public class FormMap extends JFrame
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
PictureBoxPlane.repaint();
Draw();
}
});
@ -161,6 +141,7 @@ public class FormMap extends JFrame
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
PictureBoxPlane.repaint();
Draw();
}
});
@ -177,6 +158,7 @@ public class FormMap extends JFrame
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
PictureBoxPlane.repaint();
Draw();
}
});
@ -190,6 +172,7 @@ public class FormMap extends JFrame
_plane.SetPosition(rnd.nextInt(100, 500), rnd.nextInt(10, 100),
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
SetData(_plane);
Draw();
}
});

View File

@ -1,7 +1,6 @@
import java.awt.*;
public interface IAdditionalDrawingObject
{
public interface IAdditionalDrawingObject {
void SetAddEnum(int airplaneWindow);
void DrawAirplaneWindow(Color colorAirplaneWindow, Graphics g, float _startPosX, float _startPosY);
}

View File

@ -39,13 +39,13 @@ public class SimpleMap extends AbstractMap{
protected void DrawRoadPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(roadColor);
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(_size_x), (int)(_size_y));
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j *(_size_y + 1)));
}
@Override
protected void DrawBarrierPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(barriedColor);
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(_size_x), (int)(_size_y));
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j *(_size_y + 1)));
}
}

View File

@ -42,7 +42,7 @@ public class StarWarsMap extends AbstractMap{
protected void DrawRoadPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(roadColor);
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(_size_x), (int)(_size_y));
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j *(_size_y + 1)));
}
@Override
@ -50,6 +50,6 @@ public class StarWarsMap extends AbstractMap{
Graphics2D g2d = (Graphics2D)g;
barriedColor = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
g2d.setPaint(barriedColor);
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(_size_x), (int)(_size_y));
g2d.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j *(_size_y + 1)));
}
}