This commit is contained in:
Макс Бондаренко 2022-11-16 18:24:14 +04:00
parent 63930fd35e
commit c36ffe6688
14 changed files with 456 additions and 2 deletions

View File

@ -2,7 +2,9 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

22
Deck.java Normal file
View File

@ -0,0 +1,22 @@
public enum Deck {
One(1),
Two(2),
Three(3);
Deck(int i) {
}
public static Deck GetDeck(int i){
switch (i)
{
case 1:
return One;
case 2:
return Two;
case 3:
return Three;
}
return null;
}
}

9
Direction.java Normal file
View File

@ -0,0 +1,9 @@
public enum Direction {
Left(1), //Влево
Up(2), //Вверх
Right(3), //Вправо
Down(4); //Вниз
Direction(int i) {
}
}

33
DrawDeck.java Normal file
View File

@ -0,0 +1,33 @@
import java.awt.*;
public class DrawDeck {
private Deck deckCount;
public void SetDeckCount(int count){
deckCount = Deck.GetDeck(count);
}
public void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor){
switch (deckCount)
{
case One:
break;
case Two:
g2d.setColor(bodyColor);
g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20, _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.BLACK);
g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20, _warmlyShipWidth * 3 / 5, 20);
break;
case Three:
for (int i = 1; i < 3; ++i){
g2d.setColor(bodyColor);
g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.BLACK);
g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20);
}
break;
}
}
}

119
DrawingShip.java Normal file
View File

@ -0,0 +1,119 @@
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class DrawingShip extends JPanel {
private EntityWarmlyShip warmlyShip; //Класс-сущность
public EntityWarmlyShip GetWarmlyShip(){return warmlyShip;}
public float _startPosX; //Координаты отрисовки по оси x
public float _startPosY; //Координаты отрисовки по оси y
private Integer _pictureWidth = null; //Ширина окна
private Integer _pictureHeight = null; //Высота окна
private final int _warmlyShipWidth = 125; //Ширина отрисовки корабля
private final int _warmlyShipHeight = 50; //Высота отрисовки корабля
private int deckCount = 1;
private DrawDeck dd = new DrawDeck();
//Инициализация
public void Init(int speed, float weight, Color bodyColor)
{
warmlyShip = new EntityWarmlyShip();
warmlyShip.Init(speed, weight, bodyColor);
Random random = new Random();
dd.SetDeckCount(random.nextInt(1, 4));
}
//Начальные коордитанты
public void SetPosition(int x, int y, int width, int height)
{
if (width < _warmlyShipWidth || height < _warmlyShipHeight) return;
Random random = new Random();
_startPosX = x < 0 || x + _warmlyShipWidth > width ? random.nextFloat(0, width - _warmlyShipWidth) : x;
_startPosY = y < 0 || y + _warmlyShipHeight > height ? random.nextFloat(0, height - _warmlyShipHeight) : y;
_pictureWidth = width;
_pictureHeight = height;
}
//Движение транспорта по координатам
public void MoveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null) return;
switch (direction)
{
case Left: //Влево
if (_startPosX - warmlyShip.GetStep() > 0) _startPosX -= warmlyShip.GetStep();
break;
case Up: //Вверх
if (_startPosY - warmlyShip.GetStep() > 0) _startPosY -= warmlyShip.GetStep();
break;
case Right: //Вправо
if (_startPosX + _warmlyShipWidth + warmlyShip.GetStep() < _pictureWidth) _startPosX += warmlyShip.GetStep();
break;
case Down: //Вниз
if (_startPosY + _warmlyShipHeight + warmlyShip.GetStep() < _pictureHeight) _startPosY += warmlyShip.GetStep();
break;
}
}
//Отрисовка транспорта
public void DrawTransport()
{
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null)
{
return;
}
repaint();
}
@Override
public void paintComponent(Graphics g){
if (GetWarmlyShip() == null) return;
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null)
{
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(warmlyShip.GetBodyColor());
int[] xPol = new int[] {(int)(_startPosX), (int)(_startPosX + _warmlyShipWidth), (int)(_startPosX + _warmlyShipWidth - 25), (int)(_startPosX + 25)};
int[] yPol = new int[] {(int)(_startPosY + 20), (int)(_startPosY + 20), (int)(_startPosY + _warmlyShipHeight), (int)(_startPosY + _warmlyShipHeight)};
g2d.fillPolygon(new Polygon(xPol, yPol, xPol.length));
g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY, _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.CYAN);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(new Polygon(xPol, yPol, xPol.length));
g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)(_startPosY), _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.BLUE);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20);
dd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor());
}
//Изменение границ отрисовки
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _warmlyShipWidth || _pictureHeight <= _warmlyShipHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _warmlyShipWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _warmlyShipWidth;
}
if (_startPosY + _warmlyShipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _warmlyShipHeight;
}
}
}

35
EntityWarmlyShip.java Normal file
View File

@ -0,0 +1,35 @@
import java.awt.*;
import java.util.*;
public class EntityWarmlyShip {
private int Speed; //Скорость
private float Weight; //Вес
private Color BodyColor; //Цвет
private float Step; //Шаг при перемещении
//Инициализация
public void Init(int speed, float weight, Color bodyColor)
{
Random random = new Random();
Speed = speed <= 0 ? random.nextInt(50, 150) : speed;
Weight = weight <= 0 ? random.nextInt(50, 150) : weight;
BodyColor = bodyColor;
Step = Speed * 100 / Weight;
}
public int GetSpeed(){
return Speed;
}
public float GetWeight(){
return Weight;
}
public Color GetBodyColor(){
return BodyColor;
}
public float GetStep(){
return Step;
}
}

107
FormShip.form Normal file
View File

@ -0,0 +1,107 @@
<?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="Mainpanel" layout-manager="GridLayoutManager" row-count="4" 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="500" height="400"/>
</constraints>
<properties>
<opaque value="true"/>
<preferredSize width="800" height="600"/>
</properties>
<border type="none"/>
<children>
<component id="2ea16" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" 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>
<text value=""/>
</properties>
</component>
<component id="4eb88" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" 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>
<horizontalAlignment value="0"/>
<text value=""/>
</properties>
</component>
<component id="9f55" class="DrawingShip" binding="pictureShip">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="10" height="286"/>
</grid>
</constraints>
<properties/>
</component>
<component id="fb937" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" 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>
<text value=""/>
</properties>
</component>
<toolbar id="e747d" binding="statusStrip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="20"/>
<preferred-size width="-1" height="20"/>
<maximum-size width="-1" height="20"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
<border type="none"/>
<children/>
</toolbar>
<hspacer id="d6bea">
<constraints>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="d86ff">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="9e2ce" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="118" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="4f60a" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" 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>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

114
FormShip.java Normal file
View File

@ -0,0 +1,114 @@
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
public class FormShip {
private JToolBar statusStrip;
public JPanel Mainpanel;
private DrawingShip pictureShip;
private JButton buttonRight;
private JButton buttonCreate;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonDown;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void Draw()
{
if (pictureShip.GetWarmlyShip() == null) return;
pictureShip.DrawTransport();
}
private void ButtonMove_Click(String name)
{
if (pictureShip == null) return;
switch (name)
{
case "buttonLeft":
pictureShip.MoveTransport(Direction.Left);
break;
case "buttonUp":
pictureShip.MoveTransport(Direction.Up);
break;
case "buttonRight":
pictureShip.MoveTransport(Direction.Right);
break;
case "buttonDown":
pictureShip.MoveTransport(Direction.Down);
break;
}
Draw();
}
public FormShip() {
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
statusStrip.add(LabelBox);
try {
Image img = ImageIO.read(FormShip.class.getResource("/Images/totop.png"));
buttonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/toleft.png"));
buttonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/todown.png"));
buttonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/toright.png"));
buttonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
pictureShip.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
pictureShip.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureShip.getWidth(), pictureShip.getHeight());
JLabelSpeed.setText("орость: " + pictureShip.GetWarmlyShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + pictureShip.GetWarmlyShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + pictureShip.GetWarmlyShip().GetBodyColor() + " "));
Draw();
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonUp");
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonLeft");
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonDown");
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonRight");
}
});
pictureShip.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
pictureShip.ChangeBorders(pictureShip.getWidth(), pictureShip.getHeight());
Draw();
}
});
}
}

BIN
Images/todown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 888 B

BIN
Images/toleft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 904 B

BIN
Images/toright.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 892 B

BIN
Images/totop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 895 B

13
Main.java Normal file
View File

@ -0,0 +1,13 @@
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Hard №1");
frame.setContentPane(new FormShip().Mainpanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();
frame.setSize(800, 600);
frame.setVisible(true);
}
}