Дбавлены новые классы и логика
This commit is contained in:
parent
b5be809757
commit
9538edcc1a
BIN
Resource/arrowDown.jpg
Normal file
BIN
Resource/arrowDown.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
Resource/arrowLeft.jpg
Normal file
BIN
Resource/arrowLeft.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
Resource/arrowRight.jpg
Normal file
BIN
Resource/arrowRight.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
Resource/arrowUp.jpg
Normal file
BIN
Resource/arrowUp.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
14
src/CountBlocks.java
Normal file
14
src/CountBlocks.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package src;
|
||||||
|
|
||||||
|
public enum CountBlocks {
|
||||||
|
TwoBlocks(2),
|
||||||
|
FourBlocks(4),
|
||||||
|
SixBlocks(6);
|
||||||
|
private final int Value;
|
||||||
|
CountBlocks(int count){
|
||||||
|
Value=count;
|
||||||
|
}
|
||||||
|
public int GetBlockCount(){
|
||||||
|
return Value;
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,9 @@
|
|||||||
package src;
|
package src;
|
||||||
|
|
||||||
public enum Direction {
|
public enum Direction {
|
||||||
Up,
|
Up(1),
|
||||||
Down,
|
Down(2),
|
||||||
Left,
|
Left(3),
|
||||||
Right;
|
Right(4);
|
||||||
|
Direction(int value){}
|
||||||
}
|
}
|
||||||
|
50
src/DrawingBlock.java
Normal file
50
src/DrawingBlock.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package src;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingBlock {
|
||||||
|
private CountBlocks _block;
|
||||||
|
|
||||||
|
public void SetBlockCount(int count){
|
||||||
|
for (CountBlocks temp: CountBlocks.values()){
|
||||||
|
if (temp.GetBlockCount() == count){
|
||||||
|
_block=temp;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawBlock(Graphics2D g, int _startPosX, int _startPosY) {
|
||||||
|
Graphics2D g2 = (Graphics2D) g;
|
||||||
|
if (_block.GetBlockCount() <= 2) {
|
||||||
|
g2.setColor(Color.GRAY);
|
||||||
|
g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10);
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10);
|
||||||
|
g2.setColor(Color.GRAY);
|
||||||
|
g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10);
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10);
|
||||||
|
}
|
||||||
|
if (_block.GetBlockCount() <= 4) {
|
||||||
|
g2.setColor(Color.GRAY);
|
||||||
|
g2.fillRect(_startPosX+25,_startPosY+10,10,10);
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
g2.drawRect(_startPosX+25,_startPosY+10,10,10);
|
||||||
|
g2.setColor(Color.GRAY);
|
||||||
|
g2.fillRect(_startPosX+25,_startPosY+20,10,10);
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
g2.drawRect(_startPosX+25,_startPosY+20,10,10);
|
||||||
|
}
|
||||||
|
if (_block.GetBlockCount() <= 6) {
|
||||||
|
g2.setColor(Color.GRAY);
|
||||||
|
g2.fillRect(_startPosX+35,_startPosY+10,10,10);
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
g2.drawRect(_startPosX+35,_startPosY+10,10,10);
|
||||||
|
g2.setColor(Color.GRAY);
|
||||||
|
g2.fillRect(_startPosX+35,_startPosY+20,10,10);
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
g2.drawRect(_startPosX+35,_startPosY+20,10,10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
59
src/DrawingField.java
Normal file
59
src/DrawingField.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package src;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class DrawingField extends JPanel {
|
||||||
|
private final FormWarship Field;
|
||||||
|
DrawingWarship _warship;
|
||||||
|
public DrawingField(FormWarship field) {
|
||||||
|
this.Field = field;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2 =(Graphics2D)g;
|
||||||
|
if (_warship!=null)
|
||||||
|
_warship.DrawTransport(g2);
|
||||||
|
else return;
|
||||||
|
}
|
||||||
|
public void UpButtonAction(){
|
||||||
|
if (_warship!=null)
|
||||||
|
_warship.MoveTransport(Direction.Up);
|
||||||
|
else
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
public void DownButtonAction(){
|
||||||
|
if (_warship!=null)
|
||||||
|
_warship.MoveTransport(Direction.Down);
|
||||||
|
else
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
public void RightButtonAction(){
|
||||||
|
if (_warship!=null)
|
||||||
|
_warship.MoveTransport(Direction.Right);
|
||||||
|
else
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
public void LeftButtonAction(){
|
||||||
|
if (_warship!=null)
|
||||||
|
_warship.MoveTransport(Direction.Left);
|
||||||
|
else
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
public void CreateButtonAction(){
|
||||||
|
Random rand=new Random();
|
||||||
|
_warship=new DrawingWarship();
|
||||||
|
_warship.Init(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)));
|
||||||
|
_warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
|
||||||
|
Field.SpeedLabel.setText("Скорость: "+_warship.GetWarship().GetSpeed());
|
||||||
|
Field.WeightLabel.setText("Вес: "+_warship.GetWarship().GetWeight());
|
||||||
|
Field.BodyColorLabel.setText("Цвет: "+Integer.toHexString(_warship.GetWarship().GetBodyColor().getRGB()).substring(2));
|
||||||
|
}
|
||||||
|
public void ResizeField(){
|
||||||
|
if (_warship!=null)
|
||||||
|
_warship.ChangeBorders(getWidth(),getHeight());
|
||||||
|
else return;
|
||||||
|
}
|
||||||
|
}
|
@ -1,26 +1,42 @@
|
|||||||
package src;
|
package src;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class DrawingWarship {
|
public class DrawingWarship {
|
||||||
private EntityWarship Warship;
|
private EntityWarship Warship;
|
||||||
|
public DrawingBlock Blocks;
|
||||||
public EntityWarship GetWarship(){return Warship;}
|
public EntityWarship GetWarship(){return Warship;}
|
||||||
|
|
||||||
private float _startPosX;
|
private int _startPosX;
|
||||||
private float _startPosY;
|
private int _startPosY;
|
||||||
|
|
||||||
private Integer _pictureWidth = null;
|
private Integer _pictureWidth = null;
|
||||||
private Integer _pictureHeight = null;
|
private Integer _pictureHeight = null;
|
||||||
|
|
||||||
private final int _warshipWidth = 94;
|
private final int _warshipWidth = 114;
|
||||||
private final int _warshipHeight = 40;
|
private final int _warshipHeight = 40;
|
||||||
|
|
||||||
public void Init(int speed, float weight, Color bodyColor)
|
public void Init(int speed, float weight, Color bodyColor)
|
||||||
{
|
{
|
||||||
Warship = new EntityWarship();
|
Warship = new EntityWarship();
|
||||||
Warship.Init(speed, weight, bodyColor);
|
Warship.Init(speed, weight, bodyColor);
|
||||||
|
Blocks= new DrawingBlock();
|
||||||
|
Blocks.SetBlockCount(BlockRandom());
|
||||||
|
}
|
||||||
|
public int BlockRandom(){
|
||||||
|
Random rand = new Random();
|
||||||
|
int resRand = rand.nextInt(3);
|
||||||
|
if(resRand == 0){
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
if(resRand == 1){
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
if(resRand == 2){
|
||||||
|
return 6;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetPosition(int x, int y, int width, int height)
|
public void SetPosition(int x, int y, int width, int height)
|
||||||
{
|
{
|
||||||
if (width >= x + _warshipWidth && height >= y + _warshipHeight && x >= 0 && y >= 0)
|
if (width >= x + _warshipWidth && height >= y + _warshipHeight && x >= 0 && y >= 0)
|
||||||
@ -31,7 +47,6 @@ public class DrawingWarship {
|
|||||||
_pictureHeight = height;
|
_pictureHeight = height;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MoveTransport(Direction direction)
|
public void MoveTransport(Direction direction)
|
||||||
{
|
{
|
||||||
if (_pictureWidth == null || _pictureHeight == null)
|
if (_pictureWidth == null || _pictureHeight == null)
|
||||||
@ -70,17 +85,60 @@ public class DrawingWarship {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawTransport(Graphics g){
|
public void DrawTransport(Graphics g){
|
||||||
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Graphics2D g2 = (Graphics2D) g;
|
Graphics2D g2 = (Graphics2D) g;
|
||||||
float[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4};
|
//главная палуба
|
||||||
float[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40};
|
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.DrawBlock(g2, _startPosX, _startPosY);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,15 +5,12 @@ import java.util.Random;
|
|||||||
|
|
||||||
public class EntityWarship {
|
public class EntityWarship {
|
||||||
private int Speed;
|
private int Speed;
|
||||||
private void SetSpeed(int speed){this.Speed = speed;}
|
|
||||||
public int GetSpeed(){return Speed;}
|
public int GetSpeed(){return Speed;}
|
||||||
|
|
||||||
private float Weight;
|
private float Weight;
|
||||||
private void SetWeight(float weight){this.Weight = weight;}
|
|
||||||
public float GetWeight(){return Weight;}
|
public float GetWeight(){return Weight;}
|
||||||
|
|
||||||
private Color BodyColor ;
|
private Color BodyColor ;
|
||||||
private void SetBodyColor (Color bodyColor){this.BodyColor = bodyColor;}
|
|
||||||
public Color GetBodyColor (){return BodyColor;}
|
public Color GetBodyColor (){return BodyColor;}
|
||||||
|
|
||||||
public float Step;
|
public float Step;
|
||||||
@ -23,7 +20,7 @@ public class EntityWarship {
|
|||||||
Speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed;
|
Speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed;
|
||||||
Weight = weight <= 0 ? rnd.nextInt(30)+40 : weight;
|
Weight = weight <= 0 ? rnd.nextInt(30)+40 : weight;
|
||||||
BodyColor= bodyColor;
|
BodyColor= bodyColor;
|
||||||
Step = Speed * 100 / Weight;
|
Step = Speed * 2000 / Weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,138 @@
|
|||||||
package src;
|
package src;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
public class FormWarship extends JFrame{
|
public class FormWarship extends JFrame{
|
||||||
|
private int Width;
|
||||||
|
private int Height;
|
||||||
|
|
||||||
|
JPanel BottomPanel = new JPanel();
|
||||||
|
JPanel CreatePanel = new JPanel();
|
||||||
|
JPanel BottomAndCreatePanel = new JPanel();
|
||||||
|
JPanel DimentionPanel = new JPanel();
|
||||||
|
JPanel UPanel = new JPanel();
|
||||||
|
JPanel DPanel = new JPanel();
|
||||||
|
JPanel LRPanel = new JPanel();
|
||||||
|
|
||||||
|
JLabel SpeedLabel = new JLabel("Скорость: ");
|
||||||
|
JLabel WeightLabel = new JLabel("Вес: ");
|
||||||
|
JLabel BodyColorLabel = new JLabel("Цвет: ");
|
||||||
|
|
||||||
|
DrawingField field = new DrawingField(this);
|
||||||
|
|
||||||
|
JButton ButtonCreate=new JButton("Создать");
|
||||||
|
|
||||||
|
Icon iconUp = new ImageIcon("Resource\\arrowUp.jpg");
|
||||||
|
JButton ButtonUp = new JButton(iconUp);
|
||||||
|
|
||||||
|
Icon iconDown = new ImageIcon("Resource\\arrowDown.jpg");
|
||||||
|
JButton ButtonDown = new JButton(iconDown);
|
||||||
|
|
||||||
|
Icon iconRight = new ImageIcon("Resource\\arrowRight.jpg");
|
||||||
|
JButton ButtonRight = new JButton(iconRight);
|
||||||
|
|
||||||
|
Icon iconLeft = new ImageIcon("Resource\\arrowLeft.jpg");
|
||||||
|
JButton ButtonLeft = new JButton(iconLeft);
|
||||||
|
|
||||||
|
|
||||||
|
public FormWarship(){
|
||||||
|
super("Военный корабль");
|
||||||
|
setSize(700,400);
|
||||||
|
Width = getWidth();
|
||||||
|
Height = getHeight();
|
||||||
|
ShowWindow();
|
||||||
|
RefreshWindow();
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ShowWindow(){
|
||||||
|
|
||||||
|
Dimension dimen = new Dimension(30,30);
|
||||||
|
|
||||||
|
ButtonUp.setPreferredSize(dimen);
|
||||||
|
ButtonUp.addActionListener(e->{
|
||||||
|
field.UpButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
ButtonDown.setPreferredSize(dimen);
|
||||||
|
ButtonDown.addActionListener(e->{
|
||||||
|
field.DownButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
ButtonRight.setPreferredSize(dimen);
|
||||||
|
ButtonRight.addActionListener(e->{
|
||||||
|
field.RightButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
ButtonLeft.setPreferredSize(dimen);
|
||||||
|
ButtonLeft.addActionListener(e->{
|
||||||
|
field.LeftButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0));
|
||||||
|
LRPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
LRPanel.add(ButtonLeft);
|
||||||
|
LRPanel.add(ButtonRight);
|
||||||
|
|
||||||
|
UPanel.setLayout(new FlowLayout());
|
||||||
|
UPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
UPanel.add(ButtonUp);
|
||||||
|
|
||||||
|
DPanel.setLayout(new FlowLayout());
|
||||||
|
DPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
DPanel.add(ButtonDown);
|
||||||
|
|
||||||
|
DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS));
|
||||||
|
DimentionPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
DimentionPanel.add(UPanel);
|
||||||
|
DimentionPanel.add(LRPanel);
|
||||||
|
DimentionPanel.add(DPanel);
|
||||||
|
add(DimentionPanel);
|
||||||
|
|
||||||
|
CreatePanel.setLayout(new FlowLayout());
|
||||||
|
CreatePanel.setBackground(new Color(0,0,0,0));
|
||||||
|
CreatePanel.add(ButtonCreate);
|
||||||
|
ButtonCreate.addActionListener(e->{
|
||||||
|
field.CreateButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
BottomPanel.setLayout(new FlowLayout());
|
||||||
|
BottomPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
BottomPanel.add(SpeedLabel);
|
||||||
|
BottomPanel.add(WeightLabel);
|
||||||
|
BottomPanel.add(BodyColorLabel);
|
||||||
|
|
||||||
|
BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS));
|
||||||
|
BottomAndCreatePanel.setBackground(new Color(0,0,0,0));
|
||||||
|
BottomAndCreatePanel.add(CreatePanel);
|
||||||
|
BottomAndCreatePanel.add(BottomPanel);
|
||||||
|
|
||||||
|
add(BottomAndCreatePanel);
|
||||||
|
add(field);
|
||||||
|
|
||||||
|
addComponentListener(new ComponentAdapter() {
|
||||||
|
@Override
|
||||||
|
public void componentResized(ComponentEvent e) {
|
||||||
|
super.componentResized(e);
|
||||||
|
Width=getWidth();
|
||||||
|
Height=getHeight();
|
||||||
|
|
||||||
|
field.ResizeField();
|
||||||
|
repaint();
|
||||||
|
RefreshWindow();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
public void RefreshWindow(){
|
||||||
|
field.setBounds(0,0,Width,Height);
|
||||||
|
BottomAndCreatePanel.setBounds(-220,Height-110,Width,80);
|
||||||
|
DimentionPanel.setBounds(Width-170,Height-170,190,140);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
package src;
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
new FormWarship();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user