PIbd-22. Smirnov A.A. Lab work 01 #1

Closed
spacyboy wants to merge 2 commits from laba_1 into main
16 changed files with 335 additions and 4 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

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$" isTestSource="false" />
</content>
<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_20" project-jdk-name="openjdk-21" 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/PIbd-22_Smirnov_A_A_RoadTrain_HARD.iml" filepath="$PROJECT_DIR$/.idea/PIbd-22_Smirnov_A_A_RoadTrain_HARD.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>

6
Direction.java Normal file
View File

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

87
DrawingRoadTrain.java Normal file
View File

@ -0,0 +1,87 @@
import java.awt.*;
public class DrawingRoadTrain {
private WheelDrawing wheelDrawing;
public EntityRoadTrain EntityRoadTrain;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _roadTrainWidth = 200;
private int _roadTrainHeight = 100;
public boolean Init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean wheel, boolean door, boolean light, int numWheel, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureHeight < _roadTrainHeight || _pictureWidth < _roadTrainWidth)
return false;
EntityRoadTrain = new EntityRoadTrain();
EntityRoadTrain.Init(speed, weight, bodyColor, additionalColor,wheel, door, light, numWheel);
wheelDrawing = new WheelDrawing();
wheelDrawing.setNumWheel(numWheel);
return true;
}
public void SetPosition(int x, int y)
{
_startPosX = Math.min(x, _pictureWidth-_roadTrainWidth);
_startPosY = Math.min(y, _pictureHeight- _roadTrainHeight);
}
public void MoveTransport(Direction direction)
{
if (EntityRoadTrain == null){
return;
}
switch (direction)
{
case Left:
if (_startPosX - EntityRoadTrain.Step > 0)
{
_startPosX -= (int) EntityRoadTrain.Step;
}
break;
case Up:
if (_startPosY - EntityRoadTrain.Step > 0)
{
_startPosY -= (int) EntityRoadTrain.Step;
}
break;
case Right:
if (_startPosX + _roadTrainWidth + EntityRoadTrain.Step < _pictureWidth)
{
_startPosX += (int) EntityRoadTrain.Step;
}
break;
case Down:
if (_startPosY + _roadTrainHeight + EntityRoadTrain.Step < _pictureHeight)
{
_startPosY += (int) EntityRoadTrain.Step;
}
break;
}
}
public void DrawTransport(Graphics2D g) {
if (EntityRoadTrain == null) {
return;
}
// машина
g.drawRect(_startPosX, _startPosY + 50, 160, 20); // кузов
g.drawRect(_startPosX + 120, _startPosY + 10, 40, 40); // кабина
g.drawRect(_startPosX + 10, _startPosY, 90, 50); // бак с водой
g.drawRect(_startPosX + 130, _startPosY + 20, 30, 20); // окно
g.drawLine(_startPosX + 160, _startPosY + 70, _startPosX + 180, _startPosY + 80); // держатель для щетки
g.drawRect(_startPosX + 170, _startPosY + 80, 40, 10); // щетка
// обвесы
g.setColor(EntityRoadTrain.AdditionalColor);
wheelDrawing.Draw(_startPosX, _startPosY, EntityRoadTrain.AdditionalColor, g);
if (EntityRoadTrain.Door) {
g.fillRect(_startPosX + 40, _startPosY + 20, 20, 30); // дверь
}
if (EntityRoadTrain.Light) {
g.fillRect(_startPosX + 135, _startPosY, 10, 10); // мигалка
}
}
}

26
EntityRoadTrain.java Normal file
View File

@ -0,0 +1,26 @@
import java.awt.*;
public class EntityRoadTrain {
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean Wheel;
public boolean Door;
public boolean Light;
public double Step;
public int numWheel;
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean wheel, boolean door, boolean light, int numwheel)
{
numWheel = numwheel;
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Wheel = wheel;
Door = door;
Light = light;
Step = (double)Speed * 100 / Weight;
}
}

128
FormRoadTrain.java Normal file
View File

@ -0,0 +1,128 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormRoadTrain {
private DrawingRoadTrain _drawingRoadTrain;
Canvas canv;
public void Draw(){
canv.repaint();
}
public FormRoadTrain(){
JFrame w=new JFrame ("RoadTrain");
JButton buttonCreate = new JButton("Create");
buttonCreate.setBorderPainted(false);
buttonCreate.setContentAreaFilled(false);
buttonCreate.setFont(new Font("Arial", Font.PLAIN, 20));
JButton up = new JButton();
up.setBorderPainted(false);
up.setContentAreaFilled(false);
up.setName("up");
Image img_up = new ImageIcon("images/up.png").getImage();
Image newImg_up = img_up.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
up.setIcon(new ImageIcon(newImg_up));
JButton down = new JButton();
down.setBorderPainted(false);
down.setContentAreaFilled(false);
down.setName("down");
Image img_down = new ImageIcon("images/down.png").getImage();
Image newImg_down = img_down.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
down.setIcon(new ImageIcon(newImg_down));
JButton left = new JButton();
left.setBorderPainted(false);
left.setContentAreaFilled(false);
left.setName("left");
Image img_left = new ImageIcon("images/left.png").getImage();
Image newImg_left = img_left.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
left.setIcon(new ImageIcon(newImg_left));
JButton right = new JButton();
right.setBorderPainted(false);
right.setContentAreaFilled(false);
right.setName("right");
Image img_right = new ImageIcon("images/right.png").getImage();
Image newImg_right = img_right.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
right.setIcon(new ImageIcon(newImg_right));
buttonCreate.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Random random = new Random();
_drawingRoadTrain = new DrawingRoadTrain();
_drawingRoadTrain.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.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,random.nextInt(0, 2) == 1,
random.nextInt(1, 4), 1000, 560);
_drawingRoadTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingRoadTrain = _drawingRoadTrain;
Draw();
}
}
);
ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e){
if (_drawingRoadTrain == null)
{
return;
}
switch(((JButton)(e.getSource())).getName()){
case "up":
_drawingRoadTrain.MoveTransport(Direction.Up);
break;
case "down":
_drawingRoadTrain.MoveTransport(Direction.Down);
break;
case "left":
_drawingRoadTrain.MoveTransport(Direction.Left);
break;
case "right":
_drawingRoadTrain.MoveTransport(Direction.Right);
break;
}
Draw();
}
};
up.addActionListener(actioListener);
down.addActionListener(actioListener);
left.addActionListener(actioListener);
right.addActionListener(actioListener);
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 1000, 600);
buttonCreate.setBounds(30, 500, 100, 30);
up.setBounds(880, 450, 20, 20);
down.setBounds(880, 510, 20, 20);
left.setBounds(850, 480, 20, 20);
right.setBounds(910, 480, 20, 20);
w.add(canv);
w.add(buttonCreate);
w.add(up);
w.add(down);
w.add(left);
w.add(right);
w.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();
}
}

View File

@ -1,7 +1,4 @@
public class Main
{
public static void main(String[] args) {
System.out.println("test");
}
public static void main(String[] args) { new FormRoadTrain(); }
}

6
NumWheel.java Normal file
View File

@ -0,0 +1,6 @@
public enum NumWheel
{
oneWheel,
twoWheel,
threeWheel
}

42
WheelDrawing.java Normal file
View File

@ -0,0 +1,42 @@
import java.awt.*;
public class WheelDrawing {
private NumWheel numWheel;
public NumWheel getSomeProperty() {
return numWheel;
}
public void setNumWheel(int kWheel){
switch(kWheel){
case 1:
numWheel = NumWheel.oneWheel;
break;
case 2:
numWheel = NumWheel.twoWheel;
break;
case 3:
numWheel = NumWheel.threeWheel;
break;
default:
numWheel = NumWheel.oneWheel;
System.out.println("Ошибка! Количество " + kWheel);
break;
}
}
void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){
g.setColor(color);
switch (numWheel) {
case oneWheel -> {
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
}
case twoWheel -> {
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
Review

Имеется дублирующийся код

Имеется дублирующийся код
g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30);
}
case threeWheel -> {
g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30);
g.drawOval(_startPosX + 55, _startPosY + 70, 30, 30);
g.drawOval(_startPosX + 15, _startPosY + 70, 30, 30);
}
};
}
}

BIN
images/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

BIN
images/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
images/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

BIN
images/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB