Kargin D.E Lab Work 1 #1

Merged
eegov merged 2 commits from LabWork01 into master 2022-11-07 10:06:24 +04:00
16 changed files with 472 additions and 5 deletions

9
.idea/ProjectAirFighterHard.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?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$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml generated Normal file
View File

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

9
.idea/modules.xml generated Normal file
View File

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

6
.idea/vcs.xml generated Normal file
View File

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

11
AirFighter/AirFighter.iml Normal file
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$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="18" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

BIN
AirFighter/Images/Down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
AirFighter/Images/Left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

BIN
AirFighter/Images/Right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

BIN
AirFighter/Images/Up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -0,0 +1,7 @@
// Направления
public enum Direction {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,117 @@
import java.awt.*;
public class DrawningAirFighter {
/// Объект сущности военного самолета
public EntityAirFighter AirFighter;
private DrawningEngine _engine;
// Левая координата отрисовки самолета
private int _startPosX;
// Верхняя кооридната отрисовки самолета
private int _startPosY;
// Ширина окна отрисовки
private int _pictureWidth = 0;
// Высота окна отрисовки
private int _pictureHeight = 0;
// Ширина отрисовки самолета
final int _airFighterWidth = 80;
// Высота отрисовки самолета
final int _airFighterHeight = 70;
// Инициализация свойств
public void Init(int speed, float weight, Color bodyColor, int numberEngine)
{
_engine = new DrawningEngine();
_engine.setCodeEngine(numberEngine);
AirFighter = new EntityAirFighter();
AirFighter.Init(speed, weight, bodyColor);
}
public void SetPosition(int x, int y, int width, int height) {
if(width < _airFighterWidth || height < _airFighterHeight)
{
_pictureWidth = 0;
_pictureHeight = 0;
return;
}
if(x < 0 || x + _airFighterWidth > width)
{
x = width - _airFighterWidth;
}
if(y < 0 || y + _airFighterHeight > height)
{
y = height - _airFighterHeight;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
// Изменение направления перемещения
public void MoveTransport(Direction direction)
{
if (_pictureWidth == 0 || _pictureHeight == 0) {
return;
}
switch (direction)
{
// вправо
case Right:
if (_startPosX + _airFighterWidth + AirFighter.Step() < _pictureWidth)
{
_startPosX += AirFighter.Step();
}
break;
//влево
case Left:
if(_startPosX - AirFighter.Step() >= 0){
_startPosX -= AirFighter.Step();
}
break;
//вверх
case Up:
if(_startPosY - AirFighter.Step() >= 0){
_startPosY -= AirFighter.Step();
}
break;
//вниз
case Down:
if (_startPosY + _airFighterHeight + AirFighter.Step() < _pictureHeight)
{
_startPosY += AirFighter.Step();
}
break;
}
}
// Отрисовка самолета
public void DrawTransport(Graphics g){
int[] pointX = new int[]{ _startPosX + 80, _startPosX + 70, _startPosX + 30, _startPosX + 35, _startPosX + 40, _startPosX + 40, _startPosX, _startPosX, _startPosX + 10, _startPosX + 10, _startPosX, _startPosX, _startPosX + 10, _startPosX + 10, _startPosX, _startPosX + 40, _startPosX + 40, _startPosX + 35, _startPosX + 30, _startPosX + 70, _startPosX + 80};
int[] pointY = new int[]{ _startPosY + 35, _startPosY + 30, _startPosY + 30, _startPosY, _startPosY, _startPosY + 30, _startPosY + 30, _startPosY + 15, _startPosY + 25, _startPosY + 30, _startPosY + 30, _startPosY + 55, _startPosY + 45, _startPosY + 40, _startPosY + 40, _startPosY + 40, _startPosY + 70, _startPosY + 70, _startPosY + 40, _startPosY + 40, _startPosY + 35};
g.setColor(AirFighter.getBodyColor());
g.fillPolygon(pointX, pointY, 20);
g.setColor(Color.BLACK);
g.drawPolyline(pointX, pointY, 21);
_engine.DrawEngine(g, _startPosX, _startPosY, AirFighter.getBodyColor());
}
// Смена границ формы отрисовки
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _airFighterWidth || _pictureHeight <= _airFighterHeight)
{
_pictureWidth = 0;
_pictureHeight = 0;
return;
}
if (_startPosX + _airFighterWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _airFighterWidth;
}
if (_startPosY + _airFighterHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _airFighterHeight;
}
}
}

View File

@ -0,0 +1,35 @@
import java.awt.*;
// Класс отрисовки двигателей
public class DrawningEngine {
private Engine _engine;
public void setCodeEngine(int code){
_engine = Engine.getByCode(code);
}
// Отрисовка двигателей
public void DrawEngine(Graphics g, int startX, int startY, Color _bodyColor){
if(_engine.getEngineCode() >= 2) {
g.setColor(_bodyColor);
g.fillRect(startX+ 30, startY + 15, 13, 5);
g.fillRect(startX+ 30, startY + 50, 13, 5);
g.setColor(Color.BLACK);
g.drawRect(startX+ 30, startY + 15, 13, 5);
g.drawRect(startX+ 30, startY + 50, 13, 5);
}
if(_engine.getEngineCode() >= 4){
g.setColor(_bodyColor);
g.fillRect(startX+ 30, startY + 22, 13, 5);
g.fillRect(startX+ 30, startY + 43, 13, 5);
g.setColor(Color.BLACK);
g.drawRect(startX+ 30, startY + 22, 13, 5);
g.drawRect(startX+ 30, startY + 43, 13, 5);
}
if(_engine.getEngineCode() >= 6){
g.setColor(_bodyColor);
g.fillRect(startX+ 30, startY + 8, 13, 5);
g.fillRect(startX+ 30, startY + 57, 13, 5);
g.setColor(Color.BLACK);
g.drawRect(startX+ 30, startY + 8, 13, 5);
g.drawRect(startX+ 30, startY + 57, 13, 5);
}
}
}

View File

@ -0,0 +1,21 @@
// Количество двигателей
public enum Engine {
TwoEngine(2),
FourEngine(4),
SixEngine(6),
UNKNOWN(0)
;
private final int engineCode;
Engine(int engineCode) {
this.engineCode = engineCode;
}
public int getEngineCode() {
return this.engineCode;
}
public static Engine getByCode(int code) {
for(Engine e : values()) {
if(e.engineCode == code) return e;
}
return UNKNOWN;
}
}

View File

@ -0,0 +1,29 @@
import java.awt.*;
public class EntityAirFighter {
// Скорость
private int Speed;
// Вес
private float Weight;
// Цвет корпуса
private Color BodyColor;
// Шаг
public float Step() {
return Speed * 150 / Weight;
}
public void Init(int _speed, float _weight, Color _bodyColor){
Speed = _speed;
Weight = _weight;
BodyColor = _bodyColor;
}
public int getSpeed(){
return Speed;
}
public float getWeight(){
return Weight;
}
public Color getBodyColor(){
return BodyColor;
}
}

View File

@ -0,0 +1,222 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
// Класс отображения формы
public class FormAirFighter extends JFrame implements ComponentListener{
private JPanel panelForDrawing;
private JPanel statusLabel = new JPanel();
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private String item = "2";
private DrawningAirFighter _airFighter;
// Слушатель кнопки направления
private ActionListener actionListenerDirection = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_airFighter == null){
return;
}
if (e.getActionCommand() == Direction.Up.name()){
_airFighter.MoveTransport(Direction.Up);
}else if (e.getActionCommand() == Direction.Down.name()){
_airFighter.MoveTransport(Direction.Down);
}else if (e.getActionCommand() == Direction.Left.name()){
_airFighter.MoveTransport(Direction.Left);
}else if (e.getActionCommand() == Direction.Right.name()){
_airFighter.MoveTransport(Direction.Right);
}
panelForDrawing.repaint();
}
};
public FormAirFighter(){
super("Военный самолет");
// Panel для отрисовки
panelForDrawing = new JPanel(){
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
_airFighter.DrawTransport(g);
}catch (Exception e){}
}
};
panelForDrawing.setLayout(new BorderLayout());
panelForDrawing.setSize(getWidth(), getHeight());
setContentPane(panelForDrawing);
// statusLabel
JLabel labelSpeed = new JLabel("Скорость:");
JLabel labelWeight = new JLabel("Вес:");
JLabel labelBodyColor = new JLabel("Цвет:");
statusLabel.add(labelSpeed);
statusLabel.add(labelWeight);
statusLabel.add(labelBodyColor);
statusLabel.setLayout(new FlowLayout(FlowLayout.LEFT));
statusLabel.setSize(new Dimension(getWidth(), 30));
// Кнопка создания
JButton buttonCreate = new JButton("Создать");
buttonCreate.setSize(100, 25);
buttonCreate.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_airFighter = new DrawningAirFighter();
_airFighter.Init((int)(Math.random() * 300 + 200),(int)(Math.random() * 3000 + 2000), new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)), Integer.parseInt(item));
_airFighter.SetPosition((int)(Math.random() * 100 + 10), (int)(Math.random() * 100 + 10), panelForDrawing.getWidth(), panelForDrawing.getHeight());
labelSpeed.setText("Скорость: " + _airFighter.AirFighter.getSpeed());
labelWeight.setText("Вес: " + _airFighter.AirFighter.getWeight());
labelBodyColor.setText("Цвет: " + _airFighter.AirFighter.getBodyColor());
panelForDrawing.repaint();
}
});
// Контейнер кнопок и панели с информацией
Container container = new Container();
container.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.weightx = 10;
constraints.weighty = 10;
constraints.anchor = GridBagConstraints.WEST;
constraints.gridwidth = 1;
constraints.gridx = 0;
constraints.gridy = 0;
container.add(buttonCreate, constraints);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridx = 0;
constraints.gridy = 1;
container.add(statusLabel, constraints);
constraints.anchor = GridBagConstraints.WEST;
constraints.gridx = 1;
constraints.gridy = 1;
String[] _engine = {
"2",
"4",
"6"
};
JComboBox editComboBox = new JComboBox(_engine);
editComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JComboBox box = (JComboBox)e.getSource();
item = (String)box.getSelectedItem();
}
});
editComboBox.setEditable(true);
container.add(editComboBox, constraints);
// Контейнер с кнопками движения
Container containerDirection = new Container();
containerDirection.setPreferredSize(new Dimension(120, 70));
containerDirection.setLayout(new GridBagLayout());
GridBagConstraints constraintsDirection = new GridBagConstraints();
constraintsDirection.weightx = 40;
constraintsDirection.weighty = 40;
constraintsDirection.gridwidth = 2;
constraintsDirection.gridheight = 1;
// Кнопка Up
try {
BufferedImage bufferedImage = ImageIO.read(new File("AirFighter/Images/Up.png"));
Image imageUp = bufferedImage.getScaledInstance(30, 30, Image.SCALE_DEFAULT);
ImageIcon iconUp = new ImageIcon(imageUp);
buttonUp = new JButton(iconUp);
buttonUp.setPreferredSize(new Dimension(30, 30));
buttonUp.setContentAreaFilled(false);
buttonUp.setBorderPainted(false);
buttonUp.setActionCommand(Direction.Up.name());
buttonUp.addActionListener(actionListenerDirection);
constraintsDirection.gridx = 1;
constraintsDirection.gridy = 0;
containerDirection.add(buttonUp, constraintsDirection);
}catch (IOException e){}
// Кнопка Down
try {
BufferedImage bufferedImage = ImageIO.read(new File("AirFighter/Images/Down.png"));
Image imageDown = bufferedImage.getScaledInstance(30, 30, Image.SCALE_DEFAULT);
ImageIcon iconDown = new ImageIcon(imageDown);
buttonDown = new JButton(iconDown);
buttonDown.setActionCommand(Direction.Down.name());
buttonDown.addActionListener(actionListenerDirection);
buttonDown.setPreferredSize(new Dimension(30, 30));
buttonDown.setContentAreaFilled(false);
buttonDown.setBorderPainted(false);
constraintsDirection.gridx = 1;
constraintsDirection.gridy = 1;
containerDirection.add(buttonDown, constraintsDirection);
}catch (IOException e){}
// Кнопка Left
try {
BufferedImage bufferedImage = ImageIO.read(new File("AirFighter/Images/Left.png"));
Image imageLeft = bufferedImage.getScaledInstance(30, 30, Image.SCALE_DEFAULT);
ImageIcon iconLeft = new ImageIcon(imageLeft);
buttonLeft = new JButton(iconLeft);
buttonLeft.setActionCommand(Direction.Left.name());
buttonLeft.addActionListener(actionListenerDirection);
buttonLeft.setPreferredSize(new Dimension(30, 30));
buttonLeft.setContentAreaFilled(false);
buttonLeft.setBorderPainted(false);
constraintsDirection.fill = GridBagConstraints.WEST;
constraintsDirection.gridx = 0;
containerDirection.add(buttonLeft, constraintsDirection);
}catch (IOException e){}
// Кнопка Right
try {
BufferedImage bufferedImage = ImageIO.read(new File("AirFighter/Images/Right.png"));
Image imageRight = bufferedImage.getScaledInstance(30, 30, Image.SCALE_DEFAULT);
ImageIcon iconRight = new ImageIcon(imageRight);
buttonRight = new JButton(iconRight);
buttonRight.setActionCommand(Direction.Right.name());
buttonRight.addActionListener(actionListenerDirection);
buttonRight.setPreferredSize(new Dimension(30, 30));
buttonRight.setContentAreaFilled(false);
buttonRight.setBorderPainted(false);
constraintsDirection.fill = GridBagConstraints.EAST;
constraintsDirection.gridx = 2;
containerDirection.add(buttonRight, constraintsDirection);
}catch (IOException e){}
constraints.anchor = GridBagConstraints.EAST;
constraints.gridx = 1;
constraints.gridy = 0;
container.add(containerDirection, constraints);
add(container, BorderLayout.SOUTH);
}
public static void main(String[] args) {
// Создание формы
FormAirFighter _formAirFighter = new FormAirFighter();
_formAirFighter.addComponentListener(_formAirFighter);
_formAirFighter.setSize (600,400);
_formAirFighter.getRootPane().setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
_formAirFighter.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e) {
e.getWindow ().dispose ();
}
});
_formAirFighter.setVisible(true);
}
// Слушатель изменения размера окна
@Override
public void componentResized(ComponentEvent e) {
try {
_airFighter.ChangeBorders(panelForDrawing.getWidth(), panelForDrawing.getHeight());
panelForDrawing.repaint();
}catch (Exception eo){}
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
}

View File

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