lab3 DONE!

This commit is contained in:
Галина Федоренко 2023-11-18 21:43:41 +04:00
parent 078b5ffac5
commit 4e19e9e451
9 changed files with 438 additions and 4 deletions

View File

@ -12,6 +12,12 @@ public class DrawingHydroplane extends DrawingPlane{
}
public DrawingHydroplane(EntityPlane plane, IWindowDrawing _windowDrawing, int width, int height ){
super(plane, _windowDrawing, width, height);
if (height < _pictureHeight || width < _pictureWidth)
return;
}
public void SetPosition(int x, int y)
{
_startPosX = Math.min(x, _pictureWidth-_planeWidth);

View File

@ -5,6 +5,8 @@ import java.util.*;
public class DrawingPlane{
public IMoveableObject GetMoveableObject() { return new DrawningObjectPlane(this);}
protected IWindowDrawing windowDrawing;
public EntityPlane _EntityPlane;
@ -48,6 +50,17 @@ public class DrawingPlane{
}
public DrawingPlane(EntityPlane plane, IWindowDrawing _windowDrawing, int width, int height)
{
if (height < _planeHeight || width < _planeWidth)
return;
_pictureWidth = width;
_pictureHeight = height;
_EntityPlane = plane;
windowDrawing = _windowDrawing;
windowDrawing.setNumWindow(_EntityPlane.numWindow);
}
public void SetPosition(int x, int y)
{
_startPosX = Math.min(x, _pictureWidth-_planeWidth);

View File

@ -0,0 +1,63 @@
package org.example;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Form4GenericDopClass extends JFrame {
static int pictureBoxWidth = 980;
static int pictureBoxHeight = 560;
public DrawingPlane _drawingPlane;
private class Canvas extends JComponent{
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingPlane == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingPlane.SetPosition(50, 50);
_drawingPlane.DrawTransport(g2d);
super.repaint();
}
}
GenericDopClass<EntityPlane, IWindowDrawing> genericDopClass;
public Form4GenericDopClass(){
_drawingPlane = null;
Canvas canv = new Canvas();
setSize (1000, 600);
setLayout(null);
canv.setBounds(0,0,pictureBoxWidth, pictureBoxHeight);
genericDopClass = new GenericDopClass<>(100, 100, pictureBoxWidth, pictureBoxHeight);
genericDopClass.addPlane(new EntityPlane(100, 100, Color.BLUE, 30));
genericDopClass.addPlane(new EntityPlane(100, 100, Color.RED, 10));
genericDopClass.addPlane(new EntityPlane(100, 100, Color.GRAY, 20));
genericDopClass.addPlane(new EntityHydroplane(100, 100, Color.BLUE, Color.ORANGE, true, true, 20));
genericDopClass.addWindow(new WindowDrawing());
genericDopClass.addWindow(new WindowDrawingTringle());
JButton creatButton = new JButton("createButton");
creatButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
_drawingPlane = genericDopClass.getRDrawingObject();
canv.repaint();
}
}
);
creatButton.setBounds(pictureBoxWidth/2, pictureBoxHeight-20, 120, 20);
add(canv);
add(creatButton);
setVisible(true);
}
}

View File

@ -7,20 +7,25 @@ import java.awt.event.*;
public class FormHydroplane{
private DrawingPlane _drawingPlane;
public DrawingPlane _drawingPlane;
public DrawingPlane SelectedPlane;
public boolean DialogResult = false;
public JButton buttonSelectPlane;
private AbstractStrategy abstractStrategy;
Canvas canv;
static int pictureBoxWidth = 980;
static int pictureBoxHeight = 580;
public JFrame w;
public void Draw(){
canv.repaint();
}
public FormHydroplane(){
JFrame w=new JFrame ("Hydroplane");
w=new JFrame ("Hydroplane");
JButton buttonCreatePlane = new JButton("createPlane");
JButton buttonCreateHydroplane = new JButton("createHydroplane");
buttonSelectPlane = new JButton("select plane");
JButton buttonStrategysStep = new JButton("strategys step");
JComboBox comboBoxStrategy = new JComboBox(
new String[]{
@ -162,7 +167,6 @@ public class FormHydroplane{
right.addActionListener(actioListener);
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
@ -174,6 +178,7 @@ public class FormHydroplane{
right.setBounds(940, 520, 40, 40);
comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20);
buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20);
buttonSelectPlane.setBounds(pictureBoxWidth/2, 540, 150, 20);
w.add(canv);
w.add(buttonCreatePlane);
w.add(buttonCreateHydroplane);
@ -183,6 +188,7 @@ public class FormHydroplane{
w.add(right);
w.add(comboBoxStrategy);
w.add(buttonStrategysStep);
w.add(buttonSelectPlane);
w.setVisible(true);
}

View File

@ -0,0 +1,135 @@
package org.example;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class FormPlaneCollecltion {
private class Canvas extends JComponent{
public PlanesGenericCollection<DrawingPlane, DrawningObjectPlane> _planes;
public Canvas(){}
public void paintComponent (Graphics g){
super.paintComponent(g);
if (_planes.ShowTrains() != null) {
g.drawImage(_planes.ShowTrains(), 0, 0, this);
}
super.repaint();
}
}
Canvas canv;
static int pictureBoxWidth = 800;
static int pictureBoxHeight = 580;
private PlanesGenericCollection<DrawingPlane, DrawningObjectPlane> _planes;
public void Draw(){
canv.repaint();
}
FormPlaneCollecltion() {
canv = new Canvas();
JFrame w = new JFrame("PlaneCollecltion");
_planes = new PlanesGenericCollection<DrawingPlane, DrawningObjectPlane>(pictureBoxWidth, pictureBoxHeight);
canv._planes = _planes;
JButton ButtonAddPlane = new JButton("ButtonAddPlane");
ButtonAddPlane.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
FormHydroplane form = new FormHydroplane();
form.buttonSelectPlane.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (_planes.Add(form._drawingPlane) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.w.dispose();
}
}
);
}
}
);
JTextField TextBoxNumber = new JTextField();
JButton ButtonRemovePlane = new JButton("ButtonRemovePlane");
ButtonRemovePlane.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
return;
}
for (char it : TextBoxNumber.getText().toCharArray())
if (it < '0' || it > '9') {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (TextBoxNumber.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
int pos = Integer.parseInt(TextBoxNumber.getText());
if (_planes.remove(pos) != null) {
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
}
}
);
JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection");
ButtonRefreshCollection.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Draw();
}
}
);
JButton toForm4GenericDopClass = new JButton("ToForm4GenericDopClass");
toForm4GenericDopClass.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass();
}
}
);
w.setSize(1000, 600);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
ButtonAddPlane.setBounds(pictureBoxWidth, 0, 150, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 30, 150, 20);
ButtonRemovePlane.setBounds(pictureBoxWidth, 60, 150, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 150, 20);
toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 150, 20);
w.add(canv);
w.add(ButtonAddPlane);
w.add(ButtonRemovePlane);
w.add(ButtonRefreshCollection);
w.add(TextBoxNumber);
w.add(toForm4GenericDopClass);
w.setVisible(true);
}
}

