Compare commits

...

5 Commits
main ... lab5

35 changed files with 2115 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,60 @@
package DumpTruck.DrawingObjects;
import DumpTruck.Entities.*;
import DumpTruck.Wheels.IDrawingWheels;
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));
}
}
public DrawingDumpTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels)
{
super(speed, weight, bodyColor, width, height, wheelNumber, iDrawingWheels);
if (getEntityTruck() != null)
{
setEntityTruck(new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor));
}
}
public DrawingDumpTruck(EntityDumpTruck entityDumpTruck, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels)
{
super(entityDumpTruck, width, height, wheelNumber, iDrawingWheels);
if (getEntityTruck() != null)
{
setEntityTruck(entityDumpTruck);
}
}
@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,178 @@
package DumpTruck.DrawingObjects;
import java.awt.*;
import java.util.Random;
import DumpTruck.Entities.*;
import DumpTruck.DirectionType;
import DumpTruck.MovementStrategy.*;
import DumpTruck.Wheels.*;
public class DrawingTruck {
public EntityTruck entityTruck;
public EntityTruck getEntityTruck() {
return entityTruck;
}
protected void setEntityTruck(EntityTruck entityTruck) {
this.entityTruck = entityTruck;
}
public IDrawingWheels drawingWheels;
public int _pictureWidth;
public 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 IMoveableObject GetMoveableObject() {
return new DrawingObjectTruck(this);
}
public DrawingTruck(int speed, double weight, Color bodyColor, 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(0, 3)) {
case 0 -> new DrawingWheels();
case 1 -> new DrawingWheelsStar();
case 2 -> new DrawingWheelsCircles();
default -> new DrawingWheels();
};
drawingWheels.setWheelNumber(wheelNumber);
}
public DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels)
{
if (height < _truckHeight || width < _truckWidth) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
entityTruck = new EntityTruck(speed, weight, bodyColor);
drawingWheels = iDrawingWheels;
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(0, 3)) {
case 0 -> new DrawingWheels();
case 1 -> new DrawingWheelsStar();
case 2 -> new DrawingWheelsCircles();
default -> new DrawingWheels();
};
drawingWheels.setWheelNumber(wheelNumber);
}
public DrawingTruck(EntityTruck entityTruck, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels)
{
if (height < _truckHeight || width < _truckWidth) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
this.entityTruck = entityTruck;
Random random = new Random();
drawingWheels = iDrawingWheels;
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

@ -0,0 +1,40 @@
package DumpTruck.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;
}
public void setDumpBoxColor(Color color) {
DumpBoxColor = color;
}
private Color TentColor;
public Color getTentColor() {
return TentColor;
}
public void setTentColor(Color color) {
TentColor = color;
}
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,37 @@
package DumpTruck.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 void setBodyColor(Color color) {
BodyColor = color;
}
public double getStep() {
return (double)Speed * 100 / Weight;
}
public EntityTruck(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,22 @@
package DumpTruck;
import DumpTruck.Entities.*;
import DumpTruck.Generics.*;
import DumpTruck.Wheels.*;
import javax.swing.*;
import java.awt.*;
public class FrameDop extends JFrame {
private PictureBoxDop pictureBoxDop;
public FrameDop() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxDop = new PictureBoxDop();
add(pictureBoxDop);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

View File

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

View File

@ -0,0 +1,17 @@
package DumpTruck;
import javax.swing.*;
public class FrameTruckCollection extends JFrame {
private PictureBoxCollection pictureBoxCollection;
public FrameTruckCollection() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxCollection = new PictureBoxCollection();
add(pictureBoxCollection);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

View File

@ -0,0 +1,17 @@
package DumpTruck;
import javax.swing.*;
public class FrameTruckConfig extends JFrame {
public PictureBoxTruckConfig pictureBoxTruckConfig;
public FrameTruckConfig() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pictureBoxTruckConfig = new PictureBoxTruckConfig();
add(pictureBoxTruckConfig);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

View File

@ -0,0 +1,61 @@
package DumpTruck.Generics;
import DumpTruck.Entities.*;
import DumpTruck.Wheels.*;
import DumpTruck.DrawingObjects.*;
import java.util.Random;
public class GenericDop<T extends EntityTruck, U extends IDrawingWheels> {
private T[] Trucks;
private U[] Wheels;
private int maxTruckAmount;
private int truckAmount;
private int maxWheelAmount ;
private int wheelAmount;
private Random random;
private int _pictureWidth;
private int _pictureHeight;
public GenericDop(int maxTruckAmount, int maxWheelAmount, int pictureWidth, int pictureHeight){
this.maxTruckAmount = maxTruckAmount;
this.maxWheelAmount = maxWheelAmount;
Trucks = (T[]) new EntityTruck[maxTruckAmount];
Wheels = (U[]) new IDrawingWheels[maxWheelAmount];
truckAmount = 0;
wheelAmount = 0;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
random = new Random();
}
public boolean add(T truck){
if (truck == null || truckAmount > maxTruckAmount)
return false;
Trucks[truckAmount] = truck;
truckAmount++;
return true;
}
public boolean add(U wheel){
if (wheel == null || wheelAmount > maxWheelAmount)
return false;
Wheels[wheelAmount] = wheel;
wheelAmount++;
return true;
}
public DrawingTruck DrawingTruckDop(){
if (truckAmount == 0 || wheelAmount == 0)
return null;
T truck = Trucks[random.nextInt(truckAmount)];
DrawingTruck drawingTruck;
if (truck instanceof EntityDumpTruck){
drawingTruck = new DrawingDumpTruck((EntityDumpTruck)truck, _pictureWidth, _pictureHeight, random.nextInt(2, 5), Wheels[random.nextInt(wheelAmount)]);
}
else{
drawingTruck = new DrawingTruck(truck, _pictureWidth, _pictureHeight, random.nextInt(2, 5), Wheels[random.nextInt(wheelAmount)]);
}
return drawingTruck;
}
}

View File

@ -0,0 +1,70 @@
package DumpTruck.Generics;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SetGeneric<T extends Object> {
public final ArrayList<T> _places;
private final int _maxCount;
public SetGeneric(int count) {
_maxCount = count;
_places = new ArrayList<>();
}
public int Insert(T truck) {
return Insert(truck, 0);
}
public int Insert(T truck, int position) {
if (position < 0 || position >= _maxCount)
return -1;
_places.add(position, truck);
return position;
}
public boolean Remove(int position) {
if (position < 0 || position >= _places.size())
return false;
_places.remove(position);
return true;
}
public T Get(int position) {
if (position < 0 || position >= _places.size())
return null;
return _places.get(position);
}
public Iterable<T> GetTrucks(final Integer maxTrucks) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int currentIndex = 0;
private int count = 0;
@Override
public boolean hasNext() {
return currentIndex < _places.size() && (maxTrucks == null || count < maxTrucks);
}
@Override
public T next() {
if (hasNext()) {
count++;
return _places.get(currentIndex++);
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}

View File

@ -0,0 +1,72 @@
package DumpTruck.Generics;
import DumpTruck.DrawingObjects.*;
import DumpTruck.MovementStrategy.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class TrucksGenericCollection<T extends DrawingTruck, U extends IMoveableObject>{
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 180;
private int _placeSizeHeight = 100;
private SetGeneric<T> _collection;
public TrucksGenericCollection(int pictureWidth, int pictureHeight) {
int width = pictureWidth / _placeSizeWidth;
int height = pictureHeight / _placeSizeHeight;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Add(T obj) {
if (obj == null) {
return -1;
}
return _collection.Insert(obj);
}
public T remove(int pos) {
T obj = _collection.Get(pos);
if (obj != null) {
_collection.Remove(pos);
}
return obj;
}
public U GetU(int pos) {
return (U) _collection.Get(pos).GetMoveableObject();
}
public BufferedImage ShowTrucks() {
BufferedImage bitmap = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bitmap.createGraphics();
DrawBackground(g);
DrawObjects(g);
g.dispose();
return bitmap;
}
private void DrawBackground(Graphics g) {
g.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics g) {
int index = 0;
for (T truck : _collection.GetTrucks(100)) {
if (truck != null)
{
truck._pictureWidth = _pictureWidth;
truck._pictureHeight = _pictureHeight;
truck.SetPosition(index % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, index / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
truck.DrawTransport((Graphics2D) g);
}
index++;
}
}
}

View File

@ -0,0 +1,48 @@
package DumpTruck.Generics;
import DumpTruck.DrawingObjects.*;
import DumpTruck.MovementStrategy.*;
import java.util.ArrayList;
import java.util.HashMap;
public class TrucksGenericStorage {
final HashMap<String, TrucksGenericCollection<DrawingTruck, DrawingObjectTruck>> _truckStorages;
public ArrayList<String> Keys()
{
return new ArrayList<>(_truckStorages.keySet());
}
private final int _pictureWidth;
private final int _pictureHeight;
public TrucksGenericStorage(int pictureWidth, int pictureHeight)
{
_pictureHeight = pictureHeight;
_pictureWidth = pictureWidth;
_truckStorages = new HashMap<>();
}
public void AddSet(String name)
{
if (Keys().contains(name))
return;
_truckStorages.put(name, new TrucksGenericCollection<>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name)
{
if (!Keys().contains(name))
return;
_truckStorages.remove(name);
}
public TrucksGenericCollection<DrawingTruck, DrawingObjectTruck> get(String ind)
{
if (Keys().contains(ind))
return _truckStorages.get(ind);
return null;
}
public DrawingObjectTruck get(String name, int ind)
{
if (!Keys().contains(name))
return null;
return _truckStorages.get(name).GetU(ind);
}
}

View File

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

View File

@ -0,0 +1,99 @@
package DumpTruck.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 boolean moveLeft() {
return moveTo(DirectionType.Left);
}
protected boolean moveRight() {
return moveTo(DirectionType.Right);
}
protected boolean moveUp() {
return moveTo(DirectionType.Up);
}
protected boolean moveDown() {
return 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 DumpTruck.MovementStrategy;
import DumpTruck.DirectionType;
import DumpTruck.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 DumpTruck.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 DumpTruck.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 DumpTruck.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 DumpTruck.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 DumpTruck.MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}

View File

@ -0,0 +1,199 @@
package DumpTruck;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.Random;
import DumpTruck.DrawingObjects.*;
import DumpTruck.Generics.*;
import DumpTruck.MovementStrategy.*;
public class PictureBoxCollection extends JPanel {
private TrucksGenericStorage _storage;
private LinkedList<DrawingTruck> removedTrucks;
private JLabel labelTools;
private JButton buttonAddTruck, buttonDeleteTruck, buttonRefreshCollection, buttonShowDop, buttonAddCollection, buttonDeleteCollection, buttonShowDeleted;
private JTextField textFieldNumber, textFieldCollectionNumber;
private JList<String> listStorage;
private DefaultListModel<String> listModel;
protected void ReloadCollections()
{
int index = listStorage.getSelectedIndex();
listModel.clear();
for (String key : _storage.Keys())
{
listModel.addElement(key);
}
if (!listModel.isEmpty() && (index == -1 || index >= listModel.size()))
{
listStorage.setSelectedIndex(0);
}
else if (!listModel.isEmpty() && index > -1 && index < listModel.size())
{
listStorage.setSelectedIndex(index);
}
}
public PictureBoxCollection() {
removedTrucks = new LinkedList<>();
setLayout(null);
setBounds(0, 0, 800, 750);
labelTools = new JLabel("Наборы");
labelTools.setBounds(660, 10, 150, 30);
add(labelTools);
textFieldCollectionNumber = new JTextField();
textFieldCollectionNumber.setBounds(620, 50, 150, 30);
add(textFieldCollectionNumber);
buttonAddCollection = new JButton("Добавить набор");
buttonAddCollection.setFocusable(false);
buttonAddCollection.setBounds(620, 100, 150, 30);
buttonAddCollection.addActionListener(e -> {
String name = textFieldCollectionNumber.getText();
if (name.length() == 0)
return;
_storage.AddSet(name);
ReloadCollections();
});
add(buttonAddCollection);
listModel = new DefaultListModel<>();
listStorage = new JList<String>(listModel);
listStorage.setLayout(null);
listStorage.setBounds(620, 150, 150, 100);
add(listStorage);
listStorage.addListSelectionListener(e -> {
repaint();
});
buttonDeleteCollection = new JButton("Удалить набор");
buttonDeleteCollection.setFocusable(false);
buttonDeleteCollection.setBounds(620, 270, 150, 30);
buttonDeleteCollection.addActionListener(e -> {
if (listStorage.getSelectedIndex() == -1)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Delete object " + listStorage.getSelectedValue() + "?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
_storage.DelSet(listStorage.getSelectedValue());
ReloadCollections();
});
add(buttonDeleteCollection);
_storage = new TrucksGenericStorage(this.getWidth() - 200, this.getHeight());
labelTools = new JLabel("Инструменты");
labelTools.setBounds(660, 300, 150, 30);
add(labelTools);
buttonAddTruck = new JButton("Добавить грузовик");
buttonAddTruck.setFocusable(false);
buttonAddTruck.setBounds(620, 350, 150, 30);
buttonAddTruck.addActionListener(e -> {
if (listStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listStorage.getSelectedValue());
if (obj == null)
return;
FrameTruckConfig frameTruckConfig = new FrameTruckConfig();
frameTruckConfig.pictureBoxTruckConfig.buttonAdd.addActionListener(e1 -> {
if (obj != null && obj.Add(frameTruckConfig.pictureBoxTruckConfig._truck) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
frameTruckConfig.dispose();
repaint();
}
);
frameTruckConfig.pictureBoxTruckConfig.buttonCancel.addActionListener(e1 -> frameTruckConfig.dispose());
});
add(buttonAddTruck);
textFieldNumber = new JTextField();
textFieldNumber.setBounds(620, 400, 150, 30);
add(textFieldNumber);
buttonDeleteTruck = new JButton("Удалить грузовик");
buttonDeleteTruck.setFocusable(false);
buttonDeleteTruck.setBounds(620, 450, 150, 30);
buttonDeleteTruck.addActionListener(e -> {
if (listStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listStorage.getSelectedValue());
if (obj == null)
return;
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
return;
}
for (char it : textFieldNumber.getText().toCharArray())
if (it < '0' || it > '9') {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (textFieldNumber.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
int pos = Integer.parseInt(textFieldNumber.getText());
DrawingTruck removed = null;
removed = obj.remove(pos);
if (removed != null) {
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
removedTrucks.add(removed);
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
repaint();
});
add(buttonDeleteTruck);
buttonRefreshCollection = new JButton("Обновить коллекцию");
buttonRefreshCollection.setFocusable(false);
buttonRefreshCollection.setBounds(620, 500, 150, 30);
buttonRefreshCollection.addActionListener(e -> repaint());
add(buttonRefreshCollection);
buttonShowDeleted = new JButton("Удаленные");
buttonShowDeleted.setFocusable(false);
buttonShowDeleted.setBounds(620, 550, 150, 30);
buttonShowDeleted.addActionListener(e -> {
if (removedTrucks.size() == 0){
JOptionPane.showMessageDialog(null, "Коллекция пуста", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
var truck = removedTrucks.removeLast();
FrameDumpTruck frameDumpTruck = new FrameDumpTruck(truck);
frameDumpTruck.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
});
add(buttonShowDeleted);
buttonShowDop = new JButton("Показать доп");
buttonShowDop.setFocusable(false);
buttonShowDop.setBounds(620, 650, 150, 30);
buttonShowDop.addActionListener(e -> new FrameDop());
add(buttonShowDop);
setPreferredSize(new Dimension(800, 750));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (listStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listStorage.getSelectedValue());
if (obj == null)
return;
if (obj.ShowTrucks() == null)
return;
g2d.drawImage(obj.ShowTrucks(), 0, 0, null);
}
}

View File

@ -0,0 +1,50 @@
package DumpTruck;
import DumpTruck.DrawingObjects.DrawingTruck;
import DumpTruck.Entities.*;
import DumpTruck.Generics.*;
import DumpTruck.Wheels.*;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class PictureBoxDop extends JPanel {
GenericDop<EntityTruck, IDrawingWheels> genericDop;
private JButton buttonCreate;
public DrawingTruck drawingTruck;
public PictureBoxDop(){
setLayout(null);
setBounds(0, 0, 800, 450);
genericDop = new GenericDop<>(100, 100, this.getWidth(), this.getHeight());
genericDop.add(new EntityTruck(100, 100, Color.BLUE));
genericDop.add(new EntityTruck(100, 100, Color.RED));
genericDop.add(new EntityTruck(100, 100, Color.GREEN));
genericDop.add(new EntityDumpTruck(100, 100, Color.BLUE, true, true, Color.BLACK, Color.RED));
genericDop.add(new EntityDumpTruck(100, 100, Color.GREEN, true, true, Color.MAGENTA, Color.YELLOW));
genericDop.add(new DrawingWheels());
genericDop.add(new DrawingWheelsCircles());
genericDop.add(new DrawingWheelsStar());
buttonCreate = new JButton("Создать грузовик");
buttonCreate.setFocusable(false);
buttonCreate.setBounds(12, 415, 150, 30);
buttonCreate.addActionListener(e -> {
drawingTruck = genericDop.DrawingTruckDop();
repaint();
});
add(buttonCreate);
setPreferredSize(new Dimension(800, 450));
}
@Override
protected void paintComponent(Graphics g) {
if (drawingTruck == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawingTruck.SetPosition(100, 100);
drawingTruck.DrawTransport(g2d);
}
}

View File

@ -0,0 +1,216 @@
package DumpTruck;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
import DumpTruck.DrawingObjects.*;
import DumpTruck.MovementStrategy.*;
public class PictureBoxDumpTruck extends JPanel {
public 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 JButton buttonSelectTruck;
public PictureBoxDumpTruck(DrawingTruck truck) {
Random random = new Random();
setLayout(null);
setBounds(0, 0, 800, 450);
buttonCreateTruck = new JButton("Создать грузовик");
buttonCreateTruck.setFocusable(false);
buttonCreateTruck.setBounds(12, 415, 150, 30);
add(buttonCreateTruck);
if (truck != null){
drawingTruck = truck;
drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
}
buttonCreateTruck.addActionListener(e -> {
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
bodyColor = selectedColor;
}
drawingTruck = new DrawingTruck((random.nextInt(200, 300)),
random.nextInt(1000, 3000),
bodyColor,
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
});
buttonCreateDumpTruck = new JButton("Создать самосвал");
buttonCreateDumpTruck.setFocusable(false);
buttonCreateDumpTruck.setBounds(180, 415, 150, 30);
add(buttonCreateDumpTruck);
buttonCreateDumpTruck.addActionListener(e -> {
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
bodyColor = selectedColor;
}
Color dumpBoxColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
dumpBoxColor = selectedColor;
}
Color tentColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
tentColor = selectedColor;
}
drawingTruck = new DrawingDumpTruck((random.nextInt(200, 300)),
random.nextInt(1000, 3000),
bodyColor,
random.nextBoolean(), true,
tentColor, dumpBoxColor,
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
});
buttonSelectTruck = new JButton("Выбрать");
buttonSelectTruck.setFocusable(false);
buttonSelectTruck.setBounds(350, 415, 150, 30);
add(buttonSelectTruck);
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("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);
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

@ -0,0 +1,484 @@
package DumpTruck;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random;
import DumpTruck.DrawingObjects.*;
import DumpTruck.Entities.EntityDumpTruck;
import DumpTruck.Entities.EntityTruck;
import DumpTruck.Wheels.DrawingWheels;
import DumpTruck.Wheels.DrawingWheelsCircles;
import DumpTruck.Wheels.DrawingWheelsStar;
import DumpTruck.Wheels.IDrawingWheels;
import java.awt.event.*;
public class PictureBoxTruckConfig extends JPanel {
private class LabelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel)c).getText());
}
}
private class ColorTransferable implements Transferable {
private Color color;
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
public ColorTransferable(Color color) {
this.color = color;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{colorDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return colorDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(((JPanel)c).getBackground());
}
}
private class LabelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
}
}
private class PanelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
}
}
private class WheelTransferable implements Transferable {
private IDrawingWheels wheelDrawing;
private static final DataFlavor wheelDrawingDataFlavor = new DataFlavor(IDrawingWheels.class, "Wheel Drawing");
public WheelTransferable(IDrawingWheels wheelDrawing) {
this.wheelDrawing = wheelDrawing;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{wheelDrawingDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(wheelDrawingDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return wheelDrawing;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private class WheelPanel extends JPanel{
public IDrawingWheels wheelDrawing;
public WheelPanel(IDrawingWheels _wheelDrawing){
wheelDrawing = _wheelDrawing;
wheelDrawing.setWheelNumber(2);
this.addMouseListener(
new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
((WheelPanel)e.getComponent()).getTransferHandler().exportAsDrag(((WheelPanel)e.getComponent()), e, TransferHandler.COPY);
}
}
);
this.setTransferHandler(
new TransferHandler(){
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new WheelTransferable(((WheelPanel)c).wheelDrawing);
}
}
);
}
public void paintComponent (Graphics g){
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
wheelDrawing.drawWheels(g2d,Color.BLACK,0, -50);
}
}
private class TruckPreview extends JPanel{
@Override
public void paintComponent (Graphics g) {
if (_truck == null) {
return;
}
_truck.SetPosition(20, 20);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
_truck.DrawTransport(g2d);
}
}
DrawingTruck _truck = null;
JButton buttonAdd, buttonCancel;
JLabel labelParameters, labelColors, labelSpeed, labelWeight, labelWheelAmount, labelSimpleObject, labelModifiedObject, labelBodyColor, labelDumboxColor, labelTentColor, labelWheels, labelWheelPreview;
JCheckBox checkBoxTent, checkBoxDumpBox;
SpinnerModel spinnerModelSpeed, spinnerModelWeight, spinnerModelWheelAmount;
JSpinner numericUpDownSpeed, numericUpDownWeight, numericUpDownWheelAmount;
JPanel panelRed, panelGreen, panelBlue, panelYellow, panelWhite, panelGray, panelBlack, panelMagenta;
TruckPreview truckPreview;
public PictureBoxTruckConfig() {
Random random = new Random();
setLayout(null);
setBounds(0, 0, 800, 300);
buttonAdd = new JButton("Добавить");
buttonAdd.setFocusable(false);
buttonAdd.setBounds(550, 260, 100, 30);
add(buttonAdd);
buttonCancel = new JButton("Отмена");
buttonCancel.setFocusable(false);
buttonCancel.setBounds(670, 260, 100, 30);
add(buttonCancel);
labelParameters = new JLabel("Параметры");
labelParameters.setBounds(10, 10, 100, 20);
add(labelParameters);
labelSpeed = new JLabel("Скорость:");
labelSpeed.setBounds(10, 50, 100, 20);
add(labelSpeed);
labelWeight = new JLabel("Вес:");
labelWeight.setBounds(10, 80, 100, 20);
add(labelWeight);
labelWheelAmount = new JLabel("Кол-во колес:");
labelWheelAmount.setBounds(10, 110, 100, 20);
add(labelWheelAmount);
checkBoxTent = new JCheckBox("Признак наличия кузова");
checkBoxTent.setBounds(10, 200, 200, 20);
checkBoxTent.setFocusable(false);
add(checkBoxTent);
checkBoxDumpBox = new JCheckBox("Признак наличия тента");
checkBoxDumpBox.setBounds(10, 230, 200, 20);
checkBoxDumpBox.setFocusable(false);
add(checkBoxDumpBox);
spinnerModelSpeed = new SpinnerNumberModel(100, 100, 1000, 1);
numericUpDownSpeed = new JSpinner(spinnerModelSpeed);
numericUpDownSpeed.setBounds(100, 50, 50, 20);
numericUpDownSpeed.setFocusable(false);
add(numericUpDownSpeed);
spinnerModelWeight = new SpinnerNumberModel(100, 100, 1000, 1);
numericUpDownWeight = new JSpinner(spinnerModelWeight);
numericUpDownWeight.setBounds(100, 80, 50, 20);
numericUpDownWeight.setFocusable(false);
add(numericUpDownWeight);
spinnerModelWheelAmount = new SpinnerNumberModel(2, 2, 4, 1);
numericUpDownWheelAmount = new JSpinner(spinnerModelWheelAmount);
numericUpDownWheelAmount.setBounds(100, 110, 50, 20);
numericUpDownWheelAmount.setFocusable(false);
add(numericUpDownWheelAmount);
labelColors = new JLabel("Цвета");
labelColors.setBounds(250, 20, 100, 20);
add(labelColors);
Border border = BorderFactory.createLineBorder(Color.BLACK);
labelSimpleObject = new JLabel("Простой");
labelSimpleObject.setBounds(250, 240, 100, 50);
labelSimpleObject.setHorizontalAlignment(SwingConstants.CENTER);
labelSimpleObject.setBorder(border);
labelSimpleObject.setTransferHandler(new LabelTransferHandler());
labelSimpleObject.addMouseListener(new LabelMouseAdapter());
add(labelSimpleObject);
labelModifiedObject = new JLabel("Продвинутый");
labelModifiedObject.setBounds(370, 240, 100, 50);
labelModifiedObject.setHorizontalAlignment(SwingConstants.CENTER);
labelModifiedObject.setBorder(border);
labelModifiedObject.setTransferHandler(new LabelTransferHandler());
labelModifiedObject.addMouseListener(new LabelMouseAdapter());
add(labelModifiedObject);
labelBodyColor = new JLabel("Цвет");
labelBodyColor.setBounds(530, 20, 80, 40);
labelBodyColor.setHorizontalAlignment(SwingConstants.CENTER);
labelBodyColor.setBorder(border);
labelBodyColor.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_truck == null)
return false;
_truck.entityTruck.setBodyColor(color);
truckPreview.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
add(labelBodyColor);
labelDumboxColor = new JLabel("Цвет кузова");
labelDumboxColor.setBounds(620, 20, 80, 40);
labelDumboxColor.setHorizontalAlignment(SwingConstants.CENTER);
labelDumboxColor.setBorder(border);
labelDumboxColor.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_truck == null)
return false;
if (!(_truck instanceof DrawingDumpTruck))
return false;
((EntityDumpTruck)_truck.entityTruck).setDumpBoxColor(color);
truckPreview.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
add(labelDumboxColor);
labelTentColor = new JLabel("Цвет тента");
labelTentColor.setBounds(710, 20, 80, 40);
labelTentColor.setHorizontalAlignment(SwingConstants.CENTER);
labelTentColor.setBorder(border);
labelTentColor.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_truck == null)
return false;
if (!(_truck instanceof DrawingDumpTruck))
return false;
((EntityDumpTruck)_truck.entityTruck).setTentColor(color);
truckPreview.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
add(labelTentColor);
labelWheelPreview = new JLabel("Колесо");
labelWheelPreview.setBounds(530, 70, 80, 30);
labelWheelPreview.setHorizontalAlignment(SwingConstants.CENTER);
labelWheelPreview.setBorder(border);
add(labelWheelPreview);
labelWheelPreview.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(WheelTransferable.wheelDrawingDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDrawingWheels iDrawingWheels = (IDrawingWheels) support.getTransferable().getTransferData(WheelTransferable.wheelDrawingDataFlavor);
if (_truck == null)
return false;
_truck.drawingWheels = iDrawingWheels;
_truck.drawingWheels.setWheelNumber((int)numericUpDownWheelAmount.getValue());
truckPreview.repaint();
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
panelRed = new JPanel();
panelRed.setBounds(250, 50, 40, 40);
panelRed.setBackground(Color.RED);
panelRed.setTransferHandler(new PanelTransferHandler());
panelRed.addMouseListener(new PanelMouseAdapter());
add(panelRed);
panelGreen = new JPanel();
panelGreen.setBounds(300, 50, 40, 40);
panelGreen.setBackground(Color.GREEN);
panelGreen.setTransferHandler(new PanelTransferHandler());
panelGreen.addMouseListener(new PanelMouseAdapter());
add(panelGreen);
panelBlue = new JPanel();
panelBlue.setBounds(350, 50, 40, 40);
panelBlue.setBackground(Color.BLUE);
panelBlue.setTransferHandler(new PanelTransferHandler());
panelBlue.addMouseListener(new PanelMouseAdapter());
add(panelBlue);
panelYellow = new JPanel();
panelYellow.setBounds(400, 50, 40, 40);
panelYellow.setBackground(Color.YELLOW);
panelYellow.setTransferHandler(new PanelTransferHandler());
panelYellow.addMouseListener(new PanelMouseAdapter());
add(panelYellow);
panelWhite = new JPanel();
panelWhite.setBounds(250, 100, 40, 40);
panelWhite.setBackground(Color.WHITE);
panelWhite.setTransferHandler(new PanelTransferHandler());
panelWhite.addMouseListener(new PanelMouseAdapter());
add(panelWhite);
panelGray = new JPanel();
panelGray.setBounds(300, 100, 40, 40);
panelGray.setBackground(Color.GRAY);
panelGray.setTransferHandler(new PanelTransferHandler());
panelGray.addMouseListener(new PanelMouseAdapter());
add(panelGray);
panelBlack = new JPanel();
panelBlack.setBounds(350, 100, 40, 40);
panelBlack.setBackground(Color.black);
panelBlack.setTransferHandler(new PanelTransferHandler());
panelBlack.addMouseListener(new PanelMouseAdapter());
add(panelBlack);
panelMagenta = new JPanel();
panelMagenta.setBounds(400, 100, 40, 40);
panelMagenta.setBackground(Color.MAGENTA);
panelMagenta.setTransferHandler(new PanelTransferHandler());
panelMagenta.addMouseListener(new PanelMouseAdapter());
add(panelMagenta);
labelWheels = new JLabel("Колеса");
labelWheels.setBounds(250, 150, 100, 20);
add(labelWheels);
WheelPanel wheelPanelDefault = new WheelPanel(new DrawingWheels());
wheelPanelDefault.setBounds(250, 180, 40, 40);
add(wheelPanelDefault);
WheelPanel wheelPanelCircles = new WheelPanel(new DrawingWheelsCircles());
wheelPanelCircles.setBounds(300, 180, 40, 40);
add(wheelPanelCircles);
WheelPanel wheelPanelStar = new WheelPanel(new DrawingWheelsStar());
wheelPanelStar.setBounds(350, 180, 40, 40);
add(wheelPanelStar);
truckPreview = new TruckPreview();
truckPreview.setBounds(530, 110, 260, 140);
truckPreview.setBorder(border);
add(truckPreview);
truckPreview.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "Простой":
_truck = new DrawingTruck((int)numericUpDownSpeed.getValue(), (int)numericUpDownWeight.getValue(), Color.WHITE, truckPreview.getWidth(), truckPreview.getHeight(), (int)numericUpDownWheelAmount.getValue(), new DrawingWheels());
break;
case "Продвинутый":
_truck = new DrawingDumpTruck((int)numericUpDownSpeed.getValue(), (int)numericUpDownWeight.getValue(), Color.WHITE, checkBoxDumpBox.isSelected(), checkBoxTent.isSelected(), Color.BLACK, Color.GRAY, truckPreview.getWidth(), truckPreview.getHeight(), (int)numericUpDownWheelAmount.getValue(), new DrawingWheels());
break;
}
truckPreview.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
setPreferredSize(new Dimension(800, 300));
}
}

View File

@ -0,0 +1,61 @@
package DumpTruck.Wheels;
import java.awt.*;
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;
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);
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,72 @@
package DumpTruck.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 DumpTruck.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 DumpTruck.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

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