LabWork03: До удаления FormMap

This commit is contained in:
Safgerd 2022-11-21 21:34:45 +04:00
parent 67255fd203
commit f710cc4300
12 changed files with 732 additions and 18 deletions

View File

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

View File

@ -22,6 +22,11 @@ public class DrawningTracktor {
drawningRollers = RollersType.random(countRollers, bodyColor); 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){ protected DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers, int tracktorWidth, int tracktorHeight){
this(speed, weight, bodyColor, countRollers); this(speed, weight, bodyColor, countRollers);
_tracktorWidth = tracktorWidth; _tracktorWidth = tracktorWidth;

45
EntityWithRollers.java Normal file
View File

@ -0,0 +1,45 @@
import java.util.Random;
public class EntityWithRollers<T extends EntityTracktor, U extends IDrawningRollers> {
static Random rnd = new Random();
private Object[] entities;
public int entitiesCount = 0;
private Object[] rollers;
public int rollersCount = 0;
public EntityWithRollers(int count) {
//new T[count] не работает "Type parameter 'T' cannot be instantiated directly"
entities = new Object[count];
//new U[count] не работает "Type parameter 'U' cannot be instantiated directly"
rollers = new Object[count];
}
public boolean add(T entity) {
if (entitiesCount >= entities.length) {
return false;
}
entities[entitiesCount++] = entity;
return true;
}
public boolean add(U roller) {
if (rollersCount >= rollers.length) {
return false;
}
rollers[rollersCount++] = roller;
return true;
}
public IDrawningObject constructArtillery() {
if (entitiesCount == 0 || rollersCount == 0) {
return null;
}
EntityTracktor entity = (EntityTracktor) entities[rnd.nextInt(0, entitiesCount)];
IDrawningRollers roller = (IDrawningRollers) rollers[rnd.nextInt(0, rollersCount)];
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.constructArtillery();
second = storage.constructArtillery();
third = storage.constructArtillery();
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);
}
}
}

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"?> <?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormTracktor"> <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"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="20" y="20" width="661" height="428"/> <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"> <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"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <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> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
@ -56,7 +56,7 @@
</grid> </grid>
<component id="5a9f4" class="javax.swing.JButton" binding="buttonLeft"> <component id="5a9f4" class="javax.swing.JButton" binding="buttonLeft">
<constraints> <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"/> <minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
</grid> </grid>
@ -68,7 +68,7 @@
</component> </component>
<component id="3c09f" class="javax.swing.JButton" binding="buttonDown"> <component id="3c09f" class="javax.swing.JButton" binding="buttonDown">
<constraints> <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"/> <minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
</grid> </grid>
@ -80,7 +80,7 @@
</component> </component>
<component id="6d42a" class="javax.swing.JButton" binding="buttonRight"> <component id="6d42a" class="javax.swing.JButton" binding="buttonRight">
<constraints> <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"/> <minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
</grid> </grid>
@ -92,7 +92,7 @@
</component> </component>
<component id="82038" class="javax.swing.JButton" binding="buttonUp"> <component id="82038" class="javax.swing.JButton" binding="buttonUp">
<constraints> <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"/> <minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
</grid> </grid>
@ -110,6 +110,14 @@
<text value="Модификация"/> <text value="Модификация"/>
</properties> </properties>
</component> </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> </children>
</grid> </grid>
</form> </form>

View File

@ -4,7 +4,7 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.util.Random; import java.util.Random;
public class FormTracktor extends JFrame { public class FormTracktor extends JDialog {
private JPanel ContentPanel; private JPanel ContentPanel;
private JButton buttonCreate; private JButton buttonCreate;
private JLabel speedLabel; private JLabel speedLabel;
@ -16,8 +16,10 @@ public class FormTracktor extends JFrame {
private JButton buttonUp; private JButton buttonUp;
private JPanel pictureBox; private JPanel pictureBox;
private JButton buttonCreateModif; private JButton buttonCreateModif;
private JButton buttonSelect;
private DrawningTracktor _tracktor; private DrawningTracktor _tracktor;
private DrawningTracktor selectedTracktor;
public FormTracktor(){ public FormTracktor(){
setTitle("Трактор"); setTitle("Трактор");
@ -27,24 +29,31 @@ public class FormTracktor extends JFrame {
// Обработка нажатия кнопки "Создать" // Обработка нажатия кнопки "Создать"
buttonCreate.addActionListener(e->{ buttonCreate.addActionListener(e->{
Random rnd = new Random(); Random rnd = new Random();
_tracktor = new DrawningTracktor( Color color = JColorChooser.showDialog(this, "Цвет", new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
rnd.nextInt(100, 300), if (color == null) {
rnd.nextInt(1000, 2000), color = 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.nextInt(3,8) _tracktor = new DrawningTracktor(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color, rnd.nextInt(3,8));
);
setData(); setData();
}); });
// Обработка нажатия кнопки "Модификация" // Обработка нажатия кнопки "Модификация"
buttonCreateModif.addActionListener(e->{ buttonCreateModif.addActionListener(e->{
Random rnd = new Random(); 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( _tracktor = new DrawningTrackedVehicle(
rnd.nextInt(100, 300), rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), color,
rnd.nextInt(3,8), rnd.nextInt(3,8),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), dopColor,
rnd.nextBoolean(), rnd.nextBoolean(),
rnd.nextBoolean() rnd.nextBoolean()
); );
@ -79,6 +88,11 @@ public class FormTracktor extends JFrame {
} }
}); });
buttonSelect.addActionListener(e->{
selectedTracktor = _tracktor;
dispose();
});
pictureBox.addComponentListener(new ComponentAdapter() { pictureBox.addComponentListener(new ComponentAdapter() {
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
@ -100,6 +114,10 @@ public class FormTracktor extends JFrame {
repaint(); repaint();
} }
public DrawningTracktor getSelectedTracktor() {
return selectedTracktor;
}
@Override @Override
public void paint(Graphics g){ public void paint(Graphics g){
super.paint(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 class Program {
public static void main(String[] args){ public static void main(String[] args){
FormMap formMap = new FormMap(); FormGallery form = new FormGallery();
formMap.setVisible(true); form.setSize(500, 300);
formMap.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 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];
}
}