Готовая 3 лабораторная (Усложненная)
This commit is contained in:
parent
ae45a3a851
commit
e67810013b
@ -3,6 +3,7 @@ package projectMonorail.DrawingObjects;
|
|||||||
import projectMonorail.DirectionType;
|
import projectMonorail.DirectionType;
|
||||||
import projectMonorail.Entities.*;
|
import projectMonorail.Entities.*;
|
||||||
import projectMonorail.Extras.*;
|
import projectMonorail.Extras.*;
|
||||||
|
import projectMonorail.MovementStrategy.*;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -49,6 +50,10 @@ public class DrawingLocomotive {
|
|||||||
return locomotiveHeight;
|
return locomotiveHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IMoveableObject getMoveableObject() {
|
||||||
|
return new DrawingObjectLocomotive(this);
|
||||||
|
}
|
||||||
|
|
||||||
public DrawingLocomotive(int speed, double weight, Color mainColor, int width, int height, int wheelNumber) {
|
public DrawingLocomotive(int speed, double weight, Color mainColor, int width, int height, int wheelNumber) {
|
||||||
if (width < locomotiveWidth || height < locomotiveHeight) {
|
if (width < locomotiveWidth || height < locomotiveHeight) {
|
||||||
return;
|
return;
|
||||||
@ -86,6 +91,18 @@ public class DrawingLocomotive {
|
|||||||
drawingWheels.setWheelNumber(wheelNumber);
|
drawingWheels.setWheelNumber(wheelNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DrawingLocomotive(EntityLocomotive entityLocomotive, int width, int height, int wheelNumber,
|
||||||
|
IDrawingWheels drawingWheels) {
|
||||||
|
if (width < locomotiveWidth || height < locomotiveHeight) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pictureWidth = width;
|
||||||
|
pictureHeight = height;
|
||||||
|
this.entityLocomotive = entityLocomotive;
|
||||||
|
this.drawingWheels = drawingWheels;
|
||||||
|
drawingWheels.setWheelNumber(wheelNumber);
|
||||||
|
}
|
||||||
|
|
||||||
public void setPosition(int x, int y) {
|
public void setPosition(int x, int y) {
|
||||||
if (x < 0 || x + locomotiveWidth > pictureWidth) {
|
if (x < 0 || x + locomotiveWidth > pictureWidth) {
|
||||||
x = 0;
|
x = 0;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package projectMonorail.DrawingObjects;
|
package projectMonorail.DrawingObjects;
|
||||||
|
|
||||||
import projectMonorail.Entities.*;
|
import projectMonorail.Entities.*;
|
||||||
|
import projectMonorail.Extras.*;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
@ -25,6 +26,11 @@ public class DrawingMonorail extends DrawingLocomotive {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DrawingMonorail(EntityMonorail entityMonorail, int width, int height, int wheelNumber,
|
||||||
|
IDrawingWheels drawingWheels) {
|
||||||
|
super(entityMonorail, width, height, wheelNumber, drawingWheels);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawTransport(Graphics2D g2d) {
|
public void drawTransport(Graphics2D g2d) {
|
||||||
if (!(getEntityLocomotive() instanceof EntityMonorail monorail)) {
|
if (!(getEntityLocomotive() instanceof EntityMonorail monorail)) {
|
||||||
@ -54,7 +60,7 @@ public class DrawingMonorail extends DrawingLocomotive {
|
|||||||
//дополнительная кабина
|
//дополнительная кабина
|
||||||
if (monorail.getExtraCabin()) {
|
if (monorail.getExtraCabin()) {
|
||||||
//корпус дополнительной кабины
|
//корпус дополнительной кабины
|
||||||
g2d.setColor(mainColor);
|
g2d.setColor(additionalColor);
|
||||||
g2d.fillRect(startPosX + 118, startPosY + 15, 65, 31);
|
g2d.fillRect(startPosX + 118, startPosY + 15, 65, 31);
|
||||||
g2d.setColor(Color.BLACK);
|
g2d.setColor(Color.BLACK);
|
||||||
g2d.drawRect(startPosX + 118, startPosY + 15, 65, 31);
|
g2d.drawRect(startPosX + 118, startPosY + 15, 65, 31);
|
||||||
@ -64,7 +70,7 @@ public class DrawingMonorail extends DrawingLocomotive {
|
|||||||
g2d.drawLine(startPosX + 118, startPosY + 31, startPosX + 183, startPosY + 31);
|
g2d.drawLine(startPosX + 118, startPosY + 31, startPosX + 183, startPosY + 31);
|
||||||
|
|
||||||
//дверь дополнительной кабины
|
//дверь дополнительной кабины
|
||||||
g2d.setColor(additionalColor);
|
g2d.setColor(Color.GRAY);
|
||||||
g2d.fillRect(startPosX + 146, startPosY + 21, 7, 20);
|
g2d.fillRect(startPosX + 146, startPosY + 21, 7, 20);
|
||||||
|
|
||||||
g2d.setStroke(largerWidth);
|
g2d.setStroke(largerWidth);
|
||||||
|
58
src/projectMonorail/Extras/RandomGeneric.java
Normal file
58
src/projectMonorail/Extras/RandomGeneric.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package projectMonorail.Extras;
|
||||||
|
|
||||||
|
import projectMonorail.DrawingObjects.DrawingLocomotive;
|
||||||
|
import projectMonorail.DrawingObjects.DrawingMonorail;
|
||||||
|
import projectMonorail.Entities.*;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class RandomGeneric<E extends EntityLocomotive, D extends IDrawingWheels> {
|
||||||
|
private final E[] locomotives;
|
||||||
|
|
||||||
|
private int locomotivesCurrNum = 0;
|
||||||
|
|
||||||
|
private final D[] wheels;
|
||||||
|
|
||||||
|
private int wheelsCurrNum = 0;
|
||||||
|
|
||||||
|
private int pictureWidth;
|
||||||
|
|
||||||
|
private int pictureHeight;
|
||||||
|
|
||||||
|
public RandomGeneric(int locomotivesCount, int drawingsCount, int pictureWidth, int pictureHeight) {
|
||||||
|
locomotives = (E[])new EntityLocomotive[locomotivesCount];
|
||||||
|
wheels = (D[])new IDrawingWheels[drawingsCount];
|
||||||
|
this.pictureWidth = pictureWidth;
|
||||||
|
this.pictureHeight = pictureHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(E locomotive) {
|
||||||
|
if (locomotive == null || locomotivesCurrNum >= locomotives.length)
|
||||||
|
return;
|
||||||
|
|
||||||
|
locomotives[locomotivesCurrNum++] = locomotive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(D drawingWheels) {
|
||||||
|
if (drawingWheels == null || wheelsCurrNum >= wheels.length)
|
||||||
|
return;
|
||||||
|
|
||||||
|
wheels[wheelsCurrNum++] = drawingWheels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingLocomotive getDrawingLocomotive() {
|
||||||
|
if (locomotivesCurrNum == 0 || wheelsCurrNum == 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
EntityLocomotive locomotive = locomotives[ThreadLocalRandom.current().nextInt(0, locomotivesCurrNum)];
|
||||||
|
IDrawingWheels drawingWheels = wheels[ThreadLocalRandom.current().nextInt(0, wheelsCurrNum)];
|
||||||
|
|
||||||
|
if (locomotive instanceof EntityMonorail) {
|
||||||
|
return new DrawingMonorail((EntityMonorail) locomotive, pictureWidth, pictureHeight, ThreadLocalRandom.current().nextInt(2, 5),
|
||||||
|
drawingWheels);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DrawingLocomotive(locomotive, pictureWidth, pictureHeight, ThreadLocalRandom.current().nextInt(2, 5),
|
||||||
|
drawingWheels);
|
||||||
|
}
|
||||||
|
}
|
@ -2,13 +2,17 @@ package projectMonorail;
|
|||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
public class FrameMonorail extends JFrame {
|
public class FrameMonorail extends JDialog {
|
||||||
|
|
||||||
private PictureBox pictureBox;
|
private PictureBox pictureBox;
|
||||||
|
|
||||||
public FrameMonorail() {
|
public PictureBox getPictureBox() {
|
||||||
super("Monorail");
|
return pictureBox;
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
}
|
||||||
|
|
||||||
|
public FrameMonorail(JFrame parent) {
|
||||||
|
super(parent ,"Monorail", true);
|
||||||
|
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
|
|
||||||
pictureBox = new PictureBox();
|
pictureBox = new PictureBox();
|
||||||
add(pictureBox);
|
add(pictureBox);
|
||||||
|
179
src/projectMonorail/FrameMonorailCollection.java
Normal file
179
src/projectMonorail/FrameMonorailCollection.java
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
package projectMonorail;
|
||||||
|
|
||||||
|
import projectMonorail.DrawingObjects.*;
|
||||||
|
import projectMonorail.Generics.*;
|
||||||
|
import projectMonorail.MovementStrategy.*;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.text.NumberFormatter;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.text.NumberFormat;
|
||||||
|
|
||||||
|
public class FrameMonorailCollection extends JFrame {
|
||||||
|
|
||||||
|
private final LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> locomotives;
|
||||||
|
|
||||||
|
private JPanel paddingTopPanel;
|
||||||
|
|
||||||
|
private JPanel mainPanel;
|
||||||
|
|
||||||
|
private PictureBoxCollection pictureBoxCollection;
|
||||||
|
|
||||||
|
private int pictureBoxCollectionWidth = 900;
|
||||||
|
|
||||||
|
private int pictureBoxCollectionHeight = 430;
|
||||||
|
|
||||||
|
private JPanel toolsPanel;
|
||||||
|
|
||||||
|
private JLabel nameLabel;
|
||||||
|
|
||||||
|
private JButton buttonAddMonorail;
|
||||||
|
|
||||||
|
private JButton buttonRemoveMonorail;
|
||||||
|
|
||||||
|
private JButton buttonRefreshCollection;
|
||||||
|
|
||||||
|
private JButton buttonRandomGeneration;
|
||||||
|
|
||||||
|
private JFormattedTextField textFieldNumber;
|
||||||
|
|
||||||
|
public FrameMonorailCollection() {
|
||||||
|
super("Monorail collection");
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
locomotives = new LocomotivesGenericCollection<>(pictureBoxCollectionWidth, pictureBoxCollectionHeight);
|
||||||
|
pictureBoxCollection = new PictureBoxCollection(locomotives, pictureBoxCollectionWidth,
|
||||||
|
pictureBoxCollectionHeight);
|
||||||
|
|
||||||
|
nameLabel = new JLabel(" Tools");
|
||||||
|
nameLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
|
||||||
|
nameLabel.setHorizontalTextPosition(JLabel.LEFT);
|
||||||
|
nameLabel.setMaximumSize(new Dimension(190, 30));
|
||||||
|
nameLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
buttonAddMonorail = new JButton("Add monorail");
|
||||||
|
buttonAddMonorail.setHorizontalTextPosition(JButton.CENTER);
|
||||||
|
buttonAddMonorail.setVerticalTextPosition(JButton.CENTER);
|
||||||
|
buttonAddMonorail.setFocusable(false);
|
||||||
|
buttonAddMonorail.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||||
|
buttonAddMonorail.setBackground(Color.WHITE);
|
||||||
|
buttonAddMonorail.setMaximumSize(new Dimension(155, 36));
|
||||||
|
buttonAddMonorail.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||||
|
buttonAddMonorail.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
buttonAddMonorail.addActionListener(e -> {
|
||||||
|
FrameMonorail frame = new FrameMonorail(this);
|
||||||
|
if (frame.getPictureBox().getDialogResult()) {
|
||||||
|
if (locomotives.addition(frame.getPictureBox().getSelectedLocomotive()) != -1) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Object added", "Message",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
pictureBoxCollection.repaint();
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(null, "Failed to add the object", "Message",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
NumberFormat format = NumberFormat.getInstance();
|
||||||
|
NumberFormatter formatter = new NumberFormatter(format);
|
||||||
|
formatter.setValueClass(Integer.class);
|
||||||
|
formatter.setMinimum(0);
|
||||||
|
formatter.setMaximum(Integer.MAX_VALUE);
|
||||||
|
formatter.setAllowsInvalid(false);
|
||||||
|
formatter.setCommitsOnValidEdit(true);
|
||||||
|
|
||||||
|
textFieldNumber = new JFormattedTextField(formatter);
|
||||||
|
textFieldNumber.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||||
|
textFieldNumber.setMaximumSize(new Dimension(155, 25));
|
||||||
|
textFieldNumber.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
buttonRemoveMonorail = new JButton("Remove monorail");
|
||||||
|
buttonRemoveMonorail.setHorizontalTextPosition(JButton.CENTER);
|
||||||
|
buttonRemoveMonorail.setVerticalTextPosition(JButton.CENTER);
|
||||||
|
buttonRemoveMonorail.setFocusable(false);
|
||||||
|
buttonRemoveMonorail.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||||
|
buttonRemoveMonorail.setBackground(Color.WHITE);
|
||||||
|
buttonRemoveMonorail.setMaximumSize(new Dimension(155, 36));
|
||||||
|
buttonRemoveMonorail.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||||
|
buttonRemoveMonorail.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
buttonRemoveMonorail.addActionListener(e -> {
|
||||||
|
if (JOptionPane.showConfirmDialog(null, "Delete the object?", "Deletion",
|
||||||
|
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (textFieldNumber.getText().isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||||
|
if (locomotives.subtraction(pos)) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Object deleted", "Message",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
pictureBoxCollection.repaint();
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(null, "Failed to delete the object", "Message",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buttonRefreshCollection = new JButton("Refresh collection");
|
||||||
|
buttonRefreshCollection.setHorizontalTextPosition(JButton.CENTER);
|
||||||
|
buttonRefreshCollection.setVerticalTextPosition(JButton.CENTER);
|
||||||
|
buttonRefreshCollection.setFocusable(false);
|
||||||
|
buttonRefreshCollection.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||||
|
buttonRefreshCollection.setBackground(Color.WHITE);
|
||||||
|
buttonRefreshCollection.setMaximumSize(new Dimension(155, 36));
|
||||||
|
buttonRefreshCollection.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||||
|
buttonRefreshCollection.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
buttonRefreshCollection.addActionListener(e -> pictureBoxCollection.repaint());
|
||||||
|
|
||||||
|
buttonRandomGeneration = new JButton("Show additional frame");
|
||||||
|
buttonRandomGeneration.setHorizontalTextPosition(JButton.CENTER);
|
||||||
|
buttonRandomGeneration.setVerticalTextPosition(JButton.CENTER);
|
||||||
|
buttonRandomGeneration.setFocusable(false);
|
||||||
|
buttonRandomGeneration.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||||
|
buttonRandomGeneration.setBackground(Color.WHITE);
|
||||||
|
buttonRandomGeneration.setMaximumSize(new Dimension(155, 36));
|
||||||
|
buttonRandomGeneration.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||||
|
buttonRandomGeneration.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
buttonRandomGeneration.addActionListener(e -> new FrameRandomGeneration(this));
|
||||||
|
|
||||||
|
toolsPanel = new JPanel();
|
||||||
|
toolsPanel.setMaximumSize(new Dimension(190, 430));
|
||||||
|
toolsPanel.setLayout(new BoxLayout(toolsPanel, BoxLayout.Y_AXIS));
|
||||||
|
toolsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
|
||||||
|
toolsPanel.add(nameLabel);
|
||||||
|
toolsPanel.add(Box.createVerticalStrut(14));
|
||||||
|
toolsPanel.add(buttonAddMonorail);
|
||||||
|
toolsPanel.add(Box.createVerticalStrut(77));
|
||||||
|
toolsPanel.add(textFieldNumber);
|
||||||
|
toolsPanel.add(Box.createVerticalStrut(6));
|
||||||
|
toolsPanel.add(buttonRemoveMonorail);
|
||||||
|
toolsPanel.add(Box.createVerticalStrut(74));
|
||||||
|
toolsPanel.add(buttonRefreshCollection);
|
||||||
|
toolsPanel.add(Box.createVerticalStrut(40));
|
||||||
|
toolsPanel.add(buttonRandomGeneration);
|
||||||
|
|
||||||
|
mainPanel = new JPanel();
|
||||||
|
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
|
||||||
|
mainPanel.add(Box.createHorizontalStrut(10));
|
||||||
|
mainPanel.add(pictureBoxCollection);
|
||||||
|
mainPanel.add(Box.createHorizontalStrut(15));
|
||||||
|
mainPanel.add(toolsPanel);
|
||||||
|
|
||||||
|
paddingTopPanel = new JPanel();
|
||||||
|
paddingTopPanel.setLayout(new BoxLayout(paddingTopPanel, BoxLayout.Y_AXIS));
|
||||||
|
paddingTopPanel.add(Box.createVerticalStrut(15));
|
||||||
|
paddingTopPanel.add(mainPanel);
|
||||||
|
|
||||||
|
add(paddingTopPanel);
|
||||||
|
|
||||||
|
setPreferredSize(new Dimension(990, 478));
|
||||||
|
pack();
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
20
src/projectMonorail/FrameRandomGeneration.java
Normal file
20
src/projectMonorail/FrameRandomGeneration.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package projectMonorail;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class FrameRandomGeneration extends JDialog {
|
||||||
|
|
||||||
|
private PictureBoxRandomGeneration pictureBox;
|
||||||
|
|
||||||
|
public FrameRandomGeneration(JFrame parent) {
|
||||||
|
super(parent ,"Random generation", true);
|
||||||
|
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
|
|
||||||
|
pictureBox = new PictureBoxRandomGeneration(400, 400);
|
||||||
|
add(pictureBox);
|
||||||
|
|
||||||
|
pack();
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
package projectMonorail.Generics;
|
||||||
|
import projectMonorail.DrawingObjects.*;
|
||||||
|
import projectMonorail.MovementStrategy.*;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class LocomotivesGenericCollection<T extends DrawingLocomotive, U extends IMoveableObject> {
|
||||||
|
private final int pictureWidth;
|
||||||
|
|
||||||
|
private final int pictureHeight;
|
||||||
|
|
||||||
|
private final int placeSizeWidth = 193;
|
||||||
|
|
||||||
|
private final int placeSizeHeight = 102;
|
||||||
|
|
||||||
|
private final SetGeneric<T> collection;
|
||||||
|
|
||||||
|
public LocomotivesGenericCollection(int picWidth, int picHeight) {
|
||||||
|
int width = picWidth / placeSizeWidth;
|
||||||
|
int height = picHeight / placeSizeHeight;
|
||||||
|
pictureWidth = picWidth;
|
||||||
|
pictureHeight = picHeight;
|
||||||
|
collection = new SetGeneric<T>(width * height);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int addition(T obj) {
|
||||||
|
if (obj == null)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return collection.insert(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean subtraction(int pos) {
|
||||||
|
T obj = collection.get(pos);
|
||||||
|
if (obj != null)
|
||||||
|
{
|
||||||
|
return collection.remove(pos);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public U getU(int pos) {
|
||||||
|
if (collection.get(pos) != null)
|
||||||
|
return (U)collection.get(pos).getMoveableObject();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showLocomotives(Graphics2D g2d) {
|
||||||
|
DrawBackground(g2d);
|
||||||
|
DrawObjects(g2d);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawBackground(Graphics2D g2d) {
|
||||||
|
g2d.setColor(Color.BLACK);
|
||||||
|
g2d.setStroke(new BasicStroke(3));
|
||||||
|
for (int i = 0; i < pictureWidth / placeSizeWidth; i++) {
|
||||||
|
for (int j = 0; j < pictureHeight / placeSizeHeight + 1; ++j) {
|
||||||
|
g2d.drawLine(i * placeSizeWidth, j * placeSizeHeight, i * placeSizeWidth
|
||||||
|
+ placeSizeWidth / 2, j * placeSizeHeight);
|
||||||
|
}
|
||||||
|
g2d.drawLine( i * placeSizeWidth, 0, i * placeSizeWidth,
|
||||||
|
pictureHeight / placeSizeHeight * placeSizeHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawObjects(Graphics2D g2d) {
|
||||||
|
T obj;
|
||||||
|
int width = pictureWidth / placeSizeWidth;
|
||||||
|
int height = pictureHeight / placeSizeHeight;
|
||||||
|
int diff = 1, currWidth = 0;
|
||||||
|
for (int i = 0; i < collection.count(); i++) {
|
||||||
|
currWidth++;
|
||||||
|
if (currWidth > width) {
|
||||||
|
diff++;
|
||||||
|
currWidth = 1;
|
||||||
|
}
|
||||||
|
obj = collection.get(i);
|
||||||
|
if (obj != null) {
|
||||||
|
obj.setPosition(i % width * placeSizeWidth + placeSizeWidth / 40,
|
||||||
|
(height - diff) * placeSizeHeight + placeSizeHeight / 15);
|
||||||
|
obj.drawTransport(g2d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
53
src/projectMonorail/Generics/SetGeneric.java
Normal file
53
src/projectMonorail/Generics/SetGeneric.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package projectMonorail.Generics;
|
||||||
|
|
||||||
|
public class SetGeneric<T extends Object> {
|
||||||
|
private final T[] places;
|
||||||
|
|
||||||
|
public int count() {
|
||||||
|
return places.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SetGeneric(int count) {
|
||||||
|
places = (T[])new Object[count];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int insert(T locomotive) {
|
||||||
|
return insert(locomotive, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int insert(T locomotive, int position) {
|
||||||
|
int nullIndex = -1, i;
|
||||||
|
|
||||||
|
if (position < 0 || position >= count())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
for (i = position; i < count(); i++) {
|
||||||
|
if (places[i] == null) {
|
||||||
|
nullIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nullIndex < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
for (i = nullIndex; i > position; i--) {
|
||||||
|
places[i] = places[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
places[position] = locomotive;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean remove(int position) {
|
||||||
|
if (position < 0 || position >= count())
|
||||||
|
return false;
|
||||||
|
places[position] = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get(int position) {
|
||||||
|
if (position < 0 || position >= count())
|
||||||
|
return null;
|
||||||
|
return places[position];
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@ package projectMonorail;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
FrameMonorail frameMonorail = new FrameMonorail();
|
FrameMonorailCollection frameMonorailCollection = new FrameMonorailCollection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,11 +9,22 @@ import java.awt.event.ActionListener;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class PictureBox extends JPanel {
|
public class PictureBox extends JPanel {
|
||||||
|
private boolean dialogResult = false;
|
||||||
|
|
||||||
|
public boolean getDialogResult() {
|
||||||
|
return dialogResult;
|
||||||
|
}
|
||||||
|
|
||||||
private DrawingLocomotive drawingLocomotive;
|
private DrawingLocomotive drawingLocomotive;
|
||||||
|
|
||||||
private AbstractStrategy abstractStrategy;
|
private AbstractStrategy abstractStrategy;
|
||||||
|
|
||||||
|
private DrawingLocomotive selectedLocomotive;
|
||||||
|
|
||||||
|
public DrawingLocomotive getSelectedLocomotive() {
|
||||||
|
return selectedLocomotive;
|
||||||
|
}
|
||||||
|
|
||||||
private JButton buttonLeft;
|
private JButton buttonLeft;
|
||||||
|
|
||||||
private JButton buttonUp;
|
private JButton buttonUp;
|
||||||
@ -26,6 +37,8 @@ public class PictureBox extends JPanel {
|
|||||||
|
|
||||||
private JButton buttonCreateMonorail;
|
private JButton buttonCreateMonorail;
|
||||||
|
|
||||||
|
private JButton buttonSelectLocomotive;
|
||||||
|
|
||||||
private JButton buttonStep;
|
private JButton buttonStep;
|
||||||
|
|
||||||
private JComboBox comboBoxStrategy;
|
private JComboBox comboBoxStrategy;
|
||||||
@ -41,6 +54,9 @@ public class PictureBox extends JPanel {
|
|||||||
private JPanel strategyPanel;
|
private JPanel strategyPanel;
|
||||||
|
|
||||||
public PictureBox() {
|
public PictureBox() {
|
||||||
|
abstractStrategy = null;
|
||||||
|
selectedLocomotive = null;
|
||||||
|
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
buttonsPanel = new JPanel();
|
buttonsPanel = new JPanel();
|
||||||
@ -55,11 +71,14 @@ public class PictureBox extends JPanel {
|
|||||||
|
|
||||||
buttonCreateLocomotive.addActionListener(e -> {
|
buttonCreateLocomotive.addActionListener(e -> {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||||
|
Color chosenColor = JColorChooser.showDialog(this, "Main color", Color.WHITE);
|
||||||
|
if (chosenColor != null) {
|
||||||
|
color = chosenColor;
|
||||||
|
}
|
||||||
drawingLocomotive = new DrawingLocomotive(random.nextInt(200, 300),
|
drawingLocomotive = new DrawingLocomotive(random.nextInt(200, 300),
|
||||||
random.nextInt(1000, 3000),
|
random.nextInt(1000, 3000),
|
||||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
color, this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||||||
random.nextInt(0, 256)),
|
|
||||||
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
|
||||||
drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
@ -72,17 +91,38 @@ public class PictureBox extends JPanel {
|
|||||||
|
|
||||||
buttonCreateMonorail.addActionListener(e -> {
|
buttonCreateMonorail.addActionListener(e -> {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||||
|
Color chosenColor = JColorChooser.showDialog(this, "Main color", Color.WHITE);
|
||||||
|
if (chosenColor != null) {
|
||||||
|
color = chosenColor;
|
||||||
|
}
|
||||||
|
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||||
|
Color chosenAddColor = JColorChooser.showDialog(this, "Additional Color", Color.WHITE);
|
||||||
|
if (chosenAddColor != null) {
|
||||||
|
additionalColor = chosenAddColor;
|
||||||
|
}
|
||||||
drawingLocomotive = new DrawingMonorail(random.nextInt(200, 300),
|
drawingLocomotive = new DrawingMonorail(random.nextInt(200, 300),
|
||||||
random.nextInt(1000, 3000),
|
random.nextInt(1000, 3000),
|
||||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
color, additionalColor, random.nextBoolean(), random.nextBoolean(),
|
||||||
random.nextInt(0, 256)),
|
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
|
||||||
random.nextInt(0, 256)),
|
|
||||||
random.nextBoolean(), random.nextBoolean(), this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
|
||||||
drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
buttonSelectLocomotive = new JButton("Select");
|
||||||
|
buttonSelectLocomotive.setFocusable(false);
|
||||||
|
buttonSelectLocomotive.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||||
|
buttonSelectLocomotive.setBackground(Color.LIGHT_GRAY);
|
||||||
|
buttonSelectLocomotive.setMaximumSize(new Dimension(140, 42));
|
||||||
|
|
||||||
|
buttonSelectLocomotive.addActionListener(e -> {
|
||||||
|
selectedLocomotive = drawingLocomotive;
|
||||||
|
dialogResult = true;
|
||||||
|
JComponent comp = (JComponent)e.getSource();
|
||||||
|
Window win = SwingUtilities.getWindowAncestor(comp);
|
||||||
|
win.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
ActionListener buttonMoveListener = e -> {
|
ActionListener buttonMoveListener = e -> {
|
||||||
if (drawingLocomotive == null) {
|
if (drawingLocomotive == null) {
|
||||||
return;
|
return;
|
||||||
@ -182,7 +222,9 @@ public class PictureBox extends JPanel {
|
|||||||
buttonsPanel.add(buttonCreateMonorail);
|
buttonsPanel.add(buttonCreateMonorail);
|
||||||
buttonsPanel.add(Box.createHorizontalStrut(16));
|
buttonsPanel.add(Box.createHorizontalStrut(16));
|
||||||
buttonsPanel.add(buttonCreateLocomotive);
|
buttonsPanel.add(buttonCreateLocomotive);
|
||||||
buttonsPanel.add(Box.createHorizontalStrut(460));
|
buttonsPanel.add(Box.createHorizontalStrut(16));
|
||||||
|
buttonsPanel.add(buttonSelectLocomotive);
|
||||||
|
buttonsPanel.add(Box.createHorizontalStrut(400));
|
||||||
buttonsPanel.add(movePaddingPanel);
|
buttonsPanel.add(movePaddingPanel);
|
||||||
|
|
||||||
add(buttonsPanel, BorderLayout.SOUTH);
|
add(buttonsPanel, BorderLayout.SOUTH);
|
||||||
|
25
src/projectMonorail/PictureBoxCollection.java
Normal file
25
src/projectMonorail/PictureBoxCollection.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package projectMonorail;
|
||||||
|
|
||||||
|
import projectMonorail.DrawingObjects.*;
|
||||||
|
import projectMonorail.Generics.*;
|
||||||
|
import projectMonorail.MovementStrategy.*;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class PictureBoxCollection extends JPanel {
|
||||||
|
private final LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> locomotives;
|
||||||
|
|
||||||
|
public PictureBoxCollection(LocomotivesGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> locomotives,
|
||||||
|
int width, int height) {
|
||||||
|
this.locomotives = locomotives;
|
||||||
|
setMaximumSize(new Dimension(width, height));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
locomotives.showLocomotives(g2d);
|
||||||
|
}
|
||||||
|
}
|
63
src/projectMonorail/PictureBoxRandomGeneration.java
Normal file
63
src/projectMonorail/PictureBoxRandomGeneration.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package projectMonorail;
|
||||||
|
|
||||||
|
import projectMonorail.DrawingObjects.*;
|
||||||
|
import projectMonorail.Entities.*;
|
||||||
|
import projectMonorail.Extras.*;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class PictureBoxRandomGeneration extends JPanel {
|
||||||
|
private RandomGeneric<EntityLocomotive, IDrawingWheels> randomGeneric;
|
||||||
|
|
||||||
|
private DrawingLocomotive drawingLocomotive;
|
||||||
|
|
||||||
|
private JButton buttonRandomCreation;
|
||||||
|
|
||||||
|
public PictureBoxRandomGeneration(int width, int height) {
|
||||||
|
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||||
|
|
||||||
|
buttonRandomCreation = new JButton("Create monorail");
|
||||||
|
buttonRandomCreation.setFocusable(false);
|
||||||
|
buttonRandomCreation.setMaximumSize(new Dimension(155, 36));
|
||||||
|
buttonRandomCreation.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
||||||
|
buttonRandomCreation.setBackground(Color.WHITE);
|
||||||
|
buttonRandomCreation.setHorizontalTextPosition(JButton.CENTER);
|
||||||
|
buttonRandomCreation.setVerticalTextPosition(JButton.CENTER);
|
||||||
|
buttonRandomCreation.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
|
||||||
|
buttonRandomCreation.addActionListener(e -> {
|
||||||
|
drawingLocomotive = randomGeneric.getDrawingLocomotive();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
add(Box.createVerticalStrut(height - 80));
|
||||||
|
add(buttonRandomCreation);
|
||||||
|
setPreferredSize(new Dimension(width, height));
|
||||||
|
|
||||||
|
randomGeneric = new RandomGeneric<>(7, 3, width, height);
|
||||||
|
randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.RED));
|
||||||
|
randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.GREEN));
|
||||||
|
randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.BLUE));
|
||||||
|
randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.PINK));
|
||||||
|
randomGeneric.addItem(new EntityMonorail(200, 2000, Color.WHITE, Color.BLUE,true, false));
|
||||||
|
randomGeneric.addItem(new EntityMonorail(200, 2000, Color.ORANGE, Color.LIGHT_GRAY,false, true));
|
||||||
|
randomGeneric.addItem(new EntityMonorail(200, 2000, Color.CYAN, Color.MAGENTA,true, true));
|
||||||
|
randomGeneric.addItem(new DrawingNormalWheels());
|
||||||
|
randomGeneric.addItem(new DrawingVintageWheels());
|
||||||
|
randomGeneric.addItem(new DrawingSquarePatternWheels());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
if (drawingLocomotive == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
drawingLocomotive.setPosition(getWidth() * 27 / 100, getHeight() * 3 / 10);
|
||||||
|
drawingLocomotive.drawTransport(g2d);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user