Лабораторная работа 2

This commit is contained in:
Esenia12 2024-04-28 20:53:18 +04:00
parent bc9d72d0a2
commit 2f785292e3
24 changed files with 1058 additions and 287 deletions

124
.idea/uiDesigner.xml generated Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View File

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

View File

@ -1,110 +0,0 @@
import javax.swing.*;
import java.awt.*;
public class DrawingDumpTrack extends JPanel {
public EntityDumpTrack EntityDumpTrack;
public DrawingWheels _wheels = null;
private Integer picture_width;
private Integer picture_height;
private Integer _StartPosX;
private Integer _StartPosY;
private int drawingDumpTrackWidth = 155;
private int drawingDumpTrackHeight = 85;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean _bodyWork, boolean _awning) {
EntityDumpTrack = new EntityDumpTrack();
EntityDumpTrack.Init(speed, weight, bodycolor, additionalcolor, _bodyWork, _awning);
picture_width = null;
picture_height = null;
_StartPosX = null;
_StartPosY = null;
_wheels = new DrawingWheels();
_wheels.setWheelNumber((int)(Math.random() * 8 + 0));
}
public boolean SetPictureSize(int width, int height) {
if (width < drawingDumpTrackWidth || height < drawingDumpTrackHeight) return false;
picture_width = width;
picture_height = height;
if (_StartPosX != null || _StartPosY != null) {
if (_StartPosX + drawingDumpTrackWidth > picture_width)
{
_StartPosX = _StartPosX - (_StartPosX + drawingDumpTrackWidth - picture_width);
}
else if (_StartPosX < 0) _StartPosX = 0;
if (_StartPosY + drawingDumpTrackHeight > picture_height)
{
_StartPosY = _StartPosY - (_StartPosY + drawingDumpTrackHeight - picture_height);
}
else if (_StartPosY < 0) _StartPosY = 0;
}
return true;
}
public void SetPosition(int x, int y)
{
if (x < 0 || x + drawingDumpTrackWidth > picture_width) { x = 0; }
if (y < 0 || y + drawingDumpTrackHeight > picture_height) { y = 0; }
_StartPosX = x;
_StartPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityDumpTrack == null || _StartPosX == null || _StartPosY == null) return false;
switch (direction) {
case Left:
if (_StartPosX - EntityDumpTrack.Step > 0) {
_StartPosX -= (int)EntityDumpTrack.Step;
}
return true;
case Up:
if (_StartPosY - EntityDumpTrack.Step > 0)
{
_StartPosY -= (int)EntityDumpTrack.Step;
}
return true;
case Right:
if (_StartPosX + drawingDumpTrackWidth + (int)EntityDumpTrack.Step < picture_width - EntityDumpTrack.Step)
{
_StartPosX += (int)EntityDumpTrack.Step;
}
return true;
case Down:
if (_StartPosY + drawingDumpTrackHeight + (int)EntityDumpTrack.Step < picture_height - EntityDumpTrack.Step)
{
_StartPosY += (int)EntityDumpTrack.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g2D)
{
if (EntityDumpTrack == null) {
return;
}
g2D.setStroke(new BasicStroke(3));
g2D.setPaint(EntityDumpTrack.getBodyColor());
g2D.fillRect(_StartPosX, _StartPosY + 40, 160, 10);
g2D.fillRect(_StartPosX + 120, _StartPosY, 40, 40);
_wheels.drawWheels(g2D, Color.BLACK, _StartPosX, _StartPosY);
if (EntityDumpTrack.getBodyWork())
{
g2D.setPaint(EntityDumpTrack.getAdditionalColor());
Polygon dumpBoxPolygon = new Polygon();
dumpBoxPolygon.addPoint(_StartPosX + 20, _StartPosY);
dumpBoxPolygon.addPoint(_StartPosX + 120, _StartPosY);
dumpBoxPolygon.addPoint(_StartPosX + 100, _StartPosY + 39);
dumpBoxPolygon.addPoint(_StartPosX, _StartPosY + 39);
g2D.fillPolygon(dumpBoxPolygon);
}
if (EntityDumpTrack.getAwning())
{
g2D.setPaint(EntityDumpTrack.getAdditionalColor());
Polygon tentPolygon = new Polygon();
tentPolygon.addPoint(_StartPosX + 15, _StartPosY);
tentPolygon.addPoint(_StartPosX + 120, _StartPosY);
tentPolygon.addPoint(_StartPosX + 115, _StartPosY + 10);
tentPolygon.addPoint(_StartPosX + 10, _StartPosY + 10);
g2D.fillPolygon(tentPolygon);
}
}
}

