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

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"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <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" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

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

View File

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

View File

@ -39,13 +39,13 @@ public class DesertStormMap extends AbstractMap{
protected void DrawRoadPart(Graphics g, int i, int j) { protected void DrawRoadPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(roadColor); 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 @Override
protected void DrawBarrierPart(Graphics g, int i, int j) { protected void DrawBarrierPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(barriedColor); 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) 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); Plane = new EntityAirbus(speed, weight, corpusColor, addColor, addCompartment, addEngine);
} }

View File

@ -135,8 +135,9 @@ public class DrawingPlane extends JPanel
Graphics2D g2d = (Graphics2D)g; 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); 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 + 21, 2, 2);
g2d.fillRect((int)_startPosX + 35, (int)_startPosY + 23, 4, 4); 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 + 10, (int)_startPosX, (int)_startPosY);
g2d.drawLine((int)_startPosX, (int)_startPosY, (int)_startPosX + 10, (int)_startPosY + 10); g2d.drawLine((int)_startPosX, (int)_startPosY, (int)_startPosX + 10, (int)_startPosY + 10);
@ -200,7 +197,6 @@ public class DrawingPlane extends JPanel
public float[] GetCurrentPosition() public float[] GetCurrentPosition()
{ {
return new float[] {_startPosX, _startPosX + _airbusWidth, return new float[]{_startPosX, _startPosY, _startPosX + _airbusWidth, _startPosY + _airbusHeight};
_startPosY + _airbusHeight, _startPosX};
} }
} }

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 class EntityPlane
{ {
public int Speed; //скорость //скорость
public int Speed;
public int GetSpeed(){ public int GetSpeed(){
return Speed; return Speed;
} }
public float Weight; //вес //вес
public float Weight;
public float GetWeight(){ public float GetWeight(){
return Weight; return Weight;
} }
public Color CorpusColor; //цвет корпуса //цвет корпуса
public Color GetColor(){ public Color CorpusColor;
public Color GetColor()
{
return CorpusColor; return CorpusColor;
} }
//шаг перемещения самолёта //шаг перемещения самолёта
public float GetStep(){ public float GetStep()
{
return Speed * 100 / Weight; return Speed * 100 / Weight;
} }

View File

@ -93,7 +93,9 @@
<constraints> <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"/> <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> </constraints>
<properties/> <properties>
<foreground color="-4473925"/>
</properties>
<border type="none"/> <border type="none"/>
<children/> <children/>
</grid> </grid>
@ -105,7 +107,7 @@
<model> <model>
<item value="Простая карта"/> <item value="Простая карта"/>
<item value="Буря в пустыне"/> <item value="Буря в пустыне"/>
<item value="Звёздные Войны"/> <item value="Звёздные войны"/>
</model> </model>
<toolTipText value="" noi18n="true"/> <toolTipText value="" noi18n="true"/>
</properties> </properties>

View File

@ -3,11 +3,8 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.Random; import java.util.Random;
import java.util.Set;
public class FormMap extends JFrame public class FormMap extends JFrame
{ {
@ -32,31 +29,11 @@ public class FormMap extends JFrame
public void Draw() public void Draw()
{ {
PictureBoxPlane.removeAll(); if (bufferImg == null) {
return;
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);
} }
validate(); PictureBoxPlane.getGraphics().drawImage(bufferImg, 0, 0, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), null);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
Draw();
} }
public void SetData(DrawingPlane _plane) 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() + LabelColor.setText("Цвет: r = " + _plane.GetPlane().GetColor().getRed() + " g = " + _plane.GetPlane().GetColor().getGreen() +
" b = " + _plane.GetPlane().GetColor().getBlue()); " b = " + _plane.GetPlane().GetColor().getBlue());
JLabel imageWithMapAndObject = new JLabel(); if (_abstractMap != null)
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); {
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); bufferImg = _abstractMap.CreateMap(PictureBoxPlane.getWidth(),
imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.CreateMap(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), new DrawningObjectPlane((_plane))))); PictureBoxPlane.getHeight(), new DrawningObjectPlane(_plane));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); }
PictureBoxPlane.revalidate(); PictureBoxPlane.revalidate();
} }
public FormMap() public FormMap()
{ {
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
//создание строки отображения скорости, веса и цвета объекта //создание строки отображения скорости, веса и цвета объекта
Box LableBox = Box.createHorizontalBox(); Box LableBox = Box.createHorizontalBox();
LableBox.setMinimumSize(new Dimension(1, 20)); LableBox.setMinimumSize(new Dimension(1, 20));
@ -102,8 +81,6 @@ public class FormMap extends JFrame
System.out.println(ex.getMessage()); 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() { ButtonCreate.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -113,6 +90,7 @@ public class FormMap extends JFrame
plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100),
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
SetData(plane); SetData(plane);
Draw();
} }
}); });
@ -129,6 +107,7 @@ public class FormMap extends JFrame
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate(); PictureBoxPlane.revalidate();
PictureBoxPlane.repaint(); PictureBoxPlane.repaint();
Draw();
} }
}); });
@ -145,6 +124,7 @@ public class FormMap extends JFrame
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate(); PictureBoxPlane.revalidate();
PictureBoxPlane.repaint(); PictureBoxPlane.repaint();
Draw();
} }
}); });
@ -161,6 +141,7 @@ public class FormMap extends JFrame
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate(); PictureBoxPlane.revalidate();
PictureBoxPlane.repaint(); PictureBoxPlane.repaint();
Draw();
} }
}); });
@ -177,6 +158,7 @@ public class FormMap extends JFrame
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate(); PictureBoxPlane.revalidate();
PictureBoxPlane.repaint(); PictureBoxPlane.repaint();
Draw();
} }
}); });
@ -190,6 +172,7 @@ public class FormMap extends JFrame
_plane.SetPosition(rnd.nextInt(100, 500), rnd.nextInt(10, 100), _plane.SetPosition(rnd.nextInt(100, 500), rnd.nextInt(10, 100),
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
SetData(_plane); SetData(_plane);
Draw();
} }
}); });

View File

@ -1,7 +1,6 @@
import java.awt.*; import java.awt.*;
public interface IAdditionalDrawingObject public interface IAdditionalDrawingObject {
{
void SetAddEnum(int airplaneWindow); void SetAddEnum(int airplaneWindow);
void DrawAirplaneWindow(Color colorAirplaneWindow, Graphics g, float _startPosX, float _startPosY); 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) { protected void DrawRoadPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(roadColor); 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 @Override
protected void DrawBarrierPart(Graphics g, int i, int j) { protected void DrawBarrierPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(barriedColor); 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) { protected void DrawRoadPart(Graphics g, int i, int j) {
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(roadColor); 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 @Override
@ -50,6 +50,6 @@ public class StarWarsMap extends AbstractMap{
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
barriedColor = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); barriedColor = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
g2d.setPaint(barriedColor); 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)));
} }
} }