Merge pull request 'Tsukanova I.V. HardLabWork1' (#1) from LabWork1 into master
Reviewed-on: http://student.git.athene.tech/Senju/PIbd-22_Tsukanova_I.V._AircraftCarrier_Hard/pulls/1
This commit is contained in:
commit
be91b54c2b
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/../../../ирино, не трогат!!/!/!/RPP/ProjectAircraftCarrierHard/out" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11.0.5" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/../../../ирино, не трогат/!/!/!/!/RPP/ProjectAircraftCarrierHard/out" />
|
||||
</component>
|
||||
</project>
|
5
src/BlockDirection.java
Normal file
5
src/BlockDirection.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum BlockDirection {
|
||||
TwoBlocks,
|
||||
FourBlocks,
|
||||
SixBlocks;
|
||||
}
|
6
src/Direction.java
Normal file
6
src/Direction.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right;
|
||||
}
|
45
src/DrawingBlocks.java
Normal file
45
src/DrawingBlocks.java
Normal file
@ -0,0 +1,45 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingBlocks {
|
||||
BlockDirection blockDirection;
|
||||
public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){
|
||||
switch(blockDirection){
|
||||
case TwoBlocks:
|
||||
g2.setColor(Color.GRAY);
|
||||
g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10);
|
||||
g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10);
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10);
|
||||
g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10);
|
||||
break;
|
||||
case FourBlocks:
|
||||
g2.setColor(Color.GRAY);
|
||||
g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10);
|
||||
g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10);
|
||||
g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10);
|
||||
g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10);
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10);
|
||||
g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10);
|
||||
g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10);
|
||||
g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10);
|
||||
break;
|
||||
case SixBlocks:
|
||||
g2.setColor(Color.GRAY);
|
||||
g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10);
|
||||
g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10);
|
||||
g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10);
|
||||
g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10);
|
||||
g2.fillRect(_startPosX + 35, _startPosY + 10, 10, 10);
|
||||
g2.fillRect(_startPosX + 35, _startPosY + 20, 10, 10);
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10);
|
||||
g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10);
|
||||
g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10);
|
||||
g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10);
|
||||
g2.drawRect(_startPosX + 35, _startPosY + 10, 10, 10);
|
||||
g2.drawRect(_startPosX + 35, _startPosY + 20, 10, 10);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
22
src/DrawingComponents.java
Normal file
22
src/DrawingComponents.java
Normal file
@ -0,0 +1,22 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingComponents extends JComponent {
|
||||
public DrawingWarship warship;
|
||||
public DrawingComponents(){
|
||||
super();
|
||||
}
|
||||
public void SetDrawingWarship(DrawingWarship warship){
|
||||
this.warship = warship;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g){
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
if(warship != null){
|
||||
warship.DrawTransport(g2);
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
}
|
138
src/DrawingWarship.java
Normal file
138
src/DrawingWarship.java
Normal file
@ -0,0 +1,138 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
public class DrawingWarship {
|
||||
private EntityWarship Warship;
|
||||
public EntityWarship GetWarship(){return Warship;}
|
||||
public DrawingBlocks Blocks;
|
||||
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
|
||||
private Integer _pictureWidth = null;
|
||||
private Integer _pictureHeight = null;
|
||||
|
||||
private final int _warshipWidth = 114;
|
||||
private final int _warshipHeight = 40;
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
Warship = new EntityWarship();
|
||||
Warship.Init(speed, weight, bodyColor);
|
||||
Blocks = new DrawingBlocks();
|
||||
Blocks.blockDirection = BlockRandom();
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y, int width, int height)
|
||||
{
|
||||
if (width >= x + _warshipWidth && height >= y + _warshipHeight && x >= 0 && y >= 0)
|
||||
{
|
||||
_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 + _warshipWidth + Warship.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += Warship.Step;
|
||||
}
|
||||
break;
|
||||
//влево
|
||||
case Left:
|
||||
if(_startPosX - Warship.Step > 0)
|
||||
{
|
||||
_startPosX -= Warship.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case Up:
|
||||
if (_startPosY - Warship.Step > 0)
|
||||
{
|
||||
_startPosY -= Warship.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case Down:
|
||||
if (_startPosY + _warshipHeight + Warship.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += Warship.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics2D g2){
|
||||
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//главная палуба
|
||||
int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4};
|
||||
int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40};
|
||||
g2.setColor(Warship.GetBodyColor());
|
||||
g2.fillPolygon(pointXWarship, pointYWarship, 5);
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.drawPolygon(pointXWarship, pointYWarship, 5);
|
||||
|
||||
//мачта
|
||||
g2.setColor(Color.WHITE);
|
||||
g2.fillOval(_startPosX + 80, _startPosY + 13, 15, 15);
|
||||
//границы мачты
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.drawOval( _startPosX + 80, _startPosY + 13, 15, 15);
|
||||
|
||||
//палуба
|
||||
g2.setColor(Color.WHITE);
|
||||
g2.fillRect(_startPosX + 70, _startPosY + 10, 8, 18);
|
||||
g2.fillRect(_startPosX + 55, _startPosY + 15, 15, 8);
|
||||
//границы палубы
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.drawRect(_startPosX + 70, _startPosY + 10, 8, 18);
|
||||
g2.drawRect(_startPosX + 55, _startPosY + 15, 15, 8);
|
||||
|
||||
//двигатели
|
||||
g2.fillRect(_startPosX, _startPosY + 5, 4, 10);
|
||||
g2.fillRect(_startPosX, _startPosY + 23, 4, 10);
|
||||
|
||||
//блоки
|
||||
Blocks.DrawBlocks(g2,_startPosX, _startPosY);
|
||||
}
|
||||
|
||||
public BlockDirection BlockRandom(){
|
||||
Random rand = new Random();
|
||||
int resRand = rand.nextInt(3);
|
||||
if(resRand == 0) return BlockDirection.TwoBlocks;
|
||||
if(resRand == 1) return BlockDirection.FourBlocks;
|
||||
if(resRand == 2) return BlockDirection.SixBlocks;
|
||||
return null;
|
||||
}
|
||||
|
||||
public void ChangeBorders(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_pictureWidth <= _warshipWidth || _pictureHeight <= _warshipHeight)
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
return;
|
||||
}
|
||||
if (_startPosX + _warshipWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _warshipWidth;
|
||||
}
|
||||
if (_startPosY + _warshipHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _warshipHeight;
|
||||
}
|
||||
}
|
||||
}
|
23
src/EntityWarship.java
Normal file
23
src/EntityWarship.java
Normal file
@ -0,0 +1,23 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
public class EntityWarship {
|
||||
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(100) + 50 : speed;
|
||||
Weight = weight <= 0 ? rnd.nextInt(30)+40 : weight;
|
||||
BodyColor= bodyColor;
|
||||
Step = Speed * 100 / Weight;
|
||||
}
|
||||
|
||||
}
|
112
src/FormWarship.form
Normal file
112
src/FormWarship.form
Normal file
@ -0,0 +1,112 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormWarship">
|
||||
<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="23" y="20" width="961" height="517"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="80b72" class="javax.swing.JButton" binding="buttonCreate">
|
||||
<constraints>
|
||||
<grid row="2" 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="Create"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="3f3da">
|
||||
<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="32714" 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>
|
||||
<toolbar id="3697c" binding="toolBar">
|
||||
<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">
|
||||
<preferred-size width="-1" height="20"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="7dd11" class="javax.swing.JLabel" binding="toolBarLabelSpeed">
|
||||
<constraints/>
|
||||
<properties>
|
||||
<text value="Speed"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="9db36" class="javax.swing.JLabel" binding="toolBarLabelWieght">
|
||||
<constraints/>
|
||||
<properties>
|
||||
<text value="Weight"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="3d477" class="javax.swing.JLabel" binding="toolBarLabelColor">
|
||||
<constraints/>
|
||||
<properties>
|
||||
<text value="Color"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</toolbar>
|
||||
<component id="55933" 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>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="82cc1" 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>
|
||||
<component id="bb79e" 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>
|
||||
<enabled value="true"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<grid id="dfecd" binding="drawPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
|
||||
<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/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
106
src/FormWarship.java
Normal file
106
src/FormWarship.java
Normal file
@ -0,0 +1,106 @@
|
||||
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 FormWarship extends JFrame {
|
||||
private DrawingWarship _warship;
|
||||
public DrawingComponents drawingComponents;
|
||||
private JPanel mainPanel;
|
||||
private JButton buttonCreate;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonRight;
|
||||
private JToolBar toolBar;
|
||||
private JLabel toolBarLabelSpeed;
|
||||
private JLabel toolBarLabelWieght;
|
||||
private JLabel toolBarLabelColor;
|
||||
private JPanel drawPanel;
|
||||
|
||||
public FormWarship(){
|
||||
InitializeComponent();
|
||||
}
|
||||
private void Draw(){
|
||||
drawingComponents.repaint();
|
||||
}
|
||||
|
||||
private void InitializeComponent(){
|
||||
setContentPane(mainPanel);
|
||||
setTitle("Warship");
|
||||
setSize(900,700);
|
||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
setVisible(true);
|
||||
|
||||
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");
|
||||
buttonUp.setIcon(iconUp);
|
||||
Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg");
|
||||
buttonDown.setIcon(iconDown);
|
||||
Icon iconLeft = new ImageIcon("src\\Images\\ArrowLeft.jpg");
|
||||
buttonLeft.setIcon(iconLeft);
|
||||
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
|
||||
buttonRight.setIcon(iconRight);
|
||||
|
||||
drawingComponents = new DrawingComponents();
|
||||
drawPanel.add(drawingComponents);
|
||||
buttonCreate.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Random rnd = new Random();
|
||||
_warship = new DrawingWarship();
|
||||
_warship.Init(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
|
||||
new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)));
|
||||
_warship.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90) + 10, drawPanel.getWidth(), drawPanel.getHeight());
|
||||
toolBarLabelSpeed.setText("Color: " + _warship.GetWarship().GetSpeed() + " ");
|
||||
toolBarLabelWieght.setText("Weight: " + _warship.GetWarship().GetWeight() + " ");
|
||||
toolBarLabelColor.setText("Color: " + _warship.GetWarship().GetBodyColor().getRed() + " " +
|
||||
_warship.GetWarship().GetBodyColor().getGreen() + " " + _warship.GetWarship().GetBodyColor().getBlue());
|
||||
drawingComponents.SetDrawingWarship(_warship);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
|
||||
buttonLeft.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(_warship != null) _warship.MoveTransport(Direction.Left);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
|
||||
buttonRight.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(_warship != null) _warship.MoveTransport(Direction.Right);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
|
||||
buttonUp.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(_warship != null) _warship.MoveTransport(Direction.Up);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
|
||||
buttonDown.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(_warship != null) _warship.MoveTransport(Direction.Down);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
|
||||
drawPanel.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
_warship.ChangeBorders(drawPanel.getWidth(),drawPanel.getHeight());
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
BIN
src/Images/ArrowDown.jpg
Normal file
BIN
src/Images/ArrowDown.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
src/Images/ArrowLeft.jpg
Normal file
BIN
src/Images/ArrowLeft.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
src/Images/ArrowRight.jpg
Normal file
BIN
src/Images/ArrowRight.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
src/Images/ArrowUp.jpg
Normal file
BIN
src/Images/ArrowUp.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
@ -1,4 +1,6 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
new FormWarship();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user