Lab2 Done+

This commit is contained in:
Yunusov_Niyaz 2023-11-10 00:20:05 +04:00
parent adf9a87f29
commit b02f4ac691
14 changed files with 382 additions and 7 deletions

View File

@ -2,7 +2,9 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

1
.idea/misc.xml generated
View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />

View File

@ -0,0 +1,64 @@
import java.awt.*;
public class BusesGenericCollection<T extends DrawingBus, U extends IMoveableObject> {
private int _pictureWidth;
private int _pictureHeight;
private final int _placeSizeWidth = 210;
private final int _placeSizeHeight = 135;
private SetGeneric<T> _collection;
public BusesGenericCollection(int picWidth, int picHeight) {
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height, (Class<T>) DrawingBus.class);
}
public void showBuses(Graphics2D g){
drawBackground(g);
drawObjects(g);
}
public boolean insert(T obj)
{
if (obj != null)
return _collection.insert(obj);
return false;
}
public boolean remove(int pos)
{
T obj = _collection.Get(pos);
if (obj != null)
return _collection.remove(pos);
return false;
}
public U GetU(int pos) {
if(_collection.Get(pos) == null)
return null;
return (U)_collection.Get(pos).GetMoveableObject;
}
private void drawBackground(Graphics2D g) {
BasicStroke pen = new BasicStroke(3);
g.setStroke(pen);
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.getCount(); i++)
{
DrawingBus bus =_collection.Get(i);
if (bus != null) {
int inRow = _pictureWidth / _placeSizeWidth;
bus.setPosition(_placeSizeWidth * (inRow - 1) - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight);
//bus.setPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth)-8, i / inRow * _placeSizeHeight + 20);
bus.drawTransport(g);
}
}
}
}

View File

@ -134,4 +134,5 @@ public class DrawingBus {
graphics2D.setPaint(Color.BLACK);
graphics2D.drawRect(_startPosX + 196, _startPosY + 91, 10, 20);
}
public IMoveableObject GetMoveableObject = new DrawingObjectBus(this);
}

View File

