Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a398df5403 | ||
|
|
0d85275050 | ||
|
|
96937e1972 | ||
|
|
7018134d52 | ||
|
|
f3419a40e6 | ||
|
|
7a9ec33ba1 | ||
|
|
1c466317b8 |
11
.idea/PIbd-23-Salin-O.A.-Monorail-Hard.iml
generated
Normal file
11
.idea/PIbd-23-Salin-O.A.-Monorail-Hard.iml
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" default="true">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/PIbd-23-Salin-O.A.-Monorail-Hard.iml" filepath="$PROJECT_DIR$/.idea/PIbd-23-Salin-O.A.-Monorail-Hard.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -6,6 +6,7 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningLocomotive extends DrawningMonorail{
|
||||
|
||||
public DrawningLocomotive(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb,
|
||||
int width, int height, boolean secondCabine, boolean magniteRail, Color additionalColor){
|
||||
super(speed, weight, bodyColor, wheelColor, tireColor, width, height);
|
||||
@@ -15,6 +16,10 @@ public class DrawningLocomotive extends DrawningMonorail{
|
||||
}
|
||||
}
|
||||
|
||||
public void ChangeAddColor(Color col){
|
||||
((EntityLocomotive)EntityMonorail).AdditionalColor = col;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawMonorail(Graphics2D g2d){
|
||||
if (!(EntityMonorail instanceof EntityLocomotive))
|
||||
|
||||
@@ -2,6 +2,7 @@ package MonorailHard.DrawningObjects;
|
||||
|
||||
import MonorailHard.DirectionType;
|
||||
import MonorailHard.Entities.EntityMonorail;
|
||||
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||||
import MonorailHard.MovementStrategy.IMoveableObject;
|
||||
import com.sun.source.tree.ImportTree;
|
||||
|
||||
@@ -12,12 +13,13 @@ import java.util.Random;
|
||||
public class DrawningMonorail {
|
||||
|
||||
protected EntityMonorail EntityMonorail;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
public int _pictureWidth;
|
||||
public int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected int _monorailWidth = 133;
|
||||
protected int _monorailHeight = 50;
|
||||
public int _monorailWidth = 133;
|
||||
public int _wheelNumb;
|
||||
public int _monorailHeight = 50;
|
||||
protected IDraw DrawningWheels;
|
||||
|
||||
protected int wheelSz;
|
||||
@@ -47,6 +49,17 @@ public class DrawningMonorail {
|
||||
DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6));
|
||||
}
|
||||
|
||||
public void ChangeIDraw(IDraw obj){
|
||||
DrawningWheels = obj;
|
||||
obj.ChangeWheelsNumb(_wheelNumb);
|
||||
obj.ChangeWheelsNumb(_wheelNumb);
|
||||
obj.ChangeX(_startPosX);
|
||||
obj.ChangeY(_startPosY);
|
||||
}
|
||||
public void ChangeWheelsNumb(int numb){
|
||||
_wheelNumb = numb;
|
||||
DrawningWheels.ChangeWheelsNumb(numb);
|
||||
}
|
||||
protected DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor,
|
||||
int width, int height, int monorailWidth, int monorailHeight){
|
||||
if(width <= _monorailWidth || height <= _monorailHeight)
|
||||
@@ -78,6 +91,8 @@ public class DrawningMonorail {
|
||||
return;
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
DrawningWheels.ChangeX(_startPosX);
|
||||
DrawningWheels.ChangeY(_startPosY);
|
||||
if(x + _monorailWidth >= _pictureWidth|| y + _monorailHeight >= _pictureHeight || x < 0 || y < 0){
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
@@ -111,7 +126,9 @@ public class DrawningMonorail {
|
||||
return can;
|
||||
}
|
||||
|
||||
|
||||
public IMoveableObject GetMoveableObject(){
|
||||
return new DrawningObjectMonorail(this);
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction){
|
||||
if (!CanMove(direction) || EntityMonorail == null)
|
||||
@@ -139,6 +156,10 @@ public class DrawningMonorail {
|
||||
DrawningWheels.ChangeY(_startPosY);
|
||||
}
|
||||
|
||||
public void ChangeColor(Color col){
|
||||
EntityMonorail.BodyColor = col;
|
||||
}
|
||||
|
||||
public void DrawMonorail(Graphics2D g2d){
|
||||
if (EntityMonorail == null)
|
||||
return;
|
||||
|
||||
@@ -61,7 +61,7 @@ public class DrawningWheelsCart implements IDraw{
|
||||
CurY + Height / 10 * 7};
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawLine(CurX + Width / 10 * 2, CurY + Height / 10 * 7,CurX + Width / 10 * 4, CurY + Height / 10 * 9);
|
||||
g2d.drawLine(CurX + Width / 10 * 4, CurY + Height / 10 * 7,CurX + Width / 10 * 2, CurY + Height / 10 * 9);
|
||||
|
||||
@@ -72,7 +72,7 @@ public class DrawningWheelsCart implements IDraw{
|
||||
CurY + Height / 10 * 9, CurY + Height / 10 * 9};
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawLine(CurX + Width / 10 * 6, CurY + Height / 10 * 7,CurX + Width / 10 * 9, CurY + Height / 10 * 9);
|
||||
g2d.drawLine(CurX + Width / 10 * 9, CurY + Height / 10 * 7,CurX + Width / 10 * 6, CurY + Height / 10 * 9);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package MonorailHard.Entities;
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityLocomotive extends EntityMonorail{
|
||||
private Color AdditionalColor;
|
||||
public Color AdditionalColor;
|
||||
private boolean SecondCabine;
|
||||
private boolean MagniteRail;
|
||||
public Color AdditionalColor(){return AdditionalColor;};
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.awt.*;
|
||||
public class EntityMonorail {
|
||||
private int Speed;
|
||||
private double Weight, Step;
|
||||
private Color BodyColor, WheelColor, TireColor;
|
||||
public Color BodyColor, WheelColor, TireColor;
|
||||
|
||||
public int Speed(){
|
||||
return Speed;
|
||||
|
||||
@@ -3,6 +3,7 @@ package MonorailHard;
|
||||
import MonorailHard.DrawningObjects.DrawningLocomotive;
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
import MonorailHard.MovementStrategy.*;
|
||||
import org.w3c.dom.ranges.DocumentRange;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.AbstractDocument;
|
||||
@@ -11,22 +12,48 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class FormMonorail{
|
||||
|
||||
private DrawningMonorail DrawningMonorail;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
|
||||
public JButton buttonSelect;
|
||||
|
||||
public JFrame MonorailFrame;
|
||||
|
||||
Canvas canv;
|
||||
static int pictureBoxWidth = 882;
|
||||
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(){
|
||||
if(DrawningMonorail == null)
|
||||
return;
|
||||
canv.repaint();
|
||||
}
|
||||
|
||||
public void ChangeMonorail(DrawningMonorail newMonorail){
|
||||
newMonorail.SetPosition(0,0);
|
||||
DrawningMonorail = newMonorail;
|
||||
canv.DrawningMonorail = DrawningMonorail;
|
||||
|
||||
}
|
||||
|
||||
public FormMonorail(){
|
||||
JFrame MonorailFrame =new JFrame ();
|
||||
MonorailFrame =new JFrame ();
|
||||
JButton buttonCreate = new JButton("Создать");
|
||||
JButton buttonCreateLocomotive = new JButton("Создать локомотив");
|
||||
JButton buttonStep = new JButton("Шаг");
|
||||
buttonSelect = new JButton ("Выбрать");
|
||||
JComboBox comboBoxStrategy = new JComboBox(
|
||||
new String[]{
|
||||
"Довести до центра",
|
||||
@@ -89,8 +116,13 @@ public class FormMonorail{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
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),
|
||||
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)),
|
||||
pictureBoxWidth, pictureBoxHeight);
|
||||
@@ -104,19 +136,30 @@ public class FormMonorail{
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
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),
|
||||
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)),
|
||||
random.nextInt(1, 6),
|
||||
pictureBoxWidth, pictureBoxHeight, random.nextBoolean(), random.nextBoolean(),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)));
|
||||
additionalColor);
|
||||
canv.DrawningMonorail = DrawningMonorail;
|
||||
comboBoxStrategy.enable(true);
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
RightButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@@ -156,10 +199,11 @@ public class FormMonorail{
|
||||
});
|
||||
|
||||
MonorailFrame.setSize (900, 500);
|
||||
MonorailFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
||||
MonorailFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
|
||||
MonorailFrame.setLayout(null);
|
||||
canv = new Canvas();
|
||||
canv.setSize(pictureBoxWidth, pictureBoxHeight);
|
||||
buttonSelect.setBounds(383,401, 180, 40);
|
||||
buttonCreate.setBounds(198, 401, 180, 40);
|
||||
buttonCreateLocomotive.setBounds(12, 401, 180, 40);
|
||||
RightButton.setBounds(840,411,30,30);
|
||||
@@ -177,6 +221,7 @@ public class FormMonorail{
|
||||
MonorailFrame.add(RightButton);
|
||||
MonorailFrame.add(comboBoxStrategy);
|
||||
MonorailFrame.add(buttonStep);
|
||||
MonorailFrame.add(buttonSelect);
|
||||
MonorailFrame.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
216
src/MonorailHard/FormMonorailCollection.java
Normal file
216
src/MonorailHard/FormMonorailCollection.java
Normal file
@@ -0,0 +1,216 @@
|
||||
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());
|
||||
FormMonorailConfig form = new FormMonorailConfig();
|
||||
form.addButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_monorails.Insert(form._monorail))
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
form._monorail._pictureWidth = pictureBoxWidth;
|
||||
form._monorail._pictureHeight = pictureBoxHeight;
|
||||
Draw();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
form.frameConfig.dispose();
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
form.cancelButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
form.frameConfig.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();
|
||||
}
|
||||
}
|
||||
472
src/MonorailHard/FormMonorailConfig.java
Normal file
472
src/MonorailHard/FormMonorailConfig.java
Normal file
@@ -0,0 +1,472 @@
|
||||
package MonorailHard;
|
||||
|
||||
import MonorailHard.DrawningObjects.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.DataFlavor;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.Border;
|
||||
|
||||
import java.awt.event.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import static java.lang.Integer.parseInt;
|
||||
|
||||
|
||||
public class FormMonorailConfig {
|
||||
private class LabelTransferHandler extends TransferHandler {
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new StringSelection(((JLabel)c).getText());
|
||||
}
|
||||
}
|
||||
|
||||
private class ColorTransferable implements Transferable {
|
||||
private Color color;
|
||||
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
|
||||
|
||||
public ColorTransferable(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataFlavor[] getTransferDataFlavors() {
|
||||
return new DataFlavor[]{colorDataFlavor};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||
return colorDataFlavor.equals(flavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return color;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class iDrawTransferable implements Transferable {
|
||||
private IDraw iDrawObject;
|
||||
private static final DataFlavor iDrawDataFlavor = new DataFlavor(IDraw.class, "IDraw");
|
||||
|
||||
|
||||
|
||||
public iDrawTransferable(IDraw iDrawObject) {
|
||||
this.iDrawObject = iDrawObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataFlavor[] getTransferDataFlavors() {
|
||||
return new DataFlavor[]{iDrawDataFlavor};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||
return iDrawDataFlavor.equals(flavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return iDrawObject;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class PanelTransferHandler extends TransferHandler {
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new ColorTransferable(((JPanel)c).getBackground());
|
||||
}
|
||||
}
|
||||
|
||||
private class LabelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class PanelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
private class IDrawComponent extends JComponent{
|
||||
public IDraw obj;
|
||||
|
||||
public IDrawComponent(IDraw obj){
|
||||
this.obj = obj;
|
||||
this.addMouseListener(
|
||||
new MouseAdapter(){
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((IDrawComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawComponent)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
);
|
||||
this.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new iDrawTransferable(((IDrawComponent)c).obj);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public JFrame frameConfig;
|
||||
|
||||
public JButton addButton;
|
||||
public JButton cancelButton;
|
||||
public DrawningMonorail _monorail;
|
||||
int _pictureBoxWidth = 218;
|
||||
int _pictureBoxHeight = 190;
|
||||
|
||||
public FormMonorailConfig(){
|
||||
frameConfig = new JFrame();
|
||||
addButton = new JButton("Добавить");
|
||||
cancelButton = new JButton("Отмена");
|
||||
addButton.setBounds(555,262,94,29);
|
||||
cancelButton.setBounds(679,262,94,29);
|
||||
Canvas canv = new Canvas();
|
||||
canv.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
canv.setBounds(555, 65, _pictureBoxWidth, _pictureBoxHeight);
|
||||
JLabel iDrawLabel = new JLabel("Орнамент");
|
||||
JLabel colorLabel = new JLabel("Цвет");
|
||||
DrawningMonorail toGetSize = new DrawningMonorail(0,0, null, null, null, 0, 0);
|
||||
IDrawComponent cartComp = new IDrawComponent(new DrawningWheelsCart(toGetSize._monorailWidth - toGetSize._monorailWidth / 10,
|
||||
toGetSize._monorailHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._monorailWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._monorailHeight / 2, Color.GRAY, Color.BLACK));
|
||||
IDrawComponent ornComp = new IDrawComponent(new DrawningWheelsOrn(toGetSize._monorailWidth - toGetSize._monorailWidth / 10,
|
||||
toGetSize._monorailHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._monorailWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._monorailHeight / 2, Color.GRAY, Color.BLACK));
|
||||
IDrawComponent baseComp = new IDrawComponent(new DrawningWheels(toGetSize._monorailWidth - toGetSize._monorailWidth / 10,
|
||||
toGetSize._monorailHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._monorailWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._monorailHeight / 2, Color.GRAY, Color.BLACK));
|
||||
cartComp.setLayout(new GridLayout(1,1));
|
||||
ornComp.setLayout(new GridLayout(1,1));
|
||||
baseComp.setLayout(new GridLayout(1,1));
|
||||
iDrawLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
iDrawLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
JLabel cartLabel = new JLabel("На тележке");
|
||||
JLabel ornLabel = new JLabel("На колесах");
|
||||
JLabel baseLabel = new JLabel("Без");
|
||||
cartLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
cartLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
ornLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
ornLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
baseLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
baseLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
|
||||
cartComp.add(cartLabel);
|
||||
ornComp.add(ornLabel);
|
||||
baseComp.add(baseLabel);
|
||||
cartComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
ornComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
baseComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JLabel addColorLabel = new JLabel("Доп цвет");
|
||||
addColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
addColorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setBounds(555, 20, 70, 33);
|
||||
addColorLabel.setBounds(629, 20, 70, 33);
|
||||
iDrawLabel.setBounds(703, 20, 70, 33);
|
||||
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
addColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
iDrawLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JCheckBox checkSecondCabine = new JCheckBox("Наличие 2 кабины");
|
||||
JCheckBox checkMagniteRail = new JCheckBox("Наличие магнитной рельсы");
|
||||
checkSecondCabine.setBounds(6, 132, 159, 24);
|
||||
checkMagniteRail.setBounds(6, 162, 145, 24);
|
||||
JLabel simpleLabel = new JLabel("Простой");
|
||||
JLabel advancedLabel = new JLabel("Продвинутый");
|
||||
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
simpleLabel.setBounds(171,169, 120, 50);
|
||||
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
advancedLabel.setBounds(297,169, 120, 50);
|
||||
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
cartComp.setBounds(171,229, 120, 50);
|
||||
ornComp.setBounds(297,229, 120, 50);
|
||||
baseComp.setBounds(423,229, 120, 50);
|
||||
JLabel speedLabel = new JLabel ("Скорость");
|
||||
JLabel weightLabel = new JLabel ("Вес");
|
||||
JPanel colorPanel = new JPanel();
|
||||
colorPanel.setBounds(171, 23, 234,143);
|
||||
SpinnerNumberModel speedSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
|
||||
SpinnerNumberModel weightSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
|
||||
SpinnerNumberModel wheelNumbSpinnerModel = new SpinnerNumberModel(2, 2, 4, 1.0);
|
||||
JSpinner wheelNumbSpinner = new JSpinner(wheelNumbSpinnerModel);
|
||||
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
|
||||
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
|
||||
speedSpinner.setBounds(6, 46, 150, 27);
|
||||
speedLabel.setBounds(6, 23, 73, 20);
|
||||
weightSpinner.setBounds(6, 99, 150, 27);
|
||||
weightLabel.setBounds(6, 76, 33, 20);
|
||||
wheelNumbSpinner.setBounds(6, 200, 150, 27);
|
||||
|
||||
JPanel redPanel = new JPanel();
|
||||
JPanel greenPanel = new JPanel();
|
||||
JPanel bluePanel = new JPanel();
|
||||
JPanel yellowPanel = new JPanel();
|
||||
JPanel whitePanel = new JPanel();
|
||||
JPanel grayPanel = new JPanel();
|
||||
JPanel blackPanel = new JPanel();
|
||||
JPanel purplePanel = new JPanel();
|
||||
|
||||
redPanel.setTransferHandler(new PanelTransferHandler());
|
||||
greenPanel.setTransferHandler(new PanelTransferHandler());
|
||||
bluePanel.setTransferHandler(new PanelTransferHandler());
|
||||
yellowPanel.setTransferHandler(new PanelTransferHandler());
|
||||
whitePanel.setTransferHandler(new PanelTransferHandler());
|
||||
grayPanel.setTransferHandler(new PanelTransferHandler());
|
||||
blackPanel.setTransferHandler(new PanelTransferHandler());
|
||||
purplePanel.setTransferHandler(new PanelTransferHandler());
|
||||
|
||||
redPanel.addMouseListener(new PanelMouseAdapter());
|
||||
greenPanel.addMouseListener(new PanelMouseAdapter());
|
||||
bluePanel.addMouseListener(new PanelMouseAdapter());
|
||||
yellowPanel.addMouseListener(new PanelMouseAdapter());
|
||||
whitePanel.addMouseListener(new PanelMouseAdapter());
|
||||
grayPanel.addMouseListener(new PanelMouseAdapter());
|
||||
blackPanel.addMouseListener(new PanelMouseAdapter());
|
||||
purplePanel.addMouseListener(new PanelMouseAdapter());
|
||||
|
||||
redPanel.setName("Красный");
|
||||
greenPanel.setName("Зелёный");
|
||||
bluePanel.setName("Синий");
|
||||
yellowPanel.setName("Жёлтый");
|
||||
whitePanel.setName("Белый");
|
||||
grayPanel.setName("Серый");
|
||||
blackPanel.setName("Чёрный");
|
||||
purplePanel.setName("Фиолетовый");
|
||||
|
||||
|
||||
simpleLabel.setTransferHandler(new LabelTransferHandler());
|
||||
simpleLabel.addMouseListener(new LabelMouseAdapter());
|
||||
|
||||
advancedLabel.setTransferHandler(new LabelTransferHandler());
|
||||
advancedLabel.addMouseListener(new LabelMouseAdapter());
|
||||
|
||||
|
||||
|
||||
redPanel.setBackground(Color.RED);
|
||||
greenPanel.setBackground(Color.GREEN);
|
||||
bluePanel.setBackground(Color.BLUE);
|
||||
yellowPanel.setBackground(Color.YELLOW);
|
||||
whitePanel.setBackground(Color.WHITE);
|
||||
grayPanel.setBackground(Color.GRAY);
|
||||
blackPanel.setBackground(Color.BLACK);
|
||||
purplePanel.setBackground(Color.PINK);
|
||||
|
||||
colorPanel.setLayout(new GridLayout(2, 4));
|
||||
|
||||
colorPanel.add(redPanel);
|
||||
colorPanel.add(greenPanel);
|
||||
colorPanel.add(bluePanel);
|
||||
colorPanel.add(yellowPanel);
|
||||
colorPanel.add(whitePanel);
|
||||
colorPanel.add(grayPanel);
|
||||
colorPanel.add(blackPanel);
|
||||
colorPanel.add(purplePanel);
|
||||
|
||||
|
||||
frameConfig.add(colorLabel);
|
||||
frameConfig.add(addColorLabel);
|
||||
frameConfig.add(iDrawLabel);
|
||||
frameConfig.setLayout(null);
|
||||
frameConfig.setSize(818, 350);
|
||||
frameConfig.add(speedLabel);
|
||||
frameConfig.add(speedSpinner);
|
||||
frameConfig.add(weightLabel);
|
||||
frameConfig.add(weightSpinner);
|
||||
frameConfig.add(simpleLabel);
|
||||
frameConfig.add(advancedLabel);
|
||||
frameConfig.add(checkSecondCabine);
|
||||
frameConfig.add(checkMagniteRail);
|
||||
frameConfig.add(canv);
|
||||
frameConfig.add(addButton);
|
||||
frameConfig.add(cancelButton);
|
||||
frameConfig.add(wheelNumbSpinner);
|
||||
|
||||
frameConfig.setVisible(true);
|
||||
frameConfig.add(colorPanel);
|
||||
frameConfig.add(cartComp);
|
||||
frameConfig.add(ornComp);
|
||||
frameConfig.add(baseComp);
|
||||
colorPanel.setVisible(true);
|
||||
cartComp.setVisible(true);
|
||||
ornComp.setVisible(true);
|
||||
baseComp.setVisible(true);
|
||||
|
||||
canv.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Object speedObj = speedSpinner.getValue();
|
||||
Number speedNumb = (Number) speedObj;
|
||||
int speed = speedNumb.intValue();
|
||||
|
||||
Object weightObj = weightSpinner.getValue();
|
||||
Number weightNumb = (Number) weightObj;
|
||||
int weight = weightNumb.intValue();
|
||||
|
||||
Object wheelNumbObj = wheelNumbSpinner.getValue();
|
||||
Number wheelNumbNumb = (Number) wheelNumbObj;
|
||||
int wheelNumb = wheelNumbNumb.intValue();
|
||||
|
||||
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
|
||||
switch (data) {
|
||||
case "Простой":
|
||||
|
||||
_monorail = new DrawningMonorail(speed, weight, Color.WHITE,
|
||||
Color.GRAY, Color.BLACK, _pictureBoxWidth, _pictureBoxHeight);
|
||||
_monorail.ChangeWheelsNumb(wheelNumb);
|
||||
break;
|
||||
case "Продвинутый":
|
||||
_monorail = new DrawningLocomotive(speed, weight, Color.WHITE, Color.GRAY, Color.BLACK,
|
||||
wheelNumb, _pictureBoxWidth, _pictureBoxHeight,
|
||||
checkSecondCabine.isSelected(), checkMagniteRail.isSelected(), Color.GRAY);
|
||||
_monorail.ChangeWheelsNumb(wheelNumb);
|
||||
break;
|
||||
}
|
||||
_monorail.SetPosition(_pictureBoxWidth / 2 -_monorail._monorailWidth / 2,
|
||||
_pictureBoxHeight / 2 -_monorail._monorailHeight / 2);
|
||||
|
||||
canv.DrawningMonorail = _monorail;
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
iDrawLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(iDrawTransferable.iDrawDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
IDraw obj = (IDraw) support.getTransferable().getTransferData(iDrawTransferable.iDrawDataFlavor);
|
||||
if (_monorail == null)
|
||||
return false;
|
||||
_monorail.ChangeIDraw(obj);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
colorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (_monorail == null)
|
||||
return false;
|
||||
_monorail.ChangeColor(color);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
addColorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (_monorail == null || !(_monorail instanceof DrawningLocomotive))
|
||||
return false;
|
||||
((DrawningLocomotive)_monorail).ChangeAddColor(color);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
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 static void main(String[] args) throws IOException {
|
||||
FormMonorail form = new FormMonorail();
|
||||
FormMonorailCollection form = new FormMonorailCollection();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user