Added & fixed RailMap

This commit is contained in:
Данила Мочалов 2022-10-08 20:51:36 +04:00
parent aba03789ef
commit bf246e17d4
4 changed files with 65 additions and 5 deletions

View File

@ -47,6 +47,7 @@ public class FormMap extends JComponent {
_abstractMap = new SpikeMap(); _abstractMap = new SpikeMap();
break; break;
case ("Rail Map"): case ("Rail Map"):
_abstractMap = new RailMap();
break; break;
} }
} }
@ -59,7 +60,7 @@ public class FormMap extends JComponent {
speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed());
weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight());
colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue()); colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue());
if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 500, new DrawningObjectLocomotive(locomotive)); if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawningObjectLocomotive(locomotive));
repaint(); repaint();
}); });
@ -74,7 +75,7 @@ public class FormMap extends JComponent {
speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed());
weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight());
colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue() ); colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue() );
if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 500, new DrawningObjectLocomotive(locomotive)); if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawningObjectLocomotive(locomotive));
repaint(); repaint();
}); });
statusPanel.add(mapSelectComboBox); statusPanel.add(mapSelectComboBox);
@ -114,13 +115,14 @@ public class FormMap extends JComponent {
statusPanel.add(moveRightButton); statusPanel.add(moveRightButton);
formFrame.getContentPane().add(this); formFrame.getContentPane().add(this);
super.repaint();
} }
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g; Graphics2D g2 = (Graphics2D)g;
if (bufferImg != null) g2.drawImage(bufferImg, 0,0,1000,500,null); if (bufferImg != null) g2.drawImage(bufferImg, 0,0,1000,490,null);
super.repaint(); super.repaint();
} }

58
RailMap.java Normal file
View File

@ -0,0 +1,58 @@
import java.awt.*;
public class RailMap extends AbstractMap{
/// Цвет участка закрытого
private final Color barrierColor = Color.BLACK;
/// Цвет участка открытого
private final Color roadColor = Color.PINK;
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
g.setColor(barrierColor);
g.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)
{
g.setColor(roadColor);
g.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[0].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
while (counter < 1)
{
int y = _random.nextInt(85);
for(int x = 0; x < 99; x++)
{
_map[x][y] = _barrier;
_map[x][y + 5] = _barrier;
if (x % 5 == 0)
{
_map[x][y + 1] = _barrier;
_map[x][y + 2] = _barrier;
_map[x][y + 3] = _barrier;
_map[x][y + 4] = _barrier;
}
}
counter += 1;
}
}
}

View File

@ -38,7 +38,7 @@ public class SimpleMap extends AbstractMap{
while (counter < 50) while (counter < 50)
{ {
int x = _random.nextInt(100); int x = _random.nextInt(100);
int y = _random.nextInt(100); int y = _random.nextInt(85);
if (_map[x][y] == _freeRoad) if (_map[x][y] == _freeRoad)
{ {
_map[x][y] = _barrier; _map[x][y] = _barrier;

View File

@ -38,7 +38,7 @@ public class SpikeMap extends AbstractMap{
while (counter < 15) while (counter < 15)
{ {
int x = _random.nextInt(95) + 1; int x = _random.nextInt(95) + 1;
int y = _random.nextInt(95) + 1; int y = _random.nextInt(85) + 1;
if (_map[x][y] == _freeRoad) if (_map[x][y] == _freeRoad)
{ {
_map[x][y] = _barrier; _map[x][y] = _barrier;