PIbd-11_Zubkova_S.V._LabWork01_Hard #1

Closed
sonyaz wants to merge 1 commits from LabWork01 into main
14 changed files with 522 additions and 0 deletions

11
src/src.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="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,15 @@
import javax.swing.*;
import java.awt.*;
public class CanvasGasolineTanker extends JComponent{
public DrawingGasolineTanker _gasolineTanker;
public CanvasGasolineTanker(){}
public void paintComponent(Graphics g) {
if (_gasolineTanker == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_gasolineTanker.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,10 @@
public enum DirectionType {
//Вверх
Up,
//Вниз
Down,
//Влево
Left,
//Вправо
Right
}

View File

@ -0,0 +1,192 @@
import java.awt.*;
public class DrawingGasolineTanker {
public EntityGasolineTanker EntityGasolineTanker;
private DrawingWheels drawingWheels = null;
// Ширина окна
private Integer _pictureWidth;
// Высота окна
private Integer _pictureHeight;
// Левая координата прорисовки бензовоза
private Integer _startPosX;
// Верхняя координата прорисовки бензовоза
private Integer _startPosY;
// Ширина прорисовки бензовоза
private int _drawingGasolineTankerWidth = 105;
// Высота прорисовки бензовоза
private int _drawingGasolineTankerHeight = 70;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean gasTank, boolean signalBeacon, int numWheels)
{
EntityGasolineTanker = new EntityGasolineTanker();
EntityGasolineTanker.Init(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
drawingWheels = new DrawingWheels();
drawingWheels.setNumWheels(numWheels);
}
// Установка границ поля
public boolean SetPictureSize(int width, int height)
{
// Проверка "влезает" ли объект в размеры поля
if (_drawingGasolineTankerWidth > width || _drawingGasolineTankerHeight > height)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_drawingGasolineTankerWidth + _startPosX > _pictureWidth)
{
_startPosX = _pictureWidth - _drawingGasolineTankerWidth;
}
else if (_startPosX < 0)
{
_startPosX = 0;
}
if (_drawingGasolineTankerHeight + _startPosY > _pictureHeight)
{
_startPosY = _pictureHeight - _drawingGasolineTankerHeight;
}
else if (_startPosY < 0)
{
_startPosY = 0;
}
}
return true;
}
// Установка позиции
public void SetPosition(int x, int y)
{
if (!(_pictureHeight != null && _pictureWidth != null))
{
return;
}
if (x >= 0 && x + _drawingGasolineTankerWidth <= _pictureWidth && y >= 0 && y + _drawingGasolineTankerHeight <= _pictureHeight)
{
_startPosX = x;
_startPosY = y;
}
if (x < 0)
{
_startPosX = 0;
}
if (y < 0)
{
_startPosY = 0;
}
if (x + _drawingGasolineTankerWidth > _pictureWidth)
{
_startPosX = _drawingGasolineTankerWidth - _pictureWidth;
}
if (y + _drawingGasolineTankerHeight > _pictureHeight)
{
_startPosY = _drawingGasolineTankerHeight - _pictureHeight;
}
}
// Изменение направления
public boolean MoveTransport(DirectionType direction)
{
if (EntityGasolineTanker == null || _startPosX == null || _startPosY == null)
{
return false;
}
switch(direction)
{
// влево
case Left:
if (_startPosX - EntityGasolineTanker.Step > 0)
{
_startPosX -= (int)EntityGasolineTanker.Step;
}
return true;
// вверх
case Up:
if (_startPosY - EntityGasolineTanker.Step > 0)
{
_startPosY -= (int)EntityGasolineTanker.Step;
}
return true;
// вправо
case Right:
if (_startPosX + EntityGasolineTanker.Step + _drawingGasolineTankerWidth < _pictureWidth)
{
_startPosX += (int)EntityGasolineTanker.Step;
}
return true;
// вниз
case Down:
if (_startPosY + EntityGasolineTanker.Step + _drawingGasolineTankerHeight < _pictureHeight)
{
_startPosY += (int)EntityGasolineTanker.Step;
}
return true;
default:
return false;
}
}
// Прорисовка объекта
public void DrawTransport(Graphics2D g)
{
if (EntityGasolineTanker == null || _startPosX == null || _startPosY == null)
{
return;
}
// бензобак
if (EntityGasolineTanker.getGasTank())
{
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 7, _startPosY + 18, 31, 31);
g.drawOval(_startPosX + 47, _startPosY + 18, 31, 31);
g.drawRect(_startPosX + 20, _startPosY+ 20, 45, 30);
g.setColor(EntityGasolineTanker.getAdditionalColor());
g.fillOval(_startPosX + 7, _startPosY+18, 31, 31);
g.fillOval( _startPosX+ 47, _startPosY + 18, 31, 31);
g.fillRect(_startPosX + 20, _startPosY+ 18, 45, 30);
}
// сигнальный маяк
if (EntityGasolineTanker.getSignalBeacon())
{
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 85, _startPosY + 10, 7, 7);
g.setColor(EntityGasolineTanker.getAdditionalColor());
g.fillRect( _startPosX + 85, _startPosY + 10, 7, 7);
}
// колеса
drawingWheels.drawWheels(g, Color.BLACK, _startPosX, _startPosY);
// нижняя платформа
g.setColor(EntityGasolineTanker.getBodyColor());
g.fillRect(_startPosX + 5, _startPosY + 40, 100, 10);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 5, _startPosY + 40, 100, 10);
// кабина
g.setColor(Color.BLUE);
g.fillRect(_startPosX + 80, _startPosY + 15, 25, 25);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 80, _startPosY + 15, 25, 25);
}
}

View File

@ -0,0 +1,57 @@
import java.awt.*;
public class DrawingWheels {
private NumWheels numWheels;
//определяет количество колес
public void setNumWheels(int number) {
switch (number) {
case 1:
numWheels = NumWheels.Two;
break;
case 2:
numWheels = NumWheels.Three;
break;
case 3:
numWheels = NumWheels.Four;
break;
default:
numWheels = NumWheels.Three;
}
}
public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numWheels) {
case Two:
drawTwoWheels(g2D, color, _startPosX, _startPosY);
break;
case Three:
drawThreeWheels(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourWheels(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
}
private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
Review

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

Имеется дублирующийся код
g2D.fillOval(_startPosX + 25, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
}
private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 25, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 65, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
}
}

View File

@ -0,0 +1,37 @@
import java.awt.*;
public class EntityGasolineTanker {
// Скорость
private int Speed;
// Вес
private double Weight;
// Основной цвет
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
// Дополнительный цвет(для опциональныз элементов)
public Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
// Признак (опция) наличия бака под бензин
private boolean GasTank;
public boolean getGasTank() {return GasTank;}
// Признак (опция) наличия сигнального маяка на кабине
private boolean SignalBeacon;
public boolean getSignalBeacon() {return SignalBeacon;}
// Шаг перемещения бензовоза
public double Step;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean signalBeacon, boolean gasTank){
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
GasTank = gasTank;
SignalBeacon = signalBeacon;
Step = Speed * 700 / Weight;
}
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormGasolineTanker">
<grid id="27dc6" layout-manager="GridLayoutManager" row-count="2" column-count="4" 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="662" height="422"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="6cce4" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<vspacer id="b7eca">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="bdb21" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="resources/Right.jpg"/>
<text value=""/>
</properties>
</component>
<component id="c59ad" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="resources/Left.jpg"/>
<text value=""/>
</properties>
</component>
<component id="7c97d" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="resources/Down.jpg"/>
<text value=""/>
</properties>
</component>
<component id="10d3d" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="resources/Up.jpg"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,126 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormGasolineTanker extends JFrame{
private int Width;
private int Height;
private CanvasGasolineTanker canvasGasolineTanker = new CanvasGasolineTanker();
private JButton buttonCreate = new JButton("Создание");
private JButton buttonRight = new JButton();
private JButton buttonLeft = new JButton();
private JButton buttonDown = new JButton();
private JButton buttonUp = new JButton();
public void Init(){
setTitle("Бензовоз");
setSize(900,600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Width = getWidth();
Height = getHeight();
buttonCreate.setName("CREATE");
buttonUp.setName("buttonUp");
Icon iconUp = new ImageIcon("src\\src\\resources\\Up.jpg");
buttonUp.setIcon(iconUp);
buttonDown.setName("buttonDown");
Icon iconDown = new ImageIcon("src\\src\\resources\\Down.jpg");
buttonDown.setIcon(iconDown);
buttonLeft.setName("buttonLeft");
Icon iconLeft = new ImageIcon("src\\src\\resources\\Left.jpg");
buttonLeft.setIcon(iconLeft);
buttonRight.setName("buttonRight");
Icon iconRight = new ImageIcon("src\\src\\resources\\Right.jpg");
buttonRight.setIcon(iconRight);
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int StartPositionX = 10 + (int)(Math.random() * 90);
int StartPositionY = 10 + (int)(Math.random() * 90);
int speed = 10 + (int)(Math.random() * 90);
double weight = 1000 + (double)(Math.random() * 5000);
Color bodyColor = new Color((int)(Math.random() * 255 + 0), (int)(Math.random() * 255 + 0), (int) (Math.random() * 255 + 0));
Color additionalColor = new Color((int)(Math.random() * 255 + 0), (int)(Math.random() * 255 + 0), (int) (Math.random() * 255 + 0));
boolean gasTank = new Random().nextBoolean();
boolean signalBeacon = new Random().nextBoolean();
int wheelsNum = new Random().nextInt(3)+1;
canvasGasolineTanker._gasolineTanker = new DrawingGasolineTanker();
canvasGasolineTanker._gasolineTanker.Init(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon, wheelsNum);
canvasGasolineTanker._gasolineTanker.SetPictureSize(Width, Height);
canvasGasolineTanker._gasolineTanker.SetPosition(StartPositionX, StartPositionY);
canvasGasolineTanker.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasGasolineTanker._gasolineTanker == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "buttonUp":
result = canvasGasolineTanker._gasolineTanker.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = canvasGasolineTanker._gasolineTanker.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = canvasGasolineTanker._gasolineTanker.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = canvasGasolineTanker._gasolineTanker.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasGasolineTanker.repaint();
}
}
};
buttonUp.addActionListener(actionListener);
buttonDown.addActionListener(actionListener);
buttonLeft.addActionListener(actionListener);
buttonRight.addActionListener(actionListener);
setLayout(null);
canvasGasolineTanker.setBounds(0,0, getWidth(), getHeight());
buttonCreate.setBounds(15, getHeight() - 95, 200, 35);
buttonUp.setBounds(getWidth() - 140, getHeight() - 160, 45, 45);
buttonDown.setBounds(getWidth() - 140, getHeight() - 100, 45, 45);
buttonRight.setBounds(getWidth() - 80, getHeight() - 100, 45, 45);
buttonLeft.setBounds(getWidth() - 200, getHeight() - 100, 45, 45);
add(buttonCreate);
add(buttonUp);
add(buttonDown);
add(buttonRight);
add(buttonLeft);
add(canvasGasolineTanker);
setVisible(true);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasGasolineTanker._gasolineTanker != null)
canvasGasolineTanker._gasolineTanker.SetPictureSize(Width, Height);
canvasGasolineTanker.setBounds(0,0, getWidth(), getHeight());
buttonCreate.setBounds(15, getHeight() - 95, 200, 35);
buttonUp.setBounds(getWidth() - 140, getHeight() - 160, 45, 45);
buttonDown.setBounds(getWidth() - 140, getHeight() - 100, 45, 45);
buttonRight.setBounds(getWidth() - 80, getHeight() - 100, 45, 45);
buttonLeft.setBounds(getWidth() - 200, getHeight() - 100, 45, 45);
}
});
}
}

7
src/src/Main.java Normal file
View File

@ -0,0 +1,7 @@
import java.awt.*;
public class Main{
public static void main(String[] args){
FormGasolineTanker formGasolineTanker = new FormGasolineTanker();
formGasolineTanker.Init();
}
}

5
src/src/NumWheels.java Normal file
View File

@ -0,0 +1,5 @@
public enum NumWheels {
Two,
Three,
Four
}

BIN
src/src/resources/Down.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

BIN
src/src/resources/Left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

BIN
src/src/resources/Right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

BIN
src/src/resources/Up.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB