Хард лаб 5 готова, осталось почистить код и проверить баги

This commit is contained in:
Данила Мочалов 2022-11-15 20:22:49 +04:00
parent 6e69f8b100
commit 7e8bac26f3
13 changed files with 188 additions and 20 deletions

View File

@ -18,9 +18,9 @@ public class DrawningLocomotive {
private int _locomotiveHeight = 50; private int _locomotiveHeight = 50;
/// Инициализация свойств /// Инициализация свойств
private final Random random = new Random(); private final Random random = new Random();
public DrawningLocomotive(int speed, float weight, Color bodyColor) public DrawningLocomotive(int speed, float weight, Color bodyColor, int wheelsCount)
{ {
int randExtra = random.nextInt(2); /*int randExtra = random.nextInt(2);
switch (random.nextInt(3)){ switch (random.nextInt(3)){
case 0: case 0:
drawningExtra = new ExtraStarWheelDraw(randExtra, bodyColor); drawningExtra = new ExtraStarWheelDraw(randExtra, bodyColor);
@ -31,7 +31,8 @@ public class DrawningLocomotive {
case 2: case 2:
drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor); drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor);
break; break;
} }*/
drawningExtra = new ExtraWheelsDraw(wheelsCount, bodyColor);
Locomotive = new EntityLocomotive(speed, weight, bodyColor); Locomotive = new EntityLocomotive(speed, weight, bodyColor);
} }
@ -41,13 +42,17 @@ public class DrawningLocomotive {
} }
// Новый конструктор // Новый конструктор
protected DrawningLocomotive (int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) protected DrawningLocomotive (int speed, float weight, Color bodyColor, int wheelsCount, int locomotiveWidth, int locomotiveHeight)
{ {
this(speed, weight, bodyColor); this(speed, weight, bodyColor, wheelsCount);
_locomotiveWidth = locomotiveWidth; _locomotiveWidth = locomotiveWidth;
_locomotiveHeight = locomotiveHeight; _locomotiveHeight = locomotiveHeight;
} }
public void SetColor(Color color) {
Locomotive.SetColor(color);
}
/// Установка позиции локомотива /// Установка позиции локомотива
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
{ {

View File

@ -1,8 +1,8 @@
import java.awt.*; import java.awt.*;
public class DrawningWarmlyLocomotive extends DrawningLocomotive{ public class DrawningWarmlyLocomotive extends DrawningLocomotive{
public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean storage) public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, int wheelsCount, Color extraColor, boolean pipe, boolean storage)
{ {
super(speed, weight, bodyColor, 140, 70); super(speed, weight, bodyColor,wheelsCount, 140, 70);
Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage);
} }
@ -10,6 +10,13 @@ public class DrawningWarmlyLocomotive extends DrawningLocomotive{
super(locomotive, extra); super(locomotive, extra);
Locomotive = locomotive; Locomotive = locomotive;
} }
@Override
public void SetColor(Color color) {
((EntityWarmlyLocomotive) Locomotive).SetColor(color);
}
public void SetExtraColor(Color color) {
((EntityWarmlyLocomotive) Locomotive).SetExtraColor(color);
}
@Override @Override
public void DrawTransport(Graphics2D g) public void DrawTransport(Graphics2D g)

View File

