ПИбд-23 Кислица Егор Дмитриевич Лабораторная работа №3 (усложненная) #3

Closed
Egor wants to merge 1 commits from Laba3 into Laba2
8 changed files with 471 additions and 29 deletions
Showing only changes of commit 65e66e99be - Show all commits

View File

@ -3,16 +3,18 @@ package src.DrawningObjects;
import java.awt.*;
import java.util.Random;
import src.DirectionType;
import src.MovementStrategy.DrawningObjectAirplane;
import src.MovementStrategy.IMoveableObject;
import src.Entities.EntityAirplane;
public class DrawningAirplane {
protected EntityAirplane EntityAirplane;
private int _pictureWidth;
private int _pictureHeight;
public int _pictureWidth;
public int _pictureHeight;
protected int _startPosX = 0;
protected int _startPosY = 0;
private int _airplaneWidth = 160;
private int _airplaneHeight = 68;
private int _airplaneHeight = 160;
private int _airplanekorpusHeight = 90;
protected IDraw DrawningEngines;
@ -24,7 +26,6 @@ public class DrawningAirplane {
return;
_startPosY=70;
_startPosX = 0;
_pictureWidth = width;
_pictureHeight = height;
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
@ -50,6 +51,8 @@ public class DrawningAirplane {
return;
_startPosX = x;
_startPosY = y;
DrawningEngines.ChangeX(_startPosX);
DrawningEngines.ChangeY(_startPosY);
if (x + _airplaneWidth >= _pictureWidth || y + _airplaneHeight >= _pictureHeight) {
_startPosX = 0;
_startPosY = 70;
@ -81,7 +84,9 @@ public class DrawningAirplane {
};
return can;
}
public IMoveableObject GetMoveableObject(){
return new DrawningObjectAirplane(this);
}
public void MoveTransport(DirectionType direction){
if (!CanMove(direction) || EntityAirplane == null)
return;

View File

@ -14,19 +14,34 @@ import java.util.Random;
public class FormAirFighter {
private DrawningAirplane DrawningAirplane;
private AbstractStrategy _abstractStrategy;
Canvas canv;
static int pictureBoxWidth = 882;
static int pictureBoxHeight = 453;
public JButton buttonSelect;
public JFrame AirplaneFrame;
Canvas canv;
static int pictureBoxWidth = 902;
static int pictureBoxHeight = 453;
public Color ChooseColor(JFrame AirplaneFrame){
JColorChooser dialog = new JColorChooser();
Color res = JColorChooser.showDialog(AirplaneFrame, "Выберите цвет", Color.WHITE);
return res;
}
public DrawningAirplane SelectedAirplane(){
return DrawningAirplane;
}
public void Draw(){
if(DrawningAirplane == null)
return;
canv.repaint();
}
public FormAirFighter(){
JFrame MonorailFrame =new JFrame ();
AirplaneFrame =new JFrame ();
JButton buttonCreate = new JButton("Создать");
JButton buttonCreateLocomotive = new JButton("Создать самолет с радаром");
JButton buttonCreateAirFighter = new JButton("Создать боевой самлоёт");
JButton buttonStep = new JButton("Шаг");
buttonSelect = new JButton ("Выбрать");
JComboBox comboBoxStrategy = new JComboBox(
new String[]{
"Довести до центра",
@ -89,8 +104,13 @@ public class FormAirFighter {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color choosen = ChooseColor(AirplaneFrame);
if(choosen != null){
color = choosen;
}
DrawningAirplane = new DrawningAirplane(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
color,
pictureBoxWidth, pictureBoxHeight);
canv.DrawningAirplane = DrawningAirplane;
comboBoxStrategy.enable(true);
@ -98,13 +118,23 @@ public class FormAirFighter {
}
}
);
buttonCreateLocomotive.addActionListener(
buttonCreateAirFighter.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Random random = new Random();
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(AirplaneFrame);
if(choosen != null){
color = choosen;
}
choosen = ChooseColor(AirplaneFrame);
if(choosen != null){
additionalColor = choosen;
}
DrawningAirplane = new DrawningAirFighter(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
color,
additionalColor,
random.nextBoolean(), random.nextBoolean(),
pictureBoxWidth, pictureBoxHeight);
canv.DrawningAirplane = DrawningAirplane;
@ -151,29 +181,31 @@ public class FormAirFighter {
}
});
MonorailFrame.setSize (900, 500);
MonorailFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
MonorailFrame.setLayout(null);
AirplaneFrame.setSize (900, 500);
AirplaneFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
AirplaneFrame.setLayout(null);
canv = new Canvas();
canv.setSize(pictureBoxWidth, pictureBoxHeight);
buttonSelect.setBounds(383,401, 180, 40);
buttonCreate.setBounds(198, 401, 180, 40);
buttonCreateLocomotive.setBounds(12, 401, 180, 40);
buttonCreateAirFighter.setBounds(12, 401, 180, 40);
RightButton.setBounds(840,411,30,30);
LeftButton.setBounds(768,411,30,30);
UpButton.setBounds(804,375,30,30);
DownButton.setBounds(804,411,30,30);
comboBoxStrategy.setBounds(719,12,151,28);
buttonStep.setBounds(768, 46, 94, 29);
MonorailFrame.add(canv);
MonorailFrame.add(buttonCreate);
MonorailFrame.add(buttonCreateLocomotive);
MonorailFrame.add(UpButton);
MonorailFrame.add(DownButton);
MonorailFrame.add(LeftButton);
MonorailFrame.add(RightButton);
MonorailFrame.add(comboBoxStrategy);
MonorailFrame.add(buttonStep);
MonorailFrame.setVisible(true);
AirplaneFrame.add(canv);
AirplaneFrame.add(buttonCreate);
AirplaneFrame.add(buttonCreateAirFighter);
AirplaneFrame.add(UpButton);
AirplaneFrame.add(DownButton);
AirplaneFrame.add(LeftButton);
AirplaneFrame.add(RightButton);
AirplaneFrame.add(comboBoxStrategy);
AirplaneFrame.add(buttonStep);
AirplaneFrame.add(buttonSelect);
AirplaneFrame.setVisible(true);
}
}
class Canvas extends JComponent{

View File

@ -0,0 +1,130 @@
package src;
import src.DrawningObjects.DrawningAirplane;
import src.Generics.AirplaneGenericCollection;
import src.FormAirFighter;
import src.MovementStrategy.DrawningObjectAirplane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormAirFighterCollection {
private final AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes;
private int pictureBoxWidth = 902;
private int pictureBoxHeight = 453;
CollectionCanvas canv;
void Draw(){
if(canv == null)
return;
canv.repaint();
}
public FormAirFighterCollection(){
_airplanes = new AirplaneGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
canv = new CollectionCanvas();
JPanel toolBox = new JPanel();
JFrame collectionFrame = new JFrame();
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880,497);
toolBox.setBounds(666, 12, 127, 426);
canv.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
JButton addButton = new JButton("Добавить");
JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить");
JTextField airplaneNumb = new JTextField();
GridLayout lay = new GridLayout(4,1);
toolBox.setLayout(lay);
toolBox.add(addButton);
toolBox.add(airplaneNumb);
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(_airplanes == null) {
return;
}
FormAirFighter form = new FormAirFighter();
form.buttonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_airplanes.Insert(form.SelectedAirplane()) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form.SelectedAirplane()._pictureWidth = pictureBoxWidth;
form.SelectedAirplane()._pictureHeight = pictureBoxHeight;
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
canv._airplanes = _airplanes;
form.AirplaneFrame.dispose();
Draw();
}
});
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_airplanes == null) {
return;
}
String tmp = airplaneNumb.getText();
int numb;
try{
numb = Integer.parseInt(tmp);
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_airplanes.Remove(numb);
_airplanes.ShowAirplanes();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_airplanes == null) {
return;
}
_airplanes.ShowAirplanes();
Draw();
}
});
}
}
class CollectionCanvas extends JComponent {
public AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes;
public CollectionCanvas(){
}
@Override
public void paintComponent (Graphics g){
if (_airplanes == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(_airplanes.ShowAirplanes(), 0, 0, this);
super.repaint();
}
}

View File

@ -0,0 +1,84 @@
package src.Generics;
import src.DrawningObjects.DrawningAirplane;
import src.MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class AirplaneGenericCollection<T extends DrawningAirplane, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 166;
private final int _placeSizeHeight = 160;
private final SetGeneric<T> _collection;
public AirplaneGenericCollection(int picWidth, int picHeight){
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth-202;
_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);
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)
{
for (int i = 0; i < _collection.Count; i++)
{
DrawningAirplane airplane = _collection.Get(i);
if (airplane != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
airplane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeWidth /10 * 2 , (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + (_placeSizeHeight - _placeSizeHeight * 170 / 1000) / 2);
airplane.DrawAirplane((Graphics2D) g);
}
}
}
public BufferedImage ShowAirplanes()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
}

View File

@ -0,0 +1,62 @@
package src.Generics;
import src.DrawningObjects.DrawningAirplane;
import src.DrawningObjects.IDraw;
import src.Entities.EntityAirplane;
import java.util.Random;
public class HardGeneric <T extends EntityAirplane,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 EntityAirplane[CountFirst];
arrSecond = (U[]) new IDraw[CountSecond];
pictureBoxHeight = height;
pictureBoxWidth = width;
}
public int InsertFirst(T entityAirplane){
Review

Методы добавления должны быть полиморфными

Методы добавления должны быть полиморфными
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] = entityAirplane;
return 0;
}
public int InsertSecond(U inter){
Review

Методы добавления должны быть полиморфными

Методы добавления должны быть полиморфными
if(arrSecond[CountSecond-1] != null)
return -1;
arrSecond[0] = inter;
return 0;
}
public DrawningAirplane MakeObject(){
Random rand = new Random();
int indFirst = rand.nextInt(0, curSz);
int indSecond = rand.nextInt(0,curSz);
EntityAirplane entity = arrFirst[indFirst];
IDraw inter = arrSecond[indSecond];
DrawningAirplane airplane = new DrawningAirplane(entity.Speed(), entity.Weight(), entity.BodyColor(),
pictureBoxWidth, pictureBoxHeight);
return airplane;
}
}