View File

@ -0,0 +1,63 @@
package org.example;
import java.util.ArrayList;
import java.util.Random;
public class GenericDopClass<T extends EntityPlane, U extends IWindowDrawing> {
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 GenericDopClass(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 boolean addPlane(T plane){
if (plane == null)
return false;
if (countPlanes > maxCountPlanes)
return false;
Planes.add(countPlanes++, plane);
return true;
}
public boolean addWindow(U window){
if (window == null)
return false;
if (countWindows > maxCountWindows)
return false;
Windows.add(countWindows++, window);
return true;
}
public DrawingPlane getRDrawingObject(){
if (countPlanes == 0 || countWindows == 0)
return null;
int i = random.nextInt(countPlanes);
int j = random.nextInt(countWindows);
DrawingPlane drawingPlane;
if (Planes.get(i) instanceof EntityHydroplane){
drawingPlane = new DrawingHydroplane((EntityHydroplane)Planes.get(i), Windows.get(j), _pictureWidth, _pictureHeight);
}
else{
drawingPlane = new DrawingPlane(Planes.get(i), Windows.get(j), _pictureWidth, _pictureHeight);
}
return drawingPlane;
}
}

View File

@ -2,6 +2,6 @@ package org.example;
public class Main{
public static void main(String[] args) {
FormHydroplane formHydroplane = new FormHydroplane();
FormPlaneCollecltion formPlaneCollecltion = new FormPlaneCollecltion();
}
}

View File

@ -0,0 +1,91 @@
package org.example;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveableObject> {
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 175;
private int _placeSizeHeight = 85;
private SetGeneric<T> _collection;
public PlanesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Add(T obj)
{
if (obj == null)
{
return -1;
}
return _collection.Insert(obj);
}
public T remove(int pos)
{
T obj = _collection.Get(pos);
if (obj != null)
{
_collection.Remove(pos);
}
return obj;
}
public U GetU(int pos)
{
return (U)_collection.Get(pos).GetMoveableObject();
}
public BufferedImage ShowTrains()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bmp.createGraphics();
DrawBackground(g);
DrawObjects(g);
g.dispose();
return bmp;
}
private void DrawBackground(Graphics2D 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(Graphics2D g)
{
for (int i = 0; i < _collection.Count; i++)
{
T t = _collection.Get(i);
if (t != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t instanceof DrawingHydroplane)
((DrawingHydroplane) t).DrawTransport(g);
else
t.DrawTransport(g);
}
}
}
}

View File

@ -0,0 +1,57 @@
package org.example;
public class SetGeneric<T extends Object> {
private Object[] _places;
public int Count;
public SetGeneric(int count)
{
_places = new Object[count];
Count = _places.length;
}
public int Insert(T plane)
{
if (_places[Count - 1] != null)
return -1;
return Insert(plane, 0);
}
public int Insert(T plane, int position)
{
if (position < 0 || position >= Count)
return -1;
if (_places[position] != null)
{
int indexEnd = position + 1;
while (_places[indexEnd] != null)
{
indexEnd++;
}
for (int i = indexEnd + 1; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
_places[position] = plane;
return position;
}
public boolean Remove(int position)
{
if (position < 0 || position >= _places.length)
return false;
_places[position] = null;
return true;
}
public T Get(int position)
{
if (position < 0 || position >= _places.length)
return null;
return (T)_places[position];
}
}