diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 5a4e57b..3293091 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -1,9 +1,8 @@ import java.awt.*; import java.util.Random; -class DrawningLocomotive { +public class DrawningLocomotive { public EntityLocomotive Locomotive; - public ExtraWheelsDraw extraWheelsDraw; public IDrawningExtra drawningExtra; /// Левая координата отрисовки локомотива protected float _startPosX; @@ -22,13 +21,12 @@ class DrawningLocomotive { public DrawningLocomotive(int speed, float weight, Color bodyColor) { int randExtra = random.nextInt(2); - extraWheelsDraw = new ExtraWheelsDraw(randExtra, bodyColor); switch (random.nextInt(3)){ case 0: - drawningExtra = new ExtraStarWheelDraw(randExtra); + drawningExtra = new ExtraStarWheelDraw(randExtra, bodyColor); break; case 1: - drawningExtra = new ExtraRoundWheelDraw(randExtra); + drawningExtra = new ExtraRoundWheelDraw(randExtra, bodyColor); break; case 2: drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor); @@ -37,6 +35,11 @@ class DrawningLocomotive { Locomotive = new EntityLocomotive(speed, weight, bodyColor); } + public DrawningLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) { + drawningExtra = extra; + Locomotive = locomotive; + } + // Новый конструктор protected DrawningLocomotive (int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) { @@ -122,8 +125,6 @@ class DrawningLocomotive { //дверь g.setColor(Color.BLACK); g.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); - //колеса - extraWheelsDraw.DrawExtra((int)_startPosX, (int)_startPosY, g); //extra drawningExtra.DrawExtra((int)_startPosX, (int)_startPosY, g); //движок diff --git a/DrawningWarmlyLocomotive.java b/DrawningWarmlyLocomotive.java index f7fd094..4019911 100644 --- a/DrawningWarmlyLocomotive.java +++ b/DrawningWarmlyLocomotive.java @@ -5,6 +5,12 @@ public class DrawningWarmlyLocomotive extends DrawningLocomotive{ super(speed, weight, bodyColor, 140, 70); Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); } + + public DrawningWarmlyLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) { + super(locomotive, extra); + Locomotive = locomotive; + } + @Override public void DrawTransport(Graphics2D g) { diff --git a/EntityWithExtraCreator.java b/EntityWithExtraCreator.java new file mode 100644 index 0000000..deda0ef --- /dev/null +++ b/EntityWithExtraCreator.java @@ -0,0 +1,44 @@ +import java.lang.reflect.Array; +import java.util.Random; + +public class EntityWithExtraCreator { + private final Object[] entityArr; + private final Object[] extraArr; + + int entitiesCount = 0; + int extraCount = 0; + + public EntityWithExtraCreator(int countEntities, int countExtra) { + entityArr = new Object[countEntities]; + extraArr = new Object[countExtra]; + } + + public void Insert(T entityLocomotive) { + if(entitiesCount < entityArr.length) { + entityArr[entitiesCount] = entityLocomotive; + entitiesCount++; + } + } + + public void Insert (U extra) { + if(extraCount < extraArr.length) { + extraArr[extraCount] = extra; + extraCount++; + } + } + + public DrawningLocomotive getEntityWithExtra() { + Random random = new Random(); + int getEntityRandomIndex = random.nextInt(entityArr.length); + int getExtraRandomIndex = random.nextInt(extraArr.length); + + EntityLocomotive locomotive = (T)entityArr[getEntityRandomIndex]; + IDrawningExtra extra = (U)extraArr[getExtraRandomIndex]; + + if (locomotive instanceof EntityWarmlyLocomotive) { + return new DrawningWarmlyLocomotive(locomotive, extra); + } + return new DrawningLocomotive(locomotive, extra); + } + +} diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java index 5f28863..e14642b 100644 --- a/ExtraRoundWheelDraw.java +++ b/ExtraRoundWheelDraw.java @@ -2,6 +2,7 @@ import java.awt.*; public class ExtraRoundWheelDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; + private ExtraWheelsDraw extraWheelsDraw; public void setExtraNum(int num) { switch (num) { case 0: { @@ -17,11 +18,13 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{ } } - public ExtraRoundWheelDraw (int num) { + public ExtraRoundWheelDraw (int num, Color bodyColor) { setExtraNum(num); + extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); } public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + extraWheelsDraw.DrawExtra(startPosX, startPosY, g); g.setColor(Color.BLACK); g.fillOval(startPosX + 5, startPosY + 35, 10, 10); g.fillOval(startPosX + 95, startPosY + 35, 10, 10); diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java index dac1eb8..3b415d6 100644 --- a/ExtraStarWheelDraw.java +++ b/ExtraStarWheelDraw.java @@ -2,6 +2,7 @@ import java.awt.*; public class ExtraStarWheelDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; + private ExtraWheelsDraw extraWheelsDraw; public void setExtraNum(int num) { switch (num) { case 0: { @@ -17,11 +18,13 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ } } - public ExtraStarWheelDraw (int num) { + public ExtraStarWheelDraw (int num, Color bodyColor) { setExtraNum(num); + extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); } public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + extraWheelsDraw.DrawExtra(startPosX, startPosY, g); DrawStarOnWheel(startPosX, startPosY + 30, g); DrawStarOnWheel(startPosX + 90, startPosY + 30, g); switch (wheelsCount) { diff --git a/FormEntityWithExtraGallery.java b/FormEntityWithExtraGallery.java new file mode 100644 index 0000000..80f05ec --- /dev/null +++ b/FormEntityWithExtraGallery.java @@ -0,0 +1,87 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormEntityWithExtraGallery extends JComponent { + private DrawningLocomotive _locomotiveFirst; + private DrawningLocomotive _locomotiveSecond; + private DrawningLocomotive _locomotiveThird; + EntityWithExtraCreator entityWithExtraCreator; + + public FormEntityWithExtraGallery() { + JFrame formFrame = new JFrame("Gallery"); + formFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + formFrame.setSize(900, 500); + formFrame.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new FlowLayout()); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.SOUTH); + + JButton showRandomEntity = new JButton("Create entity from parts"); + showRandomEntity.addActionListener(e -> { + + Random random = new Random(); + if (entityWithExtraCreator == null) { + entityWithExtraCreator = new EntityWithExtraCreator(20, 20); + for (int i = 0; i < 20; i ++) { + if (random.nextBoolean()) { + entityWithExtraCreator.Insert(new EntityLocomotive(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + } + else { + entityWithExtraCreator.Insert(new EntityWarmlyLocomotive(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + random.nextBoolean(), random.nextBoolean())); + } + } + for (int i = 0; i < 20; i ++) { + int extraRand = random.nextInt(3); + switch (extraRand) { + case 0: + entityWithExtraCreator.Insert(new ExtraWheelsDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 1: + entityWithExtraCreator.Insert(new ExtraStarWheelDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 2: + entityWithExtraCreator.Insert(new ExtraRoundWheelDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + } + } + } + _locomotiveFirst = entityWithExtraCreator.getEntityWithExtra(); + _locomotiveFirst.SetPosition(200, 200, formFrame.getWidth(), formFrame.getHeight() - 75); + + _locomotiveSecond = entityWithExtraCreator.getEntityWithExtra(); + _locomotiveSecond.SetPosition(400, 200, formFrame.getWidth(), formFrame.getHeight() - 75); + + _locomotiveThird = entityWithExtraCreator.getEntityWithExtra(); + _locomotiveThird.SetPosition(600, 200, formFrame.getWidth(), formFrame.getHeight() - 75); + repaint(); + }); + statusPanel.add(showRandomEntity); + + formFrame.getContentPane().add(this); + + formFrame.setVisible(true); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (_locomotiveFirst != null) _locomotiveFirst.DrawTransport(g2); + if (_locomotiveSecond != null) _locomotiveSecond.DrawTransport(g2); + if (_locomotiveThird != null) _locomotiveThird.DrawTransport(g2); + super.repaint(); + } +} diff --git a/FormLocomotive.java b/FormLocomotive.java index 7535926..8d7b5ee 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -1,30 +1,14 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.*; import java.util.Random; public class FormLocomotive extends JComponent{ private DrawningLocomotive _locomotive; - public FormLocomotive() { - JFrame formFrame = new JFrame("Locomotive"); - formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - formFrame.setSize(800, 500); - formFrame.setVisible(true); - formFrame.setLocationRelativeTo(null); - - formFrame.addComponentListener(new ComponentListener() { - @Override - public void componentResized(ComponentEvent e) { - if (_locomotive != null) _locomotive.ChangeBorders(formFrame.getWidth(), formFrame.getHeight()); - repaint(); - } - @Override - public void componentMoved(ComponentEvent e) {} - @Override - public void componentShown(ComponentEvent e) {} - @Override - public void componentHidden(ComponentEvent e) {} - }); + private DrawningLocomotive SelectedLocomotive; + public DrawningLocomotive getSelectedLocomotive() { + return SelectedLocomotive; + } + public FormLocomotive(JDialog caller) { Panel statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); @@ -39,8 +23,11 @@ public class FormLocomotive extends JComponent{ JButton createButton = new JButton("Create"); createButton.addActionListener(e -> { Random rnd = new Random(); - _locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); - _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75); + + 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.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-75); speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() ); @@ -50,20 +37,31 @@ public class FormLocomotive extends JComponent{ JButton modifiedButton = new JButton("Modified"); modifiedButton.addActionListener(e -> { Random rnd = new Random(); + + Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + _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)), + colorFirst, + colorSecond, rnd.nextBoolean(), rnd.nextBoolean()); - _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75); + _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75); speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() ); repaint(); }); + JButton selectLocomotiveButton = new JButton("Select"); + selectLocomotiveButton.addActionListener(e -> { + SelectedLocomotive = _locomotive; + caller.dispose(); + }); + statusPanel.add(createButton); statusPanel.add(modifiedButton); + statusPanel.add(selectLocomotiveButton); statusPanel.add(speedLabel); statusPanel.add(weightLabel); statusPanel.add(colorLabel); @@ -96,8 +94,6 @@ public class FormLocomotive extends JComponent{ statusPanel.add(moveDownButton); statusPanel.add(moveLeftButton); statusPanel.add(moveRightButton); - - formFrame.getContentPane().add(this); } @Override protected void paintComponent(Graphics g) { @@ -106,8 +102,7 @@ public class FormLocomotive extends JComponent{ if (_locomotive != null) _locomotive.DrawTransport(g2); super.repaint(); } - public static void main(String[] args) { - new FormMap(); + new FormMapWithSetLocomotives(); } } diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java new file mode 100644 index 0000000..318e071 --- /dev/null +++ b/FormMapWithSetLocomotives.java @@ -0,0 +1,205 @@ +import javax.swing.*; +import javax.swing.text.MaskFormatter; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.text.ParseException; + +public class FormMapWithSetLocomotives extends JComponent { + private BufferedImage bufferImg = null; + /// Объект от класса карты с набором объектов + private MapWithSetLocomotivesGeneric _mapLocomotivesCollectionGeneric; + public FormMapWithSetLocomotives() { + JFrame formFrame = new JFrame("Form Map With SetLocomotives"); + formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + formFrame.setSize(750, 500); + formFrame.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new GridLayout(0, 1, 20, 20)); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.EAST); + + // КомбоБокс с картами + String[] maps = { + "Simple Map", + "Spike Map", + "Rail Map" + }; + JComboBox mapSelectComboBox = new JComboBox(maps); + mapSelectComboBox.setEditable(true); + mapSelectComboBox.addActionListener(e -> { + AbstractMap map = null; + String item = (String)mapSelectComboBox.getSelectedItem(); + switch (item) { + case "Simple Map": + map = new SimpleMap(); + break; + case "Spike Map": + map = new SpikeMap(); + break; + case "Rail Map": + map = new RailMap(); + break; + } + if (map != null) + { + _mapLocomotivesCollectionGeneric = new MapWithSetLocomotivesGeneric + (600, 500, map); + } + else + { + _mapLocomotivesCollectionGeneric = null; + } + }); + statusPanel.add(mapSelectComboBox); + + // Кнопка добавления локомотива + JButton addLocomotiveButton = new JButton("Add Locomotive"); + addLocomotiveButton.addActionListener(e -> { + // логика добавления + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + JDialog dialog = new JDialog(formFrame, "Dialog", true); + FormLocomotive formLocomotive = new FormLocomotive(dialog); + dialog.setSize(800, 500); + dialog.setContentPane(formLocomotive); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive()); + if (_mapLocomotivesCollectionGeneric.Plus(locomotive) != -1) { + JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION); + bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); + repaint(); + } + else { + JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION); + } + }); + statusPanel.add(addLocomotiveButton); + + // Текстовое поле для ввода позиции с маской + JFormattedTextField maskedTextFieldPosition = null; + try { + MaskFormatter positionMask = new MaskFormatter("##"); + maskedTextFieldPosition = new JFormattedTextField(positionMask); + statusPanel.add(maskedTextFieldPosition); + } + catch (ParseException e) { + e.printStackTrace(); + } + //Кнопка удаления локомотива + JButton deleteLocomotiveButton = new JButton("Delete Locomotive"); + JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition; + deleteLocomotiveButton.addActionListener(e -> { + // логика удаления + if ((String)mapSelectComboBox.getSelectedItem() == null) { + return; + } + if (finalMaskedTextFieldPosition == null) { + return; + } + int position = Integer.parseInt(finalMaskedTextFieldPosition.getText()); + if (_mapLocomotivesCollectionGeneric.Minus(position) != null) { + JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION); + bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); + repaint(); + } + else{ + JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION); + } + }); + statusPanel.add(deleteLocomotiveButton); + //Кнопка просмотра хранилища + JButton showStorageButton = new JButton("Show Storage"); + showStorageButton.addActionListener(e -> { + // логика просмотра + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); + repaint(); + }); + statusPanel.add(showStorageButton); + //Кнопка просмотра карты + JButton showOnMapButton = new JButton("Show On Map"); + showOnMapButton.addActionListener(e -> { + // логика просмотра + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + bufferImg = _mapLocomotivesCollectionGeneric.ShowOnMap(); + }); + statusPanel.add(showOnMapButton); + + ActionListener moveButtonListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + String name = e.getActionCommand(); + Direction dir = Direction.None; + switch (name) + { + case "Up": + dir = Direction.Up; + break; + case "Down": + dir = Direction.Down; + break; + case "Left": + dir = Direction.Left; + break; + case "Right": + dir = Direction.Right; + break; + } + bufferImg = _mapLocomotivesCollectionGeneric.MoveObject(dir); + } + }; + + //Кнопки управления + JButton moveDownButton = new JButton("Down"); + moveDownButton.addActionListener(moveButtonListener); + + JButton moveUpButton = new JButton("Up"); + moveUpButton.addActionListener(moveButtonListener); + + JButton moveLeftButton = new JButton("Left"); + moveLeftButton.addActionListener(moveButtonListener); + + JButton moveRightButton = new JButton("Right"); + moveRightButton.addActionListener(moveButtonListener); + + statusPanel.add(moveUpButton); + statusPanel.add(moveDownButton); + statusPanel.add(moveLeftButton); + statusPanel.add(moveRightButton); + + JButton showGalleryButton = new JButton("Show Gallery"); + showGalleryButton.addActionListener(e -> { + new FormEntityWithExtraGallery(); + }); + statusPanel.add(showGalleryButton); + + formFrame.getContentPane().add(this); + formFrame.setVisible(true); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (bufferImg != null) g2.drawImage(bufferImg, 0,0,600,500,null); + super.repaint(); + } + +} diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java new file mode 100644 index 0000000..d6bd589 --- /dev/null +++ b/MapWithSetLocomotivesGeneric.java @@ -0,0 +1,168 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetLocomotivesGeneric + +{ + /// Ширина окна отрисовки + private final int _pictureWidth; + /// Высота окна отрисовки + private final int _pictureHeight; + /// Размер занимаемого объектом места (ширина) + private final int _placeSizeWidth = 210; + /// Размер занимаемого объектом места (высота) + private final int _placeSizeHeight = 90; + + /// Набор объектов + private final SetLocomotivesGeneric _setLocomotives; + /// Карта + private final U _map; + /// Конструктор + public MapWithSetLocomotivesGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setLocomotives = new SetLocomotivesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + /// Добавление + public int Plus(T locomotive) + { + return this._setLocomotives.Insert(locomotive); + } + /// Удаление + public T Minus(int position) + { + return this._setLocomotives.Remove(position); + } + /// Вывод всего набора объектов + public BufferedImage ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + DrawBackground((Graphics2D)gr); + DrawLocomotives((Graphics2D)gr); + return bmp; + } + + /// Просмотр объекта на карте + public BufferedImage ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setLocomotives.Count(); i++) + { + var locomotive = _setLocomotives.Get(i); + if (locomotive != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + /// Перемещение объекта по крате + public BufferedImage MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + private void Shaking() + { + int j = _setLocomotives.Count() - 1; + for (int i = 0; i < _setLocomotives.Count(); i++) + { + if (_setLocomotives.Get(i) == null) + { + for (; j > i; j--) + { + var locomotive = _setLocomotives.Get(j); + if (locomotive != null) + { + _setLocomotives.Insert(locomotive, i); + _setLocomotives.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + /// Метод отрисовки фона + private void DrawBackground(Graphics2D g) + { + g.setColor(Color.WHITE); + g.fillRect(0,0,600, 500); + for (int j = _placeSizeHeight; j < _pictureHeight; j+= _placeSizeHeight) + { + //нижняя линия рельс + g.setColor(Color.BLACK); + g.setStroke(new BasicStroke(5)); + + g.drawLine(0, j, _pictureWidth, j); + for (int i = 0; i < _pictureWidth; i+=20) + { + g.drawLine(i, j, i, j + 10); + } + g.drawLine(0, j + 10, _pictureWidth, j + 10); + + //верхняя линия рельс + + g.setColor(Color.GRAY); + + g.drawLine(0, j - 20, _pictureWidth, j - 20); + for (int i = 0; i < _pictureWidth; i += 20) + { + g.drawLine(i, j - 20, i, j - 10); + } + g.drawLine(0, j - 10, _pictureWidth, j - 10); + + //фонари + for (int i = _placeSizeWidth; i < _pictureWidth; i += _placeSizeWidth) + { + g.setColor(Color.BLACK); + g.setStroke(new BasicStroke(10)); + g.drawLine(i, j - _placeSizeHeight + 20, i, j); + g.setColor(Color.YELLOW); + g.setStroke(new BasicStroke(20)); + g.drawLine(i, j - _placeSizeHeight + 18, i, j - _placeSizeHeight + 38); + } + } + g.setStroke(new BasicStroke(2)); + } + + /// Метод прорисовки объектов + private void DrawLocomotives(Graphics2D g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = 0; + int curHeight = 0; + + for (int i = 0; i < _setLocomotives.Count(); i++) + { + // установка позиции + if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight); + if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).DrawningObject(g); + if (curWidth < width) curWidth++; + else + { + curWidth = 0; + curHeight++; + } + } + } + +} diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java new file mode 100644 index 0000000..7256512 --- /dev/null +++ b/SetLocomotivesGeneric.java @@ -0,0 +1,61 @@ +public class SetLocomotivesGeneric +{ + private final T[] _places; + + public int Count() { + return _places.length; + } + + public SetLocomotivesGeneric(int count) { + _places = (T[]) new Object[count]; + } + + public int Insert (T locomotive) { + return Insert(locomotive, 0); + } + + public int Insert (T locomotive, int position) { + if (position >= _places.length || position < 0) return -1; + if (_places[position] == null) { + _places[position] = locomotive; + return position; + } + + int emptyEl = -1; + for (int i = position + 1; i < Count(); i++) + { + if (_places[i] == null) + { + emptyEl = i; + break; + } + } + if (emptyEl == -1) + { + return -1; + } + for (int i = emptyEl; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = locomotive; + return position; + } + + public T Remove (int position) { + if (position >= _places.length || position < 0) return null; + T result = _places[position]; + _places[position] = null; + return result; + } + + public T Get(int position) + { + if (position >= _places.length || position < 0) + { + return null; + } + return _places[position]; + } + +}