начало

This commit is contained in:
pyzhov.egor 2024-03-25 22:48:09 +04:00
parent 7072feff6d
commit 3e27acda2c
12 changed files with 373 additions and 42 deletions

View File

@ -0,0 +1,61 @@
package CollectionGenericObjects;
import Drawnings.DrawningBoat;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class AbstractCompany {
protected final int _placeSizeWidth = 210;
protected final int _placeSizeHeight = 80;
protected final int _pictureWidth;
protected final int _pictureHeight;
protected ICollectionGenericObjects<DrawningBoat> _collection = null;
private int maxCount = getMaxCount();
public int getMaxCount() {
return _pictureWidth * _pictureHeight / (_placeSizeWidth*_placeSizeHeight);
}
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningBoat> collection) {
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = collection;
_collection.setMaxCount(maxCount);
}
public boolean add(AbstractCompany company ,DrawningBoat boat) {
if (company != null && company._collection != null) {
return company._collection.insert(boat);
}
return false;
}
public static boolean remove(AbstractCompany company, int position) {
if (company != null && company._collection != null) {
return company._collection.remove(position);
}
return false;
}
public DrawningBoat getRandomObject() {
Random rnd = new Random();
return _collection.get(rnd.nextInt(getMaxCount()));
}
public BufferedImage show() {
BufferedImage bitmap = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bitmap.createGraphics();
drawBackground(g2d);
setObjectsPosition();
for (int i = 0; i < (_collection != null ? _collection.getCount() : 0); ++i) {
DrawningBoat obj = _collection.get(i);
obj.drawBoat(g2d);
}
g2d.dispose();
return bitmap;
}
protected abstract void drawBackground(Graphics2D g2d);
protected abstract void setObjectsPosition();
}

View File

@ -0,0 +1,58 @@
package CollectionGenericObjects;
import Drawnings.DrawningBoat;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class BoatSharingService extends AbstractCompany {
private List<Point> locCoord = new ArrayList<>();
private int numRows, numCols;
public BoatSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawningBoat> collection) {
super(picWidth, picHeight, collection);
}
@Override
protected void drawBackground(Graphics2D g) {
Color backgroundColor = new Color(135, 206, 235);
g.setColor(backgroundColor);
g.fillRect(0, 0, _pictureWidth, _pictureHeight);
g.setColor(new Color(165, 42, 42)); // Brown
int offsetX = 10, offsetY = -12;
int x = 1 + offsetX, y = _pictureHeight - _placeSizeHeight + offsetY;
int numRows = 0;
while (y >= 0) {
int numCols = 0;
while (x + _placeSizeWidth <= _pictureWidth) {
numCols++;
g.drawLine(x, y, x + _placeSizeWidth / 2, y);
g.drawLine(x, y, x, y + _placeSizeHeight + 4);
locCoord.add(new Point(x, y));
x += _placeSizeWidth + 2;
}
numRows++;
x = 1 + offsetX;
y -= _placeSizeHeight + 5 + offsetY;
}
}
@Override
protected void setObjectsPosition() {
if (locCoord == null || _collection == null) {
return;
}
int row = numRows - 1, col = numCols;
for (int i=0;i< _collection.getCount(); i++, col--) {
_collection.get(i).setPictureSize(_pictureWidth, _pictureHeight);
_collection.get(i).setPosition((int)locCoord.get(row*numCols - col).getX() + 5,
(int)locCoord.get(row*numCols - col).getY() + 5);
if (col == 1) {
col = numCols + 1;
row--;
}
}
}
}

View File

@ -0,0 +1,10 @@
package CollectionGenericObjects;
public interface ICollectionGenericObjects<T extends Object> {
int getCount();
void setMaxCount(int maxCount);
boolean insert(T obj);
boolean insert(T obj, int position);
boolean remove(int position);
T get(int position);
}

View File