@ -3,6 +3,18 @@ import java.awt.*;
public class DrawingDoors implements IDrawDoors{
private DoorsNumber number;
@Override
public int getNumber(){
int x = 0;
if(number == DoorsNumber.THREE)
x = 3;
if(number == DoorsNumber.FOUR)
x = 4;
if(number == DoorsNumber.FIVE)
x = 5;
return x;
}
public int getType(){return 0;}
@Override
public void setNumber(int x){
if(x <= 3)
number = DoorsNumber.THREE;

View File

@ -3,6 +3,18 @@ import java.awt.*;
public class DrawingDoorsRoundedUp implements IDrawDoors{
private DoorsNumber number;
@Override
public int getNumber(){
int x = 0;
if(number == DoorsNumber.THREE)
x = 3;
if(number == DoorsNumber.FOUR)
x = 4;
if(number == DoorsNumber.FIVE)
x = 5;
return x;
}
public int getType(){return 1;}
@Override
public void setNumber(int x){
if(x <= 2)
number = DoorsNumber.THREE;

View File

@ -3,6 +3,18 @@ import java.awt.*;
public class DrawingDoorsRoundedUpAndDown implements IDrawDoors{
private DoorsNumber number;
@Override
public int getNumber(){
int x = 0;
if(number == DoorsNumber.THREE)
x = 3;
if(number == DoorsNumber.FOUR)
x = 4;
if(number == DoorsNumber.FIVE)
x = 5;
return x;
}
public int getType(){return 2;}
@Override
public void setNumber(int x){
if(x <= 2)
number = DoorsNumber.THREE;

View File

@ -0,0 +1,91 @@
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class FrameBusCollection extends JFrame {
private BusesGenericCollection<DrawingBus, DrawingObjectBus> buses;
JComponent pictureBoxCollection;
TextField textFieldNumber;
public FrameBusCollection() throws IOException{
super("Набор автобусов");
setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGui();
buses = new BusesGenericCollection<>(pictureBoxCollection.getWidth(),pictureBoxCollection.getHeight());
setVisible(true);
}
private void createGui(){
pictureBoxCollection = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (buses != null) buses.showBuses(graphics2D);
super.repaint();
}
};
pictureBoxCollection.setSize(new Dimension(700,450));
JButton buttonAddBus = new JButton("Добавить автобус");
buttonAddBus.setPreferredSize(new Dimension(160,30));
textFieldNumber = new TextField();
JButton buttonRemoveBus = new JButton("Удалить автобус");
buttonRemoveBus.setPreferredSize(new Dimension(160,30));
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
buttonAddBus.setPreferredSize(new Dimension(160,30));
buttonAddBus.addActionListener(e -> buttonAddBusClick());
buttonRemoveBus.addActionListener(e -> buttonRemoveBusClick());
buttonRefreshCollection.addActionListener(e -> buttonRefreshBusClick());
JPanel panelCollection = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.right = 2;
constraints.insets.top = constraints.insets.bottom = 30;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
panelCollection.add(buttonAddBus, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
panelCollection.add(textFieldNumber, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
panelCollection.add(buttonRemoveBus, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
panelCollection.add(buttonRefreshCollection, constraints);
setLayout(new BorderLayout());
add(panelCollection, BorderLayout.EAST);
add(pictureBoxCollection,BorderLayout.CENTER);
}
private void buttonAddBusClick(){
FrameTrolleybus form;
try{
form = new FrameTrolleybus();
}
catch (IOException e){
throw new RuntimeException();
}
form.selectBusButton.addActionListener(e -> {
form.selectBus();
if (buses.insert(form.getSelectedBus())) {
JOptionPane.showMessageDialog(this, "Объект добавлен");
pictureBoxCollection.repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось добавить объект");
}
form.dispose();
});
}
private void buttonRemoveBusClick() {
int pos = Integer.parseInt(textFieldNumber.getText());
if (buses.remove(pos))
{
JOptionPane.showMessageDialog(this,"Объект удалён");
}
else
{
JOptionPane.showMessageDialog(this,"Не удалось удалить объект");
}
}
private void buttonRefreshBusClick(){
pictureBoxCollection.repaint();
}
}

45
src/FrameHard.java Normal file
View File

@ -0,0 +1,45 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FrameHard extends JFrame{
HardGeneric<EntityBus, IDrawDoors> generic;
DrawingBus drawing;
private JComponent pictureBox;
private int pictureBoxWidth = 210;
private int pictureBoxHeight = 165;
public FrameHard(){
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pictureBox = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawing != null) drawing.drawTransport(graphics2D);
super.repaint();
}
};
pictureBox.setSize(pictureBoxWidth,pictureBoxHeight);
JButton buttonMakeObject = new JButton("Создать объект");
Random rand = new Random();
int size = rand.nextInt(1, 10);
generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight);
for(int i = 0; i < size; i++){
generic.insertBuses(generic.makeRandomBus());
generic.insertDoors(generic.makeRandomDoor());
}
buttonMakeObject.addActionListener(e -> {
DrawingBus drawingBus = generic.makeObject();
drawingBus.setPosition(pictureBoxWidth / 2 - drawingBus.getWidth()/2,
pictureBoxHeight / 2 - drawingBus.getHeight()/2);
drawing = drawingBus;
draw();
});
setLayout(new BorderLayout());
add(pictureBox, BorderLayout.CENTER);
add(buttonMakeObject, BorderLayout.SOUTH);
setVisible(true);
}
void draw(){
pictureBox.repaint();}
}

View File

@ -1,5 +1,6 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.colorchooser.ColorChooserComponentFactory;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
@ -10,6 +11,9 @@ public class FrameTrolleybus extends JFrame {
private DrawingBus drawingBus;
private AbstractStrategy abstractStrategy;
private JComboBox<String> comboBoxStrategy;
public JButton selectBusButton;
private DrawingBus selectedBus;
public DrawingBus getSelectedBus(){ return selectedBus;}
private final JComponent pictureBoxTrolleybus;
public FrameTrolleybus() throws IOException {
super("Троллейбус");
@ -27,6 +31,7 @@ public class FrameTrolleybus extends JFrame {
JButton stepButton = new JButton("Шаг");
JButton createBusButton = new JButton("Создать автобус");
JButton createTrolleybusButton = new JButton("Создать троллейбус");
selectBusButton = new JButton("Создание");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("img/right.png"))));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("img/left.png"))));
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("img/up.png"))));
@ -53,6 +58,9 @@ public class FrameTrolleybus extends JFrame {
constraints.gridx = 1;
constraints.gridy = 0;
createPanel.add(createTrolleybusButton, constraints);
constraints.gridx = 2;
constraints.gridy = 0;
createPanel.add(selectBusButton, constraints);
JPanel movementPanel = new JPanel(new GridBagLayout());
JPanel rightPanel = new JPanel(new BorderLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
@ -81,6 +89,7 @@ public class FrameTrolleybus extends JFrame {
constraints.gridx = 0;
constraints.gridy = 1;
stepPanel.add(stepButton, constraints);
add(pictureBoxTrolleybus);
rightPanel.add(stepPanel, BorderLayout.NORTH);
leftPanel.add(createPanel, BorderLayout.SOUTH);
@ -91,22 +100,23 @@ public class FrameTrolleybus extends JFrame {
}
private void buttonCreateTrolleybusClick() {
Random random = new Random();
Color bodyColor = JColorChooser.showDialog(null,"Основной цвет", null);
Color additColor = JColorChooser.showDialog(null,"Основной цвет", null);
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingBus = new DrawingTrolleybus(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
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(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(),
bodyColor, additColor, random.nextBoolean(), random.nextBoolean(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(),
(random.nextInt(3)+1)*2, random.nextInt(3));
drawingBus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonCreateBusClick(){
Random random = new Random();
Color bodyColor = JColorChooser.showDialog(null,"Основной цвет", null);
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingBus = new DrawingBus(
random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
bodyColor,
pictureBoxTrolleybus.getWidth(),
pictureBoxTrolleybus.getHeight(),
(random.nextInt(3)+1)*2,
@ -174,4 +184,9 @@ public class FrameTrolleybus extends JFrame {
}
pictureBoxTrolleybus.repaint();
}
public void selectBus() {
if (drawingBus == null) {
return;
}
selectedBus = drawingBus;}
}

81
src/HardGeneric.java Normal file
View File

@ -0,0 +1,81 @@
import java.awt.*;
import java.util.Random;
public class HardGeneric <T extends EntityBus,U extends IDrawDoors> {
T[] buses;
U[] doors;
private int count;
private int busesCount;
private int doorsCount;
private int pictureBoxWidth;
private int pictureBoxHeight;
public HardGeneric(int busesCount, int doorsCount, int width, int height) {
count = 0;
this.busesCount = busesCount;
this.doorsCount = doorsCount;
buses = (T[]) new EntityBus[this.busesCount];
doors = (U[]) new IDrawDoors[this.doorsCount];
pictureBoxHeight = height;
pictureBoxWidth = width;
}
public int insertBuses(T entityBus) {
if (buses[busesCount - 1] != null)
return -1;
for (int i = count - 1; i >= 0; i--) {
buses[i + 1] = buses[i];
doors[i + 1] = doors[i];
}
count++;
buses[0] = entityBus;
return 0;
}
public int insertDoors(U door) {
if (doors[doorsCount - 1] != null)
return -1;
doors[0] = door;
return 0;
}
public DrawingBus makeObject() {
Random rand = new Random();
int indBuses = rand.nextInt(0, count);
int indDoors = rand.nextInt(0, count);
EntityBus entity = buses[indBuses];
IDrawDoors doors = this.doors[indDoors];
if (entity instanceof EntityTrolleybus) {
return new DrawingTrolleybus(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(),
((EntityTrolleybus) entity).getAdditionalColor(), ((EntityTrolleybus) entity).getRoga(),
((EntityTrolleybus) entity).getBattery(), pictureBoxWidth, pictureBoxHeight, doors.getNumber(), doors.getType());
}
return new DrawingBus(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(),
pictureBoxWidth, pictureBoxHeight, doors.getNumber(), doors.getType());
}
public EntityBus makeRandomBus() {
Random random = new Random();
EntityBus bus;
switch (random.nextInt(2)) {
case 0 -> bus = new EntityBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
default -> bus = new EntityTrolleybus(random.nextInt(100, 300), random.nextDouble(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());
}
return bus;
}
public IDrawDoors makeRandomDoor(){
Random random = new Random();
IDrawDoors door;
switch (random.nextInt(3)){
case 1 -> door = new DrawingDoorsRoundedUp();
case 2 -> door = new DrawingDoorsRoundedUpAndDown();
default -> door = new DrawingDoors();
}
door.setNumber((random.nextInt(3) + 1) * 2);
return door;
}
}

View File

@ -1,6 +1,8 @@
import java.awt.*;
public interface IDrawDoors {
int getNumber();
int getType();
void setNumber(int x);
void drawDoors(Graphics2D graphics2D, int _startX, int _startY);
}

View File

@ -1,4 +1,4 @@
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException { new FrameTrolleybus(); }
public static void main(String[] args) throws IOException { new FrameHard(); }
}

39
src/SetGeneric.java Normal file
View File

@ -0,0 +1,39 @@
import java.lang.reflect.Array;
public class SetGeneric<T extends Object>{
private final T[] places;
public int getCount() {return places.length;}
public SetGeneric(int count, Class<T> type){
places = (T[]) Array.newInstance(type, count);
}
public boolean insert(T ship){
return insert(ship, 0);
}
public boolean insert(T ship, int position){
if (!(position >= 0 && position < places.length))
return false;
if (places[position] != null)
{
int ind = position;
while (ind < places.length && places[ind] != null)
ind++;
if (ind == places.length)
return false;
for (int i = ind - 1; i >= position; i--)
places[i + 1] = places[i];
}
places[position] = ship;
return true;
}
public boolean remove(int position){
if(!(position >= 0 && position < getCount()))
return false;
places[position] = null;
return true;
}
public T Get(int position){
if(!(position >= 0 && position < getCount()))
return null;
return places[position];
}
}