Compare commits

..

2 Commits

Author SHA1 Message Date
d51aa123df Initial commit 2023-12-15 20:04:24 +03:00
53b492ccc7 Initial commit 2023-12-15 20:03:51 +03:00
18 changed files with 472 additions and 1 deletions

14
.gitignore vendored
View File

@ -1,4 +1,3 @@
# ---> JetBrains
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
@ -77,3 +76,16 @@ fabric.properties
# Android studio 3.1+ serialized cache file # Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser .idea/caches/build_file_checksums.ser
# ---> VisualStudioCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

9
.idea/gitjava.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/gitjava.iml" filepath="$PROJECT_DIR$/.idea/gitjava.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

11
DoubleDeckerBus.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

BIN
images/KeyDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 398 B

BIN
images/KeyLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B

BIN
images/KeyRight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 385 B

BIN
images/KeyUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 B

View File

@ -0,0 +1,144 @@
package Drawnings;
import java.awt.*;
import Entities.EntityBus;
import MovementStrategy.*;
public class DrawingBus {
public EntityBus entityBus;
public DrawingDoors _door;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _busWidth = 190;
private int _busHeight = 115;
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isSecondFloor, boolean isStairs, int countDoors, int width, int height) {
if (width < _busHeight || height < _busWidth)
return;
_pictureWidth = width;
_pictureHeight = height;
entityBus = new EntityBus();
entityBus.Init(speed, weight, bodyColor, additionalColor, isSecondFloor, isStairs);
_door = new DrawingDoors();
_door.SetCount(countDoors);
}
public void SetPosition (int x, int y) {
if (x + _busWidth > _pictureWidth || y + _busHeight > _pictureHeight) {
_startPosX = _pictureWidth - _busWidth;
_startPosY = _pictureHeight - _busHeight;
}
else
{
_startPosX = x;
_startPosY = y;
}
}
public void MoveTransport(Direction direction){
if (entityBus == null) {
return;
}
switch (direction) {
case Left:
if (_startPosX - entityBus.Step > 0)
{
_startPosX -= entityBus.Step;
}
break;
case Right:
if (_startPosX + _busWidth + entityBus.Step < _pictureWidth)
{
_startPosX += entityBus.Step;
}
break;
case Up:
if (_startPosY - entityBus.Step > 0)
{
_startPosY -= entityBus.Step;
}
break;
case Down:
if (_startPosY + _busHeight + entityBus.Step < _pictureHeight)
{
_startPosY += entityBus.Step;
}
break;
}
}
public void DrawTransport(Graphics2D g) {
if (entityBus == null) {
return;
}
//тело
g.setColor(entityBus.getBodyColor());
g.fillRect(_startPosX + 147, _startPosY + 52, 21, 20);
g.fillRect(_startPosX + 147, _startPosY + 71, 30, 27);
g.fillRect(_startPosX + 10, _startPosY + 52, 137, 46);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 147, _startPosY + 52, 21, 20);
g.drawRect(_startPosX + 147, _startPosY + 71, 30, 27);
g.drawRect(_startPosX + 10, _startPosY + 52, 137, 46);
g.setColor(Color.blue);
g.fillRect(_startPosX + 150, _startPosY + 55, 15, 15);
g.fillRect(_startPosX + 42, _startPosY + 55, 15, 15);
g.fillRect(_startPosX + 69, _startPosY + 55, 15, 15);
g.fillRect(_startPosX + 96, _startPosY + 55, 15, 15);
g.fillRect(_startPosX + 123, _startPosY + 55, 15, 15);
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 30, _startPosY + 52, _startPosX + 30, _startPosY + 98);
g.drawLine(_startPosX + 35, _startPosY + 52, _startPosX + 35, _startPosY + 98);
//колёса
g.fillOval(_startPosX + 23, _startPosY + 88, 25, 25);
g.fillOval(_startPosX + 123, _startPosY + 88, 25, 25);
g.setColor(Color.gray);
g.fillOval(_startPosX + 25, _startPosY + 90, 21, 21);
g.fillOval(_startPosX + 125, _startPosY + 90, 21, 21);
// двери
_door.Draw(g, _startPosX, _startPosY);
// второй этаж
if (entityBus.IsSecondFloor())
{
g.setColor(entityBus.getAdditionalColor());
g.fillRect(_startPosX + 7, _startPosY + 12, 172, 40); //большой прямоугольник
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 7, _startPosY + 12, 172, 40);
g.drawLine(_startPosX + 7, _startPosY + 36, _startPosX + 178, _startPosY + 36);
g.setColor(Color.blue);
g.fillRect(_startPosX + 15, _startPosY + 15, 15, 15);
g.fillRect(_startPosX + 42, _startPosY + 15, 15, 15);
g.fillRect(_startPosX + 69, _startPosY + 15, 15, 15);
g.fillRect(_startPosX + 96, _startPosY + 15, 15, 15);
g.fillRect(_startPosX + 123, _startPosY + 15, 15, 15);
g.fillRect(_startPosX + 150, _startPosY + 15, 15, 15);
}
// лестница
if (entityBus.IsStairs())
{
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 10, _startPosY + 55, _startPosX + 34, _startPosY + 55);
g.drawLine(_startPosX + 10, _startPosY + 58, _startPosX + 34, _startPosY + 58);
g.drawLine(_startPosX + 10, _startPosY + 64, _startPosX + 34, _startPosY + 64);
g.drawLine(_startPosX + 10, _startPosY + 72, _startPosX + 34, _startPosY + 72);
g.drawLine(_startPosX + 10, _startPosY + 80, _startPosX + 34, _startPosY + 80);
g.drawLine(_startPosX + 10, _startPosY + 88, _startPosX + 34, _startPosY + 88);
g.drawLine(_startPosX + 10, _startPosY + 94, _startPosX + 34, _startPosY + 94);
}
}
}

View File

@ -0,0 +1,56 @@
package Drawnings;
import java.awt.*;
import Entities.*;
public class DrawingDoors {
private CountDoors _door;
private int Count;
public CountDoors getCount()
{
return _door;
}
public void SetCount (int count) {
Count = count;
switch (Count) {
case 3:
_door = CountDoors.Three;
break;
case 4:
_door = CountDoors.Four;
break;
case 5:
_door = CountDoors.Five;
break;
default:
_door = CountDoors.Three;
break;
}
}
public void Draw (Graphics2D g, int _startPosX, int _startPosY) {
g.setColor(Color.BLACK);
if (_door == CountDoors.Three) {
g.setColor(Color.gray);
g.fillRect(_startPosX + 40, _startPosY + 75, 12, 20);
g.fillRect(_startPosX + 60, _startPosY + 75, 12, 20);
g.fillRect(_startPosX + 80, _startPosY + 75, 12, 20);
}
if (_door == CountDoors.Four) {
g.setColor(Color.gray);
g.fillRect(_startPosX + 40, _startPosY + 75, 12, 20);
g.fillRect(_startPosX + 60, _startPosY + 75, 12, 20);
g.fillRect(_startPosX + 80, _startPosY + 75, 12, 20);
g.fillRect(_startPosX + 100, _startPosY + 75, 12, 20);
}
if (_door == CountDoors.Five){
g.setColor(Color.gray);
g.fillRect(_startPosX + 40, _startPosY + 75, 12, 20);
g.fillRect(_startPosX + 60, _startPosY + 75, 12, 20);
g.fillRect(_startPosX + 80, _startPosY + 75, 12, 20);
g.fillRect(_startPosX + 100, _startPosY + 75, 12, 20);
g.fillRect(_startPosX + 120, _startPosY + 75, 12, 20);
}
}
}

View File

@ -0,0 +1,6 @@
package Entities;
public enum CountDoors {
Three,
Four,
Five;
}

View File

@ -0,0 +1,47 @@
package Entities;
import java.awt.*;
public class EntityBus {
private int Speed;
private float Weight;
private Color BodyColor;
private Color AdditionalColor;
private boolean IsSecondFloor;
private boolean IsStairs;
public int Step;
public int getSpeed() {
return Speed;
}
public float getWeight() {
return Weight;
}
public Color getBodyColor() {
return BodyColor;
}
public Color getAdditionalColor() {
return AdditionalColor;
}
public boolean IsSecondFloor() {
return IsSecondFloor;
}
public boolean IsStairs() {
return IsStairs;
}
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isSecondFloor, boolean isStairs)
{
Weight = weight;
Speed = speed;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
IsSecondFloor = isSecondFloor;
IsStairs = isStairs;
Step = Speed * 100 / (int) Weight;
}
}

151
src/FormBus.java Normal file
View File

@ -0,0 +1,151 @@
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import Drawnings.*;
import MovementStrategy.*;
public class FormBus extends JFrame {
private DrawingBus _drawingBus;
private Canvas canvas = new Canvas();
JLabel labelCount = new JLabel("Введите число дверей:");
private JTextField fieldCount = new JTextField();
private JButton buttonCreate;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonLeft;
public FormBus() {
super("Создание автобуса");
InitializeComponent();
setVisible(true);
}
private void InitializeComponent()
{
buttonCreate = new JButton("Создать автобус");
buttonUp = new JButton();
buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon("images/KeyUp.png"));
buttonUp.setSize(48, 44);
buttonRight = new JButton();
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon("images/KeyRight.png"));
buttonRight.setSize(48, 44);
buttonLeft = new JButton();
buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon("images/KeyLeft.png"));
buttonLeft.setSize(48, 44);
buttonDown = new JButton();
buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon("images/KeyDown.png"));
buttonDown.setSize(48, 44);
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
buttonCreate.setBounds(12, 375, 170, 44);
buttonUp.setBounds(679, 363, 48, 44);
buttonRight.setBounds( 728, 408, 48, 44);
buttonLeft.setBounds(630, 408, 48, 44);
buttonDown.setBounds( 679, 408, 48, 44);
labelCount.setBounds(12, 435, 240, 20);
fieldCount.setBounds(160, 437, 48, 20);
canvas.setBounds(0,0,800, 460);
add(buttonCreate);
add(buttonUp);
add(buttonRight);
add(buttonDown);
add(buttonLeft);
add(labelCount);
add(fieldCount);
add(canvas);
buttonCreate.addActionListener(buttonCreateListener);
buttonUp.addActionListener(buttonsMoveListener);
buttonRight.addActionListener(buttonsMoveListener);
buttonDown.addActionListener(buttonsMoveListener);
buttonLeft.addActionListener(buttonsMoveListener);
}
ActionListener buttonCreateListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int countDoors;
try
{
countDoors = Integer.parseInt(fieldCount.getText());
}
catch (Exception ex)
{
countDoors = 0;
}
if (countDoors != 3 && countDoors != 4 && countDoors != 5)
{
JOptionPane.showMessageDialog(null, "Число должно быть равно 3, 4 или 5.\nКол-во дверей приравнено к 3");
countDoors = 3;
}
Random rand = new Random();
_drawingBus = new DrawingBus();
_drawingBus.Init(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
rand.nextBoolean(), rand.nextBoolean(),
countDoors,
canvas.getWidth(), canvas.getHeight());
_drawingBus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
canvas.repaint();
}
};
ActionListener buttonsMoveListener = new ActionListener() {
// реакция на нажатие
public void actionPerformed(ActionEvent e) {
if (_drawingBus == null)
{
return;
}
String command = ((JButton)(e.getSource())).getName();
switch (command) {
case "up":
_drawingBus.MoveTransport(Direction.Up);
break;
case "down":
_drawingBus.MoveTransport(Direction.Down);
break;
case "right":
_drawingBus.MoveTransport(Direction.Right);
break;
case "left":
_drawingBus.MoveTransport(Direction.Left);
break;
}
canvas.repaint();
}
};
class Canvas extends JComponent{
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingBus == null){
return;
}
super.paintComponents (g);
Graphics2D g2d = (Graphics2D)g;
_drawingBus.DrawTransport(g2d);
super.repaint();
}
}
}

5
src/Main.java Normal file
View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
new FormBus();
}
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public enum Direction {
Up,
Down,
Left,
Right;
}