LabWork01: Сделана базовая часть
This commit is contained in:
parent
5dbef32acc
commit
ac171b2770
6
Direction.java
Normal file
6
Direction.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
192
DrawningTracktor.java
Normal file
192
DrawningTracktor.java
Normal file
@ -0,0 +1,192 @@
|
||||
import java.awt.*;
|
||||
|
||||
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
public class DrawningTracktor {
|
||||
private EntityTracktor Tracktor; // Класс-сущность
|
||||
public EntityTracktor getTracktor(){
|
||||
return Tracktor;
|
||||
}
|
||||
private float _startPosX; // Левая координата отрисовки трактора
|
||||
private float _startPosY; // Верхняя кооридната отрисовки трактора
|
||||
private Integer _pictureWidth = null; // Ширина окна отрисовки
|
||||
private Integer _pictureHeight = null; // Высота окна отрисовки
|
||||
private final int _tracktorWidth = 110; // Ширина отрисовки трактора
|
||||
private final int _tracktorHeight = 87; // Высота отрисовки трактора
|
||||
|
||||
// Инициализация свойств
|
||||
public void Init(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
Tracktor = new EntityTracktor();
|
||||
Tracktor.Init(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
// Установка позиции Трактора
|
||||
public void SetPosition(int x, int y, int width, int height)
|
||||
{
|
||||
if (x + _tracktorWidth > width ||
|
||||
x < 0 ||
|
||||
y + _tracktorHeight > height ||
|
||||
y < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_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 + _tracktorWidth + Tracktor.getStep() < _pictureWidth)
|
||||
{
|
||||
_startPosX += Tracktor.getStep();
|
||||
}
|
||||
break;
|
||||
//влево
|
||||
case Left:
|
||||
if (_startPosX - Tracktor.getStep() > 0)
|
||||
{
|
||||
_startPosX -= Tracktor.getStep();
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case Up:
|
||||
if (_startPosY - Tracktor.getStep() > 0)
|
||||
{
|
||||
_startPosY -= Tracktor.getStep();
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case Down:
|
||||
if (_startPosY + _tracktorHeight + Tracktor.getStep() < _pictureHeight)
|
||||
{
|
||||
_startPosY += Tracktor.getStep();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Отрисовка Трактора
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Color penColor = Color.BLACK;
|
||||
|
||||
// корпус
|
||||
Color br = Tracktor!=null ? Tracktor.getBodyColor() : Color.GRAY ;
|
||||
g.setColor(br);
|
||||
g.fillRect((int)_startPosX + 10, (int)_startPosY + 30, 90, 25);
|
||||
g.setColor(penColor);
|
||||
g.drawRect((int)_startPosX + 10, (int)_startPosY + 30, 90, 25);
|
||||
|
||||
// окно
|
||||
g.setColor(Color.CYAN);
|
||||
g.fillRect((int)_startPosX + 65, (int)_startPosY + 1, 30, 29);
|
||||
g.setColor(penColor);
|
||||
g.drawRect((int)_startPosX + 65, (int)_startPosY + 1, 30, 29);
|
||||
|
||||
// труба
|
||||
g.setColor(Color.RED);
|
||||
g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 20);
|
||||
|
||||
// гусеница
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillOval((int)_startPosX + 1, (int)_startPosY + 57, 20, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 1, (int)_startPosY + 57, 20, 20);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillOval((int)_startPosX + 1, (int)_startPosY + 65, 20, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 1, (int)_startPosY + 65, 20, 20);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillOval((int)_startPosX + 90, (int)_startPosY + 57, 20, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 90, (int)_startPosY + 57, 20, 20);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillOval((int)_startPosX + 90, (int)_startPosY + 65, 20, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 90, (int)_startPosY + 65, 20, 20);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillRect((int)_startPosX + 10, (int)_startPosY + 57, 90, 30);
|
||||
g.fillRect((int)_startPosX + 1, (int)_startPosY + 65, 110, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawLine((int)_startPosX + 10, (int)_startPosY + 57, (int)_startPosX + 100, (int)_startPosY + 57);
|
||||
g.drawLine((int)_startPosX + 10, (int)_startPosY + 86, (int)_startPosX + 100, (int)_startPosY + 86);
|
||||
g.drawLine((int)_startPosX + 1, (int)_startPosY + 65, (int)_startPosX + 1, (int)_startPosY + 75);
|
||||
g.drawLine((int)_startPosX + 110, (int)_startPosY + 65, (int)_startPosX + 110, (int)_startPosY + 75);
|
||||
|
||||
// Крупные катки
|
||||
g.setColor(Color.LIGHT_GRAY);
|
||||
g.fillOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(Color.LIGHT_GRAY);
|
||||
g.fillOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
|
||||
|
||||
// Средние катки
|
||||
g.setColor(Color.LIGHT_GRAY);
|
||||
g.fillOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(Color.LIGHT_GRAY);
|
||||
g.fillOval((int)_startPosX + 50, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 50, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(Color.LIGHT_GRAY);
|
||||
g.fillOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
|
||||
|
||||
// Малые катки
|
||||
g.setColor(Color.LIGHT_GRAY);
|
||||
g.fillOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(Color.LIGHT_GRAY);
|
||||
g.fillOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
|
||||
|
||||
// Центры крупных катков
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6);
|
||||
g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6);
|
||||
}
|
||||
|
||||
// Смена границ формы отрисовки
|
||||
public void ChangeBorders(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_pictureWidth <= _tracktorWidth || _pictureHeight <= _tracktorHeight)
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
return;
|
||||
}
|
||||
if (_startPosX + _tracktorWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _tracktorWidth;
|
||||
}
|
||||
if (_startPosY + _tracktorHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _tracktorHeight;
|
||||
}
|
||||
}
|
||||
}
|
31
EntityTracktor.java
Normal file
31
EntityTracktor.java
Normal file
@ -0,0 +1,31 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
// Класс-сущность "Трактор"
|
||||
public class EntityTracktor {
|
||||
private int Speed;
|
||||
private float Weight;
|
||||
private Color BodyColor;
|
||||
|
||||
public int getSpeed() {
|
||||
return Speed;
|
||||
}
|
||||
public float getWeight(){
|
||||
return Weight;
|
||||
}
|
||||
public Color getBodyColor(){
|
||||
return BodyColor;
|
||||
}
|
||||
public float getStep(){
|
||||
return Speed * 100 / Weight;
|
||||
}
|
||||
|
||||
// Инициализация полей объекта-класса Трактора
|
||||
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;
|
||||
}
|
||||
}
|
107
FormTracktor.form
Normal file
107
FormTracktor.form
Normal 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="FormTracktor">
|
||||
<grid id="27dc6" binding="ContentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="8" 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="661" height="428"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="eb389" 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="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Создать"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="7a016">
|
||||
<constraints>
|
||||
<grid row="1" column="3" 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="83511" class="javax.swing.JLabel" binding="speedLabel">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Скорость:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="49134" class="javax.swing.JLabel" binding="weightLabel">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Вес:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="15fa4" class="javax.swing.JLabel" binding="colorLabel">
|
||||
<constraints>
|
||||
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Цвет:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<grid id="2fbf8" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="8" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<component id="5a9f4" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Resources/arrowLeft.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="3c09f" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Resources/arrowDown.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="6d42a" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Resources/arrowRight.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="82038" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="1" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Resources/arrowUp.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
82
FormTracktor.java
Normal file
82
FormTracktor.java
Normal file
@ -0,0 +1,82 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormTracktor extends JFrame {
|
||||
private JPanel ContentPanel;
|
||||
private JButton buttonCreate;
|
||||
private JLabel speedLabel;
|
||||
private JLabel weightLabel;
|
||||
private JLabel colorLabel;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonUp;
|
||||
private JPanel pictureBox;
|
||||
|
||||
private DrawningTracktor _tracktor;
|
||||
|
||||
public FormTracktor(){
|
||||
setTitle("Трактор");
|
||||
setContentPane(ContentPanel);
|
||||
setSize(800, 500);
|
||||
|
||||
// Обработка нажатия кнопки "Создать"
|
||||
buttonCreate.addActionListener(e->{
|
||||
Random rnd = new Random();
|
||||
_tracktor = new DrawningTracktor();
|
||||
_tracktor.Init(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))
|
||||
);
|
||||
_tracktor.SetPosition(
|
||||
rnd.nextInt(10, 100),
|
||||
rnd.nextInt(10, 100),
|
||||
pictureBox.getWidth(), pictureBox.getHeight()
|
||||
);
|
||||
speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed());
|
||||
weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight());
|
||||
colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor()));
|
||||
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonUp.addActionListener(e->{
|
||||
if (_tracktor != null){
|
||||
_tracktor.MoveTransport(Direction.Up);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
buttonLeft.addActionListener(e->{
|
||||
if (_tracktor != null){
|
||||
_tracktor.MoveTransport(Direction.Left);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
buttonDown.addActionListener(e->{
|
||||
if (_tracktor != null){
|
||||
_tracktor.MoveTransport(Direction.Down);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
buttonRight.addActionListener(e->{
|
||||
if (_tracktor != null){
|
||||
_tracktor.MoveTransport(Direction.Right);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g){
|
||||
super.paint(g);
|
||||
g = pictureBox.getGraphics();
|
||||
if (_tracktor != null){
|
||||
_tracktor.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
9
Program.java
Normal file
9
Program.java
Normal file
@ -0,0 +1,9 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class Program {
|
||||
public static void main(String[] args){
|
||||
FormTracktor formTracktor = new FormTracktor();
|
||||
formTracktor.setVisible(true);
|
||||
formTracktor.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
}
|
||||
}
|
BIN
Resources/arrowDown.png
Normal file
BIN
Resources/arrowDown.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
Resources/arrowLeft.png
Normal file
BIN
Resources/arrowLeft.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
Resources/arrowRight.png
Normal file
BIN
Resources/arrowRight.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
Resources/arrowUp.png
Normal file
BIN
Resources/arrowUp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
Loading…
Reference in New Issue
Block a user