Compare commits

...

9 Commits
main ... laba4

Author SHA1 Message Date
Alenka
e3c4b3b319 Done 2023-12-29 16:43:56 +04:00
Alenka
beff4a4b13 Done 2023-12-23 16:20:46 +04:00
Alenka
2cf68f3de9 Done 2023-12-09 21:01:39 +04:00
Alenka
94e48db236 Done 2023-12-09 17:25:15 +04:00
Alenka
b953e9735c Done 2023-12-09 15:09:27 +04:00
Alenka
2577e0d879 Done 2023-12-09 00:17:07 +04:00
Alenka
d90af74620 Start 2023-12-08 23:20:53 +04:00
Alenka
bab381dabe Done 2023-11-24 23:51:47 +04:00
Alenka
6da849d421 Start 2023-11-24 22:49:33 +04:00
26 changed files with 1370 additions and 0 deletions

70
AbstractStrategy.java Normal file
View File

@ -0,0 +1,70 @@
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private Status _state = Status.NotInit;
protected int FieldWidth;
protected int FieldHeight;
public Status GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int
height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() { return MoveTo(Direction.Left);};
protected boolean MoveRight(){return MoveTo(Direction.Right);}
protected boolean MoveUp(){return MoveTo(Direction.Up);}
protected boolean MoveDown(){return MoveTo(Direction.Down);}
protected ObjectParameters GetObjectParameters(){
if (_moveableObject != null) {
return _moveableObject.GetObjectPosition();
}
else {
return null;
}
}
protected int GetStep()
{
if (_state != Status.InProgress)
{
return Integer.parseInt(null);
}
return _moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestinaion();
private boolean MoveTo(Direction directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject.CheckCanMove(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
else {
return false;
}
}
}

182
CruiserCollectionFrame.java Normal file
View File

@ -0,0 +1,182 @@
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class CruiserCollectionFrame extends JFrame {
private CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers;
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 {
private CruiserGenericStorage _storage;
TrashCollection<DrawingCruiser> _trash = new TrashCollection<>();
private JList<String> listBoxStoragesJList;
private DefaultListModel<String> listBoxStorages;
static final int SCREEN_W = 800;
static final int SCREEN_H = 600;
CollectionPanel() {
_storage = new CruiserGenericStorage(SCREEN_W,
SCREEN_H);
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 ButtonAddCruiser = new JButton("Добавить объект");
ButtonAddCruiser.setBounds(600, 150, 150, 30);
ButtonAddCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
GameFrame form = new GameFrame();
if (form.getSelectedCruiser() != null) {
if (_cruisers.plus(_cruisers, form.SelectedCruiser)) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
repaint();
}
});
this.add(ButtonAddCruiser);
JButton ButtomRemoveCruiser = new JButton("Удалить объект");
ButtomRemoveCruiser.setBounds(600, 250, 150, 30);
ButtomRemoveCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
if (_cruisers == null) {
return;
}
String tmp = textFieldRemove.getText();
int num;
try {
num = Integer.parseInt(tmp);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
DrawingCruiser deletedCruiser = _cruisers.Get(num);
_trash.Add(deletedCruiser);
_cruisers.minus(_cruisers, num);
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
repaint();
}
});
this.add(ButtomRemoveCruiser);
JButton ButtonRefresh = new JButton("Обновить");
ButtonRefresh.setBounds(600, 300, 150, 30);
ButtonRefresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
_cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
repaint();
}
});
this.add(ButtonRefresh);
JButton ButtonDelStorage = new JButton("Удалить набор");
ButtonDelStorage.setBounds(600, 450, 170, 30);
ButtonDelStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
_storage.DelSet(listBoxStoragesJList.getSelectedValue());
ReloadObjects();
}
});
this.add(ButtonDelStorage);
JButton ButtonTrash = new JButton("Удаленные объекты");
ButtonTrash.setBounds(600, 500, 170, 30);
ButtonTrash.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_trash.GetSize() == 0)
return;
GameFrame form = new GameFrame(_trash.GetLast());
_trash.PopHead();
}
});
this.add(ButtonTrash);
JTextField textBoxStorageName = new JTextField();
textBoxStorageName.setBounds(600, 10, 150, 30);
this.add(textBoxStorageName);
JButton ButtonAddObject = new JButton("Добавить набор");
ButtonAddObject.setBounds(600, 40, 150, 30);
ButtonAddObject.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (textBoxStorageName.getText().equals( "") || textBoxStorageName.getText().equals(null))
{
return;
}
_storage.AddSet(textBoxStorageName.getText());
ReloadObjects();
repaint();
}});
this.add(ButtonAddObject);
listBoxStorages = new DefaultListModel<>();
listBoxStoragesJList = new JList<>(listBoxStorages);
listBoxStoragesJList.setBounds(600, 350, 150, 70);
listBoxStoragesJList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
_cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
repaint();
}
}
});
this.add(listBoxStoragesJList);
_cruisers = new CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>(SCREEN_W, SCREEN_H);
}
private void ReloadObjects()
{
int index = listBoxStoragesJList.getSelectedIndex();
listBoxStorages.clear();
List<String> keys = _storage.Keys();
for(int i = 0; i < keys.size(); i++){
listBoxStorages.addElement(keys.get(i));
}
if(listBoxStorages.size() > 0 && (index == -1 || index >= listBoxStorages.size()))
listBoxStoragesJList.setSelectedIndex(0);
else if(listBoxStorages.size() > 0)
listBoxStoragesJList.setSelectedIndex(index);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (listBoxStoragesJList.getSelectedIndex() != -1) {
_storage.getByIndex(listBoxStorages.get(listBoxStoragesJList.getSelectedIndex())).Showcruisers(g);
_cruisers.Showcruisers(g);
}
}
}
}

