diff --git a/CountWheels.java b/CountWheels.java new file mode 100644 index 0000000..c6803ae --- /dev/null +++ b/CountWheels.java @@ -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; + } +} \ No newline at end of file diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..a196269 --- /dev/null +++ b/Direction.java @@ -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; + } +} \ No newline at end of file diff --git a/DrawingWheels.java b/DrawingWheels.java new file mode 100644 index 0000000..a10f3fb --- /dev/null +++ b/DrawingWheels.java @@ -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){ + 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); + 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); + 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; + } + } +} \ No newline at end of file diff --git a/DrawningMonorail.java b/DrawningMonorail.java new file mode 100644 index 0000000..10181c3 --- /dev/null +++ b/DrawningMonorail.java @@ -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); + } + } +} \ No newline at end of file diff --git a/Entity.java b/Entity.java new file mode 100644 index 0000000..cf90138 --- /dev/null +++ b/Entity.java @@ -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; + } +} \ No newline at end of file diff --git a/MonoFrame.java b/MonoFrame.java new file mode 100644 index 0000000..abd8f6c --- /dev/null +++ b/MonoFrame.java @@ -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(); + } +} \ No newline at end of file diff --git a/resources/down-arrow.png b/resources/down-arrow.png new file mode 100644 index 0000000..9d67143 Binary files /dev/null and b/resources/down-arrow.png differ diff --git a/resources/left-arrow.png b/resources/left-arrow.png new file mode 100644 index 0000000..046470d Binary files /dev/null and b/resources/left-arrow.png differ diff --git a/resources/right-arrow.png b/resources/right-arrow.png new file mode 100644 index 0000000..fac4c96 Binary files /dev/null and b/resources/right-arrow.png differ diff --git a/resources/upper-arrow.png b/resources/upper-arrow.png new file mode 100644 index 0000000..0bc323d Binary files /dev/null and b/resources/upper-arrow.png differ