antihype commit 2.0

This commit is contained in:
antoc0der 2023-11-08 22:43:00 +03:00
parent 5f914ddb22
commit 82bad5c255
5 changed files with 374 additions and 0 deletions

View File

@ -0,0 +1,122 @@
package src;
import src.DrawningObjects.DrawningAirplane;
import src.Generics.AirplaneGenericCollection;
import src.MovementStrategy.DrawningObjectAirplane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormAirplaneCollection {
private final AirplaneGenericCollection<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 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(623, 12, 227, 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;
}
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 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();
}
}

70
src/FormForHard.java Normal file
View File

@ -0,0 +1,70 @@
package src;
import src.DrawningObjects.DrawningAirplane;
import src.DrawningObjects.DrawningIlluminators;
import src.DrawningObjects.IDraw;
import src.Entities.EntityAirplane;
import src.Generics.HardGeneric;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormForHard {
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 DrawningIlluminators(pictureBoxWidth, pictureBoxHeight, 0, 0);
}
public FormForHard(){
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

@ -0,0 +1,74 @@
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 = 210;
private final int _placeSizeHeight = 78;
private final SetGeneric<T> _collection;
public AirplaneGenericCollection(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,61 @@
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 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 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,47 @@
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];
}
}