Добавлено усложненное задание

This commit is contained in:
Hells Hound 2022-10-09 14:28:00 +04:00
parent cc5bd478e0
commit 460a7750b1
11 changed files with 221 additions and 39 deletions

5
src/BlockDirection.java Normal file
View File

@ -0,0 +1,5 @@
public enum BlockDirection {
TwoBlocks,
FourBlocks,
SixBlocks;
}

45
src/DrawingBlocks.java Normal file
View 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;
}
}
}

View 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();
}
}

View File

@ -2,8 +2,8 @@ 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;
@ -18,6 +18,8 @@ public class DrawingWarship {
{
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)
@ -68,12 +70,11 @@ public class DrawingWarship {
break;
}
}
public void DrawTransport(Graphics g){
public void DrawTransport(Graphics2D g2){
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
{
return;
}
Graphics2D g2 = (Graphics2D) g;
//главная палуба
int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4};
int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40};
@ -102,7 +103,19 @@ public class DrawingWarship {
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;

View File

@ -17,7 +17,7 @@ public class EntityWarship {
Speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed;
Weight = weight <= 0 ? rnd.nextInt(30)+40 : weight;
BodyColor= bodyColor;
Step = Speed * 2000 / Weight;
Step = Speed * 100 / Weight;
}
}

View File

@ -3,7 +3,7 @@
<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="993" height="515"/>
<xy x="23" y="20" width="961" height="517"/>
</constraints>
<properties/>
<border type="none"/>
@ -21,24 +21,19 @@
<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>
<vspacer id="588c1">
<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>
<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="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
<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="statusStrip">
<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"/>
@ -46,26 +41,33 @@
</constraints>
<properties/>
<border type="none"/>
<children/>
</toolbar>
<component id="9242f" 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="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<children>
<component id="7dd11" class="javax.swing.JLabel" binding="toolBarLabelSpeed">
<constraints/>
<properties>
<text value=" "/>
<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="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
@ -75,15 +77,36 @@
<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="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
<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>

View File

@ -1,32 +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 statusStrip;
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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
src/Images/ArrowUp.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB