Лабораторная работа №1

This commit is contained in:
Роман Пермяков 2024-04-22 14:04:18 +04:00
parent a9c0be751d
commit 268582bd6d
11 changed files with 440 additions and 1 deletions

View File

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="AccordionBus">
<grid id="27dc6" binding="panelAccordionBus" layout-manager="GridLayoutManager" row-count="3" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="844" height="581"/>
</constraints>
<properties>
<background color="-4671565"/>
</properties>
<border type="none"/>
<children>
<vspacer id="5c758">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="5e5b0" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="d87c2" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="4f6e6" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<hspacer id="d4bfb">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="5339d" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="80d6f" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<hideActionText value="false"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -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);
}
});
}
}

View File

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

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 662 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 684 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 689 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 684 B