Боровков М В ПИбд-22 3 лабораторная работа #3

Closed
bekodeg wants to merge 2 commits from labWork3 into labWork2
12 changed files with 501 additions and 19 deletions

View File

@ -10,12 +10,14 @@ public class DrawningElectricLocomotive extends DrawningLocomotive {
Color bodyColor, Color additionalColor,
int typeWheels, int countWheels,
boolean horns, boolean battery,
int width, int height){
int width, int height) {
super(speed, weight, bodyColor, typeWheels, countWheels, width, height, 120, 70);
if (entityLocomotive != null){
entityLocomotive = new EntityElectricLocomotive(
speed, weight, bodyColor, additionalColor, horns, battery);
}
entityLocomotive = new EntityElectricLocomotive(
speed, weight, bodyColor, additionalColor, horns, battery);
}
public DrawningElectricLocomotive(EntityLocomotive entity, IDrawWheels drawingWheels, int width, int height) {
super(entity, drawingWheels, width, height);
}
@Override
public void DrawTransport(Graphics g) {

View File

@ -2,6 +2,7 @@ package DrawningObjects;
import Entities.EntityLocomotive;
import MovementStrategy.DirectionType;
import MovementStrategy.IMoveableObject;
import java.awt.*;
@ -84,6 +85,12 @@ public class DrawningLocomotive {
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
public DrawningLocomotive(EntityLocomotive entity, IDrawWheels drawWheels, int width, int height) {
entityLocomotive = entity;
wheels = drawWheels;
_pictureWidth = width;
_pictureHeight = height;
}
public void SetPosition(int x, int y) {
x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth);
@ -96,8 +103,6 @@ public class DrawningLocomotive {
if (entityLocomotive == null) {
return;
}
g.clearRect(0, 0, _pictureWidth, _pictureHeight);
// корпус
g.setColor(entityLocomotive.BodyColor);
@ -136,6 +141,9 @@ public class DrawningLocomotive {
_pictureWidth = width;
_pictureHeight = height;
}
public IMoveableObject GetMoveableObject(){
return (IMoveableObject) this;
}
public boolean CanMove(DirectionType direction) {
if (entityLocomotive == null) {

View File

@ -13,6 +13,7 @@ public class EntityElectricLocomotive extends EntityLocomotive {
Color bodyColor, Color additionalColor,
boolean horns, boolean battery) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Horns = horns;
Battery = battery;
}

View File

@ -15,6 +15,7 @@ public class FormElectricLocomotive extends JFrame {
private Canvas canvas;
private JButton buttonCreateElectricLocomotive;
private JButton buttonCreateLocomotive;
public JButton buttonSelectLocomotive;
private JButton buttonUp;
private JButton buttonRight;
private JButton buttonDown;
@ -28,7 +29,12 @@ public class FormElectricLocomotive extends JFrame {
{
return;
}
_drawningLocomotive.DrawTransport(canvas.getGraphics());
Graphics g = canvas.getGraphics();
g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
_drawningLocomotive.DrawTransport(g);
}
public DrawningLocomotive GetSelectedLocomotive(){
return _drawningLocomotive;
}
private void InitializeComponent(){
buttonCreateElectricLocomotive = new JButton("Создать Электропоезд");
@ -37,6 +43,9 @@ public class FormElectricLocomotive extends JFrame {
buttonCreateLocomotive = new JButton("Создать локомотив");
buttonCreateLocomotive.setMargin(new Insets(0, 0, 0, 0));
buttonSelectLocomotive = new JButton("Выбор");
buttonSelectLocomotive.setMargin(new Insets(0, 0, 0, 0));
buttonStep = new JButton("шаг");
buttonStep.setName("шаг");
@ -85,12 +94,14 @@ public class FormElectricLocomotive extends JFrame {
setSize(1000, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Рисование локомотивов");
setLayout(null);
canvas = new Canvas();
canvas.setBounds(0, 0, 980, 560);
buttonCreateElectricLocomotive.setBounds(10, 470, 200, 40);
buttonCreateLocomotive.setBounds(10, 520, 200, 40);
buttonSelectLocomotive.setBounds(770, 520, 200, 40);
buttonUp.setBounds(50, 380, 40, 40);
buttonDown.setBounds(50, 420, 40, 40);
buttonRight.setBounds(90, 420, 40, 40);
@ -100,9 +111,9 @@ public class FormElectricLocomotive extends JFrame {
comboBoxStrategy.setBounds(770, 10, 200, 30);
comboBoxWheels.setBounds(220, 520, 250, 40);
add(buttonCreateElectricLocomotive);
add(buttonCreateLocomotive);
add(buttonSelectLocomotive);
add(buttonUp);
add(buttonDown);
add(buttonRight);
@ -127,14 +138,22 @@ public class FormElectricLocomotive extends JFrame {
_drawWheels.setWheelsCount(countWheels);
System.out.println(e.getActionCommand());
Random random = new Random();
Color baseColor = JColorChooser.showDialog(null,"Основной цвет", Color.RED);
if (baseColor == null){
baseColor = new Color(random.nextInt(0, 256),
random.nextInt(0, 256),
random.nextInt(0, 256));
}
Color addColor = JColorChooser.showDialog(null,"Дополнительный цвет", Color.RED);
if (addColor == null){
addColor = new Color(random.nextInt(0, 256),
random.nextInt(0, 256),
random.nextInt(0, 256));
}
_drawningLocomotive = new DrawningElectricLocomotive(
random.nextInt(100, 300), random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256),
random.nextInt(0, 256),
random.nextInt(0, 256)),
new Color(random.nextInt(0, 256),
random.nextInt(0, 256),
random.nextInt(0, 256)),
baseColor,
addColor,
comboBoxWheels.getSelectedIndex(), countWheels,
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
canvas.getWidth(), canvas.getHeight()
@ -156,11 +175,15 @@ public class FormElectricLocomotive extends JFrame {
}
System.out.println(e.getActionCommand());
Random random = new Random();
Color baseColor = JColorChooser.showDialog(null,"Основной цвет", Color.RED);
if (baseColor == null){
baseColor = new Color(random.nextInt(0, 256),
random.nextInt(0, 256),
random.nextInt(0, 256));
}
_drawningLocomotive = new DrawningLocomotive(
random.nextInt(100, 300), random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256),
random.nextInt(0, 256),
random.nextInt(0, 256)),
baseColor,
comboBoxWheels.getSelectedIndex(), countWheels,
canvas.getWidth(), canvas.getHeight()
);

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="Forms.FormGenerateLocomotiveDop">
<grid id="27dc6" binding="PictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="378" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="4e6e1" class="javax.swing.JButton" binding="generateLocomotiveButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Сгенерировать"/>
</properties>
</component>
<vspacer id="a5af5">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>

View File

@ -0,0 +1,79 @@
package Forms;
import DrawningObjects.*;
import Entities.EntityElectricLocomotive;
import Entities.EntityLocomotive;
import Generics.DopLocomotiveGenericCollection;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
public class FormGenerateLocomotiveDop extends JFrame {
private Random _random;
private DrawningLocomotive _drawingLocomotive;
private DopLocomotiveGenericCollection<EntityLocomotive, IDrawWheels> _genericDop;
private JButton generateLocomotiveButton;
private JPanel PictureBox;
public JPanel getPictureBox() {
return PictureBox;
}
public FormGenerateLocomotiveDop(){
_random = new Random();
_genericDop = new DopLocomotiveGenericCollection<>();
PictureBox.setPreferredSize(new Dimension(400, 400));
generateLocomotiveButton.addActionListener(this::generateLocomotive);
}
private void addRandomEntity(){
EntityLocomotive entityLocomotive;
if (_random.nextBoolean()){
entityLocomotive = new EntityLocomotive(
_random.nextInt(100, 300),
_random.nextInt(1000, 3000),
new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256))
);
} else {
entityLocomotive = new EntityElectricLocomotive(
_random.nextInt(100, 300),
_random.nextInt(1000, 3000),
new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)),
new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)),
_random.nextBoolean(),
_random.nextBoolean()
);
}
_genericDop.add(entityLocomotive);
}
private void addRandomWheels(){
int choice = _random.nextInt(3);
IDrawWheels drawWheels;
if (choice == 0){
drawWheels = new DrawningWheels();
} else if (choice == 1){
drawWheels = new DrawningWheelsStar();
} else {
drawWheels = new DrawningWheelsPolygon();
}
drawWheels.setWheelsCount(_random.nextInt(2, 4));
_genericDop.add(drawWheels);
}
private void generateLocomotive(ActionEvent e){
addRandomEntity();
addRandomWheels();
_drawingLocomotive = _genericDop.GenerateLocomotive(PictureBox.getWidth(), PictureBox.getHeight());
_drawingLocomotive.SetPosition((PictureBox.getWidth() - _drawingLocomotive.GetWidth()) / 2,
(PictureBox.getHeight() - _drawingLocomotive.GetHeight()) / 2);
Draw();
}
public void Draw() {
if (_drawingLocomotive == null) {
return;
}
Graphics g = PictureBox.getGraphics();
PictureBox.paint(g);
_drawingLocomotive.DrawTransport(g);
}
}

View File

@ -0,0 +1,151 @@
package Forms;
import DrawningObjects.DrawningLocomotive;
import Generics.LocomotivesGenericCollection;
import MovementStrategy.DrawningObjectLocomotive;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormLocomotiveCollection extends JFrame {
private final LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive> _locomotives;
private Canvas canvas;
private FrameGenerateLocomotiveDop _formGenerateLocomotiveDop;
private JButton buttonAddLocomotive;
private JButton buttonRemoveLocomotive;
private JButton buttonRefreshCollection;
private JButton buttonOpenGenerateWindow;
private JTextField textBoxNumber;
private JPanel buttonBox;
public void repaint(){
if (_locomotives == null)
return;
Graphics g = canvas.getGraphics();
g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.setBackground(getBackground());
g.drawImage(_locomotives.ShowLocomotives(), 0, 0, canvas);
}
private void InitializeComponent(){
canvas = new Canvas();
buttonAddLocomotive = new JButton("Добавить локомотив");
buttonAddLocomotive.setMargin(new Insets(0, 0, 0, 0));
textBoxNumber = new JTextField();
buttonRemoveLocomotive = new JButton("Удалить локомотив");
buttonRemoveLocomotive.setMargin(new Insets(0, 0, 0, 0));
buttonRefreshCollection = new JButton("Обновить колекцию");
buttonRefreshCollection.setMargin(new Insets(0, 0, 0, 0));
buttonOpenGenerateWindow = new JButton("Открыть окно генерации");
buttonOpenGenerateWindow.setMargin(new Insets(0, 0, 0, 0));
buttonBox = new JPanel(new GridLayout(0, 1, 0, 10));
JLabel labelBB = new JLabel("Инструменты");
buttonBox.add(labelBB);
buttonBox.add(buttonAddLocomotive);
buttonBox.add(textBoxNumber);
buttonBox.add(buttonRemoveLocomotive);
buttonBox.add(buttonRefreshCollection);
buttonBox.setBounds(760, 3, 200, 220);
canvas.setBounds(10, 3, 740, 700);
buttonOpenGenerateWindow.setBounds(760, 600, 200, 40);
setSize(1000, 700);
setTitle("Колекция локомотивов");
setLayout(null);
add(buttonBox);
add(buttonOpenGenerateWindow);
add(canvas);
}
void InitializeLogic(){
buttonAddLocomotive.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FormElectricLocomotive form = new FormElectricLocomotive();
form.setVisible(true);
form.buttonSelectLocomotive.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (_locomotives.Add(form.GetSelectedLocomotive()) != -1){
JOptionPane.showMessageDialog(null,
"Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект добавлен");
repaint();
}
else {
JOptionPane.showMessageDialog(null,
"Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось добавить объект");
}
form.dispose();
}
}
);
}
});
buttonRemoveLocomotive.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(null,
"Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
{
int pos = 0;
try {
pos = Integer.parseInt(textBoxNumber.getText());
}
catch (Exception ex){
System.out.println(ex.toString());
pos = 0;
}
if (_locomotives.Sub(pos) != null)
{
JOptionPane.showMessageDialog(null,
"Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
repaint();
}
else
{
JOptionPane.showMessageDialog(null,
"Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
}
repaint();
}
});
buttonRefreshCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
buttonOpenGenerateWindow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_formGenerateLocomotiveDop != null) {
_formGenerateLocomotiveDop.dispose();
}
_formGenerateLocomotiveDop = new FrameGenerateLocomotiveDop();
_formGenerateLocomotiveDop.setVisible(true);
}
});
}
public FormLocomotiveCollection(){
InitializeComponent();
_locomotives = new LocomotivesGenericCollection<>(canvas.getWidth(), canvas.getHeight());
InitializeLogic();
setVisible(true);
}
}

