This commit is contained in:
VictoriaPresnyakova 2022-12-05 00:55:23 +04:00
parent f2105be275
commit 3e05b1b31e
14 changed files with 435 additions and 2 deletions

View File

@ -2,7 +2,9 @@
<module type="JAVA_MODULE" version="4"> <module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <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="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>

View File

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

7
Direction.java Normal file
View File

@ -0,0 +1,7 @@
public enum Direction {
Up(1),
Down(2),
Left(3),
Right(4);
Direction(int value){}
}

139
DrawningBoat.java Normal file
View File

@ -0,0 +1,139 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningBoat extends JPanel {
private EntityBoat Boat;
public EntityBoat GetBoat(){
return Boat;
}
private int _startPosX;
private int _startPosY;
public DrawningOars OarsDraw;
public Integer _pictureWidth = null;
public Integer _pictureHeight = null;
private final int _BoatWidth = 80;
private final int _BoatHeight = 50;
public void SetOars() {
Random r = new Random();
int numIllum = r.nextInt(1,4);
OarsDraw.SetOarsCount(numIllum);
}
public void Init(int speed, float weight, Color bodyColor)
{
Boat = new EntityBoat();
Boat.Init(speed, weight, bodyColor);
OarsDraw = new DrawningOars();
SetOars();
}
public void SetPosition(int x, int y, int width, int height)
{
if (x >= 0 && x + _BoatWidth <= width && y >= 0 && y + _BoatHeight <= height) {
_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 + _BoatWidth + Boat.Step < _pictureWidth)
{
_startPosX += Boat.Step;
}
break;
case Left:
if (_startPosX - Boat.Step > 0)
{
_startPosX -= Boat.Step;
}
break;
case Up:
if (_startPosY - Boat.Step > 0)
{
_startPosY -= Boat.Step;
}
break;
case Down:
if (_startPosY + _BoatHeight + Boat.Step < _pictureHeight)
{
_startPosY += Boat.Step;
}
break;
}
}
public void DrawTransport() {
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
repaint();
}
@Override
public void paintComponent(Graphics g) {
if (GetBoat() == null) {
return;
}
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
//границы
g2d.drawRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight);
int[] xpoints = {(int)(_startPosX + _BoatWidth * 3 / 4), (int)(_startPosX + _BoatWidth), (int)(_startPosX + _BoatWidth * 3 / 4) };
int[] ypoints = {(int)_startPosY, (int)(_startPosY + _BoatHeight / 2), (int)(_startPosY + _BoatHeight)};
g2d.drawPolygon(xpoints, ypoints, 3);
g2d.setPaint(Boat.GetBodyColor());
g2d.fillRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight);
g2d.fillPolygon(xpoints, ypoints, 3);
// середина
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11);
g2d.setPaint(Color.CYAN);
g2d.fillOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11);
OarsDraw.DrawOars(g, _startPosX, _startPosY);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _BoatWidth || _pictureHeight <= _BoatHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _BoatWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _BoatWidth;
}
if (_startPosY + _BoatHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _BoatHeight;
}
}
}

39
DrawningOars.java Normal file
View File

@ -0,0 +1,39 @@
import javax.swing.*;
import java.awt.*;
public class DrawningOars extends JComponent {
private OarsCount _Oars;
public void SetOarsCount(int numOfOars) {
_Oars = OarsCount.GetOarsCount(numOfOars);
}
public void DrawOars(Graphics g, int _startPosX, int _startPosY) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
int numOfIlluminator = 0;
switch (_Oars)
{
case One:
numOfIlluminator = 1;
break;
case Two:
numOfIlluminator = 2;
break;
case Three:
numOfIlluminator = 3;
break;
}
for(int i = numOfIlluminator; i >= 1; --i){
g2d.setColor(Color.CYAN);
g2d.fillRect(_startPosX + (10 * (i + 1)), _startPosY -15, 5, 15);
//g2d.fillOval(_startPosX + 105 - 3 * i, _startPosY + 35, 3, 3);
g2d.setColor(Color.BLACK);
//g2d.drawOval(_startPosX + 105 - 3 * i, _startPosY + 35, 3, 3);
g2d.drawRect(_startPosX + (10 * (i + 1)), _startPosY - 15, 5, 15);
}
}
}

29
EntityBoat.java Normal file
View File

@ -0,0 +1,29 @@
import java.awt.*;
import java.util.Random;
public class EntityBoat {
private int Speed;
public int GetSpeed() {
return Speed;
}
private float Weight;
public float GetWeight() {
return Weight;
}
private Color BodyColor;
public Color GetBodyColor() {
return BodyColor;
}
public float Step;
public void Init(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(10,30) : speed;
Weight = weight <= 0 ? rnd.nextInt(40,70) : weight;
BodyColor = bodyColor;
Step = Speed * 100 / (int)Weight;
}
}

103
FormBoat.form Normal file
View File

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormBoat">
<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="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>
<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="DrawningBoat" binding="PictureBoxBoat">
<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"/>
</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="0" row-span="1" col-span="3" 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="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="9e2ce" class="javax.swing.JButton" binding="ButtonCreate">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
</children>
</grid>
</form>

78
FormBoat.java Normal file
View File

@ -0,0 +1,78 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormBoat {
public JPanel Mainpanel;
private JButton ButtonCreate;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonRight;
protected DrawningBoat PictureBoxBoat;
private JToolBar StatusStrip;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
public void Draw() {
if (PictureBoxBoat.GetBoat() == null) {
return;
}
PictureBoxBoat.DrawTransport();
}
public FormBoat() {
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(FormBoat.class.getResource("/Resource/up.jpg"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormBoat.class.getResource("/Resource/down.jpg"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormBoat.class.getResource("/Resource/left.jpg"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormBoat.class.getResource("/Resource/right.jpg"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
ButtonCreate.addActionListener(e -> {
Random random = new Random();
PictureBoxBoat.Init(random.nextInt(10, 30), random.nextInt(10, 20), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
PictureBoxBoat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBoxBoat.getWidth(), PictureBoxBoat.getHeight());
JLabelSpeed.setText("орость: " + PictureBoxBoat.GetBoat().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + PictureBoxBoat.GetBoat().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + PictureBoxBoat.GetBoat().GetBodyColor() + " "));
Draw();
});
PictureBoxBoat.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
PictureBoxBoat.ChangeBorders(PictureBoxBoat.getWidth(), PictureBoxBoat.getHeight());
Draw();
}
});
ButtonUp.addActionListener(e -> {
PictureBoxBoat.MoveTransport(Direction.Up);
Draw();
});
ButtonDown.addActionListener(e -> {
PictureBoxBoat.MoveTransport(Direction.Down);
Draw();
});
ButtonRight.addActionListener(e -> {
PictureBoxBoat.MoveTransport(Direction.Right);
Draw();
});
ButtonLeft.addActionListener(e -> {
PictureBoxBoat.MoveTransport(Direction.Left);
Draw();
});
}
}

14
Main.java Normal file
View File

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

22
OarsCount.java Normal file
View File

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

BIN
Resource/down.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Resource/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Resource/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Resource/up.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB