Minhasapov R.H. LabWork03 #3

Closed
safgerd wants to merge 4 commits from LabWork03 into LabWork02
14 changed files with 735 additions and 271 deletions

View File

@ -6,6 +6,10 @@ public class DrawningTrackedVehicle extends DrawningTracktor {
Tracktor = new EntityTrackedVehicle(speed, weight, bodyColor, dopColor, bucket, supports);
}
public DrawningTrackedVehicle(EntityTrackedVehicle entity, IDrawningRollers rollers) {
super(entity, rollers);
}
@Override
public void DrawTransport(Graphics2D g){
if (!(Tracktor instanceof EntityTrackedVehicle trackedVehicle))

View File

@ -22,6 +22,11 @@ public class DrawningTracktor {
drawningRollers = RollersType.random(countRollers, bodyColor);
}
public DrawningTracktor(EntityTracktor entity, IDrawningRollers rollers) {
Tracktor = entity;
drawningRollers = rollers;
}
protected DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers, int tracktorWidth, int tracktorHeight){
this(speed, weight, bodyColor, countRollers);
_tracktorWidth = tracktorWidth;

48
EntityWithRollers.java Normal file
View File

@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.Random;
public class EntityWithRollers<T extends EntityTracktor, U extends IDrawningRollers> {
static Random rnd = new Random();
private ArrayList<T> entities;
private ArrayList<U> rollers;
public int maxEntitiesCount = 0;
public int maxRollersCount = 0;
public EntityWithRollers(int count) {
entities = new ArrayList<T>();
rollers = new ArrayList<U>();
maxEntitiesCount = count;
maxRollersCount = count;
}
public boolean add(T entity) {
if (entities.size() >= maxEntitiesCount){
return false;
}
entities.add(entity);
return true;
}
public boolean add(U roller) {
if (rollers.size() >= maxRollersCount){
return false;
}
rollers.add(roller);
return true;
}
public IDrawningObject constructTracktor() {
if (entities.size() == 0 || rollers.size() == 0) {
return null;
}
EntityTracktor entity = (EntityTracktor) entities.get(rnd.nextInt(0, entities.size()));
IDrawningRollers roller = (IDrawningRollers) rollers.get(rnd.nextInt(0, rollers.size()));
if (entity instanceof EntityTrackedVehicle advancedEntity) {
return new DrawningObjectExcavator(new DrawningTrackedVehicle(advancedEntity, roller));
}
return new DrawningObjectExcavator(new DrawningTracktor(entity, roller));
}
}

41
FormGallery.form Normal file
View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormGallery">
<grid id="27dc6" binding="contentPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="60075" binding="pictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="eba14" class="javax.swing.JButton" binding="buttonRefresh">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Перезагрузить"/>
</properties>
</component>
<hspacer id="852b8">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="6b8fb">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</children>
</grid>
</form>

66
FormGallery.java Normal file
View File

@ -0,0 +1,66 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FormGallery extends JFrame {
private static final Random rnd = new Random();
private JPanel contentPanel;
private JButton buttonRefresh;
private JPanel pictureBox;
private IDrawningObject first;
private IDrawningObject second;
private IDrawningObject third;
private final EntityWithRollers<EntityTracktor, IDrawningRollers> storage;
public FormGallery() {
setTitle("Галлерея");
setContentPane(contentPanel);
storage = new EntityWithRollers<>(20);
for(int i = 0; i < 20; i++) {
if (rnd.nextBoolean()) {
storage.add(new EntityTrackedVehicle(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
rnd.nextBoolean(),
rnd.nextBoolean()
));
} else {
storage.add(new EntityTracktor(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))
));
}
storage.add(RollersType.random(rnd.nextInt(4, 7), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))));
}
buttonRefresh.addActionListener(e -> {
first = storage.constructTracktor();
second = storage.constructTracktor();
third = storage.constructTracktor();
first.setObject(0, 10, pictureBox.getWidth(), pictureBox.getHeight());
second.setObject(150, 10, pictureBox.getWidth(), pictureBox.getHeight());
third.setObject(300, 10, pictureBox.getWidth(), pictureBox.getHeight());
repaint();
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2g = (Graphics2D) pictureBox.getGraphics();
if (first != null && second != null && third != null) {
first.drawningObject(g2g);
second.drawningObject(g2g);
third.drawningObject(g2g);
}
}
}

