Compare commits

..

1 Commits
main ... lab1

Author SHA1 Message Date
ebf388da3d Готовая 1 лабораторная 2023-11-24 21:39:08 +04:00
13 changed files with 390 additions and 2 deletions

View File

@ -3,6 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />

BIN
Resources/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Resources/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Resources/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Resources/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -0,0 +1,11 @@
package DumpTruck;
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,110 @@
package DumpTruck;
import java.awt.*;
public class DrawingDumpTruck {
private EntityDumpTruck entityDumpTruck;
public EntityDumpTruck getEntityDumpTruck() {
return entityDumpTruck;
}
private DrawingWheels drawingWheels;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _truckWidth = 160;
private int _truckHeight = 90;
public boolean Init(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 false;
}
_pictureWidth = width;
_pictureHeight = height;
entityDumpTruck = new EntityDumpTruck();
entityDumpTruck.Init(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor);
drawingWheels = new DrawingWheels();
drawingWheels.setWheelNumber(wheelNumber);
return true;
}
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 void MoveTransport(DirectionType direction) {
if (entityDumpTruck == null) return;
switch (direction) {
//влево
case Left:
if (_startPosX - entityDumpTruck.getStep() > 0)
{
_startPosX -= (int)entityDumpTruck.getStep();
}
break;
//вверх
case Up:
if (_startPosY - entityDumpTruck.getStep() > 0)
{
_startPosY -= (int)entityDumpTruck.getStep();
}
break;
//вправо
case Right:
if (_startPosX + _truckWidth + entityDumpTruck.getStep() < _pictureWidth)
{
_startPosX += (int)entityDumpTruck.getStep();
}
break;
//вниз
case Down:
if (_startPosY + _truckHeight + entityDumpTruck.getStep() < _pictureHeight)
{
_startPosY += (int)entityDumpTruck.getStep();
}
break;
}
}
public void DrawTransport(Graphics2D g2D)
{
if (entityDumpTruck == null) {
return;
}
g2D.setStroke(new BasicStroke(3));
g2D.setPaint(entityDumpTruck.getBodyColor());
g2D.fillRect(_startPosX, _startPosY + 40, 160, 10);
g2D.fillRect(_startPosX + 120, _startPosY, 40, 40);
drawingWheels.drawWheels(g2D, Color.BLACK, _startPosX, _startPosY);
if (entityDumpTruck.getDumpBox())
{
g2D.setPaint(entityDumpTruck.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 (entityDumpTruck.getDumpBox() && entityDumpTruck.getTent())
{
g2D.setPaint(entityDumpTruck.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,55 @@
package DumpTruck;
import java.awt.*;
public class DrawingWheels {
private WheelNumber wheelNumber;
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;
}
}
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);
g2D.fillOval(_startPosX + 120, _startPosY + 50, 40, 40);
}
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);
}
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);
}
}

View File

@ -0,0 +1,61 @@
package DumpTruck;
import java.awt.*;
public class EntityDumpTruck {
private int Speed;
public int getSpeed() {
return Speed;
}
private double Weight;
public double getWeight() {
return Weight;
}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
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 double getStep() {
return (double)Speed * 100 / Weight;
}
public void Init(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
Tent = tent;
DumpBox = dumpBox;
DumpBoxColor = dumpBoxColor;
TentColor = tentColor;
}
}

View File

@ -0,0 +1,17 @@
package DumpTruck;
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

@ -1,5 +1,7 @@
package DumpTruck;
public class Main {
public static void main(String[] args) {
new FrameDumpTruck();
}
}
}

View File

@ -0,0 +1,122 @@
package DumpTruck;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
public class PictureBoxDumpTruck extends JPanel {
private DrawingDumpTruck drawingDumpTruck;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonCreateDumpTruck;
public PictureBoxDumpTruck() {
setLayout(null);
setBounds(0, 0, 800, 450);
buttonCreateDumpTruck = new JButton("Создать");
buttonCreateDumpTruck.setFocusable(false);
buttonCreateDumpTruck.setBounds(12, 415, 100, 30);
add(buttonCreateDumpTruck);
buttonCreateDumpTruck.addActionListener(e -> {
Random random = new Random();
drawingDumpTruck = new DrawingDumpTruck();
drawingDumpTruck.Init(random.nextInt(200, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
random.nextBoolean(), random.nextBoolean(),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
drawingDumpTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
});
ActionListener buttonMoveListener = e -> {
if (drawingDumpTruck == null)
{
return;
}
String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) {
case ("buttonUp"):
drawingDumpTruck.MoveTransport(DirectionType.Up);
break;
case ("buttonDown"):
drawingDumpTruck.MoveTransport(DirectionType.Down);
break;
case ("buttonLeft"):
drawingDumpTruck.MoveTransport(DirectionType.Left);
break;
case ("buttonRight"):
drawingDumpTruck.MoveTransport(DirectionType.Right);
break;
}
repaint();
};
buttonLeft = new JButton();
buttonLeft.setName("buttonLeft");
buttonLeft.setFocusable(false);
buttonLeft.setPreferredSize(new Dimension(30, 30));
buttonLeft.setIcon(new ImageIcon("Resources/left.png"));
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("Resources/right.png"));
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("Resources/down.png"));
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("Resources/up.png"));
buttonUp.addActionListener(buttonMoveListener);
buttonUp.setBounds(722, 372, 30, 30);
add(buttonUp);
setPreferredSize(new Dimension(800, 450));
}
@Override
protected void paintComponent(Graphics g) {
if (drawingDumpTruck == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawingDumpTruck.DrawTransport(g2d);
}
}

View File

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