Kharlamov A.A. Lab Work 01 Hard #1

Closed
Kharlamov wants to merge 4 commits from Lab01 into master
11 changed files with 437 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ProjectStormtrooperHard.iml" filepath="$PROJECT_DIR$/.idea/ProjectStormtrooperHard.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

6
Direction.java Normal file
View File

@ -0,0 +1,6 @@
public enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}

View File

@ -0,0 +1,5 @@
public enum DirectionEnginesOnStormtrooper {
TWO,
FOUR,
SIX
}

38
DrawingEngines.java Normal file
View File

@ -0,0 +1,38 @@
import java.awt.*;
public class DrawingEngines {
private DirectionEnginesOnStormtrooper enginesCount;
public void SetNewEngines(int countEngines){
if (countEngines == 4) {
enginesCount = DirectionEnginesOnStormtrooper.FOUR;
} else if (countEngines == 6) {
enginesCount = DirectionEnginesOnStormtrooper.SIX;
}
else {
enginesCount = DirectionEnginesOnStormtrooper.TWO;
}
}
public void Draw(Graphics2D g, int x, int y, Color color) {
g.setColor(color != null ? color : Color.BLACK);
switch (enginesCount) {
case TWO:
g.fillRect(x + 50, y, 20, 5);
g.fillRect(x + 50, y+95, 20, 5);
break;
case FOUR:
g.fillRect(x + 50, y, 20, 5);
Review

Имеется дублирующися код

Имеется дублирующися код
g.fillRect(x + 50, y+10, 20, 5);
g.fillRect(x + 50, y+85, 20, 5);
g.fillRect(x + 50, y+95, 20, 5);
break;
case SIX:
g.fillRect(x + 50, y, 20, 5);
Review

Имеется дублирующися код

Имеется дублирующися код
g.fillRect(x + 50, y+10, 20, 5);
g.fillRect(x + 50, y+20, 20, 5);
g.fillRect(x + 50, y+75, 20, 5);
g.fillRect(x + 50, y+85, 20, 5);
g.fillRect(x + 50, y+95, 20, 5);
break;
}
}
}

180
DrawingStormtrooper.java Normal file
View File

@ -0,0 +1,180 @@
import java.awt.*;
import java.util.Random;
public class DrawingStormtrooper {
public EntityStormtrooper Stormtrooper;
public DrawingEngines drawingEngines;
public float _startPosX;
public float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private static final int _StormWidth = 135;
private static final int _StormHeight = 100;
public void Init(int speed, float weight, Color bodyColor, int numberOfEngines){
Stormtrooper = new EntityStormtrooper();
drawingEngines = new DrawingEngines();
Stormtrooper.Init(speed, weight, bodyColor);
System.out.println(numberOfEngines + "");
drawingEngines.SetNewEngines(numberOfEngines);
}
public EntityStormtrooper getStormtrooper() {
return Stormtrooper;
}
public void SetPosition(int x, int y, int width, int height){
if ((x > 0 && y > 0) && (_StormHeight + y < height) && (_StormWidth + x < width))
{
_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 + _StormWidth + Stormtrooper.Step < _pictureWidth)
{
_startPosX += Stormtrooper.Step;
}
break;
//влево
case LEFT:
if (_startPosX - Stormtrooper.Step > 0)
{
_startPosX -= Stormtrooper.Step;
}
break;
//вверх
case UP:
if (_startPosY - Stormtrooper.Step > 0)
{
_startPosY -= Stormtrooper.Step;
}
break;
//вниз
case DOWN:
if (_startPosY + _StormHeight + Stormtrooper.Step < _pictureHeight)
{
_startPosY += Stormtrooper.Step;
}
break;
}
}
public void DrawTransport(Graphics2D g)
{
if (_startPosX < 0 || _startPosY < 0
|| _pictureHeight == null || _pictureWidth == null)
{
return;
}
int _startPosXInt = (int)_startPosX;
int _startPosYInt = (int)_startPosY;
//фюзеляж самолёта
g.fillRect(_startPosXInt + 20, _startPosYInt + 40, 100, 20);
//отрисовка крыла
int[] wingX =
{
_startPosXInt + 60 ,
_startPosXInt + 80 ,
_startPosXInt+90 ,
_startPosXInt+90 ,
_startPosXInt+80 ,
_startPosXInt + 60
};
int[] wingY =
{
_startPosYInt,
_startPosYInt,
_startPosYInt + 40,
_startPosYInt + 60,
_startPosYInt + 100,
_startPosYInt + 100
};
g.setColor(Stormtrooper.getBodyColor());
g.fillPolygon(wingX, wingY, wingY.length);
//нос самолёта
int[] noseX =
{
_startPosXInt + 20,
_startPosXInt + 20,
_startPosXInt
};
int[] noseY =
{
_startPosYInt + 60,
_startPosYInt + 40,
_startPosYInt +50
};
g.fillPolygon(noseX, noseY, noseX.length);
//стабилизатор
int[] stabX =
{
_startPosXInt + 120,
_startPosXInt + 135,
_startPosXInt + 135,
_startPosXInt + 120
};
int[] stabY =
{
_startPosYInt + 60,
_startPosYInt + 80,
_startPosYInt + 20,
_startPosYInt + 40
};
g.fillPolygon(stabX, stabY, stabX.length);
//отрисовка двигателей
drawingEngines.Draw(g, (int) _startPosX, (int) _startPosY,Stormtrooper.getBodyColor());
g.setColor(Color.BLACK);
g.drawRect(_startPosXInt + 20, _startPosYInt + 40, 100, 20);
g.drawPolygon(wingX, wingY, wingX.length);
g.drawPolygon(noseX, noseY, noseX.length);
g.drawPolygon(stabX, stabY, stabX.length);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _StormWidth || _pictureHeight <= _StormHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _StormWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _StormWidth;
}
if (_startPosY + _StormHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _StormHeight;
}
}
}

33
EntityStormtrooper.java Normal file
View File

@ -0,0 +1,33 @@
import java.awt.Color;
import java.util.Random;
public class EntityStormtrooper {
private int Speed = 0;
private float Weight = 0;
private Color BodyColor;
public float Step;
public int getSpeed() {
return Speed;
}
public float getWeight() {
return Weight;
}
public Color getBodyColor() {
return BodyColor;
}
public void Init(int speed, float weight, Color bodyColor){
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50 + 1) +50 : speed; //генерация в диапазоне от 50 до 100
Weight = weight <= 0 ? rnd.nextInt(50 + 1) +50 : weight;
Step = Speed * 100 / Weight;
BodyColor = bodyColor;
}
}

141
FormStormtrooper.java Normal file
View File

@ -0,0 +1,141 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Random;
public class FormStormtrooper extends JComponent {
private DrawingStormtrooper _stormtrooper;
public static void main(String[] args) {
FormStormtrooper formStormtrooper = new FormStormtrooper();
}
public FormStormtrooper() {
JFrame form = new JFrame("Штурмовик");
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setSize(800, 500);
form.setVisible(true);
form.setLocationRelativeTo(null);
form.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
if(_stormtrooper != null) _stormtrooper.ChangeBorders(getWidth(), getHeight());
repaint();
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
});
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new GridBagLayout());
setLayout(new BorderLayout());
form.add(statusPanel, BorderLayout.SOUTH);
Label speedLabel = new Label("Скорость: ");
Label weightLabel = new Label("Вес: ");
Label colorLabel = new Label("Цвет: ");
JButton createButton = new JButton("Создать");
createButton.addActionListener(e -> {
int[] countBlocks = {2, 4, 6};
Random rnd = new Random();
_stormtrooper = new DrawingStormtrooper();
_stormtrooper.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
rnd.nextInt(0, 256)), countBlocks[rnd.nextInt(0, 3)]);
_stormtrooper.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 80);
speedLabel.setText("Скорость: " + _stormtrooper.Stormtrooper.getSpeed());
weightLabel.setText("Вес: " + (int)_stormtrooper.Stormtrooper.getWeight());
colorLabel.setText("Цвет: " + _stormtrooper.Stormtrooper.getBodyColor().getRed() + " " + _stormtrooper.Stormtrooper.getBodyColor().getGreen() + " " + _stormtrooper.Stormtrooper.getBodyColor().getBlue() );
repaint();
});
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
statusPanel.add(createButton,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
statusPanel.add(speedLabel,c);
c.gridx = 2;
c.gridy = 1;
statusPanel.add(weightLabel,c);
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(colorLabel,c);
JButton upButton = new JButton("");
JButton rightButton = new JButton("");
JButton leftButton = new JButton("");
JButton downButton = new JButton("");
upButton.addActionListener(e -> {
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.UP);
repaint();
});
rightButton.addActionListener(e -> {
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.RIGHT);
repaint();
});
leftButton.addActionListener(e -> {
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.LEFT);
repaint();
});
downButton.addActionListener(e -> {
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.DOWN);
repaint();
});
c.fill = GridBagConstraints.NONE;
c.gridx = 8;
c.gridy = 0;
c.gridwidth = 1;
statusPanel.add(upButton,c);
c.gridx = 7;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(leftButton,c);
c.gridx = 9;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(rightButton,c);
c.gridx = 8;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(downButton,c);
form.getContentPane().add(this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (_stormtrooper != null) _stormtrooper.DrawTransport(g2);
super.repaint();
}
}