Лабораторная работа №3
This commit is contained in:
parent
3609408b02
commit
0a53208d5e
@ -0,0 +1,61 @@
|
||||
package CollectionAdditionalObjects;
|
||||
|
||||
import DifferentBlocks.IDrawingBlocks;
|
||||
import Drawings.DrawingWarship;
|
||||
import Drawings.DrawingBattleship;
|
||||
import Entities.EntityBattleship;
|
||||
import Entities.EntityWarship;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Random;
|
||||
|
||||
public class AdditionalCollections <T extends EntityWarship, U extends IDrawingBlocks>{
|
||||
public T[] _collectionEntity;
|
||||
public U[] _collectionBlocks;
|
||||
public AdditionalCollections(int size, Class<T> type1, Class<T> type2) {
|
||||
_collectionEntity = (T[]) Array.newInstance(type1, size);
|
||||
_collectionBlocks = (U[]) Array.newInstance(type2, size);
|
||||
CountEntities = size;
|
||||
CountBlocks = size;
|
||||
}
|
||||
public int CountEntities;
|
||||
public int CountBlocks;
|
||||
public int Insert(T entity) {
|
||||
int index = 0;
|
||||
while (index < CountEntities) {
|
||||
if (_collectionEntity[index] == null)
|
||||
{
|
||||
_collectionEntity[index] = entity;
|
||||
return index;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public int Insert(U blocks) {
|
||||
int index = 0;
|
||||
while (index < CountBlocks) {
|
||||
if (_collectionBlocks[index] == null)
|
||||
{
|
||||
_collectionBlocks[index] = blocks;
|
||||
return index;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public DrawingWarship CreateAdditionalCollectionWarship() {
|
||||
Random random = new Random();
|
||||
if (_collectionEntity == null || _collectionBlocks == null) return null;
|
||||
T entity = _collectionEntity[random.nextInt(CountEntities)];
|
||||
U blocks = _collectionBlocks[random.nextInt(CountBlocks)];
|
||||
DrawingWarship drawingWarship = null;
|
||||
if (entity instanceof EntityBattleship) {
|
||||
drawingWarship = new DrawingBattleship((EntityBattleship) entity, blocks);
|
||||
}
|
||||
else {
|
||||
drawingWarship = new DrawingWarship(entity, blocks);
|
||||
}
|
||||
return drawingWarship;
|
||||
}
|
||||
}
|
34
src/src/CollectionGenericObjects/AbstractCompany.java
Normal file
34
src/src/CollectionGenericObjects/AbstractCompany.java
Normal file
@ -0,0 +1,34 @@
|
||||
package CollectionGenericObjects;
|
||||
|
||||
import Drawings.DrawingWarship;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public abstract class AbstractCompany {
|
||||
protected int _placeSizeWidth = 210;
|
||||
protected int _placeSizeHeight = 80;
|
||||
protected int _pictureWidth;
|
||||
protected int _pictureHeight;
|
||||
public ICollectionGenericObjects<DrawingWarship> _collection = null;
|
||||
private int GetMaxCount() {
|
||||
return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
|
||||
}
|
||||
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawingWarship> collection)
|
||||
{
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = collection;
|
||||
_collection.SetMaxCount(GetMaxCount(), DrawingWarship.class);
|
||||
}
|
||||
//перегрузка операторов в джаве невозможна
|
||||
public DrawingWarship GetRandomObject()
|
||||
{
|
||||
return _collection.Get((int)(Math.random()*GetMaxCount()));
|
||||
}
|
||||
public void SetPosition()
|
||||
{
|
||||
SetObjectsPosition();
|
||||
}
|
||||
public abstract void DrawBackground(Graphics graphics);
|
||||
protected abstract void SetObjectsPosition();
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package CollectionGenericObjects;
|
||||
|
||||
public interface ICollectionGenericObjects<T>
|
||||
{
|
||||
int getCount();
|
||||
void SetMaxCount(int count, Class<T> type);
|
||||
int Insert(T obj);
|
||||
int Insert(T obj, int position);
|
||||
T Remove(int position);
|
||||
T Get(int position);
|
||||
}
|
75
src/src/CollectionGenericObjects/MassiveGenericObjects.java
Normal file
75
src/src/CollectionGenericObjects/MassiveGenericObjects.java
Normal file
@ -0,0 +1,75 @@
|
||||
package CollectionGenericObjects;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
|
||||
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
|
||||
private T[] _collection;
|
||||
private int Count;
|
||||
public void SetMaxCount(int size, Class<T> type) {
|
||||
if (size > 0) {
|
||||
_collection = (T[]) Array.newInstance(type, size);
|
||||
Count = size;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public int getCount() {
|
||||
return Count;
|
||||
}
|
||||
@Override
|
||||
public int Insert(T obj) {
|
||||
int index = 0;
|
||||
while (index < getCount())
|
||||
{
|
||||
if (_collection[index] == null)
|
||||
{
|
||||
_collection[index] = obj;
|
||||
return index;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@Override
|
||||
public int Insert(T obj, int position) {
|
||||
if (position >= getCount() || position < 0)
|
||||
return -1;
|
||||
if (_collection[position] == null) {
|
||||
_collection[position] = obj;
|
||||
return position;
|
||||
}
|
||||
int index = position + 1;
|
||||
while (index < getCount())
|
||||
{
|
||||
if (_collection[index] == null)
|
||||
{
|
||||
_collection[index] = obj;
|
||||
return index;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
index = position - 1;
|
||||
while (index >= 0)
|
||||
{
|
||||
if (_collection[index] == null)
|
||||
{
|
||||
_collection[index] = obj;
|
||||
return index;
|
||||
}
|
||||
--index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@Override
|
||||
public T Remove(int position) {
|
||||
if (position >= getCount() || position < 0)
|
||||
return null;
|
||||
T obj = (T) _collection[position];
|
||||
_collection[position] = null;
|
||||
return obj;
|
||||
}
|
||||
@Override
|
||||
public T Get(int position) {
|
||||
if (position >= getCount() || position < 0) return null;
|
||||
return (T) _collection[position];
|
||||
}
|
||||
}
|
50
src/src/CollectionGenericObjects/WarshipSharingService.java
Normal file
50
src/src/CollectionGenericObjects/WarshipSharingService.java
Normal file
@ -0,0 +1,50 @@
|
||||
package CollectionGenericObjects;
|
||||
|
||||
import Drawings.DrawingWarship;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class WarshipSharingService extends AbstractCompany{
|
||||
public WarshipSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawingWarship> collection) {
|
||||
super(picWidth, picHeight, collection);
|
||||
}
|
||||
@Override
|
||||
public void DrawBackground(Graphics g) {
|
||||
int width = _pictureWidth / _placeSizeWidth;
|
||||
int height = _pictureHeight / _placeSizeHeight;
|
||||
g.setColor(Color.BLACK);
|
||||
for (int i = 0; i < width; i++)
|
||||
{
|
||||
for (int j = 0; j < height + 1; ++j)
|
||||
{
|
||||
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||
}
|
||||
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight);
|
||||
}
|
||||
}
|
||||
//установка объектов
|
||||
@Override
|
||||
protected void SetObjectsPosition() {
|
||||
int width = _pictureWidth / _placeSizeWidth;
|
||||
int height = _pictureHeight / _placeSizeHeight;
|
||||
|
||||
int posWidth = 0;
|
||||
int posHeight = height - 1;
|
||||
for (int i = 0; i < (_collection.getCount()); i++) {
|
||||
if (_collection.Get(i) != null) {
|
||||
_collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
|
||||
_collection.Get(i).SetPosition(_placeSizeWidth * posWidth + 4, posHeight * _placeSizeHeight + 4);
|
||||
}
|
||||
if (posWidth < width - 1)
|
||||
posWidth++;
|
||||
else {
|
||||
posWidth = 0;
|
||||
posHeight--;
|
||||
}
|
||||
if (posHeight > height) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
32
src/src/Drawings/CanvasFormWarshipCollection.java
Normal file
32
src/src/Drawings/CanvasFormWarshipCollection.java
Normal file
@ -0,0 +1,32 @@
|
||||
package Drawings;
|
||||
|
||||
import CollectionGenericObjects.AbstractCompany;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class CanvasFormWarshipCollection<T> extends JComponent
|
||||
{
|
||||
public AbstractCompany company = null;
|
||||
public void SetCollectionToCanvas(AbstractCompany company) {
|
||||
this.company = company;
|
||||
}
|
||||
public CanvasFormWarshipCollection(){}
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponents(g);
|
||||
if (company == null || company._collection == null) {
|
||||
return;
|
||||
}
|
||||
company.DrawBackground(g);
|
||||
for (int i = 0; i < company._collection.getCount(); i++) {
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
T obj = (T)company._collection.Get(i);
|
||||
if (obj instanceof DrawingWarship) {
|
||||
((DrawingWarship) obj).DrawTransport(g2d);
|
||||
}
|
||||
}
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
|
@ -8,18 +8,24 @@ public class DrawingBattleship extends DrawingWarship
|
||||
{
|
||||
private IDrawingBlocks drawingBlocks;
|
||||
|
||||
public DrawingBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower)
|
||||
public DrawingBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean compartment, boolean tower)
|
||||
{
|
||||
EntityWarship = new EntityBattleship(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower);
|
||||
EntityWarship = new EntityBattleship(speed, weight, bodyColor, additionalColor, compartment, tower);
|
||||
DrawBlocks();
|
||||
}
|
||||
|
||||
public DrawingBattleship(EntityBattleship entityBattleship, IDrawingBlocks blocks) {
|
||||
EntityWarship = entityBattleship;
|
||||
drawingBlocks = blocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (EntityWarship == null || !(EntityWarship instanceof EntityBattleship battleship) || _startPosX == null || _startPosY == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
//ПРОВЕРКА
|
||||
super.DrawTransport(g); // Обращение к методу DrawTransport базового класса
|
||||
|
||||
|
@ -11,7 +11,7 @@ import java.awt.*;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class DrawingWarship extends JPanel {
|
||||
private IDrawingBlocks drawingBlocks;
|
||||
public IDrawingBlocks drawingBlocks;
|
||||
public Entities.EntityWarship EntityWarship;
|
||||
public Integer _pictureWidth;
|
||||
public Integer _pictureHeight;
|
||||
@ -36,6 +36,12 @@ public class DrawingWarship extends JPanel {
|
||||
EntityWarship = new EntityWarship(speed, weight, bodyColor);
|
||||
DrawBlocks();
|
||||
}
|
||||
|
||||
public DrawingWarship(EntityWarship entityWarship, IDrawingBlocks blocks) {
|
||||
EntityWarship = entityWarship;
|
||||
drawingBlocks = blocks;
|
||||
}
|
||||
|
||||
//////////////////////
|
||||
protected void DrawBlocks() {
|
||||
int number = (int)(Math.random() * 4 + 0);
|
||||
@ -152,6 +158,7 @@ public class DrawingWarship extends JPanel {
|
||||
g.fillRect(_startPosX + 5, _startPosY + 22, 5, 10); //нижний
|
||||
|
||||
// блоки
|
||||
if (drawingBlocks != null)
|
||||
drawingBlocks.drawBlocks(g, Color.BLACK, _startPosX, _startPosY);
|
||||
|
||||
//носик палубы
|
||||
@ -167,6 +174,5 @@ public class DrawingWarship extends JPanel {
|
||||
//заливка круга
|
||||
g.setColor(Color.BLUE);
|
||||
g.fillOval(_startPosX + 78, _startPosY + 12, 15, 15);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -7,14 +7,13 @@ public class EntityBattleship extends EntityWarship{
|
||||
public Color getAdditionalColor(){
|
||||
return AdditionalColor;
|
||||
}
|
||||
public boolean BodyDeck;
|
||||
|
||||
public boolean Compartment;
|
||||
public boolean Tower;
|
||||
|
||||
public EntityBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower){
|
||||
public EntityBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean compartment, boolean tower){
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor = additionalColor;
|
||||
BodyDeck = bodyDeck;
|
||||
Compartment = compartment;
|
||||
Tower = tower;
|
||||
}
|
||||
|
136
src/src/FormAdditionalCollection.java
Normal file
136
src/src/FormAdditionalCollection.java
Normal file
@ -0,0 +1,136 @@
|
||||
import CollectionAdditionalObjects.AdditionalCollections;
|
||||
import CollectionGenericObjects.AbstractCompany;
|
||||
import DifferentBlocks.DrawingBlocksType2;
|
||||
import DifferentBlocks.DrawingBlocksType1;
|
||||
import DifferentBlocks.DrawingBlocks;
|
||||
import DifferentBlocks.IDrawingBlocks;
|
||||
import Drawings.DrawingWarship;
|
||||
import Drawings.CanvasBattleship;
|
||||
import Drawings.DrawingBattleship;
|
||||
import Entities.EntityWarship;
|
||||
import Entities.EntityBattleship;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormAdditionalCollection extends JFrame {
|
||||
public DrawingWarship drawingWarship = null;
|
||||
private AbstractCompany company = null;
|
||||
private CanvasBattleship canvasBattleship = new CanvasBattleship();
|
||||
private AdditionalCollections<EntityWarship, IDrawingBlocks> additionalCollection = null;
|
||||
private Random random = new Random();
|
||||
private JButton buttonGenerate = new JButton("Создать");
|
||||
private JList<String> listEntity = new JList<String>();
|
||||
private JList<String> listBlocks = new JList<String>();
|
||||
public FormAdditionalCollection() {
|
||||
setTitle("Случайный объект");
|
||||
setMinimumSize(new Dimension(650,310));
|
||||
additionalCollection = new AdditionalCollections<EntityWarship, IDrawingBlocks>(3, (Class) EntityWarship.class, (Class) IDrawingBlocks.class);
|
||||
AddEntities();
|
||||
AddBlocks();
|
||||
buttonGenerate.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
drawingWarship = additionalCollection.CreateAdditionalCollectionWarship();
|
||||
drawingWarship.SetPictureSize(getWidth(), getHeight());
|
||||
drawingWarship.SetPosition(50,50);
|
||||
canvasBattleship._drawingWarship = drawingWarship;
|
||||
canvasBattleship.repaint();
|
||||
DrawingWarship copyWarship;
|
||||
if (drawingWarship instanceof DrawingBattleship)
|
||||
copyWarship = new DrawingBattleship((EntityBattleship) drawingWarship.EntityWarship, drawingWarship.drawingBlocks);
|
||||
else
|
||||
copyWarship = new DrawingWarship(drawingWarship.EntityWarship, drawingWarship.drawingBlocks);
|
||||
company._collection.Insert(copyWarship);
|
||||
FormWarshipCollection.canvasShow();
|
||||
|
||||
String[] data1 = new String[additionalCollection.CountEntities];
|
||||
for (int i = 0; i < additionalCollection.CountEntities; i++) {
|
||||
EntityWarship entity = additionalCollection._collectionEntity[i];
|
||||
data1[i] = ToString(entity);
|
||||
}
|
||||
String[] data2 = new String[additionalCollection.CountBlocks];
|
||||
for (int i = 0; i < additionalCollection.CountBlocks; i++) {
|
||||
IDrawingBlocks blocks = additionalCollection._collectionBlocks[i];
|
||||
data2[i] = ToString(blocks);
|
||||
}
|
||||
listEntity.setListData(data1);
|
||||
listBlocks.setListData(data2);
|
||||
}
|
||||
});
|
||||
buttonGenerate.setBounds(450, 10, 100, 50);
|
||||
add(buttonGenerate);
|
||||
listEntity.setBounds(10,200,300,60);
|
||||
listBlocks.setBounds(320,200,300,60);
|
||||
add(listEntity);
|
||||
add(listBlocks);
|
||||
add(canvasBattleship);
|
||||
setVisible(true);
|
||||
}
|
||||
private String ToString(EntityWarship entity) {
|
||||
String str = "";
|
||||
if (entity instanceof EntityBattleship) str += "EntityBattleship ";
|
||||
else str += "EntityWarship ";
|
||||
str += entity.getBodyColor().toString();
|
||||
return str;
|
||||
}
|
||||
private String ToString(IDrawingBlocks blocks) {
|
||||
if (blocks == null || blocks.getNumBlocks() == null)
|
||||
return "Не имеет блоки";
|
||||
String str = "Blocks ";
|
||||
if (blocks instanceof DrawingBlocksType1) str += "Type 1 ";
|
||||
else if (blocks instanceof DrawingBlocksType2) str += "Type 2 ";
|
||||
else str += "Type Blocks ";
|
||||
str += blocks.getNumBlocks().toString();
|
||||
return str;
|
||||
}
|
||||
public void AddEntities() {
|
||||
for (int i = 0; i < additionalCollection.CountEntities; i++) {
|
||||
random = new Random();
|
||||
int speed = random.nextInt(100, 300);
|
||||
double weight = random.nextInt(1000, 3000);
|
||||
Color bodycolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
|
||||
EntityWarship entity;
|
||||
if (random.nextBoolean()) {
|
||||
entity = new EntityWarship(speed, weight, bodycolor);
|
||||
}
|
||||
else {
|
||||
Color additionalcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
|
||||
boolean compartment = random.nextBoolean();
|
||||
boolean tower = random.nextBoolean();
|
||||
entity = new EntityBattleship(speed, weight, bodycolor, additionalcolor, compartment, tower);
|
||||
}
|
||||
additionalCollection.Insert(entity);
|
||||
}
|
||||
}
|
||||
public void AddBlocks() {
|
||||
for (int i = 0; i < additionalCollection.CountBlocks; i++) {
|
||||
random = new Random();
|
||||
Integer number = random.nextInt(0, 4);
|
||||
IDrawingBlocks drawingBlocks = null;
|
||||
switch (random.nextInt(0,4)) {
|
||||
case 1:
|
||||
drawingBlocks = new DrawingBlocksType1();
|
||||
break;
|
||||
case 2:
|
||||
drawingBlocks = new DrawingBlocksType2();
|
||||
break;
|
||||
case 3:
|
||||
drawingBlocks = new DrawingBlocks();
|
||||
break;
|
||||
default:
|
||||
number = null;
|
||||
break;
|
||||
}
|
||||
if (drawingBlocks != null) drawingBlocks.setNumBlocks(number);
|
||||
additionalCollection.Insert(drawingBlocks);
|
||||
}
|
||||
}
|
||||
void setCompany(AbstractCompany company) {
|
||||
this.company = company;
|
||||
}
|
||||
}
|
@ -1,27 +1,20 @@
|
||||
import Drawings.CanvasBattleship;
|
||||
import Drawings.*;
|
||||
import Drawings.DirectionType;
|
||||
import Drawings.DrawingWarship;
|
||||
import Drawings.DrawingBattleship;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormBattleship extends JFrame {
|
||||
private String title;
|
||||
private Dimension dimension;
|
||||
private int Width, Height;
|
||||
public CanvasBattleship canvasBattleship = new CanvasBattleship();
|
||||
private JButton CreateButton = new JButton("Создание линкора");;
|
||||
private JButton CreateShipButton = new JButton("Создание корабля");
|
||||
private CanvasBattleship canvasBattleship = new CanvasBattleship();
|
||||
private JButton UpButton = new JButton();
|
||||
private JButton DownButton = new JButton();;
|
||||
private JButton LeftButton = new JButton();;
|
||||
private JButton DownButton = new JButton();
|
||||
private JButton LeftButton = new JButton();
|
||||
private JButton RightButton = new JButton();
|
||||
private AbstractStrategy _strategy;
|
||||
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
|
||||
@ -30,72 +23,30 @@ public class FormBattleship extends JFrame {
|
||||
this.title = title;
|
||||
this.dimension = dimension;
|
||||
}
|
||||
private void CreateObject(String typeOfClass) {
|
||||
int StartPositionX = (int)(Math.random() * 90 + 10);
|
||||
int StartPositionY = (int)(Math.random() * 90 + 10);
|
||||
int speed = (int)(Math.random() * 300 + 100);
|
||||
double weight = (double)(Math.random() * 3000 + 1000);
|
||||
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||||
switch (typeOfClass) {
|
||||
case "DrawingWarhip":
|
||||
canvasBattleship._drawingWarship = new DrawingWarship(speed, weight, bodyColor);
|
||||
canvasBattleship._drawingWarship.SetPictureSize(Width, Height);
|
||||
canvasBattleship._drawingWarship.SetPosition(StartPositionX, StartPositionY);
|
||||
canvasBattleship.repaint();
|
||||
break;
|
||||
case "DrawingBattleship":
|
||||
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
|
||||
boolean bodyDeck = new Random().nextBoolean();
|
||||
boolean compartment = new Random().nextBoolean();;
|
||||
boolean tower = new Random().nextBoolean();;
|
||||
canvasBattleship._drawingWarship = new DrawingBattleship(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower);
|
||||
canvasBattleship._drawingWarship.SetPictureSize(Width, Height);
|
||||
canvasBattleship._drawingWarship.SetPosition(StartPositionX, StartPositionY);
|
||||
canvasBattleship.repaint();
|
||||
break;
|
||||
default: return;
|
||||
}
|
||||
_strategy = null;
|
||||
ComboBoxStrategy.setEnabled(true);
|
||||
}
|
||||
public void Init() {
|
||||
|
||||
public void Init(DrawingWarship warship) {
|
||||
setTitle(title);
|
||||
setMinimumSize(dimension);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
Width = getWidth() - 10;
|
||||
Height = getHeight() - 34;
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
ComboBoxStrategy.setEnabled(true);
|
||||
_strategy = null;
|
||||
canvasBattleship._drawingWarship = warship;
|
||||
|
||||
CreateButton.setName("CREATE");
|
||||
CreateShipButton.setName("CREATESHIPBUTTON");
|
||||
Icon iconUp = new ImageIcon("src/res/arrowUp.png");
|
||||
UpButton.setIcon(iconUp);
|
||||
UpButton.setName("UP");
|
||||
DownButton.setName("DOWN");
|
||||
UpButton.setName("Up");
|
||||
DownButton.setName("Down");
|
||||
Icon iconDown = new ImageIcon("src/res/arrowDown.png");
|
||||
DownButton.setIcon(iconDown);
|
||||
LeftButton.setName("LEFT");
|
||||
LeftButton.setName("Left");
|
||||
Icon iconLeft = new ImageIcon("src/res/arrowLeft.png");
|
||||
LeftButton.setIcon(iconLeft);
|
||||
RightButton.setName("RIGHT");
|
||||
RightButton.setName("Right");
|
||||
Icon iconRight = new ImageIcon("src/res/arrowRight.png");
|
||||
RightButton.setIcon(iconRight);
|
||||
|
||||
CreateButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawingBattleship");
|
||||
}
|
||||
});
|
||||
|
||||
CreateShipButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawingWarhip");
|
||||
}
|
||||
});
|
||||
|
||||
ButtonStrategy.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@ -114,7 +65,7 @@ public class FormBattleship extends JFrame {
|
||||
default:
|
||||
_strategy = null;
|
||||
break;
|
||||
};
|
||||
}
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
@ -141,16 +92,16 @@ public class FormBattleship extends JFrame {
|
||||
if (canvasBattleship._drawingWarship == null) return;
|
||||
boolean result = false;
|
||||
switch ((((JButton)(event.getSource())).getName())) {
|
||||
case "UP":
|
||||
case "Up":
|
||||
result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "DOWN":
|
||||
case "Down":
|
||||
result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "LEFT":
|
||||
case "Left":
|
||||
result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "RIGHT":
|
||||
case "Right":
|
||||
result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
@ -167,41 +118,19 @@ public class FormBattleship extends JFrame {
|
||||
setSize(dimension.width,dimension.height);
|
||||
setLayout(null);
|
||||
canvasBattleship.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 140, 40);
|
||||
CreateShipButton.setBounds(160, getHeight() - 90, 140, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
|
||||
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
|
||||
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
|
||||
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
|
||||
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
|
||||
add(CreateButton);
|
||||
add(CreateShipButton);
|
||||
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
|
||||
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
|
||||
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
|
||||
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
|
||||
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 25);
|
||||
ButtonStrategy.setBounds(getWidth() - 130, 45, 100, 25);
|
||||
add(UpButton);
|
||||
add(DownButton);
|
||||
add(RightButton);
|
||||
add(LeftButton);
|
||||
add(ComboBoxStrategy);
|
||||
add(ButtonStrategy);
|
||||
add(ComboBoxStrategy);
|
||||
add(canvasBattleship);
|
||||
setVisible(true);
|
||||
//обработка события изменения размеров окна
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
public void componentResized(ComponentEvent e) {
|
||||
Width = getWidth() - 10;
|
||||
Height = getHeight() - 34;
|
||||
if (canvasBattleship._drawingWarship != null)
|
||||
canvasBattleship._drawingWarship.SetPictureSize(Width, Height);
|
||||
canvasBattleship.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 140, 40);
|
||||
CreateShipButton.setBounds(160, getHeight() - 90, 140, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
|
||||
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
|
||||
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
|
||||
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
|
||||
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
201
src/src/FormWarshipCollection.java
Normal file
201
src/src/FormWarshipCollection.java
Normal file
@ -0,0 +1,201 @@
|
||||
import CollectionGenericObjects.AbstractCompany;
|
||||
import CollectionGenericObjects.WarshipSharingService;
|
||||
import CollectionGenericObjects.MassiveGenericObjects;
|
||||
import Drawings.CanvasFormWarshipCollection;
|
||||
import Drawings.DrawingWarship;
|
||||
import Drawings.DrawingBattleship;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.MaskFormatter;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.text.ParseException;
|
||||
import java.util.Random;
|
||||
|
||||
import static java.lang.Integer.parseInt;
|
||||
|
||||
public class FormWarshipCollection extends JFrame{
|
||||
private String title;
|
||||
private Dimension dimension;
|
||||
public static CanvasFormWarshipCollection<DrawingWarship> _canvasBattleship = new CanvasFormWarshipCollection<DrawingWarship>();
|
||||
private static AbstractCompany _company = null;
|
||||
private JButton CreateBatButton = new JButton("Создать линкор");
|
||||
private JButton CreateWarButton = new JButton("Создать военный корабль");
|
||||
private JButton RemoveButton = new JButton("Удалить");
|
||||
private JButton GoToCheckButton = new JButton("Тест");
|
||||
private JButton RandomButton = new JButton("Случайный объект");
|
||||
private JButton RefreshButton = new JButton("Обновить");
|
||||
private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"});
|
||||
private JFormattedTextField MaskedTextField;
|
||||
public FormWarshipCollection(String title, Dimension dimension) {
|
||||
this.title = title;
|
||||
this.dimension = dimension;
|
||||
}
|
||||
public static void canvasShow() {
|
||||
_company.SetPosition();
|
||||
_canvasBattleship.SetCollectionToCanvas(_company);
|
||||
_canvasBattleship.repaint();
|
||||
}
|
||||
private void CreateObject(String typeOfClass) {
|
||||
if (_company == null) return;
|
||||
int speed = (int)(Math.random() * 300 + 100);
|
||||
double weight = (double)(Math.random() * 3000 + 1000);
|
||||
Color bodyColor = getColor();
|
||||
DrawingWarship drawingWarship;
|
||||
switch (typeOfClass) {
|
||||
case "DrawingWarship":
|
||||
drawingWarship = new DrawingWarship(speed, weight, bodyColor);
|
||||
break;
|
||||
case "DrawingBattleship":
|
||||
Color additionalColor = getColor();
|
||||
boolean compartment = new Random().nextBoolean();
|
||||
boolean tower = new Random().nextBoolean();
|
||||
drawingWarship = new DrawingBattleship(speed, weight, bodyColor, additionalColor, compartment, tower);
|
||||
break;
|
||||
default: return;
|
||||
}
|
||||
if (_company._collection.Insert(drawingWarship, 0) != -1) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
canvasShow();
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Объект не удалось добавить");
|
||||
}
|
||||
}
|
||||
public Color getColor() {
|
||||
Color initializator = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||||
Color color = JColorChooser.showDialog(this, "Выберите цвет", initializator);
|
||||
return color;
|
||||
}
|
||||
public void Init() {
|
||||
setTitle(title);
|
||||
setMinimumSize(dimension);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
MaskFormatter mask = null;
|
||||
try {
|
||||
mask = new MaskFormatter("##");
|
||||
mask.setPlaceholder("00");
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
MaskedTextField = new JFormattedTextField(mask);
|
||||
ComboBoxCollections.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
switch (ComboBoxCollections.getSelectedItem().toString()) {
|
||||
case "Хранилище":
|
||||
_company = new WarshipSharingService(getWidth()-200, getHeight()-70, new MassiveGenericObjects<DrawingWarship>());
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
CreateWarButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawingWarship");
|
||||
}
|
||||
});
|
||||
CreateBatButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawingBattleship");
|
||||
}
|
||||
});
|
||||
|
||||
RemoveButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_company == null || MaskedTextField.getText() == null) {
|
||||
return;
|
||||
}
|
||||
int pos = parseInt(MaskedTextField.getText());
|
||||
int resultConfirmDialog = JOptionPane.showConfirmDialog(null,
|
||||
"Удалить", "Удаление",
|
||||
JOptionPane.YES_NO_OPTION);
|
||||
if (resultConfirmDialog == JOptionPane.NO_OPTION) return;
|
||||
if (_company._collection.Remove(pos) != null) {
|
||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||
canvasShow();
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Объект не удалось удалить");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
GoToCheckButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_company == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
DrawingWarship warship = null;
|
||||
int counter = 100;
|
||||
while (warship == null)
|
||||
{
|
||||
warship = _company.GetRandomObject();
|
||||
counter--;
|
||||
if (counter <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (warship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FormBattleship form = new FormBattleship("Линкор", new Dimension(900,565));
|
||||
form.Init(warship);
|
||||
}
|
||||
});
|
||||
|
||||
RandomButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_company == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FormAdditionalCollection form = new FormAdditionalCollection();
|
||||
form.setCompany(_company);
|
||||
}
|
||||
});
|
||||
|
||||
RefreshButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_company == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
canvasShow();
|
||||
}
|
||||
});
|
||||
|
||||
_canvasBattleship.setBounds(0, 0, getWidth()-200, getHeight());
|
||||
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
|
||||
CreateWarButton.setBounds(getWidth()-190, 60, 150, 30);
|
||||
CreateBatButton.setBounds(getWidth()-190, 100, 150, 30);
|
||||
MaskedTextField.setBounds(getWidth()-190,200,150,30);
|
||||
RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
|
||||
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
|
||||
RandomButton.setBounds(getWidth()-190, 320, 150, 30);
|
||||
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
|
||||
|
||||
setSize(dimension.width,dimension.height);
|
||||
setLayout(null);
|
||||
add(_canvasBattleship);
|
||||
add(ComboBoxCollections);
|
||||
add(CreateWarButton);
|
||||
add(CreateBatButton);
|
||||
add(MaskedTextField);
|
||||
add(RemoveButton);
|
||||
add(GoToCheckButton);
|
||||
add(RandomButton);
|
||||
add(RefreshButton);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
@ -2,8 +2,8 @@ import java.awt.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FormBattleship form = new FormBattleship("Линкор", new Dimension(700,500));
|
||||
FormWarshipCollection form = new FormWarshipCollection("Коллекция кораблей", new Dimension(1100,650));
|
||||
form.Init();
|
||||
|
||||
form.setLocationRelativeTo(null);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user