View File

@ -0,0 +1,81 @@
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 boolean plus(CruiserGenericCollection<T, U> collect, T obj)
{
if (obj == null)
{
return false;
}
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 T Get(int position){
if(position < 0 || position >= _collection.Count())
return null;
return _collection.Get(position);
}
public void Showcruisers(Graphics gr)
{
DrawBackground(gr);
DrawObjects(gr);
}
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)
{
DrawingCruiser cruiser;
int numPlacesInRow = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count(); i++)
{
cruiser = _collection.Get(i);
if (cruiser != null)
{
cruiser.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10);
cruiser.DrawTransport(g);
}
}
}
}

View File

@ -0,0 +1,47 @@
import java.util.*;
import java.util.stream.Collectors;
public class CruiserGenericStorage {
Map<String, CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>> _cruiserStorages;
public List<String> Keys;
public List<String> Keys(){
if(_cruiserStorages == null)
return null;
return _cruiserStorages.keySet().stream().collect(Collectors.toList());
}
private int _pictureWidth;
private int _pictureHeight;
public CruiserGenericStorage(int pictureWidth, int pictureHeight)
{
_cruiserStorages = new HashMap<String,
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(String name)
{
_cruiserStorages.put(name, new CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name)
{
if (!_cruiserStorages.containsKey(name))
{
return;
}
_cruiserStorages.remove(name);
}
public CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> getByIndex(String indexOfDict){
if (_cruiserStorages.containsKey(indexOfDict)){
return _cruiserStorages.get(indexOfDict);
}
return null;
}
public CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> Get(String name){
if(!_cruiserStorages.containsKey(name))
return null;
return _cruiserStorages.get(name);
}
public DrawingCruiser Get(String collectionName, int position){
return _cruiserStorages.get(collectionName).Get(position);
}
}

6
Direction.java Normal file
View File

@ -0,0 +1,6 @@
public enum Direction {
Up,
Down,
Left,
Right
}

34
DopClassMixed.java Normal file
View File

@ -0,0 +1,34 @@
import java.awt.*;
public class DopClassMixed implements IDop{
public int numOfWheels;
public DopClassMixed(int num){
numOfWheels = num;
}
public void setNumOfWheels(String num){
switch (Integer.valueOf(num)){
case 2:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
break;
case 3:
numOfWheels = NumberOfWheelsEnum.wheel_3.value;
break;
case 4:
numOfWheels = NumberOfWheelsEnum.wheel_4.value;
break;
default:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
}
}
public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) {
g.setColor(c);
if (numOfWheels >= 2) {
g.fillRect(_startPosX+55, _startPosY+20, 20, 15);
g.fillRect(_startPosX+55, _startPosY+40, 20, 15);
} if (numOfWheels >= 3) {
g.fillOval(_startPosX+30, _startPosY+20, 20, 15);
} if (numOfWheels >= 4) {
g.fillRect(_startPosX+85, _startPosY+20, 20, 15);
}
}
}

33
DopClassRect.java Normal file
View File

@ -0,0 +1,33 @@
import java.awt.*;
public class DopClassRect implements IDop{
public int numOfWheels;
public DopClassRect(int num){
numOfWheels = num;
}
public void setNumOfWheels(String num){
switch (Integer.valueOf(num)){
case 2:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
break;
case 3:
numOfWheels = NumberOfWheelsEnum.wheel_3.value;
break;
case 4:
numOfWheels = NumberOfWheelsEnum.wheel_4.value;
break;
default:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
}
}
public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) {
g.setColor(c);
if (numOfWheels >= 2) {
g.fillRect(_startPosX+55, _startPosY+15, 20, 15);
g.fillRect(_startPosX+55, _startPosY+30, 20, 15);
} if (numOfWheels >= 3) {
g.fillRect(_startPosX+30, _startPosY+15, 20, 15);
} if (numOfWheels >= 4) {
g.fillRect(_startPosX + 60, _startPosY + 35, 15, 15);
}
}
}

