До сдачи 3лр

This commit is contained in:
Arkadiy Radaev 2023-12-11 14:47:27 +04:00
parent caac7cbff4
commit 6b0601cabb
18 changed files with 445 additions and 18 deletions

View File

@ -1,3 +1,4 @@
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private Status _state = Status.NotInit;

View File

@ -0,0 +1,81 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class CatamaranGenericCollection<T extends DrawningCatamaran, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 140;
private final int _placeSizeHeight = 90;
private final SetGeneric<T> _collection;
public CatamaranGenericCollection(int picWidth, int picHeight){
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Insert(T obj){
if (obj == null)
{
return -1;
}
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();
}
private void DrawBackground(Graphics g)
{
g.setColor(Color.BLACK);
((Graphics2D) g).setStroke(new BasicStroke(2));
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);
}
((Graphics2D) g).setStroke(new BasicStroke(1));
}
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
{
DrawningCatamaran catamaran = _collection.Get(i);
if (catamaran != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
catamaran.SetPosition(i % inRow * _placeSizeWidth, ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight) + 5 );
catamaran.DrawnCatamaran((Graphics2D) g);
}
}
}
public BufferedImage ShowCatamaran()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
}

View File

@ -1,10 +1,11 @@
import java.awt.*;
import java.util.Random;
public class DrawningCatamaran {
protected EntityCatamaran EntityCatamaran;
private int _pictureWidth;
private int _pictureHeight;
public int _pictureWidth;
public int _pictureHeight;
protected int _startPosX = 0;
protected int _startPosY = 0;
protected int _catamaranWidth = 120;
@ -14,6 +15,9 @@ public class DrawningCatamaran {
public EntityCatamaran EntityCatamaran() {
return EntityCatamaran;
}
public IMoveableObject GetMoveableObject(){
return new DrawningObjectCatamaran(this);
}
public DrawningCatamaran(int speed, double weight, Color bodyColor, Color oarColor, int width, int height,
boolean boduKit, boolean seats) {
@ -23,8 +27,7 @@ public class DrawningCatamaran {
_pictureWidth = width;
_pictureHeight = height;
EntityCatamaran = new EntityCatamaran(speed, weight, bodyColor, oarColor, boduKit, seats);
SetPosition(rand.nextInt(10,50),rand.nextInt(10,50));
DrawningOars = new DrawningOars(_startPosX, _startPosY, oarColor);
int variant = rand.nextInt(0, 3);
if (variant == 0)
@ -34,6 +37,7 @@ public class DrawningCatamaran {
else
DrawningOars = new DrawningOarsHandle(_startPosX, _startPosY, oarColor);
DrawningOars.ChangeOarsNumb(rand.nextInt(1, 4));
}
public void SetPosition(int x, int y) {
@ -41,7 +45,8 @@ public class DrawningCatamaran {
return;
_startPosX = x;
_startPosY = y;
// проверка говна - фикс
DrawningOars.ChangeX(_startPosX);
DrawningOars.ChangeY(_startPosY);
if (x + _catamaranWidth >= _pictureWidth || y + _catamaranHeight >= _pictureHeight) {
_startPosX = 0;
_startPosY = 0;
@ -76,7 +81,7 @@ public class DrawningCatamaran {
can = _startPosX + EntityCatamaran.Step() + _catamaranWidth < _pictureWidth;
break;
case Down:
can = _startPosY + EntityCatamaran.Step() + _catamaranHeight < _pictureHeight + 20;
can = _startPosY + EntityCatamaran.Step() + _catamaranHeight < _pictureHeight;
break;
case Up:
can = _startPosY - EntityCatamaran.Step() >= 0;

View File

@ -1,3 +1,4 @@
import java.awt.*;
public class DrawningCatamaranPro extends DrawningCatamaran {
@ -9,12 +10,13 @@ public class DrawningCatamaranPro extends DrawningCatamaran {
motor, sheet, seats);
}
}
@Override
public void DrawnCatamaran(Graphics2D g2d) {
if (!(EntityCatamaran instanceof EntityCatamaranPro)) {
return;
}
EntityCatamaranPro _catamaran = (EntityCatamaranPro) EntityCatamaran;
g2d.setColor(_catamaran.AdditionalColor());
if (_catamaran.Motor()) {

View File

@ -1,3 +1,4 @@
import java.awt.*;
public class DrawningOars implements IDraw {

View File

@ -1,3 +1,4 @@
import java.awt.*;
public class DrawningOarsHandle implements IDraw {

View File

@ -1,3 +1,5 @@
public class DrawningObjectCatamaran implements IMoveableObject{
private final DrawningCatamaran _DrawningCatamaran;
public DrawningObjectCatamaran(DrawningCatamaran drawningCatamaran){

View File

@ -25,4 +25,6 @@ public class EntityCatamaran {
BodyKit = bodyKit;
Seats = seats;
}
}

View File

@ -6,6 +6,7 @@ import javax.swing.*;
public class FormCatamaran {
Canvas canv;
public JFrame CatamaranFrame;
private DrawningCatamaran DrawningCatamaran;
private AbstractStrategy _abstractStrategy;
private JButton UpButton;
@ -14,24 +15,41 @@ public class FormCatamaran {
private JButton DownButton;
private JButton CreateButton;
private JButton CreateButtonPro;
private JButton buttonStep;
public JButton buttonSelect;
final int pictureBoxWidth = 885;
final int pictureBoxHeight = 462;
public void Draw() {
canv.repaint();
}
public Color ChooseColor(JFrame CatamaranFrame) {
JColorChooser dialog = new JColorChooser();
Color res = JColorChooser.showDialog(CatamaranFrame, "Выберите цвет", Color.WHITE);
return res;
}
public DrawningCatamaran SelectedCatamaran() {
return DrawningCatamaran;
}
public FormCatamaran() {
JFrame CatamaranFrame = new JFrame();
CatamaranFrame = new JFrame();
UpButton = new JButton("");
LeftButton = new JButton("");
RightButton = new JButton("");
DownButton = new JButton("");
CreateButton = new JButton("Создать");
CreateButtonPro = new JButton("Создать про");
JButton buttonStep = new JButton("Шаг");
buttonStep = new JButton("Шаг");
buttonSelect = new JButton ("Выбрать");
JComboBox comboBoxStrategy = new JComboBox(
new String[] {"Довести до центра","Довести до края",
});
new String[] {
"Довести до центра",
"Довести до края",
});
buttonStep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (DrawningCatamaran == null) {
@ -72,9 +90,13 @@ public class FormCatamaran {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
DrawningCatamaran = new DrawningCatamaran(random.nextInt(200,1000), random.nextDouble(1000,3000),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), pictureBoxWidth,
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color choosen = ChooseColor(CatamaranFrame);
if (choosen != null) {
color = choosen;
}
DrawningCatamaran = new DrawningCatamaran(400, 700,
color, color, pictureBoxWidth,
pictureBoxHeight, random.nextBoolean(), random.nextBoolean());
canv.DrawningCatamaran = DrawningCatamaran;
comboBoxStrategy.enable(true);
@ -84,12 +106,23 @@ public class FormCatamaran {
CreateButtonPro.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random random = new Random();
DrawningCatamaran = new DrawningCatamaranPro(random.nextInt(200,1000), random.nextDouble(1000,3000),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
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(CatamaranFrame);
if (choosen != null) {
color = choosen;
}
choosen = ChooseColor(CatamaranFrame);
if (choosen != null) {
additionalColor = choosen;
}
DrawningCatamaran = new DrawningCatamaranPro(400, 700,
color,
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
pictureBoxWidth, pictureBoxHeight,
random.nextBoolean(), random.nextBoolean(),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
additionalColor,
random.nextBoolean(), random.nextBoolean());
canv.DrawningCatamaran = DrawningCatamaran;
comboBoxStrategy.enable(true);
@ -145,6 +178,7 @@ public class FormCatamaran {
DownButton.setBounds(774, 391, 50, 50);
comboBoxStrategy.setBounds(719, 12, 151, 28);
buttonStep.setBounds(768, 46, 94, 29);
buttonSelect.setBounds(383,401, 180, 40);
canv = new Canvas();
canv.setSize(pictureBoxWidth, pictureBoxHeight);
CatamaranFrame.add(canv);
@ -156,6 +190,7 @@ public class FormCatamaran {
CatamaranFrame.add(CreateButton);
CatamaranFrame.add(CreateButtonPro);
CatamaranFrame.add(buttonStep);
CatamaranFrame.add(buttonSelect);
CatamaranFrame.setVisible(true);
}
}

View File

@ -0,0 +1,119 @@
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormCatamaranCollection {
private final CatamaranGenericCollection<DrawningCatamaran, DrawningObjectCatamaran> _catamaran;
private int pictureBoxWidth = 605;
private int pictureBoxHeight = 426;
CollectionCanvas canv;
void Draw() {
if (canv == null)
return;
canv.repaint();
}
public FormCatamaranCollection() {
_catamaran = new CatamaranGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
canv = new CollectionCanvas();
JPanel toolBox = new JPanel();
JFrame collectionFrame = new JFrame();
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880, 450);
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 catamaranNumb = new JTextField();
GridLayout lay = new GridLayout(7, 1);
lay.setVgap(10);
toolBox.setLayout(lay);
toolBox.add(addButton);
toolBox.add(catamaranNumb);
toolBox.add(removeButton);
toolBox.add(refreshButton);
collectionFrame.add(toolBox);
collectionFrame.add(canv);
collectionFrame.setVisible(true);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_catamaran == null) {
return;
}
FormCatamaran form = new FormCatamaran();
form.buttonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_catamaran.Insert(form.SelectedCatamaran()) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация",
JOptionPane.INFORMATION_MESSAGE);
form.SelectedCatamaran()._pictureWidth = pictureBoxWidth;
form.SelectedCatamaran()._pictureHeight = pictureBoxHeight;
Draw();
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация",
JOptionPane.INFORMATION_MESSAGE);
}
canv._catamaran = _catamaran;
form.CatamaranFrame.dispose();
Draw();
}
});
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_catamaran == null) {
return;
}
String tmp = catamaranNumb.getText();
int numb;
try {
numb = Integer.parseInt(tmp);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_catamaran.Remove(numb);
_catamaran.ShowCatamaran();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_catamaran == null) {
return;
}
_catamaran.ShowCatamaran();
Draw();
}
});
}
}
class CollectionCanvas extends JComponent {
public CatamaranGenericCollection<DrawningCatamaran, DrawningObjectCatamaran> _catamaran;
public CollectionCanvas() {
}
@Override
public void paintComponent(Graphics g) {
if (_catamaran == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(_catamaran.ShowCatamaran(), 0, 0, this);
super.repaint();
}
}

65
HardForm.java Normal file
View File

@ -0,0 +1,65 @@
import javax.swing.*;
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 EntityCatamaran makeEntity() {
Random rand = new Random();
return new EntityCatamaran(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)), rand.nextBoolean(),
rand.nextBoolean());
}
void Draw() {
if (canv == null)
return;
canv.repaint();
}
private IDraw makeIDraw(EntityCatamaran entity) {
return new DrawningOars(0, 0, entity.OarColor());
}
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<EntityCatamaran, IDraw> toDraw = new HardGeneric<>(sz,
sz, pictureBoxWidth, pictureBoxHeight);
for (int i = 0; i < sz; i++) {
EntityCatamaran ent = makeEntity();
toDraw.InsertFirst(ent);
toDraw.InsertSecond(makeIDraw(ent));
}
makeObject.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningCatamaran DrawningCatamaran = toDraw.MakeObject();
DrawningCatamaran.SetPosition(pictureBoxWidth / 2 - DrawningCatamaran.GetWidth() / 2,
pictureBoxHeight / 2 - DrawningCatamaran.GetHeight() / 2);
canv.DrawningCatamaran = DrawningCatamaran;
Draw();
}
});
}
}

