Готовая 1 усложненная лабораторная

This commit is contained in:
Даниил Путинцев 2023-10-24 20:55:26 +04:00
parent e645bddcd6
commit 3901e3e00e
11 changed files with 300 additions and 1 deletions

BIN
Resources/ArrowDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Resources/ArrowLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Resources/ArrowRight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
Resources/ArrowUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

6
src/DirectionType.java Normal file
View File

@ -0,0 +1,6 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

83
src/DrawingRoadTrain.java Normal file
View File

@ -0,0 +1,83 @@
import java.awt.*;
public class DrawingRoadTrain {
private EntityRoadTrain entityRoadTrain;
private DrawingWheels drawingWheels;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int _trainWidth = 90;
private final int _trainHeight = 70;
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean waterContainer, boolean sweepingBrush, int width, int height, int wheelnumber){
if (width < _trainWidth || height <_trainHeight){
return false;
}
_pictureWidth = width;
_pictureHeight = height;
entityRoadTrain = new EntityRoadTrain();
entityRoadTrain.Init(speed, weight, bodyColor, additionalColor, waterContainer, sweepingBrush);
drawingWheels = new DrawingWheels();
drawingWheels.SetWheelNumber(wheelnumber);
return true;
}
public void SetPosition(int x, int y){
if (x < 0 || x + _trainWidth > _pictureWidth){
x = 200;
}
if (y < 0 || y + _trainHeight > _pictureHeight){
y = 200;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction){
if (entityRoadTrain == null){
return;
}
switch (direction){
case Left:
if (_startPosX - entityRoadTrain.GetStep() > 0){
_startPosX -= (int)entityRoadTrain.GetStep();
}
break;
case Up:
if (_startPosY - entityRoadTrain.GetStep() > 0){
_startPosY -= (int)entityRoadTrain.GetStep();
}
break;
case Right:
if (_startPosX + entityRoadTrain.GetStep() + _trainWidth < _pictureWidth){
_startPosX += (int)entityRoadTrain.GetStep();
}
break;
case Down:
if (_startPosY + entityRoadTrain.GetStep() + _trainHeight < _pictureHeight){
_startPosY += (int)entityRoadTrain.GetStep();
}
break;
}
}
public void DrawTransport(Graphics g){
if (entityRoadTrain == null){
return;
}
if (entityRoadTrain.GetWaterContainer()){
g.setColor(entityRoadTrain.GetAdditionalColor());
g.drawOval(_startPosX + 30, _startPosY, 10, 20);
g.fillOval(_startPosX + 30, _startPosY, 10, 20);
}
if (entityRoadTrain.GetSweepingBrush())
{
g.drawLine(_startPosX + 30, _startPosY + 10, _startPosX + 20, _startPosY + 10);
g.drawLine(_startPosX + 20, _startPosY + 10, _startPosX + 10, _startPosY + 30);
g.drawLine(_startPosX + 17, _startPosY + 30, _startPosX + 3, _startPosY + 30);
}
drawingWheels.drawWheels(g,entityRoadTrain.GetBodyColor(), _startPosX, _startPosY);
g.setColor(entityRoadTrain.GetBodyColor());
g.drawLine(_startPosX + 20, _startPosY + 20, _startPosX + 70, _startPosY + 20);
g.drawRect( _startPosX + 60, _startPosY, 10, 20);
g.fillRect(_startPosX + 60, _startPosY, 10, 20);
}
}

59
src/DrawingWheels.java Normal file
View File

@ -0,0 +1,59 @@
import java.awt.*;
public class DrawingWheels {
private WheelNumber wheelNumber;
public void SetWheelNumber(int num){
switch (num){
case 1:
wheelNumber = WheelNumber.One;
break;
case 2:
wheelNumber = WheelNumber.Two;
break;
default:
wheelNumber = WheelNumber.Three;
}
};
public void drawWheels(Graphics g, Color color, int startPosX, int startPosY) {
switch (wheelNumber) {
case One:
drawOneWheel(g, color, startPosX, startPosY);
break;
case Two:
drawTwoWheels(g, color, startPosX, startPosY);
break;
case Three:
drawThreeWheels(g, color, startPosX, startPosY);
break;
}
}
private void drawOneWheel(Graphics g, Color color, int startPosX, int startPosY) {
g.setColor(color);
g.drawOval(startPosX + 20, startPosY + 20, 10, 10);
g.fillOval(startPosX + 20, startPosY + 20, 10, 10);
}
private void drawTwoWheels(Graphics g, Color color, int startPosX, int startPosY) {
g.setColor(color);
g.drawOval(startPosX + 20, startPosY + 20, 10, 10);
g.drawOval( startPosX + 30, startPosY + 20, 10, 10);
g.fillOval(startPosX + 20, startPosY + 20, 10, 10);
g.fillOval(startPosX + 30, startPosY + 20, 10, 10);
}
private void drawThreeWheels(Graphics g, Color color, int startPosX, int startPosY) {
g.setColor(color);
g.drawOval(startPosX + 20, startPosY + 20, 10, 10);
g.drawOval( startPosX + 30, startPosY + 20, 10, 10);
g.drawOval(startPosX + 60, startPosY + 20, 10, 10);
g.fillOval(startPosX + 20, startPosY + 20, 10, 10);
g.fillOval(startPosX + 30, startPosY + 20, 10, 10);
g.fillOval(startPosX + 60, startPosY + 20, 10, 10);
}
}

34
src/EntityRoadTrain.java Normal file
View File

@ -0,0 +1,34 @@
import java.awt.*;
public class EntityRoadTrain {
private int Speed;
private double Weight;
private Color BodyColor;
public Color GetBodyColor(){
return BodyColor;
}
private Color AdditionalColor;
public Color GetAdditionalColor() { return AdditionalColor; }
private boolean WaterContainer;
public boolean GetWaterContainer(){
return WaterContainer;
}
private boolean SweepingBrush;
public boolean GetSweepingBrush(){
return SweepingBrush;
}
public double Step;
public double GetStep() {
return (double)Speed * 100 / Weight;
}
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean waterContainer, boolean sweepingBrush)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
WaterContainer = waterContainer;
SweepingBrush = sweepingBrush;
}
}

View File

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

112
src/RoadTrainForm.java Normal file
View File

@ -0,0 +1,112 @@
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class RoadTrainForm{
private DrawingRoadTrain _drawingRoadTrain;
Canvas canv;
public void Draw(){
canv.repaint();
}
public RoadTrainForm(){
JFrame frame = new JFrame("RoadTrain");
JButton buttonCreate = new JButton("Создать");
buttonCreate.setFocusPainted(false);
buttonCreate.setContentAreaFilled(false);
JButton buttonUp = new JButton();
buttonUp.setFocusPainted(false);
buttonUp.setContentAreaFilled(false);
buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon(((new ImageIcon("Resources/ArrowUp.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonDown = new JButton();
buttonDown.setFocusPainted(false);
buttonDown.setContentAreaFilled(false);
buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon(((new ImageIcon("Resources/ArrowDown.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonLeft = new JButton();
buttonLeft.setFocusPainted(false);
buttonLeft.setContentAreaFilled(false);
buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon(((new ImageIcon("Resources/ArrowLeft.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonRight = new JButton();
buttonRight.setFocusPainted(false);
buttonRight.setContentAreaFilled(false);
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon(((new ImageIcon("Resources/ArrowRight.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
buttonCreate.addActionListener(
e -> {
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingRoadTrain = new DrawingRoadTrain();
_drawingRoadTrain.Init(
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(), this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3));
_drawingRoadTrain.SetPosition(random.nextInt(100), random.nextInt(90));
canv._drawingRoadTrain = _drawingRoadTrain;
Draw();
}
);
ActionListener actionListener = e -> {
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingRoadTrain == null){
return;
}
switch ((((JButton)(e.getSource())).getName())){
case "up":
_drawingRoadTrain.MoveTransport(DirectionType.Up);
break;
case "down":
_drawingRoadTrain.MoveTransport(DirectionType.Down);
break;
case "left":
_drawingRoadTrain.MoveTransport(DirectionType.Left);
break;
case "right":
_drawingRoadTrain.MoveTransport(DirectionType.Right);
break;
}
Draw();
};
buttonUp.addActionListener(actionListener);
buttonDown.addActionListener(actionListener);
buttonLeft.addActionListener(actionListener);
buttonRight.addActionListener(actionListener);
frame.setSize(900, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 900, 500);
buttonCreate.setBounds(20, 420, 100, 40);
buttonUp.setBounds(800, 380, 40, 40);
buttonDown.setBounds(800, 420, 40, 40);
buttonLeft.setBounds(760, 420, 40, 40);
buttonRight.setBounds(840, 420, 40, 40);
frame.add(canv);
frame.add(buttonCreate);
frame.add(buttonUp);
frame.add(buttonDown);
frame.add(buttonLeft);
frame.add(buttonRight);
frame.setVisible(true);
}
class Canvas extends JComponent{
public DrawingRoadTrain _drawingRoadTrain;
public Canvas(){}
public void paintComponent(Graphics g){
if (_drawingRoadTrain == null){
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
_drawingRoadTrain.DrawTransport(g2d);
super.repaint();
}
}
}

5
src/WheelNumber.java Normal file
View File

@ -0,0 +1,5 @@
public enum WheelNumber {
One,
Two,
Three
}