Compare commits

...

3 Commits
main ... Lab_1

11 changed files with 406 additions and 0 deletions

BIN
Down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 562 B

BIN
Left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 548 B

BIN
Right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 552 B

View File

@ -0,0 +1,7 @@
package Trolleybus;
public enum CntOfDoors {
Three,
Four,
Five
}

View File

@ -0,0 +1,12 @@
package Trolleybus;
public enum DirectionType {
Up (1),
Down (2),
Right (3),
Left (4);
private int Direction;
DirectionType(int direction) {
Direction = direction;
}
}

View File

@ -0,0 +1,56 @@
package Trolleybus;
import java.awt.*;
public class DrawingDoors {
private CntOfDoors _cntOfDoors;
public void SetCntOfDoors(int cnt) {
if (cnt <= 3) {
_cntOfDoors = CntOfDoors.Three;
}
if (cnt == 4) {
_cntOfDoors = CntOfDoors.Four;
}
if (cnt >= 5) {
_cntOfDoors = CntOfDoors.Five;
}
}
public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY) {
switch (_cntOfDoors)
{
case Three:
g.setColor(BodyColor);
g.drawRect(_startPosX + 2, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 67, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 132, _startPosY + 50, 16, 30);
g.setColor(Color.CYAN);
g.drawOval(_startPosX + 22, _startPosY + 35, 16, 24);
g.drawOval(_startPosX + 47, _startPosY + 35, 16, 24);
g.drawOval(_startPosX + 87, _startPosY + 35, 16, 24);
g.drawOval(_startPosX + 112, _startPosY + 35, 16, 24);
break;
case Four:
g.setColor(BodyColor);
g.drawRect(_startPosX + 2, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 48, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 86, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 132, _startPosY + 50, 16, 30);
g.setColor(Color.CYAN);
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
g.drawOval(_startPosX + 67, _startPosY + 35, 16, 24);
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
break;
case Five:
g.setColor(BodyColor);
g.drawRect(_startPosX + 2, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 48, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 67, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 86, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 132, _startPosY + 50, 16, 30);
g.setColor(Color.CYAN);
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
break;
}
}
}

View File