View File

@ -0,0 +1,53 @@
package src.Generics;
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 airplane){
return Insert(airplane, 0);
}
public int Insert(T airplane, int position){
if(!(position >= 0 && position < Count))
return -1;
if(_places[position] == null){
_places[position] = airplane;
}
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] = airplane;
}
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];
}
}

75
src/HardForm.java Normal file
View File

@ -0,0 +1,75 @@
package src;
import src.DrawningObjects.DrawningAirplane;
import src.DrawningObjects.DrawningEngines;
import src.DrawningObjects.IDraw;
import src.Entities.EntityAirplane;
import src.Generics.HardGeneric;
import javax.swing.*;
import javax.swing.text.html.parser.Entity;
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 EntityAirplane makeEntity(){
Random rand = new Random();
return new EntityAirplane(rand.nextInt(100, 300), rand.nextDouble(1000,3000),
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)));
}
void Draw(){
if(canv == null)
return;
canv.repaint();
}
private IDraw makeIDraw(EntityAirplane entity){
Random rand = new Random();
return new DrawningEngines(pictureBoxWidth, pictureBoxHeight, 0, 0);
}
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<EntityAirplane, IDraw> toDraw= new HardGeneric<>(sz,
sz, pictureBoxWidth, pictureBoxHeight);
for(int i = 0; i < sz; i++){
EntityAirplane ent = makeEntity();
toDraw.InsertFirst(ent);
toDraw.InsertSecond(makeIDraw(ent));
}
makeObject.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningAirplane DrawningAirplane = toDraw.MakeObject();
DrawningAirplane.SetPosition(pictureBoxWidth / 2 - DrawningAirplane.GetWidth()/2,
pictureBoxHeight / 2 - DrawningAirplane.GetHeight()/2);
canv.DrawningAirplane = DrawningAirplane;
Draw();
}
}
);
}
}

View File

@ -1,5 +1,6 @@
package src;
import src.DrawningObjects.DrawningAirFighter;
import src.FormAirFighterCollection;
import src.DrawningObjects.DrawningAirplane;
import src.MovementStrategy.*;
import java.awt.*;
@ -14,6 +15,6 @@ import javax.swing.*;
public class Main {
public static void main(String[] args) throws IOException {
FormAirFighter form = new FormAirFighter();
FormAirFighterCollection form = new FormAirFighterCollection();
}
}