@ -0,0 +1,79 @@
package CollectionGenericObjects;
import java.sql.Array;
import java.util.Arrays;
public class MassiveGenericObjects<T extends Object> implements ICollectionGenericObjects<T> {
private T[] _collection;
public MassiveGenericObjects() {
_collection = (T[]) new Object[getCount()];
}
@Override
public int getCount() {
return _collection.length;
}
@Override
public boolean insert(T obj) {
for (int i = 0; i < getCount(); i++) {
if (_collection[i] == null) {
_collection[i] = obj;
return true;
}
}
return false;
}
@Override
public boolean insert(T obj, int position) {
if (position < 0 || position >= getCount()) {
return false;
}
if (_collection[position] == null) {
_collection[position] = obj;
return true;
}
for (int i = position + 1;i < getCount(); i++) {
if (_collection[i] == null) {
_collection[i] = obj;
return true;
}
}
for (int i = position - 1;i >= 0; i--) {
if (_collection[i] == null) {
_collection[i] = obj;
return true;
}
}
return false;
}
@Override
public boolean remove(int position) {
if (position < 0 || position >= getCount()) {
return false;
}
_collection[position] = null;
return true;
}
@Override
public T get(int position) {
if (position >= 0 && position < getCount()) {
return _collection[position];
}
else {
return null;
}
}
@Override
public void setMaxCount(int value) {
if (value > 0) {
if (_collection.length > 0) {
_collection = Arrays.copyOf(_collection, value);
} else {
_collection = (T[]) new Object[value];
}
}
}
}

View File

@ -1,6 +1,4 @@
package Drawnings;
import Drawnings.DrawningBoat;
import Entities.*;
import java.awt.*;
@ -12,12 +10,6 @@ public class DrawningCatamaran extends DrawningBoat {
EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, floaters, sail);
}
private void drawFloater(int y0, Color additionalColor, Graphics2D g2d)
{
g2d.setColor(additionalColor);
@ -75,11 +67,6 @@ public class DrawningCatamaran extends DrawningBoat {
g2d.drawPolygon(sailPolygon);
g2d.setColor(EntityBoat.getAdditionalColor());
g2d.fillPolygon(sailPolygon);
}
}
}

View File

@ -4,7 +4,6 @@ public enum PaddlesCount {
One(1),
Two(2),
Three(3);
final private int EnumNumber;
PaddlesCount(int enumNumber) {
EnumNumber = enumNumber;

View File

@ -0,0 +1,134 @@
import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.BoatSharingService;
import CollectionGenericObjects.MassiveGenericObjects;
import Drawnings.DrawningBoat;
import Drawnings.DrawningCatamaran;
import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Random;
public class FormBoatCollection extends JFrame {
private AbstractCompany _company;
private JPanel pictureBox, instrumentsBox;
private JComboBox<String> comboBoxSelectorCompany;
private JButton buttonAddBoat, buttonAddCatamaran, buttonRemoveBoat, buttonGoToCheck, buttonRefresh;
private JTextField textBoxPosition;
public FormBoatCollection() {
setTitle("Коллекция лодок");
setSize(900, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
instrumentsBox = new JPanel();
instrumentsBox.setLayout(new GridLayout(0,1));
comboBoxSelectorCompany = new JComboBox<>();
comboBoxSelectorCompany.addItem("Хранилище");
comboBoxSelectorCompany.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedItem = Integer.toString(comboBoxSelectorCompany.getSelectedIndex());
switch (selectedItem) {
case "Хранилище":
_company = new BoatSharingService(pictureBox.getWidth(), pictureBox.getHeight(),
new MassiveGenericObjects<DrawningBoat>());
break;
}
}
});
instrumentsBox.add(comboBoxSelectorCompany);
buttonAddBoat = new JButton("Добавить лодку");
buttonAddBoat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createObject("Drawnings.DrawningBoat");
}
});
instrumentsBox.add(buttonAddBoat);
buttonAddCatamaran = new JButton("Добавить катамаран");
buttonAddCatamaran.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createObject("Drawnings.DrawningCatamaran");
}
});
instrumentsBox.add(buttonAddCatamaran);
NumberFormat format = NumberFormat.getInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(100);
formatter.setAllowsInvalid(false);
formatter.setCommitsOnValidEdit(true);
textBoxPosition = new JFormattedTextField(formatter);
textBoxPosition.setPreferredSize(buttonAddBoat.getPreferredSize());
instrumentsBox.add(textBoxPosition);
buttonRemoveBoat = new JButton("Удалить объект");
buttonRemoveBoat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (textBoxPosition.getText() == null || textBoxPosition.getText().isEmpty()) {
return;
}
int result = JOptionPane.showConfirmDialog(null, "Удалить объект?",
"Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.NO_OPTION) {
return;
}
int pos = Integer.valueOf(textBoxPosition.getText());
if (_company.remove(_company, pos)) {
JOptionPane.showMessageDialog(null,"Объект удален");
}
else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
}
});
instrumentsBox.add(buttonRemoveBoat);
pictureBox = new JPanel();
getContentPane().add(instrumentsBox, BorderLayout.EAST);
getContentPane().add(pictureBox); // Добавляем pictureBox в центр
setVisible(true);
}
private void createObject(String type) {
if (_company == null) {
return;
}
DrawningBoat _drawningBoat;
Random random = new Random();
switch (type) {
case "Drawnings.DrawningBoat":
_drawningBoat = new DrawningBoat(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3));
break;
case "Drawnings.DrawningCatamaran":
_drawningBoat = new DrawningCatamaran(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean());
break;
default:
return;
}
if (_company.add(_company, _drawningBoat)) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
//pictureBox.setIcon(new ImageIcon(_company.show()));
}
else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
}