View File

@ -0,0 +1,50 @@
import java.awt.*;
public class DrawingAdvancedCruiser extends DrawingCruiser{
EntityAdvancedCruiser EntityCruiser;
public DrawingAdvancedCruiser(int speed, double weight, Color bodyColor, Color additionalColor, boolean helicopterPad, boolean coating, boolean ornamentWheels, int width, int height) {
super(speed, weight, bodyColor, width, height, 110, 60);
{
EntityCruiser = new EntityAdvancedCruiser(speed, weight, bodyColor, additionalColor, helicopterPad, coating);
}
}
public DrawingAdvancedCruiser(EntityAdvancedCruiser entityAdvancedCruiser,IDop wheels)
{
super(entityAdvancedCruiser, wheels);
{
EntityCruiser = entityAdvancedCruiser;
this.wheels = wheels;
}
}
public EntityAdvancedCruiser getEntityCruiser(){
return EntityCruiser;
}
public void DrawTransport(Graphics g)
{
if (!(EntityCruiser instanceof EntityAdvancedCruiser)) {
return;
}
super.DrawTransport(g);
EntityAdvancedCruiser advancedCruiser = (EntityAdvancedCruiser) EntityCruiser;
if (advancedCruiser == null )
{
return;
}
Color addBrush = advancedCruiser.AdditionalColor;
Color brush = advancedCruiser.BodyColor;
if (advancedCruiser.HelicopterPad)
{
g.setColor(addBrush);
g.drawRect(_startPosX + 35,
_startPosY + 23, 15, 15);
g.drawRect(_startPosX + 50,
_startPosY + 19, 30, 25);
}
if (advancedCruiser.Coating)
{
g.drawOval(_startPosX + 70,
_startPosY + 29, 20, 20);
}
if (wheels == null){return;}
wheels.drawWheels(g, _startPosX, _startPosY, brush);
}
}

147
DrawingCruiser.java Normal file
View File

