PIbd-21_MalafeevL.S._Cruiser_Hard Lab1 #1

Closed
mallafeev wants to merge 2 commits from lab1 into master
11 changed files with 332 additions and 1 deletions

22
src/Block.java Normal file
View File

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

14
src/Direction.java Normal file
View File

@ -0,0 +1,14 @@
public enum Direction {
Up(1),
Down(2),
Left (3),
Right(4)
;
private final int Direct;
Direction(int i) {
this.Direct=i;
}
public int GetDirect() {
return Direct;
}
}

45
src/DrawBlock.java Normal file
View File

@ -0,0 +1,45 @@
import java.awt.*;
public class DrawBlock {
private Block blockCount;
public void SetBlockCount(int count){
blockCount = Block.GetBlock(count);
}
public void DrawningBlock(int _startPosX, int _startPosY, int _cruiserWidth, Graphics2D g2d, Color bodyColor){
switch (blockCount)
{
case One:
break;
case Two:
DrawFirstDop(_startPosX,_startPosY,_cruiserWidth,g2d,bodyColor);
break;
case Three:
DrawSecondDop(_startPosX,_startPosY,_cruiserWidth,g2d,bodyColor);
break;
}
}
public void DrawFirstDop(int _startPosX, int _startPosY, int _cruiserWidth, Graphics2D g2d, Color bodyColor) {
Polygon elementsSec = new Polygon();
elementsSec.addPoint(_startPosX + 55,_startPosY + 25);
elementsSec.addPoint(_startPosX + 65,_startPosY + 25);
elementsSec.addPoint(_startPosX + 65,_startPosY + 35);
elementsSec.addPoint(_startPosX + 55,_startPosY + 35);
g2d.setColor(Color.WHITE);
g2d.fillPolygon(elementsSec);
}
public void DrawSecondDop(int _startPosX, int _startPosY, int _cruiserWidth, Graphics2D g2d, Color bodyColor) {
DrawFirstDop(_startPosX,_startPosY,_cruiserWidth,g2d,bodyColor);
Polygon elementsThree = new Polygon();
elementsThree.addPoint(_startPosX + 75,_startPosY + 15);
elementsThree.addPoint(_startPosX + 85,_startPosY + 15);
elementsThree.addPoint(_startPosX + 85,_startPosY + 45);
elementsThree.addPoint(_startPosX + 75,_startPosY + 45);
g2d.setColor(Color.WHITE);
g2d.fillPolygon(elementsThree);
}
}

108
src/DrawingCruiser.java Normal file
View File

@ -0,0 +1,108 @@
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.*;
public class DrawingCruiser {
private EntityCruiser cruiser; //Класс-сущность
public EntityCruiser GetCruiser(){return cruiser;}
public int _startPosX; //Координаты отрисовки по оси x
public int _startPosY; //Координаты отрисовки по оси y
private Integer _pictureWidth = null; //Ширина окна
private Integer _pictureHeight = null; //Высота окна
private final int _cruiserWidth = 150; //Ширина отрисовки крейсера
private final int _cruiserHeight = 60; //Высота отрисовки крейсера
private DrawBlock drawBlock = new DrawBlock();
//Инициализация
public void Init(int speed, float weight, Color bodyColor, boolean rocketMines, boolean helipad)
{
cruiser = new EntityCruiser();
cruiser.Init(speed,weight,bodyColor, rocketMines, helipad);
Random random = new Random();
drawBlock.SetBlockCount(random.nextInt(1, 4));
}
//Начальные коордитанты
public void SetPosition(int x, int y, int width, int height)
{
if (width < _cruiserWidth || height < _cruiserHeight) return;
Random random = new Random();
_startPosX = x < 0 || x + _cruiserWidth > width ? random.nextInt(0, width - _cruiserWidth) : x;
_startPosY = y < 0 || y + _cruiserHeight > height ? random.nextInt(0, height - _cruiserHeight) : y;
_pictureWidth = width;
_pictureHeight = height;
}
//Движение транспорта по координатам
public void MoveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null) return;
switch (direction)
{
case Left: //Влево
if (_startPosX - cruiser.GetStep() > 0) _startPosX -= cruiser.GetStep();
break;
case Up: //Вверх
if (_startPosY - cruiser.GetStep() > 0) _startPosY -= cruiser.GetStep();
break;
case Right: //Вправо
if (_startPosX + _cruiserWidth + cruiser.GetStep() < _pictureWidth) _startPosX += cruiser.GetStep();
break;
case Down: //Вниз
if (_startPosY + _cruiserHeight + cruiser.GetStep() < _pictureHeight) _startPosY += cruiser.GetStep();
break;
}
}
//Отрисовка транспорта
public void DrawTransport(Graphics g)
{
if (GetCruiser() == null) return;
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null)
{
return;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(cruiser.GetBodyColor());
// палуба
Polygon paluba = new Polygon();
paluba.addPoint(_startPosX + 10,_startPosY);
paluba.addPoint(_startPosX + 110,_startPosY);
paluba.addPoint(_startPosX + 160,_startPosY + 30);
paluba.addPoint(_startPosX + 110,_startPosY + 60);
paluba.addPoint(_startPosX + 10,_startPosY + 60);
g2d.fillPolygon(paluba);
// элементы
Polygon elements = new Polygon();
elements.addPoint(_startPosX + 50,_startPosY + 20);
elements.addPoint(_startPosX + 70,_startPosY + 20);
elements.addPoint(_startPosX + 70,_startPosY + 10);
elements.addPoint(_startPosX + 90,_startPosY + 10);
elements.addPoint(_startPosX + 90,_startPosY + 50);
elements.addPoint(_startPosX + 70,_startPosY + 50);
elements.addPoint(_startPosX + 70,_startPosY + 40);
elements.addPoint(_startPosX + 50,_startPosY + 40);
g2d.setColor(Color.BLACK);
g2d.fillPolygon(elements);
g2d.fill(new Ellipse2D.Double(_startPosX + 100, _startPosY + 20, 20, 20));
// турбины
g2d.setColor(Color.BLACK);
g2d.fillRect(_startPosX, _startPosY + 10, 10, 20);
g2d.fillRect(_startPosX, _startPosY + 35, 10, 20);
// шахты
if (cruiser.GetRocketMines()) {
g2d.setColor(Color.green);
g2d.fillRect(_startPosX + 15, _startPosY + 10, 10, 15);
g2d.fillRect(_startPosX + 30, _startPosY + 10, 10, 15);
}
// верт площадка
if (cruiser.GetHelipad()) {
g2d.setColor(Color.ORANGE);
g2d.fill(new Ellipse2D.Double(_startPosX + 15, _startPosY + 25, 25, 25));
drawBlock.DrawningBlock(_startPosX, _startPosY, _cruiserWidth, g2d, cruiser.GetBodyColor());
}
}
}