@ -14,6 +14,9 @@ public class EntityLocomotive {
public Color getBodyColor() { public Color getBodyColor() {
return BodyColor; return BodyColor;
} }
public void SetColor(Color color) {
BodyColor = color;
}
public float Step () { public float Step () {
return Speed * 100 / Weight; return Speed * 100 / Weight;

View File

@ -1,7 +1,7 @@
import java.awt.*; import java.awt.*;
public class EntityWarmlyLocomotive extends EntityLocomotive{ public class EntityWarmlyLocomotive extends EntityLocomotive{
public final Color ExtraColor; public Color ExtraColor;
public final boolean Pipe; public final boolean Pipe;
public final boolean FuelStorage; public final boolean FuelStorage;
public EntityWarmlyLocomotive (int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean fuelStorage) public EntityWarmlyLocomotive (int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean fuelStorage)
@ -12,4 +12,8 @@ public class EntityWarmlyLocomotive extends EntityLocomotive{
FuelStorage = fuelStorage; FuelStorage = fuelStorage;
} }
public void SetExtraColor(Color color) {
ExtraColor = color;
}
} }

15
EventListener.java Normal file
View File

@ -0,0 +1,15 @@
import java.util.ArrayList;
import java.util.function.Consumer;
public class EventListener<T> {
private ArrayList<Consumer<T>> listeners = new ArrayList<>();
public void Add(Consumer<T> listener) {
listeners.add(listener);
}
public void Emit(T artillery) {
for (var listener : listeners) {
listener.accept(artillery);
}
}
}

View File

@ -3,6 +3,7 @@ import java.awt.*;
public class ExtraRoundWheelDraw implements IDrawningExtra{ public class ExtraRoundWheelDraw implements IDrawningExtra{
private WheelsCount wheelsCount = WheelsCount.Two; private WheelsCount wheelsCount = WheelsCount.Two;
private ExtraWheelsDraw extraWheelsDraw; private ExtraWheelsDraw extraWheelsDraw;
private Color color;
public void setExtraNum(int num) { public void setExtraNum(int num) {
switch (num) { switch (num) {
case 0: { case 0: {
@ -23,9 +24,13 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{
extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor);
} }
public void SetColor(Color color) {
this.color = color;
}
public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { public void DrawExtra(int startPosX, int startPosY, Graphics2D g) {
extraWheelsDraw.DrawExtra(startPosX, startPosY, g); extraWheelsDraw.DrawExtra(startPosX, startPosY, g);
g.setColor(Color.BLACK); g.setColor(color);
g.fillOval(startPosX + 5, startPosY + 35, 10, 10); g.fillOval(startPosX + 5, startPosY + 35, 10, 10);
g.fillOval(startPosX + 95, startPosY + 35, 10, 10); g.fillOval(startPosX + 95, startPosY + 35, 10, 10);
switch (wheelsCount) { switch (wheelsCount) {

View File

@ -3,6 +3,7 @@ import java.awt.*;
public class ExtraStarWheelDraw implements IDrawningExtra{ public class ExtraStarWheelDraw implements IDrawningExtra{
private WheelsCount wheelsCount = WheelsCount.Two; private WheelsCount wheelsCount = WheelsCount.Two;
private ExtraWheelsDraw extraWheelsDraw; private ExtraWheelsDraw extraWheelsDraw;
private Color color;
public void setExtraNum(int num) { public void setExtraNum(int num) {
switch (num) { switch (num) {
case 0: { case 0: {
@ -23,6 +24,10 @@ public class ExtraStarWheelDraw implements IDrawningExtra{
extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor);
} }
public void SetColor(Color color) {
this.color = color;
}
public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { public void DrawExtra(int startPosX, int startPosY, Graphics2D g) {
extraWheelsDraw.DrawExtra(startPosX, startPosY, g); extraWheelsDraw.DrawExtra(startPosX, startPosY, g);
DrawStarOnWheel(startPosX, startPosY + 30, g); DrawStarOnWheel(startPosX, startPosY + 30, g);
@ -41,6 +46,7 @@ public class ExtraStarWheelDraw implements IDrawningExtra{
} }
private void DrawStarOnWheel(int startPosX, int startPosY, Graphics2D g) { private void DrawStarOnWheel(int startPosX, int startPosY, Graphics2D g) {
g.setColor(color);
g.drawLine(startPosX + 10, startPosY, startPosX + 15, startPosY + 17); g.drawLine(startPosX + 10, startPosY, startPosX + 15, startPosY + 17);
g.drawLine(startPosX + 10, startPosY, startPosX + 5, startPosY + 17); g.drawLine(startPosX + 10, startPosY, startPosX + 5, startPosY + 17);
g.drawLine(startPosX + 15, startPosY + 17, startPosX + 2, startPosY + 8); g.drawLine(startPosX + 15, startPosY + 17, startPosX + 2, startPosY + 8);

View File

@ -4,11 +4,11 @@ public class ExtraWheelsDraw implements IDrawningExtra{
private WheelsCount wheelsCount = WheelsCount.Two; private WheelsCount wheelsCount = WheelsCount.Two;
public void setExtraNum(int num) { public void setExtraNum(int num) {
switch (num) { switch (num) {
case 0: { case 3: {
wheelsCount = WheelsCount.Three; wheelsCount = WheelsCount.Three;
break; break;
} }
case 1: { case 4: {
wheelsCount = WheelsCount.Four; wheelsCount = WheelsCount.Four;
break; break;
} }
@ -17,6 +17,9 @@ public class ExtraWheelsDraw implements IDrawningExtra{
} }
} }
private Color color; private Color color;
public void SetColor(Color color) {
this.color = color;
}
public ExtraWheelsDraw(int num, Color color) { public ExtraWheelsDraw(int num, Color color) {
setExtraNum(num); setExtraNum(num);
@ -27,19 +30,19 @@ public class ExtraWheelsDraw implements IDrawningExtra{
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawOval(startPosX, startPosY + 30, 20, 20); g.drawOval(startPosX, startPosY + 30, 20, 20);
g.drawOval(startPosX + 90, startPosY + 30, 20, 20); g.drawOval(startPosX + 90, startPosY + 30, 20, 20);
g.setColor(color); g.setColor(Color.BLACK);
g.fillOval(startPosX, startPosY + 30, 20, 20); g.fillOval(startPosX, startPosY + 30, 20, 20);
g.fillOval(startPosX + 90, startPosY + 30, 20, 20); g.fillOval(startPosX + 90, startPosY + 30, 20, 20);
switch (wheelsCount) { switch (wheelsCount) {
case Four: { case Four: {
g.setColor(Color.BLACK);
g.drawOval(startPosX + 70, startPosY + 30, 20, 20);
g.setColor(color); g.setColor(color);
g.drawOval(startPosX + 70, startPosY + 30, 20, 20);
g.setColor(Color.BLACK);
g.fillOval(startPosX + 70, startPosY + 30, 20, 20); g.fillOval(startPosX + 70, startPosY + 30, 20, 20);
} }
case Three: { case Three: {
g.fillOval(startPosX + 20, startPosY + 30, 20, 20); g.fillOval(startPosX + 20, startPosY + 30, 20, 20);
g.setColor(Color.BLACK); g.setColor(color);
g.drawOval(startPosX + 20, startPosY + 30, 20, 20); g.drawOval(startPosX + 20, startPosY + 30, 20, 20);
break; break;
} }

View File

@ -30,7 +30,7 @@ public class FormLocomotive extends JComponent{
Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
_locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst); _locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst, rnd.nextInt(2) + 2);
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-75); _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-75);
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());
@ -47,6 +47,7 @@ public class FormLocomotive extends JComponent{
_locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, _locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
colorFirst, colorFirst,
rnd.nextInt(2) + 2,
colorSecond, colorSecond,
rnd.nextBoolean(), rnd.nextBoolean(),
rnd.nextBoolean()); rnd.nextBoolean());
@ -107,6 +108,7 @@ public class FormLocomotive extends JComponent{
super.repaint(); super.repaint();
} }
public static void main(String[] args) { public static void main(String[] args) {
new FormLocomotiveConfig();
new FormMapWithSetLocomotives();
} }
} }

View File

@ -1,7 +1,15 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Consumer;
public class FormLocomotiveConfig extends JFrame{ public class FormLocomotiveConfig extends JFrame{
// Рабочие поля
private DrawningLocomotive locomotive;
private EventListener<DrawningLocomotive> eventListener = new EventListener<>();
//Элементы формы
private JPanel FormPanel; private JPanel FormPanel;
private JPanel ParametersPanel; private JPanel ParametersPanel;
private JPanel CreatePanel; private JPanel CreatePanel;
@ -45,17 +53,106 @@ public class FormLocomotiveConfig extends JFrame{
MainColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); MainColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
ExtraColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); ExtraColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Модели для намериков
SpeedSpinner.setModel(new SpinnerNumberModel(100, 50, 1000, 10));
WeightSpinner.setModel(new SpinnerNumberModel(1000, 1000, 5000, 10));
WheelsCountSpinner.setModel(new SpinnerNumberModel(2, 2, 4, 1));
//Обработчик d&d
var DragDropAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mouseReleased(e);
}
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
Drop((JComponent) e.getSource());
}
};
SimpleObjectLabel.addMouseListener(DragDropAdapter);
AdvancedObjectLabel.addMouseListener(DragDropAdapter);
StarWheelsLabel.addMouseListener(DragDropAdapter);
RoundWheelsLabel.addMouseListener(DragDropAdapter);
StarWheelsLabel.addMouseListener(DragDropAdapter);
RedColorPanel.addMouseListener(DragDropAdapter);
GreenColorPanel.addMouseListener(DragDropAdapter);
BlueColorPanel.addMouseListener(DragDropAdapter);
YellowColorPanel.addMouseListener(DragDropAdapter);
WhiteColorPanel.addMouseListener(DragDropAdapter);
BlackColorPanel.addMouseListener(DragDropAdapter);
AquaColorPanel.addMouseListener(DragDropAdapter);
PurpleColorPanel.addMouseListener(DragDropAdapter);
AddButton.addActionListener(e -> {
eventListener.Emit(locomotive);
dispose();
});
CancelButton.addActionListener(e -> dispose());
// Параметры фрейма
setContentPane(FormPanel); setContentPane(FormPanel);
setSize(800, 500); setSize(800, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true); setVisible(true);
} }
// Drop обработка
public void Drop (JComponent component) {
if (component == null) {
return;
}
if (component instanceof JPanel panel) {
if (MainColorLabel.getMousePosition() != null) {
locomotive.SetColor(panel.getBackground());
locomotive.drawningExtra.SetColor(panel.getBackground());
}
if (ExtraColorLabel.getMousePosition() != null && locomotive instanceof DrawningWarmlyLocomotive warmlyLocomotive) {
warmlyLocomotive.SetExtraColor(panel.getBackground());
}
}
if (component instanceof JLabel label && ObjectViewPanel.getMousePosition() != null) {
int speed = (Integer) SpeedSpinner.getValue();
int weight = (Integer) WeightSpinner.getValue();
int wheelsCount = (Integer) WheelsCountSpinner.getValue();
boolean pipe = PipeCheckBox.isSelected();
boolean fuel = FuelCheckBox.isSelected();
if (label == SimpleObjectLabel) {
locomotive = new DrawningLocomotive(speed, weight, Color.WHITE, wheelsCount);
} else if (label == AdvancedObjectLabel) {
locomotive = new DrawningWarmlyLocomotive(speed, weight, Color.WHITE, wheelsCount, Color.WHITE, pipe, fuel);
} else if (locomotive != null && label == SimpleWheelsLabel) {
locomotive.drawningExtra = new ExtraWheelsDraw(wheelsCount, locomotive.Locomotive.getBodyColor());
locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor());
} else if (locomotive != null && label == StarWheelsLabel) {
locomotive.drawningExtra = new ExtraStarWheelDraw(wheelsCount, locomotive.Locomotive.getBodyColor());
locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor());
} else if (locomotive != null && label == RoundWheelsLabel) {
locomotive.drawningExtra = new ExtraRoundWheelDraw(wheelsCount, locomotive.Locomotive.getBodyColor());
locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor());
}
}
repaint();
}
public void AddListener(Consumer<DrawningLocomotive> listener) {
eventListener.Add(listener);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (locomotive != null) {
g = ObjectViewPanel.getGraphics();
locomotive.SetPosition(20, 20, ObjectViewPanel.getWidth(), ObjectViewPanel.getHeight());
locomotive.DrawTransport((Graphics2D) g);
}
}
} }

View File

@ -56,7 +56,7 @@ public class FormMap extends JComponent {
JButton createButton = new JButton("Create"); JButton createButton = new JButton("Create");
createButton.addActionListener(e -> { createButton.addActionListener(e -> {
Random rnd = new Random(); Random rnd = new Random();
var locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); var locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextInt(2) + 2);
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());
@ -69,6 +69,7 @@ public class FormMap extends JComponent {
Random rnd = new Random(); Random rnd = new Random();
var locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, var locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextInt(2) + 2,
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()); rnd.nextBoolean());

View File

@ -113,7 +113,7 @@ public class FormMapWithSetLocomotives extends JComponent {
// Кнопка добавления локомотива // Кнопка добавления локомотива
JButton addLocomotiveButton = new JButton("Add Locomotive"); JButton addLocomotiveButton = new JButton("Add Locomotive");
addLocomotiveButton.addActionListener(e -> { addLocomotiveButton.addActionListener(e -> {
// логика добавления /*// логика добавления
if (listBoxMaps.getSelectedIndex() == -1) if (listBoxMaps.getSelectedIndex() == -1)
{ {
return; return;
@ -132,7 +132,26 @@ public class FormMapWithSetLocomotives extends JComponent {
} }
else { else {
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION); JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
}*/
FormLocomotiveConfig formLocomotiveConfig = new FormLocomotiveConfig();
formLocomotiveConfig.setVisible(true);
formLocomotiveConfig.AddListener(locomotive -> {
if (listBoxMaps.getSelectedIndex() == -1) {
return;
} }
if (locomotive!=null) {
DrawningObjectLocomotive objectLocomotive = new DrawningObjectLocomotive(locomotive);
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
}
else {
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
}
}
});
}); });
statusPanel.add(addLocomotiveButton); statusPanel.add(addLocomotiveButton);

View File

@ -3,4 +3,5 @@ import java.awt.*;
public interface IDrawningExtra { public interface IDrawningExtra {
void setExtraNum(int num); void setExtraNum(int num);
void DrawExtra(int startPosX, int startPosY, Graphics2D g); void DrawExtra(int startPosX, int startPosY, Graphics2D g);
void SetColor(Color color);
} }