Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
cd54300b22 | |||
af5540a048 | |||
521ae8d927 |
@ -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>
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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" />
|
||||
</component>
|
||||
</project>
|
24
AdditionalEnum.java
Normal file
24
AdditionalEnum.java
Normal file
@ -0,0 +1,24 @@
|
||||
public enum AdditionalEnum {
|
||||
One,
|
||||
Two,
|
||||
Three;
|
||||
|
||||
public static AdditionalEnum FromInteger(int intValue)
|
||||
{
|
||||
switch(intValue)
|
||||
{
|
||||
case 1:
|
||||
return One;
|
||||
|
||||
case 2:
|
||||
return Two;
|
||||
|
||||
case 3:
|
||||
return Three;
|
||||
|
||||
default:
|
||||
System.out.println("Error: incorrect value for enum");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
24
Direction.java
Normal file
24
Direction.java
Normal file
@ -0,0 +1,24 @@
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right;
|
||||
|
||||
public static Direction FromInteger(int intValue)
|
||||
{
|
||||
switch(intValue)
|
||||
{
|
||||
case 1:
|
||||
return Up;
|
||||
case 2:
|
||||
return Down;
|
||||
case 3:
|
||||
return Left;
|
||||
case 4:
|
||||
return Right;
|
||||
default:
|
||||
System.out.println("Error: incorrect value for enum");
|
||||
return Up;
|
||||
}
|
||||
}
|
||||
}
|
32
DrawingDeck.java
Normal file
32
DrawingDeck.java
Normal file
@ -0,0 +1,32 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
public class DrawingDeck extends JComponent {
|
||||
private AdditionalEnum _decksEnum;
|
||||
public void SetAddEnum(int decksAmount) {
|
||||
_decksEnum = AdditionalEnum.FromInteger(decksAmount);
|
||||
}
|
||||
public void DrawDeck(Color colorDeck, Graphics g,float _startPosX,float _startPosY)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
int numOfDecks = 0;
|
||||
switch (_decksEnum)
|
||||
{
|
||||
case One:
|
||||
numOfDecks = 1;
|
||||
break;
|
||||
case Two:
|
||||
numOfDecks = 2;
|
||||
break;
|
||||
case Three:
|
||||
numOfDecks = 3;
|
||||
break;
|
||||
}
|
||||
for(int i = 0; i < numOfDecks; ++i){
|
||||
g2d.setPaint(colorDeck);
|
||||
g2d.fillRect((int)_startPosX+30 + 5*i, (int)_startPosY+25 - 5*i, 30*2 - 5*i, 5);
|
||||
g2d.setPaint(Color.BLACK);
|
||||
g2d.drawRect((int)_startPosX+30 + 5*i, (int)_startPosY+25 - 5*i, 30*2 - 5*i, 5);
|
||||
}
|
||||
}
|
||||
}
|
117
DrawingShip.java
Normal file
117
DrawingShip.java
Normal file
@ -0,0 +1,117 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
public class DrawingShip extends JPanel {
|
||||
private EntityShip Ship;
|
||||
public DrawingDeck Deck;
|
||||
private float _startPosX;
|
||||
private float _startPosY;
|
||||
private Integer _pictureWidth = null;
|
||||
private Integer _pictureHeight = null;
|
||||
protected final int _shipWidth = 120;
|
||||
protected final int _shipHeight = 40;
|
||||
public void SetEnum() {
|
||||
Random r = new Random();
|
||||
int numbDecks = r.nextInt(1, 4);
|
||||
Deck.SetAddEnum(numbDecks);
|
||||
}
|
||||
public EntityShip GetShip() {
|
||||
return Ship;
|
||||
}
|
||||
|
||||
public void Init(int speed, float weight, Color bodycolor) {
|
||||
Ship = new EntityShip();
|
||||
Ship.Init(speed, weight, bodycolor);
|
||||
Deck = new DrawingDeck();
|
||||
SetEnum();
|
||||
}
|
||||
public void SetPosition(int x, int y, int width, int height) {
|
||||
if (x < 0 || y < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (x + _shipWidth > width || 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() {
|
||||
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
|
||||
return;
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
if (GetShip() == null) {
|
||||
return;
|
||||
}
|
||||
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setPaint(Ship.GetBodyColor());
|
||||
int xValues[]={(int) _startPosX,(int) _startPosX + 100,(int) _startPosX + 80,(int) _startPosX + 20};
|
||||
int yValues[]={(int) _startPosY + 30,(int) _startPosY + 30,(int) _startPosY + 60,(int) _startPosY + 60};
|
||||
g2d.fillPolygon(xValues,yValues,4);
|
||||
g2d.setPaint(Color.BLACK);
|
||||
g2d.drawPolygon(xValues,yValues,4);
|
||||
Deck.DrawDeck(Ship.GetBodyColor(), g, _startPosX, _startPosY);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
28
EntityShip.java
Normal file
28
EntityShip.java
Normal file
@ -0,0 +1,28 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityShip {
|
||||
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 GetStep()
|
||||
{
|
||||
return Speed*100/Weight;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
107
FormShip.form
Normal file
107
FormShip.form
Normal 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="10" 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="PictureBoxShip">
|
||||
<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="10" 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>
|
96
FormShip.java
Normal file
96
FormShip.java
Normal file
@ -0,0 +1,96 @@
|
||||
import javax.imageio.ImageIO;
|
||||
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 FormShip {
|
||||
|
||||
public JPanel Mainpanel;
|
||||
private JButton ButtonCreate;
|
||||
private JButton ButtonLeft;
|
||||
private JButton ButtonUp;
|
||||
private JButton ButtonDown;
|
||||
private JButton ButtonRight;
|
||||
protected DrawingShip PictureBoxShip;
|
||||
private JToolBar StatusStrip;
|
||||
private JLabel JLabelSpeed = new JLabel();
|
||||
private JLabel JLabelWeight = new JLabel();
|
||||
private JLabel JLabelColor = new JLabel();
|
||||
public void Draw() {
|
||||
if (PictureBoxShip.GetShip() == null) {
|
||||
return;
|
||||
}
|
||||
PictureBoxShip.DrawTransport();
|
||||
}
|
||||
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/ArrowUp.png"));
|
||||
ButtonUp.setIcon(new ImageIcon(img));
|
||||
img = ImageIO.read(FormShip.class.getResource("Images/ArrowDown.png"));
|
||||
ButtonDown.setIcon(new ImageIcon(img));
|
||||
img = ImageIO.read(FormShip.class.getResource("Images/ArrowLeft.png"));
|
||||
ButtonLeft.setIcon(new ImageIcon(img));
|
||||
img = ImageIO.read(FormShip.class.getResource("Images/ArrowRight.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();
|
||||
PictureBoxShip.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
|
||||
PictureBoxShip.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBoxShip.getWidth(), PictureBoxShip.getHeight());
|
||||
JLabelSpeed.setText("Cкорость: " + PictureBoxShip.GetShip().GetSpeed() + " ");
|
||||
JLabelWeight.setText("Вес: " + PictureBoxShip.GetShip().GetWeight() + " ");
|
||||
JLabelColor.setText(("Цвет: " + PictureBoxShip.GetShip().GetBodyColor() + " "));
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
PictureBoxShip.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
super.componentResized(e);
|
||||
PictureBoxShip.ChangeBorders(PictureBoxShip.getWidth(), PictureBoxShip.getHeight());
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
ButtonUp.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
PictureBoxShip.MoveTransport(Direction.Up);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
ButtonDown.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
PictureBoxShip.MoveTransport(Direction.Down);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
ButtonRight.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
PictureBoxShip.MoveTransport(Direction.Right);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
ButtonLeft.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
PictureBoxShip.MoveTransport(Direction.Left);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
BIN
Images/ArrowDown.png
Normal file
BIN
Images/ArrowDown.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 253 B |
BIN
Images/ArrowLeft.png
Normal file
BIN
Images/ArrowLeft.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 260 B |
BIN
Images/ArrowRight.png
Normal file
BIN
Images/ArrowRight.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 266 B |
BIN
Images/ArrowUp.png
Normal file
BIN
Images/ArrowUp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 245 B |
13
Main.java
Normal file
13
Main.java
Normal file
@ -0,0 +1,13 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame("Корабль");
|
||||
frame.setContentPane(new FormShip().Mainpanel);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLocation(500, 200);
|
||||
frame.pack();
|
||||
frame.setSize(800, 600);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user