Aleikin A.M. HardLabWork1 #1

Closed
aleyckin wants to merge 2 commits from LabWork01 into master
14 changed files with 510 additions and 2 deletions

View File

@ -2,7 +2,9 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

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

12
Direction.java Normal file
View File

@ -0,0 +1,12 @@
public enum Direction
{
Up(1),
Down(2),
Left(3),
Right(4);
Direction(int i )
{
}
}

36
DrawEngines.java Normal file
View File

@ -0,0 +1,36 @@
import java.awt.*;
public class DrawEngines
{
private Engines numEngines;
public void SetNumEngines(int i) { numEngines = Engines.GetNumEngines(i); }
public void DrawningEngines(float _startPosX, float _startPosY, Graphics2D g2d, int _airBomberWidth, int _airBomberHeight)
{
switch(numEngines)
{
case TwoEngines:
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 - 20, 20, 10);
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 + 10, 20, 10);
break;
case FourEngines:
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 - 20, 20, 10);
Review

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

Имеется дублирующися код
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 + 10, 20, 10);
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 15, (int)_startPosY + _airBomberHeight / 2 - 35, 15, 10);
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 15, (int)_startPosY + _airBomberHeight / 2 + 25, 15, 10);
break;
case SixEngines:
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 - 20, 20, 10);
Review

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

Имеется дублирующися код
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 + 10, 20, 10);
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 15, (int)_startPosY + _airBomberHeight / 2 - 35, 15, 10);
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 15, (int)_startPosY + _airBomberHeight / 2 + 25, 15, 10);
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 10, (int)_startPosY + _airBomberHeight / 2 - 50, 10, 10);
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 10, (int)_startPosY + _airBomberHeight / 2 + 40, 10, 10);
break;
}
}
}

170
DrawningBomber.java Normal file
View File

@ -0,0 +1,170 @@
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class DrawningBomber extends JPanel
{
private EntityAirBomber AirBomber;
public EntityAirBomber getAirBomber() { return AirBomber; }
private float _startPosX;
private float _startPosY;
private int _pictureWidth = 0;
private int _pictureHeight = 0;
private final int _airBomberWidth = 100;
private final int _airBomberHeight = 100;
private int NumEngines = 1;
private DrawEngines drawEngines = new DrawEngines();
public void Init(int speed, float weight, Color bodyColor)
{
AirBomber = new EntityAirBomber();
AirBomber.Init(speed, weight, bodyColor);
Random random = new Random();
drawEngines.SetNumEngines(random.nextInt(1, 4));
}
public void SetPosition(int x, int y, int width, int height)
{
if (x + _airBomberWidth > width || x < 0) return;
else _startPosX = x;
if (y + _airBomberHeight> height || y < 0) return;
else _startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void MoveTransport(Direction direction)
{
if (!(_pictureWidth > 0 || _pictureHeight > 0))
{
return;
}
switch (direction)
{
case Right:
if (_startPosX + _airBomberWidth + AirBomber.GetStep() < _pictureWidth)
{
_startPosX += AirBomber.GetStep();
}
break;
case Left:
if (_startPosX - AirBomber.GetStep() > 0)
{
_startPosX -= AirBomber.GetStep();
}
break;
case Up:
if (_startPosY - AirBomber.GetStep() > 0)
{
_startPosY -= AirBomber.GetStep();
}
break;
case Down:
if (_startPosY + _airBomberHeight + AirBomber.GetStep() < _pictureHeight)
{
_startPosY += AirBomber.GetStep();
}
break;
}
}
public void DrawTransport()
{
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0)
{
return;
}
repaint();
}
@Override
public void paintComponent(Graphics g)
{
if (getAirBomber() == null || _startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0)
{
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(AirBomber.GetColor());
int[] xPos = {
(int)(_startPosX + _airBomberWidth),
(int)(_startPosX + _airBomberWidth),
(int)(_startPosX + _airBomberWidth - 10),
(int)(_startPosX + _airBomberWidth - 10)};
int[] yPos = {
(int)(_startPosY + _airBomberHeight / 2 - 20),
(int)(_startPosY + _airBomberHeight / 2 + 20),
(int)(_startPosY + _airBomberHeight / 2 + 10),
(int)(_startPosY + _airBomberHeight / 2 - 10)};
g2d.fillPolygon(xPos, yPos, xPos.length);
xPos = new int[] {
(int)(_startPosX + _airBomberWidth / 2),
(int)(_startPosX + _airBomberWidth / 2 + 10),
(int)(_startPosX + _airBomberWidth / 2 + 20),
(int)(_startPosX + _airBomberWidth / 2 + 10),
(int)(_startPosX + _airBomberWidth / 2)};
yPos = new int[] {
(int)(_startPosY),
(int)(_startPosY),
(int)(_startPosY + _airBomberHeight / 2),
(int)(_startPosY + _airBomberHeight),
(int)(_startPosY + _airBomberHeight)};
g2d.fillPolygon(xPos, yPos, xPos.length);
xPos = new int[] {
(int)(_startPosX + 20),
(int)(_startPosX + _airBomberWidth),
(int)(_startPosX + _airBomberWidth),
(int)(_startPosX + 20)};
yPos = new int[] {
(int)(_startPosY + _airBomberHeight / 2 - 5),
(int)(_startPosY + _airBomberHeight / 2 - 5),
(int)(_startPosY + _airBomberHeight / 2 + 5),
(int)(_startPosY + _airBomberHeight / 2 + 5)};
g2d.fillPolygon(xPos, yPos, xPos.length);
xPos = new int[] {
(int)(_startPosX),
(int)(_startPosX + 20),
(int)(_startPosX + 20)};
yPos = new int[] {
(int)(_startPosY + _airBomberHeight / 2),
(int)(_startPosY + _airBomberHeight / 2 - 5),
(int)(_startPosY + _airBomberHeight / 2 + 5)};
g2d.fillPolygon(xPos, yPos, xPos.length);
drawEngines.DrawningEngines(_startPosX, _startPosY, g2d, _airBomberWidth, _airBomberHeight);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _airBomberWidth || _pictureHeight <= _airBomberHeight)
{
_pictureWidth = 0;
_pictureHeight = 0;
return;
}
if (_startPosX + _airBomberWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _airBomberWidth;
}
if (_startPosY + _airBomberHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _airBomberHeight;
}
}
}

24
Engines.java Normal file
View File

@ -0,0 +1,24 @@
public enum Engines
{
TwoEngines(1),
FourEngines(2),
SixEngines(6);
Engines(int i) {
}
public static Engines GetNumEngines(int i)
{
switch(i)
{
case 1:
return TwoEngines;
case 2:
return FourEngines;
case 3:
return SixEngines;
}
return null;
}
}

29
EntityAirBomber.java Normal file
View File

@ -0,0 +1,29 @@
import java.awt.*;
import java.util.*;
public class EntityAirBomber
{
private int Speed;
private float Weight;
private Color BodyColor;
private float Step;
public void Init(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed;
Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight;
BodyColor = bodyColor;
Step = Speed * 100 / Weight;
}
public int GetSpeed() { return Speed; }
public float GetWeight() {
return Weight;
}
public Color GetColor() { return BodyColor; }
public float GetStep() { return Step; }
}

107
FormAirBomber.form Normal file
View File

@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormAirBomber">
<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="500" height="400"/>
</constraints>
<properties>
<opaque value="true"/>
<preferredSize width="800" height="600"/>
</properties>
<border type="none"/>
<children>
<component id="2ea16" 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>
<text value=""/>
</properties>
</component>
<component id="4eb88" 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>
<horizontalAlignment value="0"/>
<text value=""/>
</properties>
</component>
<component id="9f55" class="DrawningBomber" binding="pictureBomber">
<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">
<preferred-size width="10" height="286"/>
</grid>
</constraints>
<properties/>
</component>
<component id="fb937" 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>
<toolbar id="e747d" binding="statusStrip">
<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">
<minimum-size width="-1" height="20"/>
<preferred-size width="-1" height="20"/>
<maximum-size width="-1" height="20"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
<border type="none"/>
<children/>
</toolbar>
<hspacer id="d6bea">
<constraints>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="d86ff">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="9e2ce" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="118" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="4f60a" 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>
</children>
</grid>
</form>

115
FormAirBomber.java Normal file
View File

@ -0,0 +1,115 @@
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class FormAirBomber {
private JToolBar statusStrip;
public JPanel Mainpanel;
private DrawningBomber pictureBomber;
private JButton buttonRight;
private JButton buttonCreate;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonDown;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void Draw()
{
if (pictureBomber.getAirBomber() == null) return;
pictureBomber.DrawTransport();
}
private void ButtonMove_Click(String name)
{
if (pictureBomber == null) return;
switch (name)
{
case "buttonLeft":
pictureBomber.MoveTransport(Direction.Left);
break;
case "buttonUp":
pictureBomber.MoveTransport(Direction.Up);
break;
case "buttonRight":
pictureBomber.MoveTransport(Direction.Right);
break;
case "buttonDown":
pictureBomber.MoveTransport(Direction.Down);
break;
}
Draw();
}
public FormAirBomber() {
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
statusStrip.add(LabelBox);
try {
Image img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowUp.jpg"));
buttonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowLeft.jpg"));
buttonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowDown.jpg"));
buttonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowRight.jpg"));
buttonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
pictureBomber.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
pictureBomber.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBomber.getWidth(), pictureBomber.getHeight());
JLabelSpeed.setText("орость: " + pictureBomber.getAirBomber().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + pictureBomber.getAirBomber().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + pictureBomber.getAirBomber().GetColor() + " "));
Draw();
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonUp");
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonLeft");
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonDown");
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonRight");
}
});
pictureBomber.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
pictureBomber.ChangeBorders(pictureBomber.getWidth(), pictureBomber.getHeight());
Draw();
}
});
}
}

BIN
Images/ArrowDown.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 488 B

BIN
Images/ArrowLeft.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 493 B

BIN
Images/ArrowRight.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 491 B

BIN
Images/ArrowUp.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 484 B

13
Main.java Normal file
View File

@ -0,0 +1,13 @@
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Hard №1");
frame.setContentPane(new FormAirBomber().Mainpanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();
frame.setSize(800, 600);
frame.setVisible(true);
}
}