31
src/EntityCruiser.java Normal file
View File

@ -0,0 +1,31 @@
import java.awt.*;
import java.util.*;
public class EntityCruiser {
private int Speed; //Скорость
private float Weight; //Вес
private Color BodyColor; //Цвет
private float Step; //Шаг при перемещении
private boolean Helipad; //Наличие верт. площадки
private boolean RocketMines; //Наличие шахт
//Инициализация
public void Init(int speed, float weight, Color bodyColor, boolean rocketMines, boolean helipad)
{
Random random = new Random();
Speed = speed <= 0 ? random.nextInt(50, 150) : speed;
Weight = weight <= 0 ? random.nextInt(50, 150) : weight;
BodyColor = bodyColor;
RocketMines = rocketMines;
Helipad = helipad;
Step = Speed * 100 / Weight;
}
public boolean GetHelipad() {return Helipad;}
public boolean GetRocketMines() {return RocketMines;}
public Color GetBodyColor(){
return BodyColor;
}
public float GetStep(){
return Step;
}
}

111
src/FormCruiser.java Normal file
View File

@ -0,0 +1,111 @@
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.util.*;
public class FormCruiser{
private DrawingCruiser _drawingCruiser;
Canvas canv;
public void Draw(){
canv.repaint();
}
public FormCruiser(){
JFrame frame = new JFrame("Cruiser");
JButton buttonCreate = new JButton("Создать");
buttonCreate.setFocusPainted(false);
buttonCreate.setContentAreaFilled(false);
JButton buttonUp = new JButton();
buttonUp.setFocusPainted(false);
buttonUp.setContentAreaFilled(false);
buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon(((new ImageIcon("src/Images/totop.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonDown = new JButton();
buttonDown.setFocusPainted(false);
buttonDown.setContentAreaFilled(false);
buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon(((new ImageIcon("src/Images/todown.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonLeft = new JButton();
buttonLeft.setFocusPainted(false);
buttonLeft.setContentAreaFilled(false);
buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon(((new ImageIcon("src/Images/toleft.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonRight = new JButton();
buttonRight.setFocusPainted(false);
buttonRight.setContentAreaFilled(false);
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon(((new ImageIcon("src/Images/toright.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
buttonCreate.addActionListener(
e -> {
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingCruiser = new DrawingCruiser();
_drawingCruiser.Init(
random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean());
_drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома
random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома
,this.canv.getWidth(), this.canv.getHeight());
canv._drawingCruiser = _drawingCruiser;
Draw();
}
);
ActionListener actionListener = e -> {
if (_drawingCruiser == null){
return;
}
switch ((((JButton)(e.getSource())).getName())){
case "up":
_drawingCruiser.MoveTransport(Direction.Up);
break;
case "down":
_drawingCruiser.MoveTransport(Direction.Down);
break;
case "left":
_drawingCruiser.MoveTransport(Direction.Left);
break;
case "right":
_drawingCruiser.MoveTransport(Direction.Right);
break;
}
Draw();
};
buttonUp.addActionListener(actionListener);
buttonDown.addActionListener(actionListener);
buttonLeft.addActionListener(actionListener);
buttonRight.addActionListener(actionListener);
frame.setSize(910, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 895, 500 - 40); // 40 - const, высота панели сверху
buttonCreate.setBounds(20, 420, 100, 40);
buttonUp.setBounds(800, 380, 40, 40);
buttonDown.setBounds(800, 420, 40, 40);
buttonLeft.setBounds(760, 420, 40, 40);
buttonRight.setBounds(840, 420, 40, 40);
frame.add(canv);
frame.add(buttonCreate);
frame.add(buttonUp);
frame.add(buttonDown);
frame.add(buttonLeft);
frame.add(buttonRight);
frame.setVisible(true);
}
class Canvas extends JComponent{
public DrawingCruiser _drawingCruiser;
public Canvas(){}
public void paintComponent(Graphics g){
if (_drawingCruiser == null){
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
_drawingCruiser.DrawTransport(g2d);
super.repaint();
}
}
}

BIN
src/Images/todown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 888 B

BIN
src/Images/toleft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 904 B

BIN
src/Images/toright.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 892 B

BIN
src/Images/totop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 895 B

View File

@ -1,5 +1,5 @@
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
FormCruiser fm = new FormCruiser();
}
}