PIbd-22. Stroev V.M. Lab Work 01 #2

Closed
StroevVladimir wants to merge 4 commits from Lab1 into main
10 changed files with 522 additions and 0 deletions

25
CountWheels.java Normal file
View File

@ -0,0 +1,25 @@
public enum CountWheels{
Min(2),
Mid(3),
Max(4);
private final int val;
private CountWheels(int val){
this.val = val;
}
public int getCountWheels(){
return val;
}
public static CountWheels fromNumberToEnum(int number) {
try{
for (CountWheels countWheels : CountWheels.values()) {
if (countWheels.getCountWheels() == number) {
return countWheels;
}
}
}catch(NumberFormatException e){
}
return Min;
}
}

11
Direction.java Normal file
View File

@ -0,0 +1,11 @@
enum Direction{
UP(1),
DOWN(2),
LEFT(3),
RIGHT(4);
private final int val;
private Direction(int val){
this.val = val;
}
}

55
DrawingWheels.java Normal file
View File

@ -0,0 +1,55 @@
import java.awt.Color;
import java.awt.Graphics;
public class DrawingWheels{
Graphics g;
Color color;
private CountWheels _countWheels;
public void setNumWheels(int numRollers) {
if (numRollers < 4 || numRollers > 6 ){
_countWheels = CountWheels.fromNumberToEnum(numRollers);
return;
}
_countWheels = CountWheels.fromNumberToEnum(numRollers);
}
public DrawingWheels(Color color, int countWheels){
Review

Про конструктор упоминаний в задании не было

Про конструктор упоминаний в задании не было
this.color = color;
setNumWheels(countWheels);
}
public void Draw(Graphics g, int _startPosX, int _startPosY){
switch(_countWheels){
case Min:
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.drawOval(_startPosX + 110, _startPosY + 35, 15, 15);
g.setColor(color);
g.fillOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 110, _startPosY + 35, 15, 15);
break;
case Mid:
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 20, _startPosY + 35, 15, 15);
Review

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

Имеется дублирующийся код
g.drawOval(_startPosX + 65, _startPosY + 35, 15, 15);
g.drawOval(_startPosX + 110, _startPosY + 35, 15, 15);
g.setColor(color);
g.fillOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 65, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 110, _startPosY + 35, 15, 15);
break;
case Max:
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 20, _startPosY + 35, 15, 15);
Review

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

Имеется дублирующийся код
g.drawOval(_startPosX + 50, _startPosY + 35, 15, 15);
g.drawOval( _startPosX + 80, _startPosY + 35, 15, 15);
g.drawOval(_startPosX + 110, _startPosY + 35, 15, 15);
g.setColor(color);
g.fillOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 50, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 80, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 110, _startPosY + 35, 15, 15);
break;
}
}
}

184
DrawningMonorail.java Normal file
View File

@ -0,0 +1,184 @@
import java.awt.*;
import javax.swing.JPanel;
import java.util.Random;
public class DrawningMonorail extends JPanel {
private Entity entity;
private int pictureWidth;
private int pictureHeight;
private int _countWheels;
private int _startPosX;
private int _startPosY ;
private int relWidth = 150;
private final int relHeight = 46;
Random rd = new Random();
private DrawingWheels _drawingWheels;
public boolean init(int wheelCount ,int speed, double weight, Color bodyColor, Color additionalColor,
boolean monorails, boolean secondCabin, int width, int height) {
if (0 + relWidth >= width || 0 + relHeight >= height) {
return false;
}
_countWheels = wheelCount;
pictureWidth = width;
pictureHeight = height;
entity = new Entity();
entity.init(wheelCount, speed, weight, bodyColor, additionalColor, monorails, secondCabin);
_drawingWheels = new DrawingWheels(entity.getBodyColor(), _countWheels);
return true;
}
public void setPosition(int x, int y) {
_startPosX = x;
_startPosY = y;
}
public void moveTransport(Direction dr) {
if (entity == null) {
return;
}
switch (dr) {
case LEFT:
if (_startPosX - entity.getStep() > 0) {
_startPosX -= entity.getStep();
}
break;
case RIGHT:
if (_startPosX + entity.getStep() < pictureWidth - relWidth) {
_startPosX += entity.getStep();
}
break;
case UP:
if (_startPosY - entity.getStep() > 0) {
_startPosY -= entity.getStep();
}
break;
case DOWN:
if (_startPosY + entity.getStep() < pictureHeight - relHeight) {
_startPosY += entity.getStep();
}
break;
}
}
public void drawMonorail(Graphics g) {
if (entity == null) {
return;
}
relWidth = 150;
//Колёса
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 22, _startPosY + 36, 40, 8);
g.drawRect(_startPosX+82, _startPosY + 36, 40, 8);
g.drawOval(_startPosX + 3, _startPosY + 37, 30, 8);
g.drawOval(_startPosX + 110, _startPosY + 37, 29, 8);
g.fillRect(_startPosX + 22, _startPosY + 36, 40, 8);
g.fillRect(_startPosX + 82, _startPosY + 36, 40, 8);
g.fillOval( _startPosX + 3, _startPosY + 37, 30, 8);
g.fillOval( _startPosX + 110, _startPosY + 37, 29, 8);
_drawingWheels.Draw(g, _startPosX, _startPosY);
//Кабина
g.setColor(entity.getBodyColor());
g.fillRect(_startPosX + 10, _startPosY + 20, 120, 16);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 10, _startPosY + 20, 120, 16);
int[] xp1 = new int[]{_startPosX+10,_startPosX+130,_startPosX+130,_startPosX+13};
int[] yp1 = new int[]{_startPosY + 20,_startPosY + 20,_startPosY + 5,_startPosY + 5};
Polygon pol1 = new Polygon(xp1, yp1, yp1.length);
g.setColor(entity.getBodyColor());
g.fillPolygon(pol1);
g.setColor(Color.BLACK);
g.drawPolygon(pol1);
//Дверь
g.setColor(Color.WHITE);
g.fillRect(_startPosX + 49, _startPosY + 9, 12, 22);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 49, _startPosY + 9, 12, 22);
//Окна и прочее
g.setColor(Color.CYAN);
g.drawRect(_startPosX + 20, _startPosY + 8, 10, 10);
g.drawRect(_startPosX + 35, _startPosY + 8, 10, 10);
g.drawRect(_startPosX + 117, _startPosY + 8, 10, 10);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 130, _startPosY + 10, 2, 22);
g.fillRect(_startPosX + 130, _startPosY + 10, 2, 22);
//Магнитная рельса
if(entity.getMonorails())
{
g.drawRect(_startPosX, _startPosY + 50, 140, 10);
g.fillRect(_startPosX, _startPosY + 50, 140, 10);
g.setColor(Color.WHITE);
g.fillRect(_startPosX + 5, _startPosY + 53, 130, 5);
}
if (entity.getSecondCabin())
{
relWidth = 290;
//низ
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 162, _startPosY + 36, 40, 8);
g.drawRect(_startPosX + 222, _startPosY + 36, 40, 8);
g.drawOval(_startPosX + 143, _startPosY + 37, 30, 8);
g.drawOval(_startPosX + 250, _startPosY + 37, 29, 8);
g.setColor(Color.BLACK);
g.fillRect(_startPosX + 162, _startPosY + 36, 40, 8);
g.fillRect(_startPosX + 222, _startPosY + 36, 40, 8);
g.fillOval(_startPosX + 143, _startPosY + 37, 30, 8);
g.fillOval(_startPosX + 250, _startPosY + 37, 29, 8);
_drawingWheels.Draw(g, _startPosX + 140, _startPosY);
if (entity.getMonorails())
{
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 140, _startPosY + 50, 145, 10);
g.fillRect(_startPosX + 140, _startPosY + 50, 145, 10);
g.setColor(Color.WHITE);
g.fillRect(_startPosX + 135, _startPosY + 53, 145, 5);
}
//Кабина
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 150, _startPosY + 20, 120, 16);
g.fillRect(_startPosX + 150, _startPosY + 20, 120, 16);
int[] xp2 = new int[]{_startPosX + 150,_startPosX + 270,_startPosX + 267,_startPosX + 150};
int[] yp2 = new int[] {_startPosY + 20, _startPosY + 20, _startPosY + 5, _startPosY + 6};
Polygon pol2 = new Polygon(xp2, yp2, yp2.length);
g.setColor(entity.getAdditionalColor());
g.fillPolygon(pol2);
g.setColor(Color.BLACK);
g.drawPolygon(pol2);
//дверь
g.setColor(Color.WHITE);
g.fillRect(_startPosX + 189, _startPosY + 9, 12, 22);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 189, _startPosY + 9, 12, 22);
//Окна и прочее
g.setColor(Color.CYAN);
g.drawRect(_startPosX + 160, _startPosY + 8, 10, 10);
g.drawRect(_startPosX + 175, _startPosY + 8, 10, 10);
g.drawRect(_startPosX + 257, _startPosY + 8, 10, 10);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 270, _startPosY + 10, 2, 22);
g.fillRect(_startPosX + 270, _startPosY + 10, 2, 22);
}
}
}

79
Entity.java Normal file
View File

@ -0,0 +1,79 @@
import java.awt.*;
public class Entity {
private int speed;
private int wheelCount;
private double weight;
private Color bodyColor;
private Color additionalColor;
private boolean monorails;
private boolean secondCabin;
public double getStep() {
return (double) speed * 130 / weight;
}
public void init(int wheelCount, int speed, double weight, Color bodyColor, Color additionalColor, boolean monorails, boolean secondCabin) {
setSpeed(speed);
setWeight(weight);
setBodyColor(bodyColor);
setAdditionalColor(additionalColor);
setMonorails(monorails);
setSecondCabin(secondCabin);
setWheelCount(wheelCount);
}
public int getSpeed() {
return speed;
}
private void setSpeed(int speed) {
this.speed = speed;
}
public double getWeight() {
return weight;
}
private void setWeight(double weight) {
this.weight = weight;
}
public Color getBodyColor() {
return bodyColor;
}
private void setBodyColor(Color bodyColor) {
this.bodyColor = bodyColor;
}
public Color getAdditionalColor() {
return additionalColor;
}
private void setAdditionalColor(Color additionalColor) {
this.additionalColor = additionalColor;
}
public boolean getMonorails() {
return monorails;
}
private void setMonorails(boolean monorails) {
this.monorails = monorails;
}
public boolean getSecondCabin() {
return secondCabin;
}
private void setSecondCabin(boolean secondCabin) {
this.secondCabin = secondCabin;
}
public int getWheelCount(){
return wheelCount;
}
private void setWheelCount(int wheelCount){
this.wheelCount = wheelCount;
}
}