@ -0,0 +1,147 @@
import java.awt.*;
public class DrawingCruiser {
public EntityCruiser EntityCruiser;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private int _cruiserWidth = 110;
private int _cruiserHeight = 60;
public int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _cruiserWidth;
}
public int GetHeight() {
return _cruiserHeight;
}
public IDop wheels;
public DrawingCruiser(int speed, double weight, Color bodyColor, int width, int height) {
if (width < _cruiserWidth || height < _cruiserHeight) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
public EntityCruiser getEntity() {
return EntityCruiser;
}
protected DrawingCruiser(int speed, double weight, Color bodyColor, int
width, int height, int cruiserWidth, int cruiserHeight) {
if (width <= _cruiserWidth || height <= _cruiserHeight) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
_cruiserWidth = cruiserWidth;
_cruiserHeight = cruiserHeight;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
public DrawingCruiser(EntityCruiser entityCruiser, IDop wheels) {
EntityCruiser = entityCruiser;
this.wheels = wheels;
_pictureWidth = 1000;
_pictureHeight = 1000;
_cruiserWidth = 110;
_cruiserHeight = 60;
}
public void SetPosition(int x, int y) {
if (x < 0 || x + _cruiserWidth > _pictureWidth) {
x = Math.max(0, _pictureWidth - _cruiserWidth);
}
if (y < 0 || y + _cruiserHeight > _pictureHeight) {
y = Math.max(0, _pictureHeight - _cruiserHeight);
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction) {
if (!CanMove(direction) || EntityCruiser == null) {
return;
}
switch (direction) {
case Left:
if (_startPosX - EntityCruiser.Step() >= 0) {
_startPosX -= (int) EntityCruiser.Step();
}
break;
case Up:
if (_startPosY - EntityCruiser.Step() >= 0) {
_startPosY -= (int) EntityCruiser.Step();
}
break;
case Right:
if (_startPosX + EntityCruiser.Step() + _cruiserWidth <= _pictureWidth) {
_startPosX += (int) EntityCruiser.Step();
}
break;
case Down:
if (_startPosY + EntityCruiser.Step() + _cruiserHeight <= _pictureHeight) {
_startPosY += (int) EntityCruiser.Step();
}
break;
}
}
public boolean CanMove(Direction direction) {
if (EntityCruiser == null) {
return false;
}
switch (direction) {
case Left:
return (_startPosX - EntityCruiser.Step()) > 0;
case Up:
return _startPosY - EntityCruiser.Step() > 0;
case Right:
return _startPosX + EntityCruiser.Step() + _cruiserWidth < _pictureWidth;
case Down:
return _startPosY + EntityCruiser.Step() + _cruiserHeight < _pictureHeight;
default:
return false;
}
}
public void DrawTransport(Graphics g) {
if (EntityCruiser == null) {
return;
}
g.setColor(EntityCruiser.BodyColor);
g.drawRect(_startPosX + 9, _startPosY + 15, 10, 30);
g.drawRect(_startPosX + 90, _startPosY + 15, 10,
30);
g.drawRect(_startPosX + 20, _startPosY + 4, 70, 52);
g.fillRect(_startPosX + 10, _startPosY + 15, 10, 30);
g.fillRect(_startPosX + 90, _startPosY + 15, 10, 30);
g.fillRect(_startPosX + 20, _startPosY + 5, 70, 50);
g.fillRect(_startPosX + 10, _startPosY + 15, 10, 30);
g.fillRect(_startPosX + 90, _startPosY + 15, 10, 30);
g.fillRect(_startPosX + 20, _startPosY + 5, 70, 50);
Point[] points1 = new Point[3];
points1[0] = new Point(_startPosX + 100, _startPosY + 5);
points1[1] = new Point(_startPosX + 100, _startPosY + 55);
points1[2] = new Point(_startPosX + 150, _startPosY + 30);
g.fillPolygon(new int[]{points1[0].x, points1[1].x, points1[2].x},
new int[]{points1[0].y, points1[1].y, points1[2].y}, 3);
g.fillRect(_startPosX + 5, _startPosY + 15, 10, 10);
g.fillRect(_startPosX + 5, _startPosY + 35, 10, 10);
if (wheels == null) {
return;
}
wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor);
}
public IMoveableObject GetMoveableObject(){return new DrawingObjectCruiser(this);}
}

32
DrawingObjectCruiser.java Normal file
View File

@ -0,0 +1,32 @@
public class DrawingObjectCruiser implements IMoveableObject{
private DrawingCruiser _drawningCruiser;
public DrawingObjectCruiser(DrawingCruiser drawningCruiser)
{
_drawningCruiser = drawningCruiser;
}
public ObjectParameters GetObjectPosition()
{
if (_drawningCruiser == null || _drawningCruiser.EntityCruiser ==
null)
{
return null;
}
return new ObjectParameters(_drawningCruiser.GetPosX(),
_drawningCruiser.GetPosY(), _drawningCruiser.GetWidth(), _drawningCruiser.GetHeight());
}
public int GetStep() {
if(_drawningCruiser.getEntity() == null)
return 0;
return (int)_drawningCruiser.getEntity().Step();
}
public boolean CheckCanMove(Direction direction){
if(_drawningCruiser == null)
return false;
return _drawningCruiser.CanMove(direction);
}
public void MoveObject(Direction direction){
if(_drawningCruiser == null)
return;
_drawningCruiser.MoveTransport(direction);
}
}

View File

@ -0,0 +1,14 @@
import java.awt.*;
public class EntityAdvancedCruiser extends EntityCruiser{
public Color AdditionalColor;
public boolean HelicopterPad ;
public boolean Coating ;
public EntityAdvancedCruiser(int speed, double weight, Color bodyColor, Color
additionalColor, boolean helicopterPad, boolean coating)
{
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
HelicopterPad = helicopterPad;
Coating = coating;
}
}

14
EntityCruiser.java Normal file
View File

@ -0,0 +1,14 @@
import java.awt.*;
public class EntityCruiser {
public int Speed;
public double Weight;
public Color BodyColor;
public double Step() {return (double)Speed * 100 / Weight;};
public boolean OrnamentWheels;
public EntityCruiser(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

248
GameFrame.java Normal file
View File

@ -0,0 +1,248 @@
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.Random;
public class GameFrame extends JDialog {
protected DrawingCruiser SelectedCruiser;
GamePanel panel;
public DrawingCruiser _drawningCruiser;
GameFrame() {
this.setSize(710, 535);
this.setTitle("Cruiser");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
this.setModalityType(ModalityType.APPLICATION_MODAL);
this.setResizable(false);
this.setLocationRelativeTo(null);
panel = new GamePanel();
this.add(panel);
this.setVisible(true);
}
GameFrame(DrawingCruiser newCruiser){
_drawningCruiser = newCruiser;
this.setSize(710, 535);
this.setTitle("Cruiser");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
this.setModalityType(ModalityType.APPLICATION_MODAL);
this.setResizable(false);
this.setLocationRelativeTo(null);
panel = new GamePanel();
this.add(panel);
this.setVisible(true);
}
public DrawingCruiser getSelectedCruiser(){
return SelectedCruiser;
}
public void ChangeCruiser(DrawingCruiser newCruiser){panel.ChangeCruiserOnPanel(newCruiser);}
public class GamePanel extends JPanel {
static final int SCREEN_W = 700;
static final int SCREEN_H = 500;
int xx = 0;
int yy = 0;
private AbstractStrategy _abstractStrategy;
JColorChooser dialogColor = new JColorChooser();
GamePanel() {
if (_drawningCruiser != null){repaint();}
this.setLayout(null);
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
GridBagConstraints layers = new GridBagConstraints();
JButton buttonCruiser = new JButton("Создать объект");
buttonCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color color = Color.BLACK;
Color choosenColor = JColorChooser.showDialog(getParent(), "Выберите цвет", Color.WHITE);
if(choosenColor != null){
color = choosenColor;
}
_drawningCruiser = new DrawingCruiser(random.nextInt(100, 300),
random.nextInt(1000, 3000),
color,
GamePanel.SCREEN_W, GamePanel.SCREEN_H);
_drawningCruiser.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));
break;
case 2:
_drawningCruiser.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCruiser.wheels = new DopClassMixed(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonCruiser.setBounds(20 + 150, 390, 120, 30);
this.add(buttonCruiser);
JButton buttonAdvancedCruiser = new JButton("Создать продвинутый объект");
buttonAdvancedCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
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;
}
_drawningCruiser = new DrawingAdvancedCruiser(random.nextInt(100, 300),
random.nextInt(1000, 3000),
color, colorAdd, random.nextBoolean(),random.nextBoolean(),random.nextBoolean(),
GamePanel.SCREEN_W, GamePanel.SCREEN_H);
_drawningCruiser.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));
break;
case 2:
_drawningCruiser.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCruiser.wheels = new DopClassMixed(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonAdvancedCruiser.setBounds( 20, 390, 120, 30);
this.add(buttonAdvancedCruiser);
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)
{
return;
}
if (textStrategy.getText() != null)
{
switch(textStrategy.getText())
{
case "center":
_abstractStrategy = new MoveToCenter();
break;
case "border":
_abstractStrategy = new MoveToBorder();
break;
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData((IMoveableObject) _drawningCruiser, SCREEN_W, SCREEN_H);
}
if (_abstractStrategy == null)
{
return;
}
System.out.println("step");
_abstractStrategy.MakeStep();
repaint();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
_abstractStrategy = null;
}
}
});
buttonStep.setBounds(550, 60, 70, 30);
this.add(buttonStep);
JButton up = new BasicArrowButton(BasicArrowButton.NORTH);
up.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Up);
repaint();
}
});
JButton left = new BasicArrowButton(BasicArrowButton.WEST);
left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Left);
repaint();
}
});
JButton down = new BasicArrowButton(BasicArrowButton.SOUTH);
down.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Down);
repaint();
}
});
JButton right = new BasicArrowButton(BasicArrowButton.EAST);
right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Right);
repaint();
}
});
up.setBounds(570, 350, 30, 30);
this.add(up);
down.setBounds(570, 350 + 40, 30, 30);
this.add(down);
left.setBounds(570 - 40, 350 + 40, 30, 30);
this.add(left);
right.setBounds(570 + 40, 350 + 40, 30, 30);
this.add(right);
JButton buttonSelectedCruiser = new JButton("Выбрать объект");
buttonSelectedCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SelectedCruiser = _drawningCruiser;
dispose();
}
});
buttonSelectedCruiser.setBounds(510, 100, 150, 30);
this.add(buttonSelectedCruiser);
}
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
Graphics gr =new DebugGraphics();
_drawningCruiser.DrawTransport(gr);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (_drawningCruiser != null) {
_drawningCruiser.DrawTransport(g);
}
}
public void ChangeCruiserOnPanel(DrawingCruiser newCruiser){
newCruiser.SetPosition(0,0);
_drawningCruiser = newCruiser;
repaint();
}
}
public Color ChooseColor(JFrame MonorailFrame){
JColorChooser dialog = new JColorChooser();
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.WHITE);
return res;
}
}