View File

@ -0,0 +1,17 @@
package Forms;
import javax.swing.*;
public class FrameGenerateLocomotiveDop extends JFrame {
public FormGenerateLocomotiveDop formGenerateLocomotiveDop;
public FrameGenerateLocomotiveDop(){
super();
setTitle("Генерация локомотивов");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
formGenerateLocomotiveDop = new FormGenerateLocomotiveDop();
setContentPane(formGenerateLocomotiveDop.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
}
}

View File

@ -0,0 +1,45 @@
package Generics;
import DrawningObjects.DrawningElectricLocomotive;
import DrawningObjects.DrawningLocomotive;
import DrawningObjects.IDrawWheels;
import Entities.EntityElectricLocomotive;
import Entities.EntityLocomotive;
import java.util.ArrayList;
import java.util.Random;
public class DopLocomotiveGenericCollection<T extends EntityLocomotive, U extends IDrawWheels> {
private ArrayList<T> _entitiesStorage;
private ArrayList<U> _drawWheelsStorage;
public DopLocomotiveGenericCollection(){
_entitiesStorage = new ArrayList<>();
_drawWheelsStorage = new ArrayList<>();
}
public void add(T obj){
_entitiesStorage.add(obj);
}
public void add(U obj){
_drawWheelsStorage.add(obj);
}
public DrawningLocomotive GenerateLocomotive(int pictureWidth, int pictureHeight) {
Random random = new Random();
if (_entitiesStorage.isEmpty()) {
return null;
}
T entityLocomotive = _entitiesStorage.get(random.nextInt(_entitiesStorage.size()));
U drawingWheels = null;
if (!_drawWheelsStorage.isEmpty()) {
drawingWheels = _drawWheelsStorage.get(random.nextInt(_drawWheelsStorage.size()));
}
DrawningLocomotive drawingLocomotive;
if (entityLocomotive instanceof EntityElectricLocomotive) {
drawingLocomotive = new DrawningElectricLocomotive((EntityLocomotive) entityLocomotive, drawingWheels, pictureWidth, pictureHeight);
} else {
drawingLocomotive = new DrawningLocomotive(entityLocomotive, drawingWheels, pictureWidth, pictureHeight);
}
return drawingLocomotive;
}
}

View File

@ -0,0 +1,79 @@
package Generics;
import DrawningObjects.DrawningLocomotive;
import MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class LocomotivesGenericCollection<T extends DrawningLocomotive, U extends IMoveableObject> {
private int _pictureWidth;
private int _pictureHeight;
private final int _placeSizeWidth = 210;
private final int _placeSizeHeight = 90;
private SetGeneric<T> _collection;
public LocomotivesGenericCollection(int pictureWidth, int pictureHeight){
int width = pictureWidth / _placeSizeWidth;
int height = pictureHeight / _placeSizeHeight;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Add(T obj)
{
if (obj == null) {
return -1;
}
obj.SetPictureSize(_pictureWidth, _pictureHeight);
if (_collection.Insert(obj)) {
return 1;
}
return -1;
}
public T Sub(int pos){
T obj = _collection.Get(pos);
_collection.Remove(pos);
return obj;
}
public U GetU(int pos){
return (U) (_collection.Get(pos)).GetMoveableObject();
}
public Image ShowLocomotives()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics g = bmp.getGraphics();
DrawBackground(g);
DrawObjects(g);
return bmp;
}
private void DrawBackground(Graphics g){
g.setColor(Color.BLACK);
g.setFont(new Font("myFont", Font.PLAIN, 3));
for (int i = 0; i < _pictureHeight / _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) {
int ColumnCount = _pictureWidth / _placeSizeWidth;
int RowsCount = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count(); ++i) {
DrawningLocomotive drawningLocomotive = _collection.Get(i);
if (drawningLocomotive == null) {
continue;
}
drawningLocomotive.SetPosition(
(i % ColumnCount) * _placeSizeWidth,
((RowsCount - (i / ColumnCount) - 1) * _placeSizeHeight));
drawningLocomotive.DrawTransport(g);
}
}
}

View File

@ -0,0 +1,50 @@
package Generics;
import DrawningObjects.DrawningLocomotive;
public class SetGeneric<T extends DrawningLocomotive> {
private T[] _places;
public int Count(){
return _places.length;
}
public SetGeneric(int count){
_places = (T[])new DrawningLocomotive[count];
}
boolean Insert(T obj, int position){
if (position >= _places.length || position < 0){
return false;
}
int fillLineEnd = _places.length;
for (int i = position; i < _places.length; ++i){
if (_places[i] == null){
fillLineEnd = i;
break;
}
}
if (fillLineEnd == _places.length){
return false;
}
for (int i = fillLineEnd; i > position; --i){
_places[i] = _places[i - 1];
}
_places[position] = obj;
return true;
}
boolean Insert(T obj){
return Insert(obj, 0);
}
public boolean Remove(int position){
if (position >= _places.length || position < 0)
{
return false;
}
_places[position] = null;
return true;
}
public T Get(int position){
if (position >= _places.length || position < 0){
return null;
}
return _places[position];
}
}

View File

@ -2,6 +2,7 @@ import Forms.*;
public class Main {
public static void main(String[] args) {
FormElectricLocomotive form = new FormElectricLocomotive();
FormLocomotiveCollection form = new FormLocomotiveCollection();
}
}