View File

@ -0,0 +1,44 @@
package DrawingObjects;
import Entities.*;
import java.awt.*;
public class DrawingDumpTruck extends DrawingTruck {
public DrawingDumpTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber)
{
super(speed, weight, bodyColor, width, height, 160, 90, wheelNumber);
if (getEntityTruck() != null)
{
setEntityTruck(new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor));
}
}
@Override
public void DrawTransport(Graphics2D g2D) {
if (!(getEntityTruck() instanceof EntityDumpTruck dumpTruck)) {
return;
}
super.DrawTransport(g2D);
if (dumpTruck.getDumpBox())
{
g2D.setPaint(dumpTruck.getDumpBoxColor());
Polygon dumpBoxPolygon = new Polygon();
dumpBoxPolygon.addPoint(_startPosX + 20, _startPosY);
dumpBoxPolygon.addPoint(_startPosX + 120, _startPosY);
dumpBoxPolygon.addPoint(_startPosX + 100, _startPosY + 39);
dumpBoxPolygon.addPoint(_startPosX, _startPosY + 39);
g2D.fillPolygon(dumpBoxPolygon);
}
if (dumpTruck.getDumpBox() && dumpTruck.getTent())
{
g2D.setPaint(dumpTruck.getTentColor());
Polygon tentPolygon = new Polygon();
tentPolygon.addPoint(_startPosX + 15, _startPosY);
tentPolygon.addPoint(_startPosX + 120, _startPosY);
tentPolygon.addPoint(_startPosX + 115, _startPosY + 10);
tentPolygon.addPoint(_startPosX + 10, _startPosY + 10);
g2D.fillPolygon(tentPolygon);
}
}
}

View File

@ -0,0 +1,150 @@
package DrawingObjects;
import java.awt.*;
import java.util.Random;
import Entities.*;
import DumpTruck.DirectionType;
import Wheels.*;
public class DrawingTruck {
private EntityTruck entityTruck;
public EntityTruck getEntityTruck() {
return entityTruck;
}
protected void setEntityTruck(EntityTruck entityTruck) {
this.entityTruck = entityTruck;
}
private IDrawingWheels drawingWheels;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected int _truckWidth = 160;
protected int _truckHeight = 90;
public int getPosX() {
return _startPosX;
}
public int getPosY() {
return _startPosY;
}
public int getWidth() {
return _truckWidth;
}
public int getHeight() {
return _truckHeight;
}
public DrawingTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber)
{
if (height < _truckHeight || width < _truckWidth) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
entityTruck = new EntityTruck(speed, weight, bodyColor);
Random random = new Random();
drawingWheels = switch (random.nextInt(3)) {
case 0 -> new DrawingWheels();
case 1 -> new DrawingWheelsStar();
case 2 -> new DrawingWheelsCircles();
default -> new DrawingWheels();
};
drawingWheels.setWheelNumber(wheelNumber);
}
protected DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int truckWidth, int truckHeight, int wheelNumber)
{
if (width < truckWidth || height < truckHeight) return;
_pictureWidth = width;
_pictureHeight = height;
_truckWidth = truckWidth;
_truckHeight = truckHeight;
entityTruck = new EntityTruck(speed, weight, bodyColor);
Random random = new Random();
drawingWheels = switch (random.nextInt( 3)) {
case 0 -> new DrawingWheels();
case 1 -> new DrawingWheelsStar();
case 2 -> new DrawingWheelsCircles();
default -> new DrawingWheels();
};
drawingWheels.setWheelNumber(wheelNumber);
}
public void SetPosition(int x, int y)
{
if (x < 0 || x + _truckWidth > _pictureWidth) { x = 0; }
if (y < 0 || y + _truckHeight > _pictureHeight) { y = 0; }
_startPosX = x;
_startPosY = y;
}
public boolean CanMove(DirectionType direction) {
if (entityTruck == null) {
return false;
}
return switch (direction) {
case Left -> _startPosX - entityTruck.getStep() > 0;
case Up -> _startPosY - entityTruck.getStep() > 0;
case Right -> _startPosX + _truckWidth + entityTruck.getStep() < _pictureWidth;
case Down -> _startPosY + _truckHeight + entityTruck.getStep() < _pictureHeight;
default -> false;
};
}
public void MoveTransport(DirectionType direction) {
if (!CanMove(direction) || entityTruck == null) return;
switch (direction) {
//влево
case Left:
if (_startPosX - entityTruck.getStep() > 0)
{
_startPosX -= (int)entityTruck.getStep();
}
break;
//вверх
case Up:
if (_startPosY - entityTruck.getStep() > 0)
{
_startPosY -= (int)entityTruck.getStep();
}
break;
//вправо
case Right:
if (_startPosX + _truckWidth + entityTruck.getStep() < _pictureWidth)
{
_startPosX += (int)entityTruck.getStep();
}
break;
//вниз
case Down:
if (_startPosY + _truckHeight + entityTruck.getStep() < _pictureHeight)
{
_startPosY += (int)entityTruck.getStep();
}
break;
}
}
public void DrawTransport(Graphics2D g2D)
{
if (entityTruck == null) {
return;
}
g2D.setStroke(new BasicStroke(3));
g2D.setPaint(entityTruck.getBodyColor());
g2D.fillRect(_startPosX, _startPosY + 40, 160, 10);
g2D.fillRect(_startPosX + 120, _startPosY, 40, 40);
drawingWheels.drawWheels(g2D, Color.BLACK, _startPosX, _startPosY);
}
}

View File

@ -1,3 +1,5 @@
package DumpTruck;
public enum DirectionType {
Up,
Down,

View File

@ -0,0 +1,37 @@
package Entities;
import java.awt.*;
public class EntityDumpTruck extends EntityTruck {
private boolean Tent;
public boolean getTent() {
return Tent;
}
private boolean DumpBox;
public boolean getDumpBox() {
return DumpBox;
}
private Color DumpBoxColor;
public Color getDumpBoxColor() {
return DumpBoxColor;
}
private Color TentColor;
public Color getTentColor() {
return TentColor;
}
public EntityDumpTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor) {
super(speed, weight, bodyColor);
Tent = tent;
DumpBox = dumpBox;
DumpBoxColor = dumpBoxColor;
TentColor = tentColor;
}
}

View File

@ -0,0 +1,34 @@
package Entities;
import java.awt.*;
public class EntityTruck
{
private int Speed;
public int getSpeed() {
return Speed;
}
private double Weight;
public double getWeight() {
return Weight;
}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
public double getStep() {
return (double)Speed * 100 / Weight;
}
public EntityTruck(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -1,25 +0,0 @@
import java.awt.*;
public class EntityDumpTrack {
private int Speed;
private double Weight;
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
private Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
private boolean BodyWork;
public boolean getBodyWork() {return BodyWork;}
private boolean Awning;
public boolean getAwning() {return Awning;}
public double Step;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean _bodyWork, boolean _awning)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
AdditionalColor = additionalcolor;
BodyWork = _bodyWork;
Awning = _awning;
Step = Speed * 100 / Weight;
}
}

View File

@ -1,124 +0,0 @@
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 FormDumpTrack extends JFrame {
private String title;
private Dimension dimension;
private int Width, Height;
private CanvasDumpTrack canvasAirbus = new CanvasDumpTrack();
private JButton CreateButton = new JButton("Create");;
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton();
public FormDumpTrack(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init() {
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 15;
Height = getHeight() - 35;
CreateButton.setName("CREATE");
Icon iconUp = new ImageIcon("src\\images\\up.jpg");
UpButton.setIcon(iconUp);
UpButton.setName("UP");
DownButton.setName("DOWN");
Icon iconDown = new ImageIcon("src\\images\\down.jpg");
DownButton.setIcon(iconDown);
LeftButton.setName("LEFT");
Icon iconLeft = new ImageIcon("src\\images\\left.jpg");
LeftButton.setIcon(iconLeft);
RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("src\\images\\right.jpg");
RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int StartPositionX = (int)(Math.random() * 90 + 10);
int StartPositionY = (int)(Math.random() * 90 + 10);
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
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 sheepPipes = new Random().nextBoolean();
boolean fuelTank = new Random().nextBoolean();;
canvasAirbus._drawingDumpTrack = new DrawingDumpTrack();
canvasAirbus._drawingDumpTrack.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
canvasAirbus._drawingDumpTrack.SetPictureSize(Width, Height);
canvasAirbus._drawingDumpTrack.SetPosition(StartPositionX, StartPositionY);
canvasAirbus.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasAirbus._drawingDumpTrack == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "UP":
result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Up);
break;
case "DOWN":
result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Down);
break;
case "LEFT":
result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Left);
break;
case "RIGHT":
result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasAirbus.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
canvasAirbus.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
add(CreateButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(canvasAirbus);
setVisible(true);
//обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasAirbus._drawingDumpTrack != null)
canvasAirbus._drawingDumpTrack.SetPictureSize(Width, Height);
canvasAirbus.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
}
});
}
}