168
MonoFrame.java Normal file
View File

@ -0,0 +1,168 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.util.Random;
class Form extends JFrame{
private JLabel pictureBoxMonorail;
private JPanel mainPanel;
private JButton btUp;
private JButton btDown;
private JButton btRight;
private JButton btLeft;
private JButton btCreate;
private JLabel wheelsLabel;
private JTextField wheelsTextField;
Random rd = new Random();
private DrawningMonorail _drawningMonorail;
public Form(){
initComponents();
}
private void Draw(){
if(_drawningMonorail == null){
return;
}
BufferedImage bmp = new BufferedImage(pictureBoxMonorail.getWidth(),
pictureBoxMonorail.getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
_drawningMonorail.drawMonorail(gr);
ImageIcon imageIcon = new ImageIcon(bmp);
pictureBoxMonorail.setIcon(imageIcon);
}
private void initComponents(){
mainPanel = new JPanel();
btCreate = new JButton("Создать");
btUp = new JButton(new ImageIcon("resources/upper-arrow.png"));
btDown = new JButton(new ImageIcon("resources/down-arrow.png"));
btLeft = new JButton(new ImageIcon("resources/left-arrow.png"));
btRight = new JButton(new ImageIcon("resources/right-arrow.png"));
pictureBoxMonorail = new JLabel();
wheelsLabel = new JLabel("Кол-во колёс: ");
wheelsTextField = new JTextField();
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(null);
pictureBoxMonorail.setBounds(0, 0, 882, 453);
mainPanel.add(pictureBoxMonorail);
wheelsLabel.setBounds(22, 410, 150, 29);
mainPanel.add(wheelsLabel);
wheelsTextField.setBounds(150, 410, 80, 29);
mainPanel.add(wheelsTextField);
btCreate.setBounds(250, 410, 100, 29);
mainPanel.add(btCreate);
btCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonCreateActionPerformed(e);
}
});
btUp.setBounds(790, 355, 40, 40);
mainPanel.add(btUp);
ImageIcon iconUp = new ImageIcon("resources/upper-arrow.png");
btUp.setIcon(iconUp);
btUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btUp, e);
}
});
btDown.setBounds(790, 401, 40, 40);
mainPanel.add(btDown);
ImageIcon iconDown = new ImageIcon("resources/down-arrow.png");
btDown.setIcon(iconDown);
btDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btDown, e);
}
});
btLeft.setBounds(744, 401, 40, 40);
mainPanel.add(btLeft);
ImageIcon iconLeft = new ImageIcon("resources/left-arrow.png");
btLeft.setIcon(iconLeft);
btLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btLeft, e);
}
});
btRight.setBounds(836, 401, 40, 40);
mainPanel.add(btRight);
ImageIcon iconRight = new ImageIcon("resources/right-arrow.png");
btRight.setIcon(iconRight);
btRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btRight, e);
}
});
btUp.setName("buttonUp");
btDown.setName("buttonDown");
btLeft.setName("buttonLeft");
btRight.setName("buttonRight");
setPreferredSize(new Dimension(900, 500));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
pack();
}
protected void buttonCreateActionPerformed(ActionEvent e) {
int wheelCount;
try {
wheelCount = Integer.parseInt(wheelsTextField.getText());
} catch (NumberFormatException ex) {
wheelCount = 0;
}
if (wheelCount > 0) {
Random random = new Random();
_drawningMonorail = new DrawningMonorail();
_drawningMonorail.init(wheelCount, random.nextInt(500,800),
random.nextInt(1000,3000),
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(),
pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight());
_drawningMonorail.setPosition(random.nextInt(90) + 10,
random.nextInt(90) + 10); Draw();
}
}
protected void buttonMoveActionPerformed(Object sender, ActionEvent e) {
if (_drawningMonorail == null) {
return;
}
String name = (sender instanceof JButton) ? ((JButton) sender).getName() : "";
switch (name) {
case "buttonUp":
_drawningMonorail.moveTransport(Direction.UP);
break;
case "buttonDown":
_drawningMonorail.moveTransport(Direction.DOWN);
break;
case "buttonLeft":
_drawningMonorail.moveTransport(Direction.LEFT);
break;
case "buttonRight":
_drawningMonorail.moveTransport(Direction.RIGHT);
break;
}
Draw();
}
}
class MonoFrame{
public static void main(String[] args) {
Form fr = new Form();
}
}

BIN
resources/down-arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 281 B

BIN
resources/left-arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 265 B

BIN
resources/right-arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 B

BIN
resources/upper-arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 B