Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96937e1972 | ||
|
|
7018134d52 | ||
|
|
f3419a40e6 | ||
|
|
7a9ec33ba1 | ||
|
|
1c466317b8 |
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@@ -1,4 +1,3 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" default="true">
|
<component name="ProjectRootManager" version="2" default="true">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package MonorailHard.DrawningObjects;
|
|||||||
|
|
||||||
import MonorailHard.DirectionType;
|
import MonorailHard.DirectionType;
|
||||||
import MonorailHard.Entities.EntityMonorail;
|
import MonorailHard.Entities.EntityMonorail;
|
||||||
|
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||||||
import MonorailHard.MovementStrategy.IMoveableObject;
|
import MonorailHard.MovementStrategy.IMoveableObject;
|
||||||
import com.sun.source.tree.ImportTree;
|
import com.sun.source.tree.ImportTree;
|
||||||
|
|
||||||
@@ -12,8 +13,8 @@ import java.util.Random;
|
|||||||
public class DrawningMonorail {
|
public class DrawningMonorail {
|
||||||
|
|
||||||
protected EntityMonorail EntityMonorail;
|
protected EntityMonorail EntityMonorail;
|
||||||
private int _pictureWidth;
|
public int _pictureWidth;
|
||||||
private int _pictureHeight;
|
public int _pictureHeight;
|
||||||
protected int _startPosX;
|
protected int _startPosX;
|
||||||
protected int _startPosY;
|
protected int _startPosY;
|
||||||
protected int _monorailWidth = 133;
|
protected int _monorailWidth = 133;
|
||||||
@@ -78,6 +79,8 @@ public class DrawningMonorail {
|
|||||||
return;
|
return;
|
||||||
_startPosX = x;
|
_startPosX = x;
|
||||||
_startPosY = y;
|
_startPosY = y;
|
||||||
|
DrawningWheels.ChangeX(_startPosX);
|
||||||
|
DrawningWheels.ChangeY(_startPosY);
|
||||||
if(x + _monorailWidth >= _pictureWidth|| y + _monorailHeight >= _pictureHeight || x < 0 || y < 0){
|
if(x + _monorailWidth >= _pictureWidth|| y + _monorailHeight >= _pictureHeight || x < 0 || y < 0){
|
||||||
_startPosX = 0;
|
_startPosX = 0;
|
||||||
_startPosY = 0;
|
_startPosY = 0;
|
||||||
@@ -111,7 +114,9 @@ public class DrawningMonorail {
|
|||||||
return can;
|
return can;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IMoveableObject GetMoveableObject(){
|
||||||
|
return new DrawningObjectMonorail(this);
|
||||||
|
}
|
||||||
|
|
||||||
public void MoveTransport(DirectionType direction){
|
public void MoveTransport(DirectionType direction){
|
||||||
if (!CanMove(direction) || EntityMonorail == null)
|
if (!CanMove(direction) || EntityMonorail == null)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package MonorailHard;
|
|||||||
import MonorailHard.DrawningObjects.DrawningLocomotive;
|
import MonorailHard.DrawningObjects.DrawningLocomotive;
|
||||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||||
import MonorailHard.MovementStrategy.*;
|
import MonorailHard.MovementStrategy.*;
|
||||||
|
import org.w3c.dom.ranges.DocumentRange;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.text.AbstractDocument;
|
import javax.swing.text.AbstractDocument;
|
||||||
@@ -11,22 +12,48 @@ import java.awt.event.ActionEvent;
|
|||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
|
||||||
public class FormMonorail{
|
public class FormMonorail{
|
||||||
|
|
||||||
private DrawningMonorail DrawningMonorail;
|
private DrawningMonorail DrawningMonorail;
|
||||||
private AbstractStrategy _abstractStrategy;
|
private AbstractStrategy _abstractStrategy;
|
||||||
|
|
||||||
|
public JButton buttonSelect;
|
||||||
|
|
||||||
|
public JFrame MonorailFrame;
|
||||||
|
|
||||||
Canvas canv;
|
Canvas canv;
|
||||||
static int pictureBoxWidth = 882;
|
static int pictureBoxWidth = 882;
|
||||||
static int pictureBoxHeight = 453;
|
static int pictureBoxHeight = 453;
|
||||||
|
|
||||||
|
public Color ChooseColor(JFrame MonorailFrame){
|
||||||
|
JColorChooser dialog = new JColorChooser();
|
||||||
|
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.WHITE);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawningMonorail SelectedMonorail(){
|
||||||
|
return DrawningMonorail;
|
||||||
|
}
|
||||||
public void Draw(){
|
public void Draw(){
|
||||||
|
if(DrawningMonorail == null)
|
||||||
|
return;
|
||||||
canv.repaint();
|
canv.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ChangeMonorail(DrawningMonorail newMonorail){
|
||||||
|
newMonorail.SetPosition(0,0);
|
||||||
|
DrawningMonorail = newMonorail;
|
||||||
|
canv.DrawningMonorail = DrawningMonorail;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public FormMonorail(){
|
public FormMonorail(){
|
||||||
JFrame MonorailFrame =new JFrame ();
|
MonorailFrame =new JFrame ();
|
||||||
JButton buttonCreate = new JButton("Создать");
|
JButton buttonCreate = new JButton("Создать");
|
||||||
JButton buttonCreateLocomotive = new JButton("Создать локомотив");
|
JButton buttonCreateLocomotive = new JButton("Создать локомотив");
|
||||||
JButton buttonStep = new JButton("Шаг");
|
JButton buttonStep = new JButton("Шаг");
|
||||||
|
buttonSelect = new JButton ("Выбрать");
|
||||||
JComboBox comboBoxStrategy = new JComboBox(
|
JComboBox comboBoxStrategy = new JComboBox(
|
||||||
new String[]{
|
new String[]{
|
||||||
"Довести до центра",
|
"Довести до центра",
|
||||||
@@ -89,8 +116,13 @@ public class FormMonorail{
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||||
|
Color choosen = ChooseColor(MonorailFrame);
|
||||||
|
if(choosen != null){
|
||||||
|
color = choosen;
|
||||||
|
}
|
||||||
DrawningMonorail = new DrawningMonorail(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
DrawningMonorail = new DrawningMonorail(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
color,
|
||||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||||
pictureBoxWidth, pictureBoxHeight);
|
pictureBoxWidth, pictureBoxHeight);
|
||||||
@@ -104,19 +136,30 @@ public class FormMonorail{
|
|||||||
new ActionListener() {
|
new ActionListener() {
|
||||||
public void actionPerformed(ActionEvent e){
|
public void actionPerformed(ActionEvent e){
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||||
|
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||||
|
Color choosen = ChooseColor(MonorailFrame);
|
||||||
|
if(choosen != null){
|
||||||
|
color = choosen;
|
||||||
|
}
|
||||||
|
choosen = ChooseColor(MonorailFrame);
|
||||||
|
if(choosen != null){
|
||||||
|
additionalColor = choosen;
|
||||||
|
}
|
||||||
DrawningMonorail = new DrawningLocomotive(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
DrawningMonorail = new DrawningLocomotive(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
color,
|
||||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||||
random.nextInt(1, 6),
|
random.nextInt(1, 6),
|
||||||
pictureBoxWidth, pictureBoxHeight, random.nextBoolean(), random.nextBoolean(),
|
pictureBoxWidth, pictureBoxHeight, random.nextBoolean(), random.nextBoolean(),
|
||||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)));
|
additionalColor);
|
||||||
canv.DrawningMonorail = DrawningMonorail;
|
canv.DrawningMonorail = DrawningMonorail;
|
||||||
comboBoxStrategy.enable(true);
|
comboBoxStrategy.enable(true);
|
||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
RightButton.addActionListener(new ActionListener() {
|
RightButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@@ -156,10 +199,11 @@ public class FormMonorail{
|
|||||||
});
|
});
|
||||||
|
|
||||||
MonorailFrame.setSize (900, 500);
|
MonorailFrame.setSize (900, 500);
|
||||||
MonorailFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
MonorailFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
|
||||||
MonorailFrame.setLayout(null);
|
MonorailFrame.setLayout(null);
|
||||||
canv = new Canvas();
|
canv = new Canvas();
|
||||||
canv.setSize(pictureBoxWidth, pictureBoxHeight);
|
canv.setSize(pictureBoxWidth, pictureBoxHeight);
|
||||||
|
buttonSelect.setBounds(383,401, 180, 40);
|
||||||
buttonCreate.setBounds(198, 401, 180, 40);
|
buttonCreate.setBounds(198, 401, 180, 40);
|
||||||
buttonCreateLocomotive.setBounds(12, 401, 180, 40);
|
buttonCreateLocomotive.setBounds(12, 401, 180, 40);
|
||||||
RightButton.setBounds(840,411,30,30);
|
RightButton.setBounds(840,411,30,30);
|
||||||
@@ -177,6 +221,7 @@ public class FormMonorail{
|
|||||||
MonorailFrame.add(RightButton);
|
MonorailFrame.add(RightButton);
|
||||||
MonorailFrame.add(comboBoxStrategy);
|
MonorailFrame.add(comboBoxStrategy);
|
||||||
MonorailFrame.add(buttonStep);
|
MonorailFrame.add(buttonStep);
|
||||||
|
MonorailFrame.add(buttonSelect);
|
||||||
MonorailFrame.setVisible(true);
|
MonorailFrame.setVisible(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
209
src/MonorailHard/FormMonorailCollection.java
Normal file
209
src/MonorailHard/FormMonorailCollection.java
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
package MonorailHard;
|
||||||
|
|
||||||
|
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||||
|
import MonorailHard.Generics.MonorailGenericCollection;
|
||||||
|
import MonorailHard.Generics.MonorailGenericStorage;
|
||||||
|
import MonorailHard.Generics.MonorailTrashCollection;
|
||||||
|
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.text.Normalizer;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
public class FormMonorailCollection {
|
||||||
|
private final MonorailGenericStorage _storage;
|
||||||
|
|
||||||
|
private JList<String> listBoxStorages;
|
||||||
|
private DefaultListModel<String> listBoxModel;
|
||||||
|
private int pictureBoxWidth = 605;
|
||||||
|
private int pictureBoxHeight = 426;
|
||||||
|
|
||||||
|
CollectionCanvas canv;
|
||||||
|
|
||||||
|
void Draw(){
|
||||||
|
if(canv == null)
|
||||||
|
return;
|
||||||
|
canv.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReloadObjects(){
|
||||||
|
int index = listBoxStorages.getSelectedIndex();
|
||||||
|
listBoxModel.clear();
|
||||||
|
List<String> keys = _storage.Keys();
|
||||||
|
for(int i = 0; i < keys.size(); i++){
|
||||||
|
listBoxModel.addElement(keys.get(i));
|
||||||
|
}
|
||||||
|
if(listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size()))
|
||||||
|
listBoxStorages.setSelectedIndex(0);
|
||||||
|
else if(listBoxModel.size() > 0)
|
||||||
|
listBoxStorages.setSelectedIndex(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FormMonorailCollection(){
|
||||||
|
MonorailTrashCollection<DrawningMonorail> _trashCollection = new MonorailTrashCollection<>();
|
||||||
|
JButton callTrashButton = new JButton("мусор");
|
||||||
|
_storage = new MonorailGenericStorage(pictureBoxWidth, pictureBoxHeight);
|
||||||
|
JScrollPane scrollPane = new JScrollPane();
|
||||||
|
canv = new CollectionCanvas();
|
||||||
|
JPanel toolBox = new JPanel();
|
||||||
|
JTextField storageName = new JTextField();
|
||||||
|
JButton addStorageButton = new JButton("Добавить набор");
|
||||||
|
listBoxModel = new DefaultListModel<>();
|
||||||
|
listBoxStorages= new JList<>(listBoxModel);
|
||||||
|
scrollPane.setViewportView(listBoxStorages);
|
||||||
|
JButton delStorageButton = new JButton("Удалить набор");
|
||||||
|
toolBox.setBounds(623,12, 227, 80);
|
||||||
|
JFrame collectionFrame = new JFrame();
|
||||||
|
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
collectionFrame.setSize(880,497);
|
||||||
|
toolBox.setBounds(623, 12, 227, 426);
|
||||||
|
canv.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
|
||||||
|
JButton addButton = new JButton("Добавить");
|
||||||
|
JButton removeButton = new JButton("Удалить");
|
||||||
|
JButton refreshButton = new JButton("Обновить");
|
||||||
|
JTextField monorailNumb = new JTextField();
|
||||||
|
GridLayout lay = new GridLayout(9,1);
|
||||||
|
toolBox.add(storageName);
|
||||||
|
toolBox.add(addStorageButton);
|
||||||
|
toolBox.add(scrollPane);
|
||||||
|
toolBox.add(delStorageButton);
|
||||||
|
toolBox.setLayout(lay);
|
||||||
|
toolBox.add(addButton);
|
||||||
|
toolBox.add(monorailNumb);
|
||||||
|
toolBox.add(removeButton);
|
||||||
|
toolBox.add(refreshButton);
|
||||||
|
toolBox.add(callTrashButton);
|
||||||
|
collectionFrame.add(toolBox);
|
||||||
|
collectionFrame.add(canv);
|
||||||
|
collectionFrame.setVisible(true);
|
||||||
|
canv._storage = _storage;
|
||||||
|
canv.listBoxStorages = listBoxStorages;
|
||||||
|
|
||||||
|
addStorageButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if(storageName.getText() == null)
|
||||||
|
return;
|
||||||
|
_storage.AddSet(storageName.getText());
|
||||||
|
ReloadObjects();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
delStorageButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_storage.DelSet(listBoxStorages.getSelectedValue());
|
||||||
|
ReloadObjects();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
callTrashButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if(_trashCollection.GetSize() == 0)
|
||||||
|
return;
|
||||||
|
FormMonorail form = new FormMonorail();
|
||||||
|
form.ChangeMonorail(_trashCollection.GetTop());
|
||||||
|
_trashCollection.Pop();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
addButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||||||
|
FormMonorail form = new FormMonorail();
|
||||||
|
form.buttonSelect.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_monorails.Insert(form.SelectedMonorail()))
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
form.SelectedMonorail()._pictureWidth = pictureBoxWidth;
|
||||||
|
form.SelectedMonorail()._pictureHeight = pictureBoxHeight;
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
}
|
||||||
|
form.MonorailFrame.dispose();
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
removeButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||||||
|
if(_monorails == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String tmp = monorailNumb.getText();
|
||||||
|
int numb;
|
||||||
|
|
||||||
|
try{
|
||||||
|
numb = Integer.parseInt(tmp);
|
||||||
|
}
|
||||||
|
catch(Exception ex){
|
||||||
|
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
DrawningMonorail curMonorail = _monorails.Get(numb);
|
||||||
|
_trashCollection.Push(curMonorail);
|
||||||
|
_monorails.Remove(numb);
|
||||||
|
_monorails.ShowMonorails();
|
||||||
|
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
refreshButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||||||
|
if(_monorails == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_monorails.ShowMonorails();
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CollectionCanvas extends JComponent {
|
||||||
|
public MonorailGenericStorage _storage;
|
||||||
|
public JList<String> listBoxStorages;
|
||||||
|
|
||||||
|
|
||||||
|
public CollectionCanvas(){
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void paintComponent (Graphics g){
|
||||||
|
if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.paintComponents (g) ;
|
||||||
|
Graphics2D g2d = (Graphics2D)g;
|
||||||
|
g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowMonorails(), 0, 0, this);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
61
src/MonorailHard/Generics/HardGeneric.java
Normal file
61
src/MonorailHard/Generics/HardGeneric.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package MonorailHard.Generics;
|
||||||
|
|
||||||
|
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||||
|
import MonorailHard.DrawningObjects.IDraw;
|
||||||
|
import MonorailHard.Entities.EntityMonorail;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class HardGeneric <T extends EntityMonorail,U extends IDraw>{
|
||||||
|
T[] arrFirst;
|
||||||
|
U[] arrSecond;
|
||||||
|
|
||||||
|
private int curSz;
|
||||||
|
private int CountFirst;
|
||||||
|
private int CountSecond;
|
||||||
|
|
||||||
|
private int pictureBoxWidth;
|
||||||
|
|
||||||
|
private int pictureBoxHeight;
|
||||||
|
|
||||||
|
public HardGeneric(int countFirst, int countSecond, int width, int height){
|
||||||
|
curSz = 0;
|
||||||
|
CountFirst = countFirst;
|
||||||
|
CountSecond = countSecond;
|
||||||
|
arrFirst = (T[]) new EntityMonorail[CountFirst];
|
||||||
|
arrSecond = (U[]) new IDraw[CountSecond];
|
||||||
|
pictureBoxHeight = height;
|
||||||
|
pictureBoxWidth = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int InsertFirst(T entityMonorail){
|
||||||
|
if(arrFirst[CountFirst-1] != null)
|
||||||
|
return -1;
|
||||||
|
for(int i = curSz -1; i>= 0; i--) {
|
||||||
|
arrFirst[i + 1] = arrFirst[i];
|
||||||
|
arrSecond[i + 1] = arrSecond[i];
|
||||||
|
}
|
||||||
|
curSz++;
|
||||||
|
arrFirst[0] = entityMonorail;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int InsertSecond(U inter){
|
||||||
|
if(arrSecond[CountSecond-1] != null)
|
||||||
|
return -1;
|
||||||
|
arrSecond[0] = inter;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawningMonorail MakeObject(){
|
||||||
|
Random rand = new Random();
|
||||||
|
int indFirst = rand.nextInt(0, curSz);
|
||||||
|
int indSecond = rand.nextInt(0,curSz);
|
||||||
|
EntityMonorail entity = arrFirst[indFirst];
|
||||||
|
IDraw inter = arrSecond[indSecond];
|
||||||
|
DrawningMonorail monorail = new DrawningMonorail(entity.Speed(), entity.Weight(), entity.BodyColor(),
|
||||||
|
entity.WheelColor(), entity.TireColor(), pictureBoxWidth, pictureBoxHeight);
|
||||||
|
return monorail;
|
||||||
|
}
|
||||||
|
}
|
||||||
92
src/MonorailHard/Generics/MonorailGenericCollection.java
Normal file
92
src/MonorailHard/Generics/MonorailGenericCollection.java
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
package MonorailHard.Generics;
|
||||||
|
|
||||||
|
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||||
|
import MonorailHard.MovementStrategy.IMoveableObject;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
public class MonorailGenericCollection<T extends DrawningMonorail, U extends IMoveableObject> {
|
||||||
|
private final int _pictureWidth;
|
||||||
|
|
||||||
|
private final int _pictureHeight;
|
||||||
|
|
||||||
|
private final int _placeSizeWidth = 133;
|
||||||
|
|
||||||
|
private final int _placeSizeHeight = 50;
|
||||||
|
|
||||||
|
private final SetGeneric<T> _collection;
|
||||||
|
|
||||||
|
public MonorailGenericCollection(int picWidth, int picHeight){
|
||||||
|
int width = picWidth / _placeSizeWidth;
|
||||||
|
int height = picHeight / _placeSizeHeight;
|
||||||
|
_pictureWidth = picWidth;
|
||||||
|
_pictureHeight = picHeight;
|
||||||
|
_collection = new SetGeneric<T>(width * height);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Size(){
|
||||||
|
return _collection.Count;
|
||||||
|
}
|
||||||
|
public boolean Insert(T obj){
|
||||||
|
if (obj == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return _collection.Insert(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean Remove(int position){
|
||||||
|
return _collection.Remove(position);
|
||||||
|
}
|
||||||
|
public U GetU(int pos){
|
||||||
|
T ans = _collection.Get(pos);
|
||||||
|
if(ans == null)
|
||||||
|
return null;
|
||||||
|
return (U)ans.GetMoveableObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Get(int position){
|
||||||
|
if(position < 0 || position >= _collection.Count)
|
||||||
|
return null;
|
||||||
|
return _collection.Get(position);
|
||||||
|
}
|
||||||
|
private void DrawBackground(Graphics g)
|
||||||
|
{
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
|
||||||
|
1; ++j)
|
||||||
|
{
|
||||||
|
g.drawLine(i * _placeSizeWidth, j *
|
||||||
|
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
|
||||||
|
_placeSizeHeight);
|
||||||
|
}
|
||||||
|
g.drawLine(i * _placeSizeWidth, 0, i *
|
||||||
|
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawObjects(Graphics g)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _collection.Count; i++)
|
||||||
|
{
|
||||||
|
DrawningMonorail monorail = _collection.Get(i);
|
||||||
|
if (monorail != null)
|
||||||
|
{
|
||||||
|
int inRow = _pictureWidth / _placeSizeWidth;
|
||||||
|
monorail.SetPosition(i % inRow * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight);
|
||||||
|
monorail.DrawMonorail((Graphics2D) g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public BufferedImage ShowMonorails()
|
||||||
|
{
|
||||||
|
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
|
||||||
|
Graphics gr = bmp.createGraphics();
|
||||||
|
DrawBackground(gr);
|
||||||
|
DrawObjects(gr);
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/MonorailHard/Generics/MonorailGenericStorage.java
Normal file
49
src/MonorailHard/Generics/MonorailGenericStorage.java
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package MonorailHard.Generics;
|
||||||
|
|
||||||
|
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||||
|
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||||||
|
|
||||||
|
import java.util.Dictionary;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class MonorailGenericStorage {
|
||||||
|
final HashMap<String, MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail>> _monorailStorages;
|
||||||
|
public List<String> Keys(){
|
||||||
|
if(_monorailStorages == null)
|
||||||
|
return null;
|
||||||
|
return _monorailStorages.keySet().stream().collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
private final int _pictureWidth;
|
||||||
|
private final int _pictureHeight;
|
||||||
|
|
||||||
|
public MonorailGenericStorage(int pictureWidth, int pictureHeight){
|
||||||
|
_monorailStorages = new HashMap<>();
|
||||||
|
_pictureWidth = pictureWidth;
|
||||||
|
_pictureHeight = pictureHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSet(String name){
|
||||||
|
if(_monorailStorages.containsKey(name))
|
||||||
|
return;
|
||||||
|
_monorailStorages.put(name, new MonorailGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelSet(String name){
|
||||||
|
if(!_monorailStorages.containsKey(name))
|
||||||
|
return;
|
||||||
|
_monorailStorages.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> Get(String name){
|
||||||
|
if(!_monorailStorages.containsKey(name))
|
||||||
|
return null;
|
||||||
|
return _monorailStorages.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawningMonorail Get(String collectionName, int position){
|
||||||
|
return _monorailStorages.get(collectionName).Get(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/MonorailHard/Generics/MonorailTrashCollection.java
Normal file
32
src/MonorailHard/Generics/MonorailTrashCollection.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package MonorailHard.Generics;
|
||||||
|
|
||||||
|
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
public class MonorailTrashCollection <T extends DrawningMonorail> {
|
||||||
|
Queue <T> _queue;
|
||||||
|
|
||||||
|
public MonorailTrashCollection(){
|
||||||
|
_queue = new ArrayDeque<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Push(T monorail){
|
||||||
|
_queue.add(monorail);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetSize(){
|
||||||
|
return _queue.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Pop(){
|
||||||
|
if(_queue.size() ==0)
|
||||||
|
return;
|
||||||
|
_queue.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T GetTop(){
|
||||||
|
return _queue.peek();
|
||||||
|
}
|
||||||
|
}
|
||||||
56
src/MonorailHard/Generics/SetGeneric.java
Normal file
56
src/MonorailHard/Generics/SetGeneric.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package MonorailHard.Generics;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SetGeneric <T extends Object>{
|
||||||
|
private final List<T> _places;
|
||||||
|
|
||||||
|
public int Count;
|
||||||
|
|
||||||
|
private final int _maxCount;
|
||||||
|
|
||||||
|
public SetGeneric(int count){
|
||||||
|
_maxCount = count;
|
||||||
|
_places = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean Insert(T monorail){
|
||||||
|
if(_places.size() == _maxCount)
|
||||||
|
return false;
|
||||||
|
Insert(monorail, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean Insert(T monorail, int position){
|
||||||
|
if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount))
|
||||||
|
return false;
|
||||||
|
_places.add(position, monorail);
|
||||||
|
Count++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean Remove(int position){
|
||||||
|
if(!(position >= 0 && position < _places.size()))
|
||||||
|
return false;
|
||||||
|
_places.remove(position);
|
||||||
|
Count--;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Get(int position){
|
||||||
|
if(!(position >= 0 && position < Count))
|
||||||
|
return null;
|
||||||
|
return (T)_places.get(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<T> GetMonorails(int maxMonorails){
|
||||||
|
ArrayList<T> toRet = new ArrayList<>();
|
||||||
|
for(int i = 0; i < _places.size(); i++){
|
||||||
|
toRet.add(_places.get(i));
|
||||||
|
if(i == maxMonorails)
|
||||||
|
return toRet;
|
||||||
|
}
|
||||||
|
return toRet;
|
||||||
|
}
|
||||||
|
}
|
||||||
77
src/MonorailHard/HardForm.java
Normal file
77
src/MonorailHard/HardForm.java
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package MonorailHard;
|
||||||
|
|
||||||
|
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||||
|
import MonorailHard.DrawningObjects.DrawningWheels;
|
||||||
|
import MonorailHard.DrawningObjects.IDraw;
|
||||||
|
import MonorailHard.Entities.EntityMonorail;
|
||||||
|
import MonorailHard.Generics.HardGeneric;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.text.html.parser.Entity;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class HardForm {
|
||||||
|
private Canvas canv;
|
||||||
|
|
||||||
|
private int pictureBoxWidth;
|
||||||
|
private int pictureBoxHeight;
|
||||||
|
|
||||||
|
private EntityMonorail makeEntity(){
|
||||||
|
Random rand = new Random();
|
||||||
|
return new EntityMonorail(rand.nextInt(100, 300), rand.nextDouble(1000,3000),
|
||||||
|
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)),
|
||||||
|
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)),
|
||||||
|
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)));
|
||||||
|
}
|
||||||
|
void Draw(){
|
||||||
|
if(canv == null)
|
||||||
|
return;
|
||||||
|
canv.repaint();
|
||||||
|
}
|
||||||
|
private IDraw makeIDraw(EntityMonorail entity){
|
||||||
|
Random rand = new Random();
|
||||||
|
return new DrawningWheels(pictureBoxWidth, pictureBoxHeight, 0, 0, entity.WheelColor(), entity.TireColor());
|
||||||
|
}
|
||||||
|
public HardForm(){
|
||||||
|
Random rand = new Random();
|
||||||
|
int sz = rand.nextInt(1, 10);
|
||||||
|
|
||||||
|
|
||||||
|
JFrame HardFrame = new JFrame();
|
||||||
|
HardFrame.setSize(700, 400);
|
||||||
|
HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
canv = new Canvas();
|
||||||
|
canv.setBounds(0,0,pictureBoxWidth,pictureBoxHeight);
|
||||||
|
JButton makeObject = new JButton("Создать");
|
||||||
|
makeObject.setBounds(0, 0, 100, 40);
|
||||||
|
canv.add(makeObject);
|
||||||
|
HardFrame.setContentPane(canv);
|
||||||
|
HardFrame.setVisible(true);
|
||||||
|
pictureBoxHeight = canv.getHeight();
|
||||||
|
pictureBoxWidth = canv.getWidth();
|
||||||
|
HardGeneric<EntityMonorail, IDraw> toDraw= new HardGeneric<>(sz,
|
||||||
|
sz, pictureBoxWidth, pictureBoxHeight);
|
||||||
|
for(int i = 0; i < sz; i++){
|
||||||
|
EntityMonorail ent = makeEntity();
|
||||||
|
toDraw.InsertFirst(ent);
|
||||||
|
toDraw.InsertSecond(makeIDraw(ent));
|
||||||
|
}
|
||||||
|
makeObject.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
DrawningMonorail DrawningMonorail = toDraw.MakeObject();
|
||||||
|
DrawningMonorail.SetPosition(pictureBoxWidth / 2 - DrawningMonorail.GetWidth()/2,
|
||||||
|
pictureBoxHeight / 2 - DrawningMonorail.GetHeight()/2);
|
||||||
|
canv.DrawningMonorail = DrawningMonorail;
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -15,6 +15,6 @@ import javax.swing.*;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
FormMonorail form = new FormMonorail();
|
FormMonorailCollection form = new FormMonorailCollection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user