67
GenericCreate.java Normal file
View File

@ -0,0 +1,67 @@
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> _EntityCruiser;
ArrayList<U> _Wheels;
int pointerCruiser = 0;
int pointerWheels = 0;
public ArrayList<T> getEntities(){
return _EntityCruiser;
}
public ArrayList<U> getWheels(){
return _Wheels;
}
public GenericCreate(int countEntities,int countWheels){
_EntityCruiser=new ArrayList<>(countEntities);
_Wheels=new ArrayList<>(countWheels);
}
public int Add(T cruiser){
if(pointerCruiser <= _EntityCruiser.size()){
_EntityCruiser.add(cruiser);
pointerCruiser++;
return pointerCruiser;
}
return -1;
}
public int Add(U wheels){
if(pointerWheels <= _Wheels.size()){
_Wheels.add(wheels);
pointerWheels++;
return pointerWheels;
}
return -1;
}
public DrawingCruiser DrawingGeneratedCruiser()
{
Random rand=new Random();
if (_EntityCruiser.size() == 0 || _Wheels.size() == 0){
return null;
}
T entity = (_EntityCruiser.get(rand.nextInt(pointerCruiser)));
U wheel = (_Wheels.get(rand.nextInt(pointerWheels)));
if(entity instanceof EntityAdvancedCruiser){
return new DrawingAdvancedCruiser((EntityAdvancedCruiser) entity, wheel);
}
return new DrawingCruiser(entity, wheel);
}
public DrawingCruiser[] DrawEntitiesCruiser(Graphics g, int startX, int startY) {
DrawingCruiser[] drawingCruisers = new DrawingCruiser[_EntityCruiser.size()];
int height = 70;
DrawingCruiser cruiser;
DrawingAdvancedCruiser advancedcruiser;
for (int i = 0; i < _EntityCruiser.size(); i++) {
if (_EntityCruiser.get(i) instanceof EntityAdvancedCruiser) {
advancedcruiser = new DrawingAdvancedCruiser((EntityAdvancedCruiser) _EntityCruiser.get(i), null);
advancedcruiser.SetPosition(startX , startY + i * height);
advancedcruiser.DrawTransport(g);
} else {
cruiser = new DrawingCruiser(_EntityCruiser.get(i), null);
cruiser.SetPosition(startX , startY + i * height);
cruiser.DrawTransport(g);
}
}
return drawingCruisers;
}
}