View File

@ -0,0 +1,15 @@
import javax.swing.*;
public class FrameDumpTruck extends JFrame {
private PictureBoxDumpTruck pictureBoxDumpTruck;
public FrameDumpTruck() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxDumpTruck = new PictureBoxDumpTruck();
add(pictureBoxDumpTruck);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

View File

@ -0,0 +1,100 @@
package MovementStrategy;
import DumpTruck.DirectionType;
public abstract class AbstractStrategy {
private IMoveableObject moveableObject;
private Status state = Status.NotInit;
private int fieldWidth;
protected int getFieldWidth() {
return fieldWidth;
}
private int fieldHeight;
protected int getFieldHeight() {
return fieldHeight;
}
public Status getStatus() {
return state;
}
public void setData(IMoveableObject moveableObject, int width, int height) {
if (moveableObject == null) {
state = Status.NotInit;
return;
}
state = Status.InProgress;
this.moveableObject = moveableObject;
fieldWidth = width;
fieldHeight = height;
}
public void makeStep() {
if (state != Status.InProgress) {
return;
}
if (isTargetDestination()) {
state = Status.Finish;
return;
}
moveToTarget();
}
protected void moveLeft() {
moveTo(DirectionType.Left);
}
protected void moveRight() {
moveTo(DirectionType.Right);
}
protected void moveUp() {
moveTo(DirectionType.Up);
}
protected void moveDown() {
moveTo(DirectionType.Down);
}
protected ObjectParameters getObjectParameters() {
if (moveableObject == null) {
return null;
}
return moveableObject.getObjectsPosition();
}
protected Integer getStep() {
if (state != Status.InProgress) {
return null;
}
if (moveableObject == null) {
return null;
}
return moveableObject.getStep();
}
protected abstract void moveToTarget();
protected abstract boolean isTargetDestination();
private boolean moveTo(DirectionType directionType) {
if (state != Status.InProgress) {
return false;
}
if (moveableObject == null) {
return false;
}
if (moveableObject.checkCanMove(directionType)) {
moveableObject.moveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,42 @@
package MovementStrategy;
import DumpTruck.DirectionType;
import DrawingObjects.DrawingTruck;
public class DrawingObjectTruck implements IMoveableObject {
private DrawingTruck drawingTruck = null;
public DrawingObjectTruck(DrawingTruck drawingTruck) {
this.drawingTruck = drawingTruck;
}
@Override
public ObjectParameters getObjectsPosition() {
if (drawingTruck == null || drawingTruck.getEntityTruck() == null) {
return null;
}
return new ObjectParameters(drawingTruck.getPosX(), drawingTruck.getPosY(),
drawingTruck.getWidth(), drawingTruck.getHeight());
}
@Override
public int getStep() {
if (drawingTruck == null)
return 0;
return (int) ((drawingTruck.getEntityTruck() != null) ? drawingTruck.getEntityTruck().getStep() : 0);
}
@Override
public boolean checkCanMove(DirectionType direction) {
if (drawingTruck == null)
return false;
return drawingTruck.CanMove(direction);
}
@Override
public void moveObject(DirectionType direction) {
if (drawingTruck != null)
drawingTruck.MoveTransport(direction);
}
}

View File

@ -0,0 +1,13 @@
package MovementStrategy;
import DumpTruck.DirectionType;
public interface IMoveableObject {
ObjectParameters getObjectsPosition();
int getStep();
boolean checkCanMove(DirectionType direction);
void moveObject(DirectionType direction);
}

View File

@ -0,0 +1,36 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean isTargetDestination() {
var objParams = getObjectParameters();
if (objParams == null) {
return false;
}
return objParams.rightBorder() <= getFieldWidth() &&
objParams.rightBorder() + getStep() >= getFieldWidth() &&
objParams.downBorder() <= getFieldHeight() &&
objParams.downBorder() + getStep() >= getFieldHeight();
}
@Override
protected void moveToTarget() {
var objParams = getObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.objectMiddleHorizontal() - getFieldWidth();
if (Math.abs(diffX) > getStep()) {
if (diffX < 0) {
moveRight();
}
}
var diffY = objParams.objectMiddleVertical() - getFieldHeight();
if (Math.abs(diffY) > getStep()) {
if (diffY < 0) {
moveDown();
}
}
}
}

View File

@ -0,0 +1,40 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean isTargetDestination() {
var objParams = getObjectParameters();
if (objParams == null) {
return false;
}
return objParams.objectMiddleHorizontal() <= getFieldWidth() / 2 &&
objParams.objectMiddleHorizontal() + getStep() >= getFieldWidth() / 2 &&
objParams.objectMiddleVertical() <= getFieldHeight() / 2 &&
objParams.objectMiddleVertical() + getStep() >= getFieldHeight() / 2;
}
@Override
protected void moveToTarget() {
var objParams = getObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.objectMiddleHorizontal() - getFieldWidth() / 2;
if (Math.abs(diffX) > getStep()) {
if (diffX > 0) {
moveLeft();
} else {
moveRight();
}
}
var diffY = objParams.objectMiddleVertical() - getFieldHeight() / 2;
if (Math.abs(diffY) > getStep()) {
if (diffY > 0) {
moveUp();
} else {
moveDown();
}
}
}
}

View File

@ -0,0 +1,43 @@
package MovementStrategy;
public class ObjectParameters {
private final int x;
private final int y;
private final int width;
private final int height;
public int leftBorder() {
return x;
}
public int topBorder() {
return y;
}
public int rightBorder() {
return x + width;
}
public int downBorder() {
return y + height;
}
public int objectMiddleHorizontal() {
return x + width / 2;
}
public int objectMiddleVertical() {
return y + height / 2;
}
public ObjectParameters(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}

View File

@ -0,0 +1,9 @@
package MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}

View File

@ -0,0 +1,191 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
import DrawingObjects.*;
import DumpTruck.DirectionType;
import MovementStrategy.*;
public class PictureBoxDumpTruck extends JPanel {
private DrawingTruck drawingTruck;
private AbstractStrategy abstractStrategy;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonCreateDumpTruck;
private JButton buttonCreateTruck;
private JComboBox comboBoxStrategy;
private JButton buttonStep;
public PictureBoxDumpTruck() {
setLayout(null);
setBounds(0, 0, 800, 450);
buttonCreateTruck = new JButton("Создать грузовик");
buttonCreateTruck.setFocusable(false);
buttonCreateTruck.setBounds(12, 415, 150, 30);
add(buttonCreateTruck);
buttonCreateTruck.addActionListener(e -> {
Random random = new Random();
drawingTruck = new DrawingTruck((random.nextInt((200-300)+300)),
random.nextInt((1000-3000)+ 3000),
new Color(random.nextInt( 256), random.nextInt(256),
random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(),
new Color(random.nextInt(256), random.nextInt( 256),
random.nextInt(256)),
new Color(random.nextInt( 256), random.nextInt(256),
random.nextInt( 256)),
this.getWidth(), this.getHeight(), random.nextInt(5));
drawingTruck.SetPosition(random.nextInt((-90)+ 100), random.nextInt((-90)+ 100));
repaint();
});
buttonCreateDumpTruck = new JButton("Создать самосвал");
buttonCreateDumpTruck.setFocusable(false);
buttonCreateDumpTruck.setBounds(180, 415, 150, 30);
add(buttonCreateDumpTruck);
buttonCreateDumpTruck.addActionListener(e -> {
Random random = new Random();
drawingTruck = new DrawingDumpTruck((random.nextInt((200-300)+300)),
random.nextInt((1000-3000)+ 3000),
new Color(random.nextInt( 256), random.nextInt(256),
random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(),
new Color(random.nextInt(256), random.nextInt( 256),
random.nextInt(256)),
new Color(random.nextInt( 256), random.nextInt(256),
random.nextInt( 256)),
this.getWidth(), this.getHeight(), random.nextInt(5));
drawingTruck.SetPosition(random.nextInt((-90)+ 100), random.nextInt((-90)+ 100));
repaint();
});
ActionListener buttonMoveListener = e -> {
if (drawingTruck == null)
{
return;
}
String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) {
case ("buttonUp"):
drawingTruck.MoveTransport(DirectionType.Up);
break;
case ("buttonDown"):
drawingTruck.MoveTransport(DirectionType.Down);
break;
case ("buttonLeft"):
drawingTruck.MoveTransport(DirectionType.Left);
break;
case ("buttonRight"):
drawingTruck.MoveTransport(DirectionType.Right);
break;
}
repaint();
};
buttonLeft = new JButton();
buttonLeft.setName("buttonLeft");
buttonLeft.setFocusable(false);
buttonLeft.setPreferredSize(new Dimension(30, 30));
buttonLeft.setIcon(new ImageIcon("G:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\left.jpg"));
buttonLeft.addActionListener(buttonMoveListener);
buttonLeft.setBounds(686, 408, 30, 30);
add(buttonLeft);
buttonRight = new JButton();
buttonRight.setName("buttonRight");
buttonRight.setFocusable(false);
buttonRight.setPreferredSize(new Dimension(30, 30));
buttonRight.setIcon(new ImageIcon("G:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\right.jpg"));
buttonRight.addActionListener(buttonMoveListener);
buttonRight.setBounds(758, 408, 30, 30);
add(buttonRight);
buttonDown = new JButton();
buttonDown.setName("buttonDown");
buttonDown.setFocusable(false);
buttonDown.setPreferredSize(new Dimension(30, 30));
buttonDown.setIcon(new ImageIcon("G:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\down.jpg"));
buttonDown.addActionListener(buttonMoveListener);
buttonDown.setBounds(722, 408, 30, 30);
add(buttonDown);
buttonUp = new JButton();
buttonUp.setName("buttonUp");
buttonUp.setFocusable(false);
buttonUp.setPreferredSize(new Dimension(30, 30));
buttonUp.setIcon(new ImageIcon("G:\\1 КУРС\\ООП Hard\\PIbd-11_Andrianova_E.N._Hard\\src\\images\\up.jpg"));
buttonUp.addActionListener(buttonMoveListener);
buttonUp.setBounds(722, 372, 30, 30);
add(buttonUp);
String[] items = {
"0",
"1"
};
comboBoxStrategy = new JComboBox(items);
comboBoxStrategy.setBounds(667, 10, 120, 25);
buttonStep = new JButton("Шаг");
buttonStep.setFocusable(false);
buttonStep.setBounds(710, 40, 75, 30);
buttonStep.addActionListener(e -> {
if (drawingTruck == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()) {
case 0 -> new MoveToCenter();
case 1 -> new MoveToBorder();
default -> null;
};
if (abstractStrategy == null)
{
return;
}
abstractStrategy.setData(new DrawingObjectTruck(drawingTruck), this.getWidth(), this.getHeight());
}
if (abstractStrategy == null)
{
return;
}
comboBoxStrategy.setEnabled(false);
abstractStrategy.makeStep();
repaint();
if (abstractStrategy.getStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
});
add(comboBoxStrategy);
add(buttonStep);
setPreferredSize(new Dimension(800, 450));
}
@Override
protected void paintComponent(Graphics g) {
if (drawingTruck == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawingTruck.DrawTransport(g2d);
}
}

View File

@ -1,34 +1,42 @@
package Wheels;
import java.awt.*;
public class DrawingWheels {
public class DrawingWheels implements IDrawingWheels {
private WheelNumber wheelNumber;
@Override
public WheelNumber getWheelNumber() {
return wheelNumber;
}
@Override
public void setWheelNumber(int number) {
switch (number) {
case 2:
wheelNumber = wheelNumber.Two;
wheelNumber = WheelNumber.Two;
break;
case 3:
wheelNumber = wheelNumber.Three;
wheelNumber = WheelNumber.Three;
break;
case 4:
wheelNumber = wheelNumber.Four;
wheelNumber = WheelNumber.Four;
break;
default: wheelNumber = wheelNumber.Two;
default:
wheelNumber = WheelNumber.Two;
}
}
public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
@Override
public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
switch (wheelNumber) {
case Two:
drawTwoWheels(g2D, color, _startPosX, _startPosY);
drawTwoWheels(g2d, color, startPosX, startPosY);
break;
case Three:
drawThreeWheels(g2D, color, _startPosX, _startPosY);
drawThreeWheels(g2d, color, startPosX, startPosY);
break;
case Four:
drawFourWheels(g2D, color, _startPosX, _startPosY);
drawFourWheels(g2d, color, startPosX, startPosY);
break;
}
}
@ -51,4 +59,3 @@ public class DrawingWheels {
g2D.fillOval(_startPosX + 80, _startPosY + 50, 40, 40);
}
}

View File

@ -0,0 +1,72 @@
package Wheels;
import java.awt.*;
public class DrawingWheelsCircles implements IDrawingWheels {
private WheelNumber wheelNumber;
@Override
public WheelNumber getWheelNumber() {
return wheelNumber;
}
@Override
public void setWheelNumber(int number) {
switch (number) {
case 2:
wheelNumber = WheelNumber.Two;
break;
case 3:
wheelNumber = WheelNumber.Three;
break;
case 4:
wheelNumber = WheelNumber.Four;
break;
default:
wheelNumber = WheelNumber.Two;
}
}
@Override
public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
switch (wheelNumber) {
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, _startPosY + 50, 40, 40);
drawPattern(g2D, _startPosX, _startPosY + 50);
g2D.fillOval(_startPosX + 120, _startPosY + 50, 40, 40);
drawPattern(g2D, _startPosX + 120, _startPosY + 50);
}
private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
drawTwoWheels(g2D, color, _startPosX, _startPosY);
g2D.fillOval(_startPosX + 40, _startPosY + 50, 40, 40);
drawPattern(g2D, _startPosX + 40, _startPosY + 50);
}
private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
drawThreeWheels(g2D, color, _startPosX, _startPosY);
g2D.fillOval(_startPosX + 80, _startPosY + 50, 40, 40);
drawPattern(g2D, _startPosX + 80, _startPosY + 50);
}
private void drawPattern(Graphics2D g2D, int x, int y){
g2D.setColor(Color.WHITE);
g2D.drawOval(x + 15, y + 15, 10, 10);
g2D.drawOval(x + 5, y + 5, 30, 30);
g2D.setColor(Color.BLACK);
}
}

View File

@ -0,0 +1,74 @@
package Wheels;
import java.awt.*;
public class DrawingWheelsStar implements IDrawingWheels {
private WheelNumber wheelNumber;
@Override
public WheelNumber getWheelNumber() {
return wheelNumber;
}
@Override
public void setWheelNumber(int number) {
switch (number) {
case 2:
wheelNumber = WheelNumber.Two;
break;
case 3:
wheelNumber = WheelNumber.Three;
break;
case 4:
wheelNumber = WheelNumber.Four;
break;
default:
wheelNumber = WheelNumber.Two;
}
}
@Override
public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) {
switch (wheelNumber) {
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, _startPosY + 50, 40, 40);
drawPattern(g2D, _startPosX, _startPosY + 50);
g2D.fillOval(_startPosX + 120, _startPosY + 50, 40, 40);
drawPattern(g2D, _startPosX + 120, _startPosY + 50);
}
private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
drawTwoWheels(g2D, color, _startPosX, _startPosY);
g2D.fillOval(_startPosX + 40, _startPosY + 50, 40, 40);
drawPattern(g2D, _startPosX + 40, _startPosY + 50);
}
private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
drawThreeWheels(g2D, color, _startPosX, _startPosY);
g2D.fillOval(_startPosX + 80, _startPosY + 50, 40, 40);
drawPattern(g2D, _startPosX + 80, _startPosY + 50);
}
private void drawPattern(Graphics2D g2D, int x, int y){
g2D.setColor(Color.WHITE);
g2D.drawLine(x + 20, y, x + 20, y + 40);
g2D.drawLine(x, y + 20, x + 40, y + 20);
g2D.drawLine(x + 10, y + 10, x + 30, y + 30);
g2D.drawLine(x + 30, y + 10, x + 10, y + 30);
g2D.setColor(Color.BLACK);
}
}

View File

@ -0,0 +1,11 @@
package Wheels;
import java.awt.*;
public interface IDrawingWheels {
void setWheelNumber(int number);
WheelNumber getWheelNumber();
void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY);
}

View File

@ -1,7 +1,9 @@
package Wheels;
public enum WheelNumber {
Two,
Three,
Four
}
}