55
HardGeneric.java Normal file
View File

@ -0,0 +1,55 @@
import java.util.Random;
public class HardGeneric<T extends EntityCatamaran, 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 EntityCatamaran[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 DrawningCatamaran MakeObject() {
Random rand = new Random();
int indFirst = rand.nextInt(0, curSz);
int indSecond = rand.nextInt(0, curSz);
EntityCatamaran entity = arrFirst[indFirst];
IDraw inter = arrSecond[indSecond];
DrawningCatamaran catamaran = new DrawningCatamaran(entity.Speed(), entity.Weight(), entity.BodyColor(),
entity.OarColor(), pictureBoxWidth, pictureBoxHeight, entity.BodyKit(), entity.Seats());
return catamaran;
}
}

View File

@ -2,6 +2,6 @@ import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
new FormCatamaran();
new FormCatamaranCollection();
}
}

View File

@ -1,3 +1,4 @@
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean IsTargetDestination() {

View File

@ -1,3 +1,4 @@
public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean IsTargetDestination() {

View File

@ -1,3 +1,5 @@
public class ObjectParameters {
private final int _x;
private final int _y;

53
SetGeneric.java Normal file
View File

@ -0,0 +1,53 @@
public class SetGeneric <T extends Object>{
private final Object[] _places;
public int Count;
public SetGeneric(int count){
_places = new Object[count];
Count = count;
}
public int Insert(T catamaran){
return Insert(catamaran, 0);
}
public int Insert(T catamaran, int position){
if(!(position >= 0 && position < Count))
return -1;
if(_places[position] == null){
_places[position] = catamaran;
}
else{
int place = -1;
for(int i = position; i < Count; i++){
if(_places[i] == null){
place = i;
break;
}
}
if(place == -1)
return -1;
for(int i = place - 1; i >= position; i--)
_places[i+1] = _places[i];
_places[position] = catamaran;
}
return position;
}
public boolean Remove(int position){
if(!(position >= 0 && position < Count))
return false;
_places[position] = null;
return true;
}
public T Get(int position){
if(!(position >= 0 && position < Count))
return null;
return (T)_places[position];
}
}

View File

@ -1,3 +1,4 @@
public enum Status {
NotInit,
InProgress,