PIbd-22 Puchkina A.A. LabWork_03_HARD #3

Closed
a.puchkina wants to merge 2 commits from LabWork_03 into LabWork_02
10 changed files with 421 additions and 42 deletions

View File

@ -4,29 +4,9 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="d76a7414-ef61-4559-85d8-aee9cbb6c975" name="Changes" comment="лаба 1, готовая">
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/DrawningObjects/DrawningAirplaneWithRadar.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/DrawningObjects/DrawningIlluminatorsCirc.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/DrawningObjects/DrawningIlluminatorsQuad.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/DrawningObjects/IDraw.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Entities/EntityAirplane.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Entities/EntityAirplaneWithRadar.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/FormAirplaneWithRadar.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/MovementStrategy/AbstractStrategy.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/MovementStrategy/DrawningObjectAirplane.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/MovementStrategy/IMoveableObject.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/MovementStrategy/MoveToBorder.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/MovementStrategy/MoveToCenter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/MovementStrategy/ObjectParameters.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/MovementStrategy/Status.java" afterDir="false" />
<list default="true" id="d76a7414-ef61-4559-85d8-aee9cbb6c975" name="Changes" comment="лаба 2, убрала комменты">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/DirectionType.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/DirectionType.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/DrawningAirplaneWithRadar.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/DrawningObjects/DrawningAirplane.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/DrawningIlluminators.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/DrawningObjects/DrawningIlluminators.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/EntityAirplaneWithRadar.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/NumberType.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/NumberType.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -57,14 +37,14 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"last_opened_file_path": "D:/Проекты Idea/ProjectAirplaneWithRadar_HARD/src"
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;D:/Проекты Idea/airplanewithradar_hard&quot;
}
}]]></component>
}</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="D:\Проекты Idea\ProjectAirplaneWithRadar_HARD\src" />

View File

@ -1,5 +1,6 @@
package DrawningObjects;
import MovementStrategy.DrawningObjectAirplane;
import MovementStrategy.IMoveableObject;
import Entities.EntityAirplane;
import src.DirectionType;
@ -9,11 +10,11 @@ public class DrawningAirplane {
protected EntityAirplane EntityAirplane;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
public int _startPosX;
public int _startPosY;
protected IDraw DrawningIlluminators;
private int _airplaneWidth = 190;
private int _airplaneHeight = 80;
public int _airplaneWidth = 190;
public int _airplaneHeight = 80;
public EntityAirplane EntityAirplane(){
return EntityAirplane;
}
@ -68,6 +69,8 @@ public class DrawningAirplane {
return;
_startPosX = x;
_startPosY = y;
DrawningIlluminators.ChangeX(_startPosX);
DrawningIlluminators.ChangeY(_startPosY);
if (x < 0 || y < 0 || x + _airplaneWidth >= _pictureWidth || y + _airplaneHeight >= _pictureHeight)
{
x = y = 10;
@ -75,6 +78,9 @@ public class DrawningAirplane {
_startPosY = y;
}
}
public IMoveableObject GetMoveableObject(){
return new DrawningObjectAirplane(this);
}
public int GetPosX(){return _startPosX;}
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _airplaneWidth;}

View File

@ -2,9 +2,7 @@ package DrawningObjects;
import java.awt.*;
public interface IDraw {
public void ChangeIlluminatorNumb(int x);
//public NumberType IlluminatorNumb();
public void DrawIlluminators(Graphics2D g2d);
//public void DrawIlluminatorsQuad(Graphics2D g2d);
public void ChangeX(int x);
public void ChangeY(int y);
}

View File

@ -0,0 +1,130 @@
import DrawningObjects.DrawningAirplane;
import Generics.AirplanesGenericCollection;
import MovementStrategy.DrawningObjectAirplane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormAirplaneCollection {
private final AirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes;
private int pictureBoxWidth = 630;
private int pictureBoxHeight = 426;
CollectionCanvas canv;
void Draw(){
if(canv == null)
return;
canv.repaint();
}
public FormAirplaneCollection(){
_airplanes = new AirplanesGenericCollection<>(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(623, 12, 227, 300);
canv.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
JButton addButton = new JButton("Добавить");
JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить");
JButton toFormForHard = 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);
toolBox.add(toFormForHard);
toFormForHard.setBounds(pictureBoxWidth, 120, 150, 20);
collectionFrame.add(toolBox);
collectionFrame.add(canv);
collectionFrame.setVisible(true);
toFormForHard.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
FormForHard form4GenericDopClass = new FormForHard();
}
}
);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_airplanes == null) {
return;
}
FormAirplaneWithRadar form = new FormAirplaneWithRadar();
form.buttonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_airplanes.Insert(form.SelectedAirplane()) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
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 AirplanesGenericCollection<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

@ -15,16 +15,27 @@ public class FormAirplaneWithRadar {
Canvas canv;
static int pictureBoxWidth = 882;
static int pictureBoxHeight = 453;
public JButton buttonSelect;
public JFrame AirplaneFrame;
public void Draw(){
canv.repaint();
}
public Color ChooseColor(JFrame MonorailFrame){
JColorChooser dialog = new JColorChooser();
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.WHITE);
return res;
}
public DrawningAirplane SelectedAirplane(){
return DrawningAirplane;
}
public FormAirplaneWithRadar(){
JFrame AirplaneFrame =new JFrame ();
AirplaneFrame =new JFrame ();
JButton buttonCreate = new JButton("Создать");
JButton buttonCreateLocomotive = new JButton("Создать самолет с радаром");
JButton buttonStep = new JButton("Шаг");
buttonSelect = new JButton ("Выбрать");
JComboBox comboBoxStrategy = new JComboBox(
new String[]{
"Довести до центра",
@ -87,9 +98,13 @@ public class FormAirplaneWithRadar {
@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)),
pictureBoxWidth, pictureBoxHeight);
color, pictureBoxWidth, pictureBoxHeight);
canv.DrawningAirplane = DrawningAirplane;
comboBoxStrategy.enable(true);
Draw();
@ -100,9 +115,19 @@ public class FormAirplaneWithRadar {
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 DrawningAirplaneWithRadar(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;
@ -150,10 +175,11 @@ public class FormAirplaneWithRadar {
});
AirplaneFrame.setSize (900, 500);
AirplaneFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
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);
RightButton.setBounds(840,411,30,30);
@ -171,6 +197,7 @@ public class FormAirplaneWithRadar {
AirplaneFrame.add(RightButton);
AirplaneFrame.add(comboBoxStrategy);
AirplaneFrame.add(buttonStep);
AirplaneFrame.add(buttonSelect);
AirplaneFrame.setVisible(true);
}
}

59
src/FormForHard.java Normal file
View File

@ -0,0 +1,59 @@
import DrawningObjects.*;
import Entities.EntityAirplane;
import Generics.HardGeneric;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormForHard extends JFrame {
static int pictureBoxWidth = 980;
static int pictureBoxHeight = 560;
public DrawningAirplane _drawningAirplane;
private class Canvas extends JComponent{
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawningAirplane == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawningAirplane.SetPosition(50, 50);
_drawningAirplane.DrawAirplane(g2d);
super.repaint();
}
}
HardGeneric<EntityAirplane, IDraw> hardGeneric;
public FormForHard(){
_drawningAirplane = null;
Canvas canv = new Canvas();
setSize (1000, 600);
setLayout(null);
canv.setBounds(0,0,pictureBoxWidth, pictureBoxHeight);
hardGeneric = new HardGeneric<>(100, 100, pictureBoxWidth, pictureBoxHeight);
hardGeneric.Add(new EntityAirplane(100, 100, Color.CYAN));
hardGeneric.Add(new EntityAirplane(100, 100, Color.PINK));
hardGeneric.Add(new EntityAirplane(100, 100, Color.LIGHT_GRAY));
hardGeneric.Add(new DrawningIlluminators(190, 80, 50, 50));
hardGeneric.Add(new DrawningIlluminatorsQuad(190, 80, 50, 50));
hardGeneric.Add(new DrawningIlluminatorsCirc(190, 80, 50, 50));
JButton creatButton = new JButton("Создать");
creatButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
_drawningAirplane = hardGeneric.getRDrawingObject();
canv.repaint();
}
}
);
creatButton.setBounds(pictureBoxWidth/2, pictureBoxHeight-50, 120, 50);
add(canv);
add(creatButton);
setVisible(true);
}
}

View File

@ -0,0 +1,73 @@
package Generics;
import DrawningObjects.DrawningAirplane;
import MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class AirplanesGenericCollection<T extends DrawningAirplane, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 210;
private final int _placeSizeHeight = 85;
private final SetGeneric<T> _collection;
public AirplanesGenericCollection(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);
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), i / inRow * _placeSizeHeight );
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,59 @@
package Generics;
import DrawningObjects.DrawningAirplane;
import DrawningObjects.IDraw;
import Entities.EntityAirplane;
import java.util.ArrayList;
import java.util.Random;
public class HardGeneric<T extends EntityAirplane, U extends IDraw> {
private ArrayList<T> Planes;
private ArrayList<U> Windows;
private int maxCountPlanes;
private int countPlanes;
private int maxCountWindows;
private int countWindows;
private Random random;
private int _pictureWidth;
private int _pictureHeight;
public HardGeneric(int _maxCountPlanes, int _maxCountWindows, int pictureWidth, int pictureHeight){
maxCountPlanes = _maxCountPlanes;
maxCountWindows = _maxCountWindows;
Planes = new ArrayList<T>(maxCountPlanes);
Windows = new ArrayList<U>(maxCountWindows);
countPlanes = 0;
countWindows = 0;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
random = new Random();
}
public void Add(T plane){
if (plane == null)
return;
if (countPlanes > maxCountPlanes)
return;
Planes.add(countPlanes++, plane);
}
public void Add(U window){
if (window == null)
return;
if (countWindows > maxCountWindows)
return;
Windows.add(countWindows++, window);
}
public DrawningAirplane getRDrawingObject(){
if (countPlanes == 0 || countWindows == 0)
return null;
Random rand = new Random();
int i = rand.nextInt(0, countPlanes);
int j = rand.nextInt(0,countWindows);
EntityAirplane entity = Planes.get(i);
IDraw inter = Windows.get(j);
DrawningAirplane airplane = new DrawningAirplane(entity.Speed(), entity.Weight(), entity.BodyColor(),
_pictureWidth, _pictureHeight);
return airplane;
}
}

View File

@ -0,0 +1,47 @@
package 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];
}
}

View File

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