@ -0,0 +1,130 @@
package Trolleybus;
import java.awt.*;
import java.util.Random;
public class DrawingTrolleybus {
// Класс-сущность
public EntityTrolleybus EntityTrolleybus;
// Замена свойства
public EntityTrolleybus getEntityTrolleybus() {return EntityTrolleybus;}
private void setEntityTrolleybus(EntityTrolleybus entityTrolleybus) {EntityTrolleybus = entityTrolleybus;}
public DrawingDoors Doors;
// Ширина окна
private int _pictureWidth;
// Высота окна
private int _pictureHeight;
// Левая координата прорисовки троллейбуса
private int _startPosX;
// Верхняя координата прорисовки троллейбуса
private int _startPosY;
// Ширина прорисовки троллейбуса
private final int _trolleybusWidth = 150;
// Высота прорисовки троллейбуса
private final int _trolleybusHeight = 95;
// Инициализация свойств
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean batteries, int width, int height)
{
if (width < _trolleybusWidth || height < _trolleybusHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntityTrolleybus = new EntityTrolleybus();
EntityTrolleybus.Init(speed, weight, bodyColor, additionalColor, horns, batteries);
//Кол-во дверей
Random random = new Random();
Doors = new DrawingDoors();
Doors.SetCntOfDoors(random.nextInt(3, 6));
return true;
}
// Установка позиции
public void SetPosition(int x, int y)
{
if (x > _pictureWidth || y > _pictureHeight) {
return;
}
_startPosX = Math.min(Math.max(x, 0), _pictureWidth - _trolleybusWidth);
_startPosY = Math.min(Math.max(y, 0), _pictureHeight - _trolleybusHeight);
}
// Изменение направления перемещения
public void MoveTransport(DirectionType directionType)
{
if (EntityTrolleybus == null)
{
return;
}
switch (directionType)
{
//влево
case Left:
if (_startPosX - EntityTrolleybus.Step > 0)
{
_startPosX -= (int)EntityTrolleybus.Step;
}
break;
//вверх
case Up:
if (_startPosY - EntityTrolleybus.Step > 0)
{
_startPosY -= (int)EntityTrolleybus.Step;
}
break;
// вправо
case Right:
if (_startPosX + _trolleybusWidth + EntityTrolleybus.Step < _pictureWidth)
{
_startPosX += (int)EntityTrolleybus.Step;
}
break;
// вниз
case Down:
if (_startPosY + _trolleybusHeight + EntityTrolleybus.Step < _pictureHeight)
{
_startPosY += (int)EntityTrolleybus.Step;
}
break;
}
}
// Прорисовка объекта
public void DrawTransport(Graphics g)
{
if (EntityTrolleybus == null)
{
return;
}
Graphics2D g2d = (Graphics2D)g;
//РИСОВАНИЕ САМОГО ТРРОЛЛЕЙБУСА
//Корпус
g2d.setColor(EntityTrolleybus.getBodyColor());
g2d.drawLine(_startPosX, _startPosY + 30, _startPosX, _startPosY + 80);
g2d.drawLine(_startPosX, _startPosY + 80, _startPosX + 20, _startPosY + 80);
g2d.drawLine(_startPosX + 45, _startPosY + 80, _startPosX + 105, _startPosY + 80);
g2d.drawLine(_startPosX + 130, _startPosY + 80, _startPosX + 150, _startPosY + 80);
g2d.drawLine(_startPosX + 150, _startPosY + 80, _startPosX + 150, _startPosY + 30);
g2d.drawLine(_startPosX + 150, _startPosY + 30, _startPosX, _startPosY + 30);
//Колёса
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX + 20, _startPosY + 70, 25, 25);
g2d.drawOval(_startPosX + 105, _startPosY + 70, 25, 25);
//Двери
Doors.DrawDoors(g2d, EntityTrolleybus.getBodyColor(), _startPosX, _startPosY);
//Опциональные "рога"
g2d.setColor(EntityTrolleybus.getAdditionalColor());
if (EntityTrolleybus.Horns)
{
g2d.drawLine(_startPosX + 70, _startPosY + 30, _startPosX + 40, _startPosY);
g2d.drawLine(_startPosX + 70, _startPosY + 30, _startPosX + 60, _startPosY);
}
//Опциональный отсек для батареи
if (EntityTrolleybus.Batteries)
{
int[] xOfBatteries = {_startPosX + 70, _startPosX + 70, _startPosX + 100, _startPosX + 110};
int[] yOfBatteries = {_startPosY + 30, _startPosY + 25, _startPosY + 25, _startPosY + 30};
g2d.fillPolygon(xOfBatteries, yOfBatteries, 4);
}
}
}

View File

@ -0,0 +1,55 @@
package Trolleybus;
import java.awt.*;
public class EntityTrolleybus {
//Скорость
public int Speed;
// Замена свойства
public int getSpeed() {return Speed;}
private void setSpeed(int speed) {Speed = speed;}
// Вес
public double Weight;
// Замена свойства
public double getWeight() {return Weight;}
private void setWeight(double weight) {Weight = weight;}
// Основной цвет
public Color BodyColor;
// Замена свойства
public Color getBodyColor() {return BodyColor;}
private void setBodyColor(Color bodyColor) {BodyColor = bodyColor;}
// Дополнительный цвет (для опциональных элементов)
public Color AdditionalColor;
// Замена свойства
public Color getAdditionalColor() {return AdditionalColor;}
private void setAdditionalColor(Color additionalColor) {AdditionalColor = additionalColor;}
// Признак (опция) наличия "рогов"
public boolean Horns;
// Замена свойства
public boolean getHorns() {return Horns;}
private void setHorns(boolean horns) {Horns = horns;}
// Признак (опция) наличия отсека под электрические батареи
public boolean Batteries;
// Замена свойства
public boolean getBatteries() {return Batteries;}
private void setBatteries(boolean batteries) {Batteries = batteries;}
// Шаг перемещения троллейбуса
public double Step;
// Инициализация полей объекта-класса троллейбуса
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean batteries)
{
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Horns = horns;
Batteries = batteries;
}
}

