diff --git a/AccordionBus/src/AccordionBus.form b/AccordionBus/src/AccordionBus.form new file mode 100644 index 0000000..cdce57d --- /dev/null +++ b/AccordionBus/src/AccordionBus.form @@ -0,0 +1,66 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/AccordionBus/src/AccordionBus.java b/AccordionBus/src/AccordionBus.java new file mode 100644 index 0000000..275c886 --- /dev/null +++ b/AccordionBus/src/AccordionBus.java @@ -0,0 +1,82 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class AccordionBus extends JFrame { + private JPanel panelAccordionBus; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + private JButton buttonUp; + private JButton buttonCreate; + + private DrawningAccordionBus drawningAccordionBus; + + public void paint(Graphics g){ + super.paint(g); + if (drawningAccordionBus == null) return; + drawningAccordionBus.drawTransport(g); + } + + public AccordionBus(){ + add(panelAccordionBus); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + setBounds(300, 150, 1200, 800); + setResizable(false); + Graphics g = getGraphics().create(); + Icon iconDown = new ImageIcon("AccordionBus\\src\\Resources\\buttDown.png"); + Icon iconUp = new ImageIcon("AccordionBus\\src\\Resources\\buttUp.png"); + Icon iconLeft = new ImageIcon("AccordionBus\\src\\Resources\\buttLeft.png"); + Icon iconRight = new ImageIcon("AccordionBus\\src\\Resources\\buttRight.png"); + buttonDown.setIcon(iconDown); + buttonLeft.setIcon(iconLeft); + buttonUp.setIcon(iconUp); + buttonRight.setIcon(iconRight); + drawningAccordionBus = new DrawningAccordionBus(); + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + + drawningAccordionBus.Init(rnd.nextInt(90) + 10, rnd.nextInt(990) + 10, + new Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255)), + new Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255)), + rnd.nextBoolean(), rnd.nextBoolean(), rnd.nextBoolean(), EnumerateDoors.getRandomValue()); + drawningAccordionBus.setPictureSize(panelAccordionBus.getWidth(), panelAccordionBus.getHeight()); + drawningAccordionBus.setPosition(rnd.nextInt(100) + 50, rnd.nextInt(100) + 50); + paint(g); + } + }); + + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (drawningAccordionBus.moveTransport(DirectionType.Right)) paint(g); + } + }); + + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (drawningAccordionBus.moveTransport(DirectionType.Left)) paint(g); + } + }); + + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (drawningAccordionBus.moveTransport(DirectionType.Up)) paint(g); + } + }); + + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (drawningAccordionBus.moveTransport(DirectionType.Down)) paint(g); + } + }); + } +} diff --git a/AccordionBus/src/DirectionType.java b/AccordionBus/src/DirectionType.java new file mode 100644 index 0000000..1afad5c --- /dev/null +++ b/AccordionBus/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right; +} \ No newline at end of file diff --git a/AccordionBus/src/DrawningAccordionBus.java b/AccordionBus/src/DrawningAccordionBus.java new file mode 100644 index 0000000..9a4e7f8 --- /dev/null +++ b/AccordionBus/src/DrawningAccordionBus.java @@ -0,0 +1,217 @@ +import java.awt.*; + +public class DrawningAccordionBus { + private EntityAccordionBus entityAccordionBus; + + public EntityAccordionBus getEntityAccordionBus(){return entityAccordionBus;} + + private Integer pictureWidth; + + private Integer pictureHeight; + + private Integer startPosX; + + private Integer startPosY; + + private final int drawingBusWidth = 130; + + private final int drawingBusHeight = 35; + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, + boolean hatch, boolean fourWheels, boolean additionalLine, EnumerateDoors enumerateDoors){ + entityAccordionBus = new EntityAccordionBus(); + entityAccordionBus.Init(speed, weight, bodyColor, additionalColor, + hatch, fourWheels, additionalLine, enumerateDoors); + pictureHeight = null; + pictureWidth = null; + startPosX = null; + startPosY = null; + } + + public boolean setPictureSize(int width, int height){ + if (drawingBusHeight > height || drawingBusWidth > width) { + return false; + } + if (startPosY != null && startPosX != null) { + if (startPosX + drawingBusWidth > width) { + startPosX = width - drawingBusWidth; + } + if (startPosY + drawingBusHeight > height) { + startPosY = height - drawingBusHeight; + } + } + pictureWidth = width; + pictureHeight = height; + return true; + } + + public void setPosition(int x, int y) { + if (pictureHeight == null || pictureWidth == null) { + return; + } + if (x + drawingBusWidth > pictureWidth || x < 0) { + startPosX = pictureWidth - drawingBusWidth; + } else { + startPosX = x; + } + if (y + drawingBusHeight > pictureHeight || y < 0) { + startPosY = pictureHeight - drawingBusHeight; + } else { + startPosY = y; + } + } + + public boolean moveTransport(DirectionType direction){ + if (entityAccordionBus == null || startPosX == null || startPosY == null) return false; + + int heightCap = 30; + double step = entityAccordionBus.getStep(); + switch (direction){ + case Left: + if (startPosX - step > 0) startPosX -= (int)step; + else startPosX = 0; + return true; + case Right: + if (startPosX + drawingBusWidth + step < pictureWidth - heightCap) startPosX += (int)step; + else startPosX = pictureWidth - drawingBusWidth; + return true; + case Up: + if (startPosY - step > heightCap) startPosY -= (int)step; + else startPosY = heightCap; + return true; + case Down: + if (startPosY + drawingBusHeight < pictureHeight) startPosY += (int)step; + else startPosY = pictureHeight - drawingBusHeight; + return true; + default: + return false; + } + } + + public void drawTransport(Graphics g){ + if (entityAccordionBus == null || startPosY == null || startPosX == null) return; + + //корпус + g.setColor(entityAccordionBus.getBodyColor()); + g.fillRect(startPosX.intValue(), startPosY.intValue() + 10, 60, 15); + g.fillRect(startPosX.intValue() + 70, startPosY.intValue() + 10, 60, 15); + g.setColor(Color.black); + g.drawRect(startPosX.intValue() + 70, startPosY.intValue() + 10, 60, 15); + g.drawRect(startPosX.intValue(), startPosY.intValue() + 10, 60, 15); + + //доп. линия + if (entityAccordionBus.getAdditionalLine()){ + g.setColor(entityAccordionBus.getAdditionalColor()); + g.fillRect(startPosX, startPosY + 20, 60, 3); + g.fillRect(startPosX + 70, startPosY + 20, 60, 3); + g.setColor(Color.black); + g.drawRect(startPosX, startPosY + 20, 60, 3); + g.drawRect(startPosX + 70, startPosY + 20, 60, 3); + } + + //стёкла + g.setColor(Color.cyan); + g.fillRect(startPosX + 2, startPosY + 13, 5, 5); + g.fillRect(startPosX + 12, startPosY + 13, 5, 5); + g.fillRect(startPosX + 32, startPosY + 13, 5, 5); + g.fillRect(startPosX + 42, startPosY + 13, 5, 5); + + g.fillRect(startPosX + 72, startPosY + 13, 5, 5); + g.fillRect(startPosX + 82, startPosY + 13, 5, 5); + g.fillRect(startPosX + 92, startPosY + 13, 5, 5); + g.fillRect(startPosX + 102, startPosY + 13, 5, 5); + g.fillRect(startPosX + 112, startPosY + 13, 5, 5); + + g.setColor(Color.black); + g.drawRect(startPosX + 2, startPosY + 13, 5, 5); + g.drawRect(startPosX + 12, startPosY + 13, 5, 5); + g.drawRect(startPosX + 32, startPosY + 13, 5, 5); + g.drawRect(startPosX + 42, startPosY + 13, 5, 5); + + g.drawRect(startPosX + 72, startPosY + 13, 5, 5); + g.drawRect(startPosX + 82, startPosY + 13, 5, 5); + g.drawRect(startPosX + 92, startPosY + 13, 5, 5); + g.drawRect(startPosX + 102, startPosY + 13, 5, 5); + g.drawRect(startPosX + 112, startPosY + 13, 5, 5); + + //гормошка + g.drawLine(startPosX + 60, startPosY + 10, startPosX + 62, startPosY + 13); + g.drawLine(startPosX + 62, startPosY + 13, startPosX + 65, startPosY + 10); + g.drawLine(startPosX + 65, startPosY + 10, startPosX + 67, startPosY + 13); + g.drawLine(startPosX + 67, startPosY + 13, startPosX + 70, startPosY + 10); + g.drawLine(startPosX + 60, startPosY + 25, startPosX + 62, startPosY + 22); + g.drawLine(startPosX + 62, startPosY + 22, startPosX + 65, startPosY + 25); + g.drawLine(startPosX + 65, startPosY + 25, startPosX + 67, startPosY + 22); + g.drawLine(startPosX + 67, startPosY + 22, startPosX + 70, startPosY + 25); + g.drawLine(startPosX + 62, startPosY + 13, startPosX + 62, startPosY + 22); + g.drawLine(startPosX + 67, startPosY + 13, startPosX + 67, startPosY + 22); + g.drawLine(startPosX + 65, startPosY + 10, startPosX + 65, startPosY + 25); + + //колёса + g.setColor(Color.white); + g.fillOval(startPosX + 5, startPosY + 20, 10, 10); + g.fillOval(startPosX + 40, startPosY + 20, 10, 10); + + g.fillOval(startPosX + 110, startPosY + 20, 10, 10); + if (entityAccordionBus.getFourWheels()) + g.fillOval(startPosX + 75, startPosY + 20, 10, 10); + + g.setColor(Color.black); + g.drawOval(startPosX + 5, startPosY + 20, 10, 10); + g.drawOval(startPosX + 40, startPosY + 20, 10, 10); + + g.drawOval(startPosX + 110, startPosY + 20, 10, 10); + if (entityAccordionBus.getFourWheels()) + g.drawOval(startPosX + 75, startPosY + 20, 10, 10); + + //люки + if (entityAccordionBus.getHatch()){ + g.setColor(entityAccordionBus.getAdditionalColor()); + g.fillRect(startPosX + 10, startPosY + 7, 10, 3); + g.fillRect(startPosX + 90, startPosY + 7, 10, 3); + g.setColor(Color.black); + g.drawRect(startPosX + 10, startPosY + 7, 10, 3); + g.drawRect(startPosX + 90, startPosY + 7, 10, 3); + } + + //двери + switch(entityAccordionBus.getEnumerateDoors()){ + case threeDoors: + g.setColor(entityAccordionBus.getAdditionalColor()); + g.fillRect(startPosX + 20, startPosY + 15, 5, 10); + g.fillRect(startPosX + 123, startPosY + 15, 5, 10); + g.fillRect(startPosX + 87, startPosY + 15, 5, 10); + g.setColor(Color.black); + g.drawRect(startPosX + 20, startPosY + 15, 5, 10); + g.drawRect(startPosX + 123, startPosY + 15, 5, 10); + g.drawRect(startPosX + 87, startPosY + 15, 5, 10); + break; + case fourDoors: + g.setColor(entityAccordionBus.getAdditionalColor()); + g.fillRect(startPosX + 20, startPosY + 15, 5, 10); + g.fillRect(startPosX + 123, startPosY + 15, 5, 10); + g.fillRect(startPosX + 87, startPosY + 15, 5, 10); + g.fillRect(startPosX + 53, startPosY + 15, 5, 10); + g.setColor(Color.black); + g.drawRect(startPosX + 20, startPosY + 15, 5, 10); + g.drawRect(startPosX + 123, startPosY + 15, 5, 10); + g.drawRect(startPosX + 87, startPosY + 15, 5, 10); + g.drawRect(startPosX + 53, startPosY + 15, 5, 10); + break; + case fiveDoors: + g.setColor(entityAccordionBus.getAdditionalColor()); + g.fillRect(startPosX + 20, startPosY + 15, 5, 10); + g.fillRect(startPosX + 123, startPosY + 15, 5, 10); + g.fillRect(startPosX + 87, startPosY + 15, 5, 10); + g.fillRect(startPosX + 53, startPosY + 15, 5, 10); + g.fillRect(startPosX + 27, startPosY + 19, 11, 5); + g.setColor(Color.black); + g.drawRect(startPosX + 20, startPosY + 15, 5, 10); + g.drawRect(startPosX + 123, startPosY + 15, 5, 10); + g.drawRect(startPosX + 87, startPosY + 15, 5, 10); + g.drawRect(startPosX + 53, startPosY + 15, 5, 10); + g.drawRect(startPosX + 27, startPosY + 19, 11, 5); + break; + } + } +} \ No newline at end of file diff --git a/AccordionBus/src/EntityAccordionBus.java b/AccordionBus/src/EntityAccordionBus.java new file mode 100644 index 0000000..1630a08 --- /dev/null +++ b/AccordionBus/src/EntityAccordionBus.java @@ -0,0 +1,52 @@ +import java.awt.*; + +public class EntityAccordionBus { + private int speed; + + public int getSpeed(){return speed;} + + private double weight; + + public double getWeight(){return weight;} + + private double step; + + public double getStep(){return step;} + + private Color bodyColor; + + public Color getBodyColor(){return bodyColor;} + + private Color additionalColor; + + public Color getAdditionalColor(){return additionalColor;} + + private boolean hatch; + + public boolean getHatch(){return hatch;} + + private boolean fourWheels; + + public boolean getFourWheels(){return fourWheels;} + + private boolean additionalLine; + + public boolean getAdditionalLine(){return additionalLine;} + + private EnumerateDoors enumerateDoors; + + public EnumerateDoors getEnumerateDoors(){return enumerateDoors;} + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, + boolean hatch, boolean fourWheels, boolean additionalLine, EnumerateDoors enumerateDoors){ + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + this.additionalColor = additionalColor; + this.hatch = hatch; + this.fourWheels = fourWheels; + this.additionalLine = additionalLine; + this.enumerateDoors = enumerateDoors; + step = speed * 100 / weight; + } +} \ No newline at end of file diff --git a/AccordionBus/src/EnumerateDoors.java b/AccordionBus/src/EnumerateDoors.java new file mode 100644 index 0000000..d0e2218 --- /dev/null +++ b/AccordionBus/src/EnumerateDoors.java @@ -0,0 +1,16 @@ +import java.util.Random; + +public enum EnumerateDoors { + threeDoors, + fourDoors, + fiveDoors; + + private static Random rnd = new Random(); + + public static EnumerateDoors getRandomValue(){ + int rez = rnd.nextInt(3); + if (rez == 0) return EnumerateDoors.threeDoors; + else if (rez == 1) return EnumerateDoors.fourDoors; + return EnumerateDoors.fiveDoors; + } +} \ No newline at end of file diff --git a/AccordionBus/src/Main.java b/AccordionBus/src/Main.java index 1d70cc0..64e9660 100644 --- a/AccordionBus/src/Main.java +++ b/AccordionBus/src/Main.java @@ -1,6 +1,6 @@ public class Main { public static void main(String[] args) { - + AccordionBus form = new AccordionBus(); } } \ No newline at end of file diff --git a/AccordionBus/src/Resources/buttDown.png b/AccordionBus/src/Resources/buttDown.png new file mode 100644 index 0000000..5b5eb7c Binary files /dev/null and b/AccordionBus/src/Resources/buttDown.png differ diff --git a/AccordionBus/src/Resources/buttLeft.png b/AccordionBus/src/Resources/buttLeft.png new file mode 100644 index 0000000..fe44c5c Binary files /dev/null and b/AccordionBus/src/Resources/buttLeft.png differ diff --git a/AccordionBus/src/Resources/buttRIght.png b/AccordionBus/src/Resources/buttRIght.png new file mode 100644 index 0000000..60d656d Binary files /dev/null and b/AccordionBus/src/Resources/buttRIght.png differ diff --git a/AccordionBus/src/Resources/buttUp.png b/AccordionBus/src/Resources/buttUp.png new file mode 100644 index 0000000..4f1a857 Binary files /dev/null and b/AccordionBus/src/Resources/buttUp.png differ