View File

@ -1,137 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMap">
<grid id="27dc6" binding="ContentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="649" height="378"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="c78b3">
<constraints>
<grid row="1" column="2" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="a12e7" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/arrowUp.png"/>
<text value=""/>
</properties>
</component>
<component id="5942f" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/arrowLeft.png"/>
<text value=""/>
</properties>
</component>
<component id="65b3a" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/arrowDown.png"/>
<text value=""/>
</properties>
</component>
<component id="233b7" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/arrowRight.png"/>
<text value=""/>
</properties>
</component>
<component id="22c" class="javax.swing.JLabel" binding="speedLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость:"/>
</properties>
</component>
<component id="98c1e" class="javax.swing.JLabel" binding="weightLabel">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес:"/>
</properties>
</component>
<component id="252d3" class="javax.swing.JLabel" binding="colorLabel">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Цвет:"/>
</properties>
</component>
<component id="7e6d3" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="f3e60" class="javax.swing.JButton" binding="buttonCreateModif">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
<grid id="98ed5" binding="pictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="7" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="ee7c6" class="javax.swing.JComboBox" binding="comboBoxMapSelector">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Свалка карта"/>
</model>
</properties>
</component>
<hspacer id="4e1e1">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="d19f">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -1,116 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.util.Random;
public class FormMap extends JFrame {
private JPanel ContentPanel;
private JButton buttonCreate;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
private JButton buttonLeft;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonUp;
private JPanel pictureBox;
private JButton buttonCreateModif;
private JComboBox<String> comboBoxMapSelector;
private AbstractMap _abstractMap;
private Image imageBuffer;
public FormMap(){
setTitle("Трактор");
setContentPane(ContentPanel);
setSize(800, 500);
_abstractMap = new SimpleMap();
// Обработка нажатия кнопки "Создать"
buttonCreate.addActionListener(e->{
Random rnd = new Random();
var _tracktor = new DrawningTracktor(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
rnd.nextInt(3,8)
);
setData(_tracktor);
});
// Обработка нажатия кнопки "Модификация"
buttonCreateModif.addActionListener(e->{
Random rnd = new Random();
var _tracktor = new DrawningTrackedVehicle(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
rnd.nextInt(3,8),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
rnd.nextBoolean(),
rnd.nextBoolean()
);
setData(_tracktor);
});
buttonUp.addActionListener(e->{
if (_abstractMap != null){
imageBuffer = _abstractMap.MoveObject(Direction.Up);
repaint();
}
});
buttonLeft.addActionListener(e->{
if (_abstractMap != null){
imageBuffer = _abstractMap.MoveObject(Direction.Left);
repaint();
}
});
buttonDown.addActionListener(e->{
if (_abstractMap != null){
imageBuffer = _abstractMap.MoveObject(Direction.Down);
repaint();
}
});
buttonRight.addActionListener(e->{
if (_abstractMap != null){
imageBuffer = _abstractMap.MoveObject(Direction.Right);
repaint();
}
});
comboBoxMapSelector.addItemListener(e->{
if (e.getStateChange()== ItemEvent.SELECTED){
switch (e.getItem().toString()) {
case "Простая карта" -> _abstractMap = new SimpleMap();
case "Свалка карта" -> _abstractMap = new DumpMap();
}
repaint();
}
});
}
private void setData(DrawningTracktor _tracktor) {
Random rnd = new Random();
_tracktor.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed());
weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight());
colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor()));
imageBuffer = _abstractMap.CreateMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawningObjectExcavator(_tracktor));
repaint();
}
@Override
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)pictureBox.getGraphics();
if (imageBuffer != null){
pictureBox.paintComponents(imageBuffer.getGraphics());
g2d.drawImage(imageBuffer,0,0,null);
}
}
}

146
FormMapWithSetTracktor.form Normal file
View File