View File

@ -0,0 +1,139 @@
package Trolleybus;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormTrolleybus{
private DrawingTrolleybus _drawingTrolleybus;
private JFrame frameTrolleybus;
private JPanel panelTrolleybus;
private JButton buttonCreate, buttonUp, buttonDown, buttonRight, buttonLeft;
public FormTrolleybus(){
//Само окно
frameTrolleybus = new JFrame();
frameTrolleybus.setLayout(new BorderLayout());
frameTrolleybus.setSize(900, 500);
frameTrolleybus.setTitle("Троллейбус");
frameTrolleybus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Панель, на которую добавляю всё
panelTrolleybus = new JPanel();
panelTrolleybus.setLayout(null);
//Кнопка создания
buttonCreate = new JButton("Создать");
//Кнопка вверх
buttonUp = new JButton();
buttonUp.setIcon(new ImageIcon("Up.png"));
buttonUp.setToolTipText("buttonUp");
//Кнопка вниз
buttonDown = new JButton();
buttonDown.setIcon(new ImageIcon("Down.png"));
buttonDown.setToolTipText("buttonDown");
//Кнопка вправо
buttonRight = new JButton();
buttonRight.setIcon(new ImageIcon("Right.png"));
buttonRight.setToolTipText("buttonRight");
//Кнопка влево
buttonLeft = new JButton();
buttonLeft.setIcon(new ImageIcon("Left.png"));
buttonLeft.setToolTipText("buttonLeft");
//Размеры, позиция кнопок
buttonCreate.setBounds(10,400,90,30);
buttonUp.setBounds(800,380,30,30);
buttonDown.setBounds(800,420,30,30);
buttonLeft.setBounds(760,420,30,30);
buttonRight.setBounds(840,420,30,30);
//Добавление листенеров к кнопкам
buttonCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonCreate_Click(e);
}
});
buttonUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonMove_Click(buttonUp, e);
}
});
buttonDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonMove_Click(buttonDown, e);
}
});
buttonRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonMove_Click(buttonRight, e);
}
});
buttonLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonMove_Click(buttonLeft, e);
}
});
panelTrolleybus.add(buttonCreate);
panelTrolleybus.add(buttonUp);
panelTrolleybus.add(buttonDown);
panelTrolleybus.add(buttonLeft);
panelTrolleybus.add(buttonRight);
frameTrolleybus.add(panelTrolleybus, BorderLayout.CENTER);
frameTrolleybus.setVisible(true);
}
// Метод прорисовки троллейбуса
private void Draw(){
if (_drawingTrolleybus == null) {
return;
}
Graphics g = panelTrolleybus.getGraphics();
// Очистка перед перерисовкой
panelTrolleybus.paint(g);
_drawingTrolleybus.DrawTransport(g);
}
private void ButtonCreate_Click(ActionEvent e) {
Random random = new Random();
_drawingTrolleybus = new DrawingTrolleybus();
_drawingTrolleybus.Init(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)),
random.nextBoolean(),
random.nextBoolean(),
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
_drawingTrolleybus.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw();
}
protected void ButtonMove_Click(Object sender, ActionEvent e) {
if (_drawingTrolleybus == null)
{
return;
}
JButton info = (JButton)e.getSource();
String name = info.getToolTipText();
switch (name)
{
case "buttonUp":
_drawingTrolleybus.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawingTrolleybus.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawingTrolleybus.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawingTrolleybus.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
}

7
Trolleybus/Main.java Normal file
View File

@ -0,0 +1,7 @@
package Trolleybus;
public class Main {
public static void main(String[] args) {
new FormTrolleybus();
}
}

BIN
Up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 549 B