Merge pull request 'Borschevskaya A.A. Lab Work 1 Hard' (#1) from lab1 into master

Reviewed-on: http://student.git.athene.tech/pgirl1/PIbd-23_Borschevskaya_A.A._ArmoredCar_Hard/pulls/1
This commit is contained in:
Евгений Эгов 2022-10-28 09:19:14 +04:00
commit 3b5af9bbb1
12 changed files with 417 additions and 4 deletions

View File

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

View File

@ -0,0 +1,119 @@
import java.awt.*;
import java.util.Random;
public class DrawingArmoredCar {
private EntityArmoredCar armoredCar;
private float startPosX;
private float startPosY;
private int pictureWidth;
private int pictureHeight;
private static final int carWidth = 80;
private static final int carHeight = 50;
private DrawingCaterpillar drawingCaterpillar;
public EntityArmoredCar getArmoredCar() {
return armoredCar;
}
public void Init(int speed, float weight, Color bodyColor) {
this.armoredCar = new EntityArmoredCar();
this.armoredCar.init(speed, weight, bodyColor);
Random r = new Random();
this.drawingCaterpillar = new DrawingCaterpillar();
this.drawingCaterpillar.Init(r.nextInt(4, 7), bodyColor);
}
public void SetPosition(int x, int y, int width, int height) {
if (x >= 0 && y >= 0 && x + carWidth < width && y + carHeight < height) {
startPosX = x;
startPosY = y;
pictureWidth = width;
pictureHeight = height;
}
}
public void MoveTransport(Direction direction) {
if (pictureWidth < 1|| pictureHeight < 1)
{
return;
}
switch (direction)
{
// вправо
case Right:
if (startPosX + carWidth + armoredCar.step < pictureWidth)
{
startPosX += armoredCar.step;
}
break;
//влево
case Left:
if (startPosX - armoredCar.step > 0)
{
startPosX -= armoredCar.step;
}
break;
//вверх
case Up:
if (startPosY - armoredCar.step > 0)
{
startPosY -= armoredCar.step;
}
break;
//вниз
case Down:
if (startPosY + carHeight + armoredCar.step < pictureHeight)
{
startPosY += armoredCar.step;
}
break;
}
}
public void DrawTransport(Graphics2D g2d)
{
if (startPosX < 0 || startPosY < 0
|| pictureHeight < 1 || pictureWidth < 1)
{
return;
}
// отрисовка корпуса и гусеницы
g2d.setPaint(armoredCar.getBodyColor());
g2d.fillRect((int ) startPosX + 20, (int) startPosY, 40, 20);
g2d.setPaint(Color.LIGHT_GRAY);
g2d.fillRect((int ) startPosX, (int ) startPosY + 20, 80, 20);
g2d.fillOval((int ) startPosX, (int ) startPosY + 30, 20, 20);
g2d.fillOval((int ) startPosX + 80 - 20, (int ) startPosY + 30, 20, 20);
g2d.fillRect((int ) startPosX + 15, (int ) startPosY + 20, 60, 30);
// отрисовка катков в гусенице
drawingCaterpillar.DrawCaterpillar(g2d, (int)startPosX, (int)startPosY);
}
public void ChangeBorders(int width, int height)
{
pictureWidth = width;
pictureHeight = height;
if (pictureWidth <= carWidth || pictureHeight <= carHeight)
{
pictureWidth = 0;
pictureHeight = 0;
return;
}
if (startPosX + carWidth > pictureWidth)
{
startPosX = pictureWidth - carWidth;
}
if (startPosY + carHeight > pictureHeight)
{
startPosY = pictureHeight - carHeight;
}
}
}

View File

@ -0,0 +1,37 @@
import java.awt.*;
public class DrawingCaterpillar {
private NumRinks numRinks = NumRinks.Four;
private Color color;
public void Init(int n, Color color) {
setNumRinks(n);
this.color = color;
}
public void setNumRinks(int n) {
switch (n) {
case 4 -> numRinks = NumRinks.Four;
case 5 -> numRinks = NumRinks.Five;
case 6 -> numRinks = NumRinks.Six;
default -> {
break;
}
}
}
public void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY)
{
color = color != null ? color : Color.YELLOW;
g2d.setPaint(color);
int size = numRinks == NumRinks.Four ? 15 : 10;
int dist = numRinks == NumRinks.Four ? 20 : 13;
startPosX = numRinks == NumRinks.Five ? startPosX + 5 : startPosX;
for (int i = 0; i < numRinks.val(); i++) {
g2d.fillOval(startPosX + dist * i, startPosY + 30, size, size);
}
}
}

View File

@ -0,0 +1,28 @@
import java.awt.*;
public class EntityArmoredCar {
private int speed;
private float weight;
private Color bodyColor;
public float step;
public void init(int speed, float weight, Color bodyColor) {
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
this.step = speed * 100 / weight;
}
public int getSpeed() {
return speed;
}
public float getWeight() {
return weight;
}
public Color getBodyColor() {
return bodyColor;
}
}

View File

@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormArmoredCar">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="3" 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="617" height="499"/>
</constraints>
<properties>
<background color="-4473925"/>
<foreground color="-4473925"/>
<inheritsPopupMenu value="false"/>
</properties>
<border type="none"/>
<children>
<grid id="cfb06" binding="drawPanel" custom-create="true" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="2" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-1"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="7c72e" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f0a85" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" 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="f4d6e" class="javax.swing.JButton" binding="buttonRight">
<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>
<icon value="Right.png"/>
<text value=""/>
</properties>
</component>
<component id="12974" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" 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>
<enabled value="true"/>
<hideActionText value="true"/>
<icon value="Up.png"/>
<text value=""/>
</properties>
</component>
<component id="da478" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" 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>
<icon value="Down.png"/>
<text value=""/>
</properties>
</component>
<component id="43d8e" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Left.png"/>
<text value=""/>
<visible value="true"/>
</properties>
</component>
</children>
</grid>
<component id="2cd31" class="javax.swing.JLabel" binding="labelSpeed">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="9ff3" class="javax.swing.JLabel" binding="labelWeight">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="aa5be" class="javax.swing.JLabel" binding="labelColor">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,112 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormArmoredCar extends JFrame{
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonLeft;
private JPanel mainPanel;
private JPanel drawPanel;
private JButton buttonCreate;
private JLabel labelSpeed;
private JLabel labelWeight;
private JLabel labelColor;
private DrawingArmoredCar armoredCar;
public FormArmoredCar() {
super("Бронированная машина");
setBounds(100, 100, 700, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent componentEvent) {
if (armoredCar != null) {
armoredCar.ChangeBorders(drawPanel.getWidth(), drawPanel.getHeight());
repaint();
}
}
});
labelSpeed.setText("Скорость: ");
labelWeight.setText("Вес: ");
labelColor.setText("Цвет: ");
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Random rnd = new Random();
armoredCar = new DrawingArmoredCar();
armoredCar.Init(rnd.nextInt(100,300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
armoredCar.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100),
drawPanel.getWidth(), drawPanel.getHeight());
labelSpeed.setText("Скорость: " + armoredCar.getArmoredCar().getSpeed());
labelWeight.setText("Вес: " + armoredCar.getArmoredCar().getWeight());
labelColor.setText("Цвет: " + armoredCar.getArmoredCar().getBodyColor().getRGB());
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (armoredCar != null)
armoredCar.MoveTransport(Direction.Up);
drawPanel.repaint();
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (armoredCar != null)
armoredCar.MoveTransport(Direction.Down);
drawPanel.repaint();
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (armoredCar != null)
armoredCar.MoveTransport(Direction.Left);
drawPanel.repaint();
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (armoredCar != null)
armoredCar.MoveTransport(Direction.Right);
drawPanel.repaint();
}
});
setContentPane(mainPanel);
setVisible(true);
}
private void createUIComponents() {
drawPanel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (armoredCar != null) {
Graphics2D g2d = (Graphics2D) g;
armoredCar.DrawTransport(g2d);
}
super.repaint();
}
};
}
}

View File

@ -0,0 +1,6 @@
public enum NumRinks {
Four{public int val() {return 4;}},
Five{public int val() {return 5;}},
Six{public int val() {return 6;}};
public abstract int val();
}

View File

@ -1,8 +1,5 @@
import javax.swing.*;
import java.awt.*;
public class Program {
public static void main(String[] args) {
new FormArmoredCar();
}
}

BIN
src/main/resources/Down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
src/main/resources/Left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
src/main/resources/Up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB