Bogdanov D.S. LabHard01 #1

Closed
the wants to merge 1 commits from LabWork01 into main
12 changed files with 426 additions and 0 deletions

5
DecksCount.java Normal file
View File

@ -0,0 +1,5 @@
public enum DecksCount {
One,
Two,
Three
}

6
Direction.java Normal file
View File

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

36
DrawingDecks.java Normal file
View File

@ -0,0 +1,36 @@
import java.awt.*;
public class DrawingDecks {
private DecksCount deckCount;
private Color color;
public void Init(int deckCount, Color bodyColor) {
setdecksCount(deckCount);
color = bodyColor;
}
public void setdecksCount(int num) {
if (num <= 1) {
deckCount = DecksCount.One;
} else if (num >= 3) {
deckCount = DecksCount.Three;
}
else {
deckCount = DecksCount.Two;
}
}
public void draw(Graphics2D g, int x, int y, int shipWidth, int shipHeight) {
g.setColor(color != null ? color : Color.BLACK);
switch (deckCount) {
case Two: {
g.fillRect(x, y + 5, 15, 5);
g.fillPolygon(new int[] {x, x, x + 5}, new int[] {y + 5, y + 10, y + 10}, 3);
}
case Three: {
g.fillRect(x + shipWidth - 20, y, 20, 10);
g.fillPolygon(new int[] {x + shipWidth - 20, x + shipWidth - 20, x + shipWidth - 25}, new int[] {y, y + 10, y + 10}, 3);
}
}
}
}

111
DrawingShip.java Normal file
View File

@ -0,0 +1,111 @@
import java.awt.*;
public class DrawingShip {
private EntityShip ship;
private DrawingDecks drawingDecks;
private float _startPosX;
private float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private final int _shipWidth = 80;
private final int _shipHeight = 30;
public EntityShip getShip() {
return ship;
}
public void Init(int speed, float weight, Color bodyColor, int decksCount) {
ship = new EntityShip();
ship.Init(speed, weight, bodyColor);
drawingDecks = new DrawingDecks();
drawingDecks.Init(decksCount, bodyColor);
}
public void SetPosition(int x, int y, int width, int height) {
if (x < 0 || x + _shipWidth >= width)
{
return;
}
if (y < 0 || y + _shipHeight >= height)
{
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void moveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null)
{
return;
}
switch (direction)
{
case Right:
if (_startPosX + _shipWidth + ship.getStep() < _pictureWidth)
{
_startPosX += ship.getStep();
}
break;
case Left:
if (_startPosX - ship.getStep() >= 0)
{
_startPosX -= ship.getStep();
}
break;
case Up:
if (_startPosY - ship.getStep() >= 0)
{
_startPosY -= ship.getStep();
}
break;
case Down:
if (_startPosY + _shipHeight + ship.getStep() < _pictureHeight)
{
_startPosY += ship.getStep();
}
break;
}
}
public void drawTransport(Graphics2D g) {
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
{
return;
}
g.setColor(ship.getBodyColor() != null ? ship.getBodyColor() : Color.BLACK);
int xPoly[] = {(int)_startPosX, (int)_startPosX + 80, (int)_startPosX + 70, (int)_startPosX + 10};
int yPoly[] = {(int)_startPosY + 10, (int)_startPosY + 10, (int)_startPosY + 30, (int)_startPosY + 30};
g.fillPolygon(xPoly, yPoly, 4);
g.fillRect((int)_startPosX + 30, (int)_startPosY, 20, 10);
drawingDecks.draw(g, (int) _startPosX, (int) _startPosY, _shipWidth, _shipHeight);
}
public void changeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _shipWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _shipWidth;
}
if (_startPosY + _shipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _shipHeight;
}
}
}

31
EntityShip.java Normal file
View File

@ -0,0 +1,31 @@
import java.awt.*;
import java.util.Random;
public class EntityShip {
private int speed;
private float weight;
private Color bodyColor;
public void Init(int speed, float weight, Color bodyColor) {
Random rnd = new Random();
this.speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed;
this.weight = weight <= 0 ? rnd.nextInt(30) + 40 : weight;
this.bodyColor = bodyColor;
}
public int getSpeed() {
return speed;
}
public float getWeight() {
return weight;
}
public Color getBodyColor() {
return bodyColor;
}
public float getStep() {
return speed * 100 / weight;
}
}

152
FormShip.form Normal file
View File

@ -0,0 +1,152 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormShip">
<grid id="27dc6" binding="shipPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" 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="664" height="436"/>
</constraints>
<properties>
<background color="-4210753"/>
<inheritsPopupMenu value="false"/>
<maximumSize width="640" height="480"/>
<minimumSize width="80" height="50"/>
<preferredSize width="640" height="480"/>
</properties>
<border type="none">
<font/>
<title-color color="-1908517"/>
</border>
<children>
<grid id="460f0" layout-manager="GridLayoutManager" row-count="1" 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>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="20"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="ec1f2" class="javax.swing.JLabel" binding="speedLabel">
<constraints>
<grid row="0" 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="1ffd0" class="javax.swing.JLabel" binding="colorLabel">
<constraints>
<grid row="0" 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>
<component id="64b86" class="javax.swing.JLabel" binding="weightLabel">
<constraints>
<grid row="0" 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>
</children>
</grid>
<grid id="fa947" binding="pictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="f89d0">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="402e3">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="bbd8c">
<constraints>
<grid row="0" column="2" 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="271ef" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<borderPainted value="true"/>
<contentAreaFilled value="false"/>
<icon value="Resources/ArrowUp.png"/>
<text value=""/>
</properties>
</component>
<component id="4b967" class="javax.swing.JButton" binding="buttonDown">
<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">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<contentAreaFilled value="false"/>
<icon value="Resources/ArrowDown.png"/>
<text value=""/>
</properties>
</component>
<component id="2e663" class="javax.swing.JButton" binding="buttonLeft">
<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">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<contentAreaFilled value="false"/>
<icon value="Resources/ArrowLeft.png"/>
<label value=""/>
<text value=""/>
</properties>
</component>
<component id="c2d76" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<contentAreaFilled value="false"/>
<icon value="Resources/ArrowRight.png"/>
<label value=""/>
<text value=""/>
</properties>
</component>
<component id="7bc68" class="javax.swing.JButton" binding="createButton">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<alignmentX value="0.0"/>
<margin top="0" left="0" bottom="0" right="0"/>
<text value="Создать"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

75
FormShip.java Normal file
View File

@ -0,0 +1,75 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormShip extends JFrame {
private JPanel shipPane;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
private JPanel pictureBox;
private JButton createButton;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private DrawingShip _ship;
public FormShip() {
this.setTitle("Ship");
this.setContentPane(shipPane);
createButton.addActionListener(e -> {
Random rnd = new Random();
_ship = new DrawingShip();
_ship.Init(
rnd.nextInt(200) + 100,
rnd.nextInt(1000) + 1000,
new Color(
rnd.nextInt(256),
rnd.nextInt(256),
rnd.nextInt(256)),
rnd.nextInt(3) + 1
);
_ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText(String.format("Speed: %s", _ship.getShip().getSpeed()));
weightLabel.setText(String.format("Weight: %s", _ship.getShip().getWeight()));
colorLabel.setText(String.format("Color: %x", _ship.getShip().getBodyColor().getRGB()));
repaint();
});
pictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (_ship != null) _ship.changeBorders(pictureBox.getWidth(), pictureBox.getHeight());
repaint();
}
});
buttonLeft.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Left);
repaint();
});
buttonRight.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Right);
repaint();
});
buttonUp.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Up);
repaint();
});
buttonDown.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Down);
repaint();
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) shipPane.getGraphics();
if (_ship != null) {
_ship.drawTransport(g2d);
}
}
}

10
Program.java Normal file
View File

@ -0,0 +1,10 @@
import javax.swing.*;
public class Program {
public static void main(String[] args) {
FormShip form = new FormShip();
form.setSize(640, 480);
form.setVisible(true);
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

BIN
Resources/ArrowDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 B

BIN
Resources/ArrowLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 B

BIN
Resources/ArrowRight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 B

BIN
Resources/ArrowUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B