@ -0,0 +1,146 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetTracktor">
<grid id="27dc6" binding="ContentPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="663" height="479"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="afeba" binding="toolsGroup" layout-manager="GridLayoutManager" row-count="10" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<maximum-size width="200" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="412e9" class="javax.swing.JLabel" binding="toolsLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Инструменты"/>
</properties>
</component>
<component id="c455c" class="javax.swing.JComboBox" binding="comboBoxMapSelector">
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Свалка карта"/>
</model>
</properties>
</component>
<component id="ca274" class="javax.swing.JButton" binding="buttonAddTracktor">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить трактор"/>
</properties>
</component>
<component id="8925f" class="javax.swing.JFormattedTextField" binding="textBoxPosition">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="e424c" class="javax.swing.JButton" binding="buttonRemoveTracktor">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить трактор"/>
</properties>
</component>
<component id="5e5ae" class="javax.swing.JButton" binding="buttonShowStorage">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Посмотреть хранилище"/>
</properties>
</component>
<component id="52c38" class="javax.swing.JButton" binding="buttonShowOnMap">
<constraints>
<grid row="6" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Показать карту"/>
</properties>
</component>
<component id="d1efe" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/arrowLeft.png"/>
<text value=""/>
</properties>
</component>
<component id="11d8a" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="9" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/arrowRight.png"/>
<text value=""/>
</properties>
</component>
<component id="bffec" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="8" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/arrowUp.png"/>
<text value=""/>
</properties>
</component>
<component id="1f0dc" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="9" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/arrowDown.png"/>
<text value=""/>
</properties>
</component>
<vspacer id="71dd1">
<constraints>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
<grid id="c1cce" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
</form>

152
FormMapWithSetTracktor.java Normal file
View File

@ -0,0 +1,152 @@
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.text.ParseException;
public class FormMapWithSetTracktor extends JFrame {
private JPanel ContentPanel;
private JPanel pictureBox;
private JPanel toolsGroup;
private JLabel toolsLabel;
private JComboBox comboBoxMapSelector;
private JButton buttonAddTracktor;
private JFormattedTextField textBoxPosition;
private JButton buttonRemoveTracktor;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonDown;
private Image bufferedImage;
private MapWithSetTracktorGeneric<DrawningObjectExcavator, AbstractMap> _mapTracktorCollectionGeneric;
public FormMapWithSetTracktor() {
this.setTitle("Трактор");
this.setContentPane(ContentPanel);
this.setSize(800,500);
try {
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
} catch (ParseException e) {
e.printStackTrace();
}
comboBoxMapSelector.addActionListener(e -> {
AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) {
case "Простая карта" -> new SimpleMap();
case "Свалка карта" -> new DumpMap();
default -> null;
};
if (map != null) {
_mapTracktorCollectionGeneric = new MapWithSetTracktorGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map);
} else {
_mapTracktorCollectionGeneric = null;
}
});
buttonAddTracktor.addActionListener(e -> {
if (_mapTracktorCollectionGeneric == null) {
return;
}
FormTracktor dialog = new FormTracktor();
dialog.setSize(800, 500);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
if (dialog.getSelectedTracktor() != null) {
DrawningObjectExcavator tracktor = new DrawningObjectExcavator(dialog.getSelectedTracktor());
if (_mapTracktorCollectionGeneric.addTracktor(tracktor) != -1)
{
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapTracktorCollectionGeneric.showSet();
repaint();
}
else
{
JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
}
});
buttonRemoveTracktor.addActionListener(e -> {
String text = textBoxPosition.getText();
if (text == null || text.isEmpty()) {
return;
}
if (JOptionPane.showConfirmDialog(this, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
return;
}
int position = Integer.parseInt(text);
if (_mapTracktorCollectionGeneric.removeTracktorAt(position) != null) {
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapTracktorCollectionGeneric.showSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
});
buttonShowStorage.addActionListener(e -> {
if (_mapTracktorCollectionGeneric == null) {
return;
}
bufferedImage = _mapTracktorCollectionGeneric.showSet();
repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (_mapTracktorCollectionGeneric == null) {
return;
}
bufferedImage = _mapTracktorCollectionGeneric.showOnMap();
repaint();
});
buttonLeft.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Left);
repaint();
}
});
buttonRight.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Right);
repaint();
}
});
buttonUp.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Up);
repaint();
}
});
buttonDown.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Down);
repaint();
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
pictureBox.paintComponents(bufferedImage.getGraphics());
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormTracktor">
<grid id="27dc6" binding="ContentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="8" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="ContentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="9" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="661" height="428"/>
@ -48,7 +48,7 @@
<grid id="2fbf8" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" 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="0" column="0" row-span="1" col-span="9" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@ -56,7 +56,7 @@
</grid>
<component id="5a9f4" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
@ -68,7 +68,7 @@
</component>
<component id="3c09f" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
@ -80,7 +80,7 @@
</component>
<component id="6d42a" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="2" column="8" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
@ -92,7 +92,7 @@
</component>
<component id="82038" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="1" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
@ -110,6 +110,14 @@
<text value="Модификация"/>
</properties>
</component>
<component id="a48e3" class="javax.swing.JButton" binding="buttonSelect">
<constraints>
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Выбрать"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -4,7 +4,7 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormTracktor extends JFrame {
public class FormTracktor extends JDialog {
private JPanel ContentPanel;
private JButton buttonCreate;
private JLabel speedLabel;
@ -16,8 +16,10 @@ public class FormTracktor extends JFrame {
private JButton buttonUp;
private JPanel pictureBox;
private JButton buttonCreateModif;
private JButton buttonSelect;
private DrawningTracktor _tracktor;
private DrawningTracktor selectedTracktor;
public FormTracktor(){
setTitle("Трактор");
@ -27,24 +29,31 @@ public class FormTracktor extends JFrame {
// Обработка нажатия кнопки "Создать"
buttonCreate.addActionListener(e->{
Random rnd = new Random();
_tracktor = new DrawningTracktor(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
rnd.nextInt(3,8)
);
Color color = JColorChooser.showDialog(this, "Цвет", new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
if (color == null) {
color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
}
_tracktor = new DrawningTracktor(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color, rnd.nextInt(3,8));
setData();
});
// Обработка нажатия кнопки "Модификация"
buttonCreateModif.addActionListener(e->{
Random rnd = new Random();
Color color = JColorChooser.showDialog(this, "Основной цвет", Color.white);
if (color == null) {
color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
}
Color dopColor = JColorChooser.showDialog(this, "Дополнительный цвет", Color.white);
if (dopColor == null) {
dopColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
}
_tracktor = new DrawningTrackedVehicle(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
color,
rnd.nextInt(3,8),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
dopColor,
rnd.nextBoolean(),
rnd.nextBoolean()
);
@ -79,6 +88,11 @@ public class FormTracktor extends JFrame {
}
});
buttonSelect.addActionListener(e->{
selectedTracktor = _tracktor;
dispose();
});
pictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
@ -100,6 +114,10 @@ public class FormTracktor extends JFrame {
repaint();
}
public DrawningTracktor getSelectedTracktor() {
return selectedTracktor;
}
@Override
public void paint(Graphics g){
super.paint(g);

View File

@ -0,0 +1,164 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetTracktorGeneric <T extends IDrawningObject, U extends AbstractMap>{
public final int _pictureWidth;
public final int _pictureHeight;
public final int _placeSizeWidth = 150;
public final int _placeSizeHeight = 100;
private final SetTracktorGeneric<T> _setTracktor;
private final U _map;
public MapWithSetTracktorGeneric(int picWidth, int picHeight, U map) {
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setTracktor = new SetTracktorGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addTracktor(T tracktor) {
return _setTracktor.insert(tracktor);
}
public T removeTracktorAt(int position) {
return _setTracktor.remove(position);
}
public Image showSet() {
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = (Graphics2D)img.getGraphics();
drawBackground(gr);
drawTracktor(gr);
return img;
}
public Image showOnMap() {
shaking();
for (int i = 0; i < _setTracktor.getCount(); i++)
{
var tracktor = _setTracktor.get(i);
if (tracktor != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, tracktor);
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
public Image moveObject(Direction direction) {
if (_map != null) {
return _map.MoveObject(direction);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
private void shaking() {
int j = _setTracktor.getCount() - 1;
for (int i = 0; i < _setTracktor.getCount(); i++)
{
if (_setTracktor.get(i) == null)
{
for (; j > i; j--)
{
var tracktor = _setTracktor.get(j);
if (tracktor != null)
{
_setTracktor.insert(tracktor , i);
_setTracktor.remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void drawBackground(Graphics2D g) {
Color pen = Color.WHITE;
Stroke penStroke = new BasicStroke(3);
Color brush = Color.LIGHT_GRAY;
Color disabledPersonBadgeBrush = Color.WHITE;
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
{
// Асфальт
g.setColor(brush);
g.fillRect( i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
// Разметка
g.setColor(pen);
g.setStroke(penStroke);
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, (i * _placeSizeWidth) + (_placeSizeWidth / 2), j * _placeSizeHeight);
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth, (j * _placeSizeHeight) + _placeSizeHeight);
g.drawLine(i * _placeSizeWidth, (j + 1) * _placeSizeHeight, (i * _placeSizeWidth) + (_placeSizeWidth / 2), (j + 1) * _placeSizeHeight);
if (j%2==0)
{
// Знак паркинга
g.setColor(pen);
g.setStroke(penStroke);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 60, i * _placeSizeWidth + 60, j * _placeSizeHeight + 60);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 60, i * _placeSizeWidth + 20, j * _placeSizeHeight + 40);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 40, i * _placeSizeWidth + 40, j * _placeSizeHeight + 40);
g.drawLine(i * _placeSizeWidth + 40, j * _placeSizeHeight + 40, i * _placeSizeWidth + 40, j * _placeSizeHeight + 60);
}
else
{
// Инвалид
// Колесо
g.setColor(pen);
g.setStroke(penStroke);
g.drawOval(i * _placeSizeWidth + 40, j * _placeSizeHeight + 50, 30, 30);
// Голова
g.setColor(disabledPersonBadgeBrush);
g.setStroke(new BasicStroke(1));
g.fillOval(i * _placeSizeWidth + 20, j * _placeSizeHeight + 60, 10, 10);
// Туловище
g.setColor(pen);
g.setStroke(penStroke);
g.drawLine(i * _placeSizeWidth + 25, j * _placeSizeHeight + 65, i * _placeSizeWidth + 55, j * _placeSizeHeight + 60);
// Рука
g.drawLine(i * _placeSizeWidth + 32, j * _placeSizeHeight + 65, i * _placeSizeWidth + 38, j * _placeSizeHeight + 55);
// Нога
g.drawLine(i * _placeSizeWidth + 55, j * _placeSizeHeight + 60, i * _placeSizeWidth + 60, j * _placeSizeHeight + 40);
// Голеностоп
g.drawLine(i * _placeSizeWidth + 60, j * _placeSizeHeight + 40, i * _placeSizeWidth + 65, j * _placeSizeHeight + 38);
}
}
}
g.setStroke(new BasicStroke(1));
}
private void drawTracktor(Graphics2D g) {
int width = _pictureWidth / _placeSizeWidth;
int curWidth = width-1;
int curHeight = 0;
for (int i = 0; i < _setTracktor.getCount(); i++)
{
// установка позиции
// Влево - вниз
if (_setTracktor.get(i) != null){
_setTracktor.get(i).setObject(
curWidth * _placeSizeWidth,
curHeight * _placeSizeHeight,
_pictureWidth,
_pictureHeight);
_setTracktor.get(i).drawningObject(g);
if (curWidth > 0)
{
curWidth--;
}
else
{
curWidth = width-1;
curHeight++;
}
}
}
}
}

View File

@ -2,8 +2,9 @@ import javax.swing.*;
public class Program {
public static void main(String[] args){
FormMap formMap = new FormMap();
formMap.setVisible(true);
formMap.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
FormGallery form = new FormGallery();
form.setSize(500, 300);
form.setVisible(true);
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

64
SetTracktorGeneric.java Normal file
View File

@ -0,0 +1,64 @@
public class SetTracktorGeneric<T> {
private final Object[] _places;
public int getCount() {
return _places.length;
}
public SetTracktorGeneric(int count) {
_places = new Object[count];
}
public int insert(T tracktor) {
return insert(tracktor, 0);
}
public int insert(T tracktor, int position) {
if (position < 0 || position >= getCount()) {
return -1;
}
if (_places[position] == null) {
_places[position] = tracktor;
return position;
}
int tmp = -1;
for(int i = position; i < getCount(); i++)
{
if (_places[i] == null)
{
tmp = i;
break;
}
}
if (tmp != -1)
{
System.arraycopy(_places, position, _places, position + 1, tmp - position);
_places[position] = tracktor;
return position;
}
return -1;
}
@SuppressWarnings("unchecked")
public T remove(int position) {
if (position < getCount() && position >= 0)
{
T temp = (T) _places[position];
_places[position] = null;
return temp;
}
return null;
}
@SuppressWarnings("unchecked")
public T get(int position) {
if (position < 0 || position >= getCount()) {
return null;
}
return (T) _places[position];
}
}