Done
This commit is contained in:
parent
b953e9735c
commit
94e48db236
111
CruiserCollectionFrame.java
Normal file
111
CruiserCollectionFrame.java
Normal file
@ -0,0 +1,111 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class CruiserCollectionFrame extends JFrame {
|
||||
private CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cars;
|
||||
|
||||
public CruiserCollectionFrame() {
|
||||
this.setSize(800, 600);
|
||||
this.setTitle("Collection");
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setResizable(false);
|
||||
this.setLocationRelativeTo(null);
|
||||
|
||||
CollectionPanel panel = new CollectionPanel();
|
||||
|
||||
this.add(panel);
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
public class CollectionPanel extends JPanel {
|
||||
|
||||
static final int SCREEN_W = 800;
|
||||
static final int SCREEN_H = 600;
|
||||
CollectionPanel() {
|
||||
this.setLayout(null);
|
||||
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
|
||||
|
||||
JTextField textFieldRemove = new JTextField();
|
||||
textFieldRemove.setBounds(600, 200, 150, 30);
|
||||
this.add(textFieldRemove);
|
||||
|
||||
JButton ButtonAddCar = new JButton("Добавить машину");
|
||||
ButtonAddCar.setBounds(600, 150, 150, 30);
|
||||
ButtonAddCar.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
GameFrame form = new GameFrame();
|
||||
if (form.getSelectedCar() != null) {
|
||||
if (_cars.plus(_cars, form.SelectedCar) != -1) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||
|
||||
}
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
this.add(ButtonAddCar);
|
||||
|
||||
JButton ButtonRemoveCar = new JButton("Удалить машину");
|
||||
ButtonRemoveCar.setBounds(600, 250, 150, 30);
|
||||
ButtonRemoveCar.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_cars == null) {
|
||||
return;
|
||||
}
|
||||
String tmp = textFieldRemove.getText();
|
||||
int num;
|
||||
|
||||
try {
|
||||
num = Integer.parseInt(tmp);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
_cars.minus(_cars, num);
|
||||
|
||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
repaint();
|
||||
|
||||
}
|
||||
});
|
||||
this.add(ButtonRemoveCar);
|
||||
|
||||
JButton ButtonRefresh = new JButton("Обновить");
|
||||
ButtonRefresh.setBounds(600, 300, 150, 30);
|
||||
ButtonRefresh.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_cars == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
this.add(ButtonRefresh);
|
||||
|
||||
|
||||
_cars = new CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>(SCREEN_W, SCREEN_H);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
draw(g);
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
_cars.ShowCars(g);
|
||||
}
|
||||
}
|
||||
}
|
85
CruiserGenericCollection.java
Normal file
85
CruiserGenericCollection.java
Normal file
@ -0,0 +1,85 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class CruiserGenericCollection<T extends DrawingCruiser, U extends IMoveableObject> {
|
||||
private int _pictureWidth;
|
||||
|
||||
private int _pictureHeight;
|
||||
|
||||
private int _placeSizeWidth = 210;
|
||||
|
||||
private int _placeSizeHeight = 90;
|
||||
|
||||
private SetGeneric<T> _collection;
|
||||
public CruiserGenericCollection(int picWidth, int picHeight)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
public int plus(CruiserGenericCollection<T, U> collect, T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return collect._collection.Insert(obj);
|
||||
}
|
||||
public boolean minus(CruiserGenericCollection<T, U> collect, int pos)
|
||||
{
|
||||
T obj = collect._collection.Get(pos);
|
||||
if (obj != null)
|
||||
{
|
||||
collect._collection.Remove(pos);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public U GetU(int pos)
|
||||
{
|
||||
T ans = _collection.Get(pos);
|
||||
if(ans == null)
|
||||
return null;
|
||||
return (U)ans.GetMoveableObject();
|
||||
}
|
||||
|
||||
public void ShowCars(Graphics gr)
|
||||
{
|
||||
DrawBackground(gr);
|
||||
DrawObjects(gr);
|
||||
}
|
||||
private void DrawBackground(Graphics g)
|
||||
{
|
||||
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)
|
||||
{
|
||||
DrawingCruiser car;
|
||||
int numPlacesInRow = _pictureWidth / _placeSizeWidth;
|
||||
for (int i = 0; i < _collection.Count(); i++)
|
||||
{
|
||||
// TODO получение объекта
|
||||
// TODO установка позиции
|
||||
// TODO прорисовка объекта
|
||||
car = _collection.Get(i);
|
||||
if (car != null)
|
||||
{
|
||||
car.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10);
|
||||
car.DrawTransport(g);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -7,7 +7,20 @@ public class DrawingAdvancedCruiser extends DrawingCruiser{
|
||||
EntityCruiser = new EntityAdvancedCruiser(speed, weight, bodyColor, additionalColor, helicopterPad, coating);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public DrawingAdvancedCruiser(EntityAdvancedCruiser entityDumpTruck,IDop wheels)
|
||||
{
|
||||
super(entityDumpTruck, wheels);
|
||||
{
|
||||
EntityCruiser = entityDumpTruck;
|
||||
this.wheels = wheels;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public EntityAdvancedCruiser getEntityCruiser(){
|
||||
return EntityCruiser;
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (!(EntityCruiser instanceof EntityAdvancedCruiser)) {
|
||||
|
@ -143,4 +143,5 @@ public class DrawingCruiser {
|
||||
}
|
||||
wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor);
|
||||
}
|
||||
public IMoveableObject GetMoveableObject(){return new DrawingObjectCruiser(this);}
|
||||
}
|
@ -1,24 +1,36 @@
|
||||
public class DrawingObjectCruiser implements IMoveableObject {
|
||||
private DrawingCruiser _drawningCruiser = null;
|
||||
public DrawingObjectCruiser(DrawingCruiser drawningCruiser) {
|
||||
_drawningCruiser = drawningCruiser;
|
||||
public class DrawingObjectCruiser implements IMoveableObject{
|
||||
private DrawingCruiser _drawningCar;
|
||||
public DrawingObjectCruiser(DrawingCruiser drawningCar)
|
||||
{
|
||||
_drawningCar = drawningCar;
|
||||
}
|
||||
public ObjectParameters GetObjectPosition() {
|
||||
if (_drawningCruiser == null || _drawningCruiser.EntityCruiser == null) {
|
||||
public ObjectParameters GetObjectPosition()
|
||||
{
|
||||
|
||||
|
||||
if (_drawningCar == null || _drawningCar.EntityCruiser ==
|
||||
null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawningCruiser.GetPosX(), _drawningCruiser.GetPosY(), _drawningCruiser.GetWidth(), _drawningCruiser.GetHeight());
|
||||
return new ObjectParameters(_drawningCar.GetPosX(),
|
||||
_drawningCar.GetPosY(), _drawningCar.GetWidth(), _drawningCar.GetHeight());
|
||||
|
||||
}
|
||||
public int GetStep() {
|
||||
return (int) (_drawningCruiser.EntityCruiser.Step());
|
||||
if(_drawningCar.getEntity() == null)
|
||||
return 0;
|
||||
return (int)_drawningCar.getEntity().Step();
|
||||
}
|
||||
public boolean CheckCanMove(Direction direction) {
|
||||
if (_drawningCruiser.CanMove(direction)){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
public boolean CheckCanMove(Direction direction){
|
||||
if(_drawningCar == null)
|
||||
return false;
|
||||
return _drawningCar.CanMove(direction);
|
||||
}
|
||||
public void MoveObject(Direction direction) {
|
||||
_drawningCruiser.MoveTransport(direction);
|
||||
|
||||
public void MoveObject(Direction direction){
|
||||
if(_drawningCar == null)
|
||||
return;
|
||||
_drawningCar.MoveTransport(direction);
|
||||
}
|
||||
}
|
143
GameFrame.java
143
GameFrame.java
@ -5,12 +5,15 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class GameFrame extends JFrame {
|
||||
public class GameFrame extends JDialog {
|
||||
protected DrawingCruiser SelectedCar;
|
||||
|
||||
GameFrame() {
|
||||
this.setSize(710, 535);
|
||||
this.setTitle("Cruiser");
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setTitle("DumpTruck");
|
||||
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
|
||||
this.setModalityType(ModalityType.APPLICATION_MODAL);
|
||||
this.setResizable(false);
|
||||
this.setLocationRelativeTo(null);
|
||||
|
||||
@ -19,90 +22,115 @@ public class GameFrame extends JFrame {
|
||||
|
||||
this.setVisible(true);
|
||||
}
|
||||
public DrawingCruiser getSelectedCar(){
|
||||
return SelectedCar;
|
||||
}
|
||||
|
||||
public class GamePanel extends JPanel {
|
||||
static final int SCREEN_W = 700;
|
||||
static final int SCREEN_H = 500;
|
||||
int xx = 0;
|
||||
int yy = 0;
|
||||
private DrawingAdvancedCruiser _drawningAdvancedCruiser;
|
||||
private DrawingCruiser _drawningCruiser;
|
||||
private DrawingCruiser _drawningCar;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
JColorChooser dialogColor = new JColorChooser();
|
||||
|
||||
|
||||
GamePanel() {
|
||||
this.setLayout(null);
|
||||
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
|
||||
GridBagConstraints layers = new GridBagConstraints();
|
||||
|
||||
JButton buttonCruiser = new JButton("Создать простой объект");
|
||||
buttonCruiser.addActionListener(new ActionListener() {
|
||||
JButton buttonCar = new JButton("Создать грузовик");
|
||||
buttonCar.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Random random = new Random();
|
||||
_drawningCruiser = new DrawingCruiser(random.nextInt(100, 300),
|
||||
Color color = Color.BLACK;
|
||||
|
||||
Color choosenColor = JColorChooser.showDialog(getParent(), "Выберите цвет", Color.WHITE);
|
||||
if(choosenColor != null){
|
||||
color = choosenColor;
|
||||
}
|
||||
|
||||
_drawningCar = new DrawingCruiser(random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
color,
|
||||
GamePanel.SCREEN_W, GamePanel.SCREEN_H);
|
||||
_drawningCruiser.SetPosition(random.nextInt(10, 100), random.nextInt(10,
|
||||
_drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10,
|
||||
100));
|
||||
|
||||
int ornament = random.nextInt(1, 4);
|
||||
switch (ornament){
|
||||
case 1:
|
||||
_drawningCruiser.wheels = new NumberOfWheels(random.nextInt(2, 5));
|
||||
_drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5));
|
||||
break;
|
||||
case 2:
|
||||
_drawningCruiser.wheels = new DopClassRect(random.nextInt(2, 5));
|
||||
_drawningCar.wheels = new DopClassRect(random.nextInt(2, 5));
|
||||
break;
|
||||
default:
|
||||
_drawningCruiser.wheels = new DopClassMixed(random.nextInt(2, 5));
|
||||
_drawningCar.wheels = new DopClassMixed(random.nextInt(2, 5));
|
||||
break;
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonCruiser.setBounds(20 + 150, 390, 120, 30);
|
||||
this.add(buttonCruiser);
|
||||
buttonCar.setBounds(20 + 150, 390, 120, 30);
|
||||
this.add(buttonCar);
|
||||
|
||||
JButton buttonAdvancedCruiser = new JButton("Создать продвинутый объект");
|
||||
buttonAdvancedCruiser.addActionListener(new ActionListener() {
|
||||
JButton buttonDumpCar = new JButton("Создать самосвал");
|
||||
buttonDumpCar.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Random random = new Random();
|
||||
_drawningCruiser = new DrawingAdvancedCruiser(random.nextInt(100, 300),
|
||||
Color color = Color.BLACK; Color colorAdd = Color.WHITE;
|
||||
|
||||
Color choosenColor = JColorChooser.showDialog(getParent(), "Выберите цвет", Color.WHITE);
|
||||
Color choosenAddColor = JColorChooser.showDialog(getParent(), "Выберите цвет", Color.WHITE);
|
||||
|
||||
if(choosenColor != null){
|
||||
color = choosenColor;
|
||||
}
|
||||
if(choosenAddColor != null){
|
||||
colorAdd = choosenAddColor;
|
||||
}
|
||||
|
||||
_drawningCar = new DrawingAdvancedCruiser(random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)), Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)), random.nextBoolean(),random.nextBoolean(),random.nextBoolean(),
|
||||
color, colorAdd, random.nextBoolean(),random.nextBoolean(),random.nextBoolean(),
|
||||
GamePanel.SCREEN_W, GamePanel.SCREEN_H); //TODO
|
||||
_drawningCruiser.SetPosition(random.nextInt(10, 100), random.nextInt(10,
|
||||
_drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10,
|
||||
100));
|
||||
|
||||
int ornament = random.nextInt(1, 4);
|
||||
switch (ornament){
|
||||
case 1:
|
||||
_drawningCruiser.wheels = new NumberOfWheels(random.nextInt(2, 5));
|
||||
_drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5));
|
||||
break;
|
||||
case 2:
|
||||
_drawningCruiser.wheels = new DopClassRect(random.nextInt(2, 5));
|
||||
_drawningCar.wheels = new DopClassRect(random.nextInt(2, 5));
|
||||
break;
|
||||
default:
|
||||
_drawningCruiser.wheels = new DopClassMixed(random.nextInt(2, 5));
|
||||
_drawningCar.wheels = new DopClassMixed(random.nextInt(2, 5));
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonAdvancedCruiser.setBounds( 20, 390, 120, 30);
|
||||
this.add(buttonAdvancedCruiser);
|
||||
buttonDumpCar.setBounds( 20, 390, 120, 30);
|
||||
this.add(buttonDumpCar);
|
||||
|
||||
JTextField textStrategy = new JTextField();
|
||||
textStrategy.setBounds(550, 20, 120, 30);
|
||||
this.add(textStrategy);
|
||||
|
||||
JButton buttonStep = new JButton("Шаг");
|
||||
buttonStep.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_drawningCruiser == null)
|
||||
if (_drawningCar == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -121,30 +149,35 @@ public class GameFrame extends JFrame {
|
||||
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new DrawingObjectCruiser(_drawningCruiser), SCREEN_W, SCREEN_H);
|
||||
_abstractStrategy.SetData((IMoveableObject) _drawningCar, SCREEN_W, SCREEN_H);
|
||||
//textStrategy.setText();
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
|
||||
return;
|
||||
}
|
||||
System.out.println("step");
|
||||
_abstractStrategy.MakeStep();
|
||||
repaint();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
//comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
buttonStep.setBounds(550, 60, 70, 30);
|
||||
this.add(buttonStep);
|
||||
JPanel panel = new JPanel(new GridBagLayout());
|
||||
|
||||
JButton up = new BasicArrowButton(BasicArrowButton.NORTH);
|
||||
up.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
_drawningCruiser.MoveTransport(Direction.Up);
|
||||
_drawningCar.MoveTransport(Direction.Up);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
@ -152,7 +185,7 @@ public class GameFrame extends JFrame {
|
||||
left.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
_drawningCruiser.MoveTransport(Direction.Left);
|
||||
_drawningCar.MoveTransport(Direction.Left);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
@ -160,7 +193,7 @@ public class GameFrame extends JFrame {
|
||||
down.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
_drawningCruiser.MoveTransport(Direction.Down);
|
||||
_drawningCar.MoveTransport(Direction.Down);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
@ -168,10 +201,12 @@ public class GameFrame extends JFrame {
|
||||
right.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
_drawningCruiser.MoveTransport(Direction.Right);
|
||||
_drawningCar.MoveTransport(Direction.Right);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
up.setBounds(570, 350, 30, 30);
|
||||
this.add(up);
|
||||
|
||||
@ -183,16 +218,48 @@ public class GameFrame extends JFrame {
|
||||
|
||||
right.setBounds(570 + 40, 350 + 40, 30, 30);
|
||||
this.add(right);
|
||||
|
||||
JButton buttonSelectedCar = new JButton("Выбрать машину");
|
||||
|
||||
buttonSelectedCar.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
SelectedCar = _drawningCar;
|
||||
dispose();
|
||||
|
||||
}
|
||||
});
|
||||
buttonSelectedCar.setBounds(510, 100, 150, 30);
|
||||
this.add(buttonSelectedCar);
|
||||
}
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningCar == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Graphics gr =new DebugGraphics();
|
||||
_drawningCar.DrawTransport(gr);
|
||||
|
||||
}
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
draw(g);
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
if (_drawningCruiser != null) {
|
||||
_drawningCruiser.DrawTransport(g);
|
||||
if (_drawningCar != null) {
|
||||
_drawningCar.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
public Color ChooseColor(JFrame MonorailFrame){
|
||||
JColorChooser dialog = new JColorChooser();
|
||||
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.WHITE);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
72
GenericCreate.java
Normal file
72
GenericCreate.java
Normal file
@ -0,0 +1,72 @@
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class GenericCreate<T extends EntityCruiser, U extends IDop> {
|
||||
ArrayList<T> _EntityCar;
|
||||
ArrayList<U> _Wheels;
|
||||
int pointerCars = 0;
|
||||
int pointerWheels = 0;
|
||||
public ArrayList<T> getEntities(){
|
||||
return _EntityCar;
|
||||
}
|
||||
|
||||
public ArrayList<U> getWheels(){
|
||||
return _Wheels;
|
||||
}
|
||||
public GenericCreate(int countEntities,int countWheels){
|
||||
_EntityCar=new ArrayList<>(countEntities);
|
||||
_Wheels=new ArrayList<>(countWheels);
|
||||
}
|
||||
public int Add(T car){
|
||||
if(pointerCars <= _EntityCar.size()){
|
||||
_EntityCar.add(car);
|
||||
pointerCars++;
|
||||
return pointerCars;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public int Add(U wheels){
|
||||
if(pointerWheels <= _Wheels.size()){
|
||||
_Wheels.add(wheels);
|
||||
pointerWheels++;
|
||||
return pointerWheels;
|
||||
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public DrawingCruiser DrawingGeneratedCar()
|
||||
{
|
||||
Random rand=new Random();
|
||||
if (_EntityCar.size() == 0 || _Wheels.size() == 0){
|
||||
return null;
|
||||
}
|
||||
T entity = (_EntityCar.get(rand.nextInt(pointerCars)));
|
||||
U wheel = (_Wheels.get(rand.nextInt(pointerWheels)));
|
||||
if(entity instanceof EntityAdvancedCruiser){
|
||||
return new DrawingAdvancedCruiser((EntityAdvancedCruiser)entity, wheel);
|
||||
}
|
||||
return new DrawingCruiser(entity, wheel);
|
||||
}
|
||||
public DrawingCruiser[] DrawEntitiesCars(Graphics g, int startX, int startY) {
|
||||
DrawingCruiser[] drawingCars = new DrawingCruiser[_EntityCar.size()];
|
||||
int height = 70;
|
||||
DrawingCruiser car;
|
||||
DrawingAdvancedCruiser truck;
|
||||
for (int i = 0; i < _EntityCar.size(); i++) {
|
||||
if (_EntityCar.get(i) instanceof EntityAdvancedCruiser) {
|
||||
truck = new DrawingAdvancedCruiser((EntityAdvancedCruiser) _EntityCar.get(i), null);
|
||||
truck.SetPosition(startX , startY + i * height);
|
||||
truck.DrawTransport(g);
|
||||
} else {
|
||||
car = new DrawingCruiser(_EntityCar.get(i), null);
|
||||
car.SetPosition(startX , startY + i * height);
|
||||
car.DrawTransport(g);
|
||||
}
|
||||
|
||||
}
|
||||
return drawingCars;
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
GameFrame frame = new GameFrame();
|
||||
RandomFrame frame = new RandomFrame();
|
||||
}
|
||||
}
|
178
RandomFrame.java
Normal file
178
RandomFrame.java
Normal file
@ -0,0 +1,178 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.plaf.basic.BasicArrowButton;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
public class RandomFrame extends JFrame {
|
||||
RandomPanel panel = new RandomPanel();
|
||||
RandomFrame() {
|
||||
this.setSize(710, 535);
|
||||
this.setTitle("DumpTruckRandom");
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setResizable(false);
|
||||
this.setLocationRelativeTo(null);
|
||||
|
||||
|
||||
this.add(panel);
|
||||
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
public class RandomPanel extends JPanel {
|
||||
static final int SCREEN_W = 700;
|
||||
static final int SCREEN_H = 500;
|
||||
GenericCreate<EntityCruiser, IDop> _genericCreate = new GenericCreate<>(10, 10);
|
||||
|
||||
ComponentsPanel entitiesPanel = new ComponentsPanel();
|
||||
ComponentsPanel wheelsPanel = new ComponentsPanel();
|
||||
DrawingCruiser generatedCar;
|
||||
RandomPanel() {
|
||||
this.setLayout(null);
|
||||
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
|
||||
|
||||
|
||||
|
||||
JButton buttonCar = new JButton("Создать грузовик");
|
||||
buttonCar.setBounds(20, 20, 130, 30);
|
||||
buttonCar.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
GameFrame dialogWindow = new GameFrame();
|
||||
if (dialogWindow.getSelectedCar() != null){
|
||||
if (dialogWindow.getSelectedCar() instanceof DrawingAdvancedCruiser) {
|
||||
_genericCreate.Add(((DrawingAdvancedCruiser) dialogWindow.getSelectedCar()).getEntityCruiser());
|
||||
}
|
||||
else {
|
||||
_genericCreate.Add(dialogWindow.getSelectedCar().EntityCruiser);
|
||||
}
|
||||
repaint();}
|
||||
|
||||
else{
|
||||
System.out.println("Selected car is null!");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
this.add(buttonCar);
|
||||
|
||||
JButton buttonIDop = new JButton("Создать колеса");
|
||||
buttonIDop.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Random random = new Random();
|
||||
IDop interWheels;
|
||||
switch(random.nextInt(1, 4)){
|
||||
case 1:
|
||||
interWheels = new NumberOfWheels(random.nextInt(2, 5));
|
||||
break;
|
||||
case 2:
|
||||
interWheels = new DopClassRect(random.nextInt(2, 5));
|
||||
break;
|
||||
default:
|
||||
interWheels = new DopClassMixed(random.nextInt(2, 5));
|
||||
break;
|
||||
}
|
||||
|
||||
_genericCreate.Add(interWheels);
|
||||
repaint();
|
||||
|
||||
}
|
||||
});
|
||||
buttonIDop.setBounds(20 + 130 + 20, 20, 130, 30);
|
||||
this.add(buttonIDop);
|
||||
|
||||
JButton buttonCreateRandomCar = new JButton("Сгенерировать машину");
|
||||
buttonCreateRandomCar.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
generatedCar = _genericCreate.DrawingGeneratedCar();
|
||||
if (generatedCar != null) {
|
||||
generatedCar.SetPosition(430, 160);
|
||||
}
|
||||
repaint();
|
||||
|
||||
}
|
||||
});
|
||||
buttonCreateRandomCar.setBounds(20 + 130 + 20 + 235, 20 + 80, 190, 30);
|
||||
this.add(buttonCreateRandomCar);
|
||||
|
||||
|
||||
/*entitiesPanel.setLocation(20, 20 + 50);
|
||||
this.add(entitiesPanel);*/
|
||||
|
||||
/*wheelsPanel.setLocation(20 + 150, 20 + 50);
|
||||
this.add(wheelsPanel);*/
|
||||
|
||||
/*CarRandomPanel carRandomPanel = new CarRandomPanel();
|
||||
carRandomPanel.setLocation(400, 150);
|
||||
this.add(carRandomPanel);*/
|
||||
|
||||
}
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
draw(g);
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
int startX = 20 ;
|
||||
int startY = 70;
|
||||
|
||||
int startXForWheels = 170;
|
||||
int startYForWheels = 30;
|
||||
int height = 70;
|
||||
int width = 110;
|
||||
_genericCreate.DrawEntitiesCars(g, startX, startY);
|
||||
|
||||
for (int i=0; i < _genericCreate.getWheels().size(); i++) {
|
||||
_genericCreate.getWheels().get(i).drawWheels(g, startXForWheels, startYForWheels + i * 30, Color.black);
|
||||
}
|
||||
|
||||
if (generatedCar != null){
|
||||
generatedCar.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ComponentsPanel extends JPanel {
|
||||
static final int SCREEN_W = 130;
|
||||
static final int SCREEN_H = 400;
|
||||
|
||||
ComponentsPanel() {
|
||||
this.setSize(SCREEN_W, SCREEN_H);
|
||||
//this.setBackground(Color.WHITE);
|
||||
}
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
|
||||
}
|
||||
}
|
||||
public class CarRandomPanel extends JPanel {
|
||||
static final int SCREEN_W = 200;
|
||||
static final int SCREEN_H = 200;
|
||||
|
||||
CarRandomPanel() {
|
||||
this.setSize(SCREEN_W, SCREEN_H);
|
||||
this.setBackground(Color.LIGHT_GRAY);
|
||||
}
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
draw(g);
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
54
SetGeneric.java
Normal file
54
SetGeneric.java
Normal file
@ -0,0 +1,54 @@
|
||||
public class SetGeneric<T extends Object> {
|
||||
private Object[] _places;
|
||||
|
||||
public int Count() {
|
||||
return _places.length;
|
||||
}
|
||||
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new Object[count];
|
||||
}
|
||||
|
||||
public int Insert(T car)
|
||||
{
|
||||
return Insert(car,0);
|
||||
}
|
||||
|
||||
public int Insert(T car, int position)
|
||||
{
|
||||
if (!(position >= 0 && position < Count()))
|
||||
return -1;
|
||||
if (_places[position] != null)
|
||||
{
|
||||
int indexEnd = position + 1;
|
||||
while (_places[indexEnd] != null)
|
||||
{
|
||||
indexEnd++;
|
||||
}
|
||||
for (int i = indexEnd + 1; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
|
||||
}
|
||||
_places[position] = car;
|
||||
return position;
|
||||
}
|
||||
|
||||
public boolean Remove(int position)
|
||||
{
|
||||
if (position < Count() && position >= 0)
|
||||
{
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position < Count() && position >= 0) { return (T)_places[position]; }
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user