View File

@ -2,13 +2,14 @@ import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(false);
JFrame frame = new JFrame("Катамаран");
frame.setContentPane(new FormCatamaran().PanelWrapper);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();
frame.setSize(700, 500);
frame.setVisible(true);
// JFrame.setDefaultLookAndFeelDecorated(false);
// JFrame frame = new JFrame("Катамаран");
// frame.setContentPane(new FormBoatCollection());
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setLocation(500, 200);
// frame.pack();
// frame.setSize(700, 500);
// frame.setVisible(true);
new FormBoatCollection();
}
}

View File

@ -7,7 +7,6 @@ public abstract class AbstractStrategy {
protected int FieldHeight;
public StrategyStatus GetStatus() { return _state; }
// Изменить статус, установить поля
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
@ -21,7 +20,6 @@ public abstract class AbstractStrategy {
FieldHeight = height;
}
// сделать шаг
public void MakeStep()
{
if (_state != StrategyStatus.InProgress)
@ -36,13 +34,11 @@ public abstract class AbstractStrategy {
MoveToTarget();
}
// перемещения
protected boolean MoveLeft() { return MoveTo(MovementDirection.Left); }
protected boolean MoveRight() { return MoveTo(MovementDirection.Right); }
protected boolean MoveUp() { return MoveTo(MovementDirection.Up); }
protected boolean MoveDown() { return MoveTo(MovementDirection.Down); }
// параметры
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
// шаг
protected int GetStep()
@ -53,13 +49,10 @@ public abstract class AbstractStrategy {
}
return _moveableObject.GetStep();
}
// перемещение
protected abstract void MoveToTarget();
// достигнута ли цель
protected abstract boolean IsTargetDestination();
// попытка перемещения по направлению
private boolean MoveTo(MovementDirection directionType)
{
if (_state != StrategyStatus.InProgress)

View File

@ -20,17 +20,29 @@ public class MoveToBorder extends AbstractStrategy {
if (objParams == null) {
return;
}
var diffX = FieldWidth - objParams.ObjectMiddleHorizontal();
if (Math.abs(diffX) > GetStep()) {
MoveRight();
int diffX = objParams.RightBorder() - FieldWidth;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = FieldHeight - objParams.ObjectMiddleVertical();
if (Math.abs(diffY) > GetStep()) {
MoveDown();
int diffY = objParams.DownBorder() - FieldHeight;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}

View File

@ -1,7 +1,5 @@
package MovementStrategy;
import Drawnings.*;
import Entities.EntityBoat;
public class MoveableBoat implements IMoveableObject {
private DrawningBoat _boat = null;

View File

@ -11,7 +11,6 @@ public class ObjectParameters {
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; }