Compare commits
No commits in common. "labWork1" and "main" have entirely different histories.
Binary file not shown.
Before Width: | Height: | Size: 537 B |
Binary file not shown.
Before Width: | Height: | Size: 502 B |
Binary file not shown.
Before Width: | Height: | Size: 497 B |
Binary file not shown.
Before Width: | Height: | Size: 538 B |
@ -1,7 +0,0 @@
|
||||
package DrawningObjects;
|
||||
|
||||
public enum CountEnum {
|
||||
Min,
|
||||
Mid,
|
||||
Max
|
||||
}
|
@ -1,140 +0,0 @@
|
||||
package DrawningObjects;
|
||||
|
||||
import Entities.*;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningElectricLocomotive {
|
||||
public EntityElectricLocomotive _entityElectricLocomotive;
|
||||
private DrawningWheels drawningWheels;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private final int _locomotiveWidth = 120;
|
||||
private final int _locomotiveHeight = 70;
|
||||
|
||||
public boolean Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, int countWheels, boolean horns, boolean battery, int width, int height) {
|
||||
if (width < _locomotiveWidth || height < _locomotiveHeight) {
|
||||
return false;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_entityElectricLocomotive = new EntityElectricLocomotive();
|
||||
_entityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor,
|
||||
horns, battery);
|
||||
drawningWheels = new DrawningWheels();
|
||||
drawningWheels.setCount(countWheels);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y) {
|
||||
x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth);
|
||||
y = Math.min(Math.max(0, y), _pictureHeight - _locomotiveHeight);
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction) {
|
||||
if (_entityElectricLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
switch (direction) {
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - _entityElectricLocomotive.Step > 0) {
|
||||
_startPosX -= (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - _entityElectricLocomotive.Step > 0) {
|
||||
_startPosY -= (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + _locomotiveWidth + _entityElectricLocomotive.Step < _pictureWidth) {
|
||||
_startPosX += (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _locomotiveHeight + _entityElectricLocomotive.Step < _pictureHeight) {
|
||||
_startPosY += (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g) {
|
||||
if (_entityElectricLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
g.clearRect(0, 0, _pictureWidth, _pictureHeight);
|
||||
// корпус электровоза
|
||||
g.setColor(_entityElectricLocomotive.BodyColor);
|
||||
|
||||
int[] posX = {
|
||||
_startPosX,
|
||||
_startPosX + 10,
|
||||
_startPosX + 120,
|
||||
_startPosX + 120,
|
||||
_startPosX
|
||||
};
|
||||
|
||||
int[] posY = {
|
||||
_startPosY + 40,
|
||||
_startPosY + 20,
|
||||
_startPosY + 20,
|
||||
_startPosY + 60,
|
||||
_startPosY + 60
|
||||
};
|
||||
|
||||
g.fillPolygon(posX, posY, 5);
|
||||
g.drawLine(_startPosX, _startPosY + 40,
|
||||
_startPosX + 120, _startPosY + 40);
|
||||
|
||||
// окна
|
||||
g.setColor(Color.blue);
|
||||
g.fillRect(_startPosX + 12, _startPosY + 24, 30, 11);
|
||||
g.fillRect(_startPosX + 55, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 75, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 95, _startPosY + 24, 11, 11);
|
||||
|
||||
// колёса
|
||||
drawningWheels.DrawWheels(g, _startPosX, _startPosY);
|
||||
|
||||
// рога
|
||||
if (_entityElectricLocomotive.Horns) {
|
||||
g.setColor(Color.black);
|
||||
posX = new int[]{
|
||||
_startPosX + 50,
|
||||
_startPosX + 40,
|
||||
_startPosX + 50,
|
||||
_startPosX + 60
|
||||
};
|
||||
|
||||
posY = new int[]{
|
||||
_startPosY + 20,
|
||||
_startPosY + 10,
|
||||
_startPosY,
|
||||
_startPosY + 10
|
||||
};
|
||||
g.drawPolygon(posX, posY, 4);
|
||||
}
|
||||
// отсек для батарей
|
||||
if (_entityElectricLocomotive.Battery) {
|
||||
g.setColor(_entityElectricLocomotive.AdditionalColor);
|
||||
g.fillRect(_startPosX + 80, _startPosY + 45, 35, 9);
|
||||
}
|
||||
}
|
||||
public void SetPictureSize(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package DrawningObjects;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWheels {
|
||||
private CountEnum countWhils;
|
||||
|
||||
public void setCount(int count){
|
||||
count = Math.min(4, count);
|
||||
count = Math.max(2, count);
|
||||
switch (count) {
|
||||
case 2:
|
||||
countWhils = CountEnum.Min;
|
||||
return;
|
||||
case 3:
|
||||
countWhils = CountEnum.Mid;
|
||||
return;
|
||||
case 4:
|
||||
countWhils = CountEnum.Max;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void DrawWheels(Graphics g, int posX, int posY){
|
||||
g.setColor(Color.black);
|
||||
int count = 0;
|
||||
switch (countWhils){
|
||||
case Min -> {
|
||||
count = 2;
|
||||
posX += 15;
|
||||
}
|
||||
case Mid -> {
|
||||
count = 3;
|
||||
posX += 5;
|
||||
}
|
||||
case Max -> {
|
||||
count = 4;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < count; ++i){
|
||||
g.fillOval(posX + (i * 15), posY + 55, 15, 15);
|
||||
g.fillOval(posX + 60 + (i * 15), posY + 55, 15, 15);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package Entities;
|
||||
|
||||
import java.awt.*;
|
||||
import java.lang.FunctionalInterface;
|
||||
|
||||
public class EntityElectricLocomotive {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public Color AdditionalColor;
|
||||
public boolean Horns;
|
||||
public boolean Battery;
|
||||
public double Step;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean horns, boolean battery)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Horns = horns;
|
||||
Battery = battery;
|
||||
Step = Speed * 100.0f / Weight;
|
||||
}
|
||||
|
||||
}
|
@ -1,150 +0,0 @@
|
||||
package Forms;
|
||||
|
||||
import DrawningObjects.CountEnum;
|
||||
import DrawningObjects.DrawningElectricLocomotive;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormElectricLocomotive extends JFrame {
|
||||
private DrawningElectricLocomotive _drawningElectricLocomotive;
|
||||
private Canvas canvas;
|
||||
private JButton buttonCreate;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JTextField numberField;
|
||||
public void Draw() {
|
||||
if (_drawningElectricLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_drawningElectricLocomotive.DrawTransport(canvas.getGraphics());
|
||||
}
|
||||
private void InitializeComponent(){
|
||||
buttonCreate = new JButton("Создать");
|
||||
|
||||
buttonUp = new JButton();
|
||||
buttonUp.setBorderPainted(false);
|
||||
buttonUp.setFocusPainted(false);
|
||||
buttonUp.setContentAreaFilled(false);
|
||||
buttonUp.setName("up");
|
||||
buttonUp.setIcon(new ImageIcon("Resources/arrowUp.png"));
|
||||
buttonUp.setSize(40, 40);
|
||||
|
||||
buttonRight = new JButton();
|
||||
buttonRight.setBorderPainted(false);
|
||||
buttonRight.setFocusPainted(false);
|
||||
buttonRight.setContentAreaFilled(false);
|
||||
buttonRight.setName("right");
|
||||
buttonRight.setIcon(new ImageIcon("Resources/arrowRight.png"));
|
||||
buttonRight.setSize(40, 40);
|
||||
|
||||
buttonDown = new JButton();
|
||||
buttonDown.setBorderPainted(false);
|
||||
buttonDown.setFocusPainted(false);
|
||||
buttonDown.setContentAreaFilled(false);
|
||||
buttonDown.setName("down");
|
||||
buttonDown.setIcon(new ImageIcon("Resources/arrowDown.png"));
|
||||
buttonDown.setSize(40, 40);
|
||||
|
||||
buttonLeft = new JButton();
|
||||
buttonLeft.setBorderPainted(false);
|
||||
buttonLeft.setFocusPainted(false);
|
||||
buttonLeft.setContentAreaFilled(false);
|
||||
buttonLeft.setName("left");
|
||||
buttonLeft.setIcon(new ImageIcon("Resources/arrowLeft.png"));
|
||||
buttonLeft.setSize(40, 40);
|
||||
|
||||
numberField = new JTextField();
|
||||
|
||||
setSize(1000, 600);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLayout(null);
|
||||
|
||||
canvas = new Canvas();
|
||||
canvas.setBounds(0, 0, 1000, 600);
|
||||
buttonCreate.setBounds(10, 520, 120, 40);
|
||||
buttonUp.setBounds(50, 430, 40, 40);
|
||||
buttonDown.setBounds(50, 470, 40, 40);
|
||||
buttonRight.setBounds(90, 470, 40, 40);
|
||||
buttonLeft.setBounds(10, 470, 40, 40);
|
||||
numberField.setBounds(150, 520, 40, 40);
|
||||
|
||||
|
||||
add(buttonCreate);
|
||||
add(buttonUp);
|
||||
add(buttonDown);
|
||||
add(buttonRight);
|
||||
add(buttonLeft);
|
||||
add(numberField);
|
||||
add(canvas);
|
||||
}
|
||||
private void InitializeLogic(){
|
||||
buttonCreate.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
int countWheels;
|
||||
try {
|
||||
countWheels = Integer.parseInt(numberField.getText());
|
||||
}
|
||||
catch (Exception ex){
|
||||
countWheels = 0;
|
||||
}
|
||||
System.out.println(e.getActionCommand());
|
||||
Random random = new Random();
|
||||
_drawningElectricLocomotive = new DrawningElectricLocomotive();
|
||||
_drawningElectricLocomotive.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)),
|
||||
countWheels,
|
||||
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
|
||||
1000, 560);
|
||||
_drawningElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
System.out.println(((JButton)(e.getSource())).getName());
|
||||
if (_drawningElectricLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch(((JButton)(e.getSource())).getName()){
|
||||
case "up":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "down":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "left":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "right":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
};
|
||||
|
||||
buttonUp.addActionListener(actionListener);
|
||||
buttonDown.addActionListener(actionListener);
|
||||
buttonLeft.addActionListener(actionListener);
|
||||
buttonRight.addActionListener(actionListener);
|
||||
|
||||
}
|
||||
public FormElectricLocomotive(){
|
||||
InitializeComponent();
|
||||
InitializeLogic();
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
import Forms.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FormElectricLocomotive form = new FormElectricLocomotive();
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
Loading…
Reference in New Issue
Block a user