5
IDop.java Normal file
View File

@ -0,0 +1,5 @@
import java.awt.*;
public interface IDop {
public void setNumOfWheels(String num);
public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c);
}

6
IMoveableObject.java Normal file
View File

@ -0,0 +1,6 @@
public interface IMoveableObject {
public ObjectParameters GetObjectPosition();
public int GetStep();
boolean CheckCanMove(Direction direction);
void MoveObject(Direction direction);
}

5
Main.java Normal file
View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
CruiserCollectionFrame frame = new CruiserCollectionFrame();
}
}

28
MoveToBorder.java Normal file
View File

@ -0,0 +1,28 @@
public class MoveToBorder extends AbstractStrategy {
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.RightBorder() <= FieldWidth &&
objParams.RightBorder() + GetStep() >= FieldWidth &&
objParams.DownBorder() <= FieldHeight &&
objParams.DownBorder() + GetStep() >= FieldHeight;
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = FieldWidth - objParams.ObjectMiddleHorizontal();
if (Math.abs(diffX) > GetStep()) {
MoveRight();
}
var diffY = FieldHeight - objParams.ObjectMiddleVertical();
if (Math.abs(diffY) > GetStep()) {
MoveDown();
}
}
}

38
MoveToCenter.java Normal file
View File

@ -0,0 +1,38 @@
public class MoveToCenter extends AbstractStrategy {
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return (objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2);
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0) {
MoveLeft();
}
else {
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
}
else {
MoveDown();
}
}
}
}

33
NumberOfWheels.java Normal file
View File

@ -0,0 +1,33 @@
import java.awt.*;
public class NumberOfWheels implements IDop{
public int numOfWheels;
public NumberOfWheels(int num){
numOfWheels = num;
}
public void setNumOfWheels(String num){
switch (Integer.valueOf(num)){
case 2:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
break;
case 3:
numOfWheels = NumberOfWheelsEnum.wheel_3.value;
break;
case 4:
numOfWheels = NumberOfWheelsEnum.wheel_4.value;
break;
default:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
}
}
public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) {
g.setColor(c);
if (numOfWheels >= 2) {
g.fillOval(_startPosX+55, _startPosY+15, 20, 15);
g.fillOval(_startPosX+55, _startPosY+30, 20, 15);
} if (numOfWheels >= 3) {
g.fillOval(_startPosX+30, _startPosY+15, 20, 15);
} if(numOfWheels >= 4){
g.fillOval(_startPosX+85, _startPosY+15, 20, 15);
}
}
}

9
NumberOfWheelsEnum.java Normal file
View File

@ -0,0 +1,9 @@
public enum NumberOfWheelsEnum {
wheel_2(2),
wheel_3(3),
wheel_4(4);
public int value;
NumberOfWheelsEnum(int value) {
this.value = value;
}
}

