148 lines
6.3 KiB
Java
148 lines
6.3 KiB
Java
import CollectionGenericObjects.AbstractCompany;
|
|
import CollectionGenericObjects.AdditionalCollection;
|
|
import DifferenceOfWheels.DrawningOrnamentRectangle;
|
|
import DifferenceOfWheels.DrawningOrnamentStar;
|
|
import DifferenceOfWheels.DrawningOrnamentTriangle;
|
|
import DifferenceOfWheels.IOrnaments;
|
|
import Drawnings.CanvasDumpTruck;
|
|
import Drawnings.DrawningDumpTruck;
|
|
import Drawnings.DrawningTruck;
|
|
import Entities.EntityDumpTruck;
|
|
import Entities.EntityTruck;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.Random;
|
|
|
|
public class FormAdditionalCollection extends JFrame{
|
|
DrawningTruck _drawningTruck;
|
|
private AbstractCompany _company = null;
|
|
private CanvasDumpTruck _canvasTruck = new CanvasDumpTruck();
|
|
private AdditionalCollection<EntityTruck, IOrnaments> _additionalCollection = null;
|
|
private Random random = new Random();
|
|
private JButton buttonGenerate = new JButton("Создать");
|
|
private JButton buttonAdd = new JButton("Добавить");
|
|
private JList<String> listEntity = new JList<String>();
|
|
private JList<String> listOrnament = new JList<String>();
|
|
public FormAdditionalCollection() {
|
|
setTitle("Случайный грузовик");
|
|
setMinimumSize(new Dimension(650,310));
|
|
_additionalCollection = new AdditionalCollection<EntityTruck, IOrnaments>(3,
|
|
(Class) EntityTruck.class, (Class) IOrnaments.class);
|
|
AddEntities();
|
|
AddOrnaments();
|
|
buttonGenerate.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
_drawningTruck = _additionalCollection.CreateDrawningTruckClass();
|
|
_drawningTruck.SetPictureSize(getWidth(), getHeight());
|
|
_drawningTruck.SetPosition(50,50);
|
|
_canvasTruck.repaint();
|
|
_canvasTruck._drawningTruck = _drawningTruck;
|
|
DrawningTruck _copyTruck;
|
|
if (_drawningTruck instanceof DrawningDumpTruck)
|
|
_copyTruck = new DrawningDumpTruck((EntityDumpTruck) _drawningTruck.EntityTruck, _drawningTruck._ornament);
|
|
else
|
|
_copyTruck = new DrawningTruck(_drawningTruck.EntityTruck, _drawningTruck._ornament);
|
|
_company._collection.Insert(_copyTruck);
|
|
|
|
String[] data1 = new String[_additionalCollection.Len_entity];
|
|
for (int i = 0; i < _additionalCollection.Len_entity; i++) {
|
|
EntityTruck _entity = _additionalCollection._collectionEntityTruck[i];
|
|
data1[i] = ToString(_entity);
|
|
}
|
|
String[] data2 = new String[_additionalCollection.Len_ornament];
|
|
for (int i = 0; i < _additionalCollection.Len_ornament; i++) {
|
|
IOrnaments ornament = _additionalCollection._collectionIOrnaments[i];
|
|
data2[i] = ToString(ornament);
|
|
}
|
|
listEntity.setListData(data1);
|
|
listOrnament.setListData(data2);
|
|
}
|
|
});
|
|
|
|
buttonAdd.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
FormDumpTruckCollection.canvasShow();
|
|
}
|
|
});
|
|
|
|
buttonGenerate.setBounds(450, 10, 100, 50);
|
|
buttonAdd.setBounds(450, 70, 100, 50);
|
|
add(buttonGenerate);
|
|
add(buttonAdd);
|
|
listEntity.setBounds(10,200,300,60);
|
|
listOrnament.setBounds(320,200,300,60);
|
|
add(listEntity);
|
|
add(listOrnament);
|
|
add(_canvasTruck);
|
|
setVisible(true);
|
|
}
|
|
private String ToString(EntityTruck entity) {
|
|
String str = "";
|
|
if (entity instanceof EntityDumpTruck) str += "Самосвал ";
|
|
else str += "Грузовик ";
|
|
str += entity.get_BodyColor().toString();
|
|
return str;
|
|
}
|
|
private String ToString(IOrnaments ornament) {
|
|
if (ornament == null || ornament.get_count_weels() == null)
|
|
return "Нет орнамента";
|
|
String str = "Орнамент ";
|
|
if (ornament instanceof DrawningOrnamentRectangle) str += "Квадрат ";
|
|
else if (ornament instanceof DrawningOrnamentStar) str += "Звезда ";
|
|
else if (ornament instanceof DrawningOrnamentTriangle) str += "Треугольник ";
|
|
str += ornament.get_count_weels().toString();
|
|
return str;
|
|
}
|
|
public void AddEntities() {
|
|
for (int i = 0; i < _additionalCollection.Len_entity; i++) {
|
|
random = new Random();
|
|
int speed = random.nextInt(100, 300);
|
|
double weight = random.nextInt(1000, 3000);
|
|
Color bodycolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
|
|
EntityTruck entity;
|
|
if (random.nextBoolean()) {
|
|
entity = new EntityTruck(speed, weight, bodycolor);
|
|
}
|
|
else {
|
|
Color additionalcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
|
|
boolean body = random.nextBoolean();
|
|
boolean tent = random.nextBoolean();
|
|
entity = new EntityDumpTruck(speed, weight, bodycolor,
|
|
additionalcolor, body, tent);
|
|
}
|
|
_additionalCollection.Insert(entity);
|
|
}
|
|
}
|
|
public void AddOrnaments() {
|
|
for (int i = 0; i < _additionalCollection.Len_ornament; i++) {
|
|
random = new Random();
|
|
Integer count_of_wheels = random.nextInt(2, 5);
|
|
IOrnaments _ornament = null;
|
|
switch (random.nextInt(0,3)) {
|
|
case 0:
|
|
_ornament = new DrawningOrnamentStar();
|
|
break;
|
|
case 1:
|
|
_ornament = new DrawningOrnamentTriangle();
|
|
break;
|
|
case 2:
|
|
_ornament = new DrawningOrnamentRectangle();
|
|
break;
|
|
default:
|
|
count_of_wheels = null;
|
|
break;
|
|
}
|
|
if (_ornament != null) _ornament.SetCount(count_of_wheels);
|
|
_additionalCollection.Insert(_ornament);
|
|
}
|
|
}
|
|
void setCompany(AbstractCompany company) {
|
|
this._company = company;
|
|
}
|
|
}
|