19
ObjectParameters.java Normal file
View File

@ -0,0 +1,19 @@
public class ObjectParameters {
private final int _x;
private final int _y;
private final int _width;
private final int _height;
public int LeftBorder(){return _x;}
public int TopBorder(){return _y;}
public int RightBorder(){return _x + _width;}
public int DownBorder(){return _y + _height;}
public int ObjectMiddleHorizontal(){return _x + _width / 2;}
public int ObjectMiddleVertical(){return _y + _height / 2;}
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}

135
RandomFrame.java Normal file
View File

@ -0,0 +1,135 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class RandomFrame extends JFrame {
RandomPanel panel = new RandomPanel();
RandomFrame() {
this.setSize(710, 535);
this.setTitle("CruiserRandom");
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 generatedCruiser;
RandomPanel() {
this.setLayout(null);
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
JButton buttonCruiser = new JButton("Создать простой объект");
buttonCruiser.setBounds(20, 20, 130, 30);
buttonCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GameFrame dialogWindow = new GameFrame();
if (dialogWindow.getSelectedCruiser() != null){
if (dialogWindow.getSelectedCruiser() instanceof DrawingAdvancedCruiser) {
_genericCreate.Add(((DrawingAdvancedCruiser) dialogWindow.getSelectedCruiser()).getEntityCruiser());
}
else {
_genericCreate.Add(dialogWindow.getSelectedCruiser().EntityCruiser);
}
repaint();}
else{
System.out.println("Selected cruiser is null!");
}
}
});
this.add(buttonCruiser);
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 buttonCreateRandomCruiser = new JButton("Сгенерировать объект");
buttonCreateRandomCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
generatedCruiser = _genericCreate.DrawingGeneratedCruiser();
if (generatedCruiser != null) {
generatedCruiser.SetPosition(430, 160);
}
repaint();
}
});
buttonCreateRandomCruiser.setBounds(20 + 130 + 20 + 235, 20 + 80, 190, 30);
this.add(buttonCreateRandomCruiser);
}
@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.DrawEntitiesCruiser(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 (generatedCruiser != null){
generatedCruiser.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);
}
@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;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
}
}
}

43
SetGeneric.java Normal file
View File

@ -0,0 +1,43 @@
import java.util.ArrayList;
import java.util.List;
public class SetGeneric<T extends Object> {
private final List<T> _places;
private final int _maxCount;
public int Count;
public int Count() {
return Count;
}
public SetGeneric(int count)
{
_places = new ArrayList<>();
_maxCount = count;
}
public boolean Insert(T cruiser){
if(_places.size() == _maxCount)
return false;
Insert(cruiser, 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 < Count() && position >= 0) { return (T)_places.get(position); }
return null;
}
}

5
Status.java Normal file
View File

@ -0,0 +1,5 @@
public enum Status {
NotInit,
InProgress,
Finish
}

19
TrashCollection.java Normal file
View File

@ -0,0 +1,19 @@
import java.util.LinkedList;
public class TrashCollection <T extends DrawingCruiser>{
LinkedList<T> linkedList;
public TrashCollection(){
linkedList = new LinkedList<>();
}
public void Add(T cruiser){
linkedList.add(cruiser);
}
public void PopHead(){
if(linkedList.size() ==0)
return;
linkedList.remove();
}
public T GetLast(){
return linkedList.getFirst();
}
public int GetSize(){return linkedList.size();}
}