This commit is contained in:
Zakharov_Rostislav 2023-12-07 19:56:16 +04:00
parent 234aa190c8
commit 49c7245c3b
8 changed files with 440 additions and 32 deletions

View File

@ -44,4 +44,7 @@ public class DrawingBattleship extends DrawingShip {
graphics2D.drawRect(startPosX + 26, startPosY + 26, 10, 10);
}
}
public void setAdditionalColor(Color color){
((EntityBattleship)entityShip).additionalColor = color;
}
}

View File

@ -7,18 +7,31 @@ import java.awt.*;
public class DrawingShip {
protected EntityShip entityShip;
public EntityShip getEntityShip(){return entityShip;}
public EntityShip getEntityShip(){
return entityShip;
}
private IDrawBlocks drawingBlocks;
private int pictureWidth;
private int pictureHeight;
public void setDrawingBlocks(IDrawBlocks blocks){
drawingBlocks = blocks;
}
public int pictureWidth;
public int pictureHeight;
protected int startPosX;
public int getPosX() {return startPosX;}
public int getPosX() {
return startPosX;
}
protected int startPosY;
public int getPosY() {return startPosY;}
public int getPosY() {
return startPosY;
}
private int shipWidth = 150;
public int getWidth() {return shipWidth;}
public int getShipWidth() {
return shipWidth;
}
private int shipHeight = 50;
public int getHeight() {return shipHeight;}
public int getShipHeight() {
return shipHeight;
}
public IMoveableObject getMoveableObject() {return new DrawingObjectShip(this);}
public DrawingShip(int speed, double weight, Color bodyColor, int width, int height, int blocksType, int blocksNumber) {
if (width < shipWidth || height < shipHeight)
@ -115,4 +128,10 @@ public class DrawingShip {
case DOWN -> startPosY += entityShip.step.get().intValue();
}
}
public void setBlocksNumber(int number){
drawingBlocks.setNumber(number);
}
public void setBodyColor(Color color){
entityShip.bodyColor = color;
}
}

View File

@ -3,7 +3,7 @@ package entities;
import java.awt.*;
public class EntityBattleship extends EntityShip {
private Color additionalColor;
public Color additionalColor;
public Color getAdditionalColor(){
return additionalColor;
}

View File

@ -12,7 +12,7 @@ public class EntityShip{
public double getWeight(){
return weight;
}
private Color bodyColor;
public Color bodyColor;
public Color getBodyColor(){
return bodyColor;
}

View File

@ -0,0 +1,390 @@
package frames;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import drawing_objects.*;
public class FrameShipConfig extends JFrame {
private static 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 record ColorTransferable(Color color) implements Transferable {
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "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 {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private record IDrawBlocksTransferable(IDrawBlocks IDrawBlocksObject) implements Transferable {
private static final DataFlavor IDrawBlocksDataFlavor = new DataFlavor(IDrawBlocks.class, "IDrawBlocks");
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{IDrawBlocksDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return IDrawBlocksDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return IDrawBlocksObject;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private static class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(c.getBackground());
}
}
private static class LabelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
}
}
private static class PanelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
}
}
private static class IDrawBlocksComponent extends JComponent{
public IDrawBlocks obj;
public IDrawBlocksComponent(IDrawBlocks obj){
this.obj = obj;
this.addMouseListener(
new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
((IDrawBlocksComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawBlocksComponent)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 IDrawBlocksTransferable(((IDrawBlocksComponent) c).obj);
}
}
);
}
}
private final JComponent pictureBox = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingShip != null) drawingShip.drawTransport(graphics2D);
super.repaint();
}
};
public final JButton addButton = new JButton("Добавить");
public final JButton cancelButton = new JButton("Отмена");
public DrawingShip drawingShip;
private final int pictureBoxWidth = 218;
private final int pictureBoxHeight = 178;
public FrameShipConfig(){
super("Создание объекта");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGui();
setVisible(true);
}
private void createGui(){
pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
IDrawBlocksComponent blocks = new IDrawBlocksComponent(new DrawingBlocks());
IDrawBlocksComponent roundBlocks = new IDrawBlocksComponent(new DrawingRoundBlocks());
IDrawBlocksComponent crossBlocks = new IDrawBlocksComponent(new DrawingCrossBlocks());
JLabel squareLabel = new JLabel("Квадратные");
squareLabel.setHorizontalAlignment(SwingConstants.CENTER);
squareLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel roundLabel = new JLabel("Круглые");
roundLabel.setHorizontalAlignment(SwingConstants.CENTER);
roundLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel crossLabel = new JLabel("Крестовидные");
crossLabel.setHorizontalAlignment(SwingConstants.CENTER);
crossLabel.setVerticalAlignment(SwingConstants.CENTER);
blocks.setLayout(new GridLayout(1,1));
roundBlocks.setLayout(new GridLayout(1,1));
crossBlocks.setLayout(new GridLayout(1,1));
blocks.add(squareLabel);
roundBlocks.add(roundLabel);
crossBlocks.add(crossLabel);
blocks.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
roundBlocks.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
crossBlocks.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel colorLabel = new JLabel("Цвет");
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel additionalColorLabel = new JLabel("Доп цвет");
additionalColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
additionalColorLabel.setVerticalAlignment(SwingConstants.CENTER);
additionalColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel IDrawBlocksLabel = new JLabel("Блоки");
IDrawBlocksLabel.setHorizontalAlignment(SwingConstants.CENTER);
IDrawBlocksLabel.setVerticalAlignment(SwingConstants.CENTER);
IDrawBlocksLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JCheckBox checkBoxTurret = new JCheckBox("Наличие башни");
JCheckBox checkBoxRocketLauncher = new JCheckBox("Наличие ракет");
JLabel simpleLabel = new JLabel("Простой");
simpleLabel.setTransferHandler(new LabelTransferHandler());
simpleLabel.addMouseListener(new LabelMouseAdapter());
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel advancedLabel = new JLabel("Продвинутый");
advancedLabel.setTransferHandler(new LabelTransferHandler());
advancedLabel.addMouseListener(new LabelMouseAdapter());
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel speedLabel = new JLabel ("Скорость");
JLabel weightLabel = new JLabel ("Вес");
SpinnerNumberModel speedSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
SpinnerNumberModel weightSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
SpinnerNumberModel blocksNumberSpinnerModel = new SpinnerNumberModel(2, 2, 6, 2.0);
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
JSpinner blocksNumberSpinner = new JSpinner(blocksNumberSpinnerModel);
JPanel colorPanel = new JPanel();
JPanel redPanel = new JPanel();
JPanel greenPanel = new JPanel();
JPanel bluePanel = new JPanel();
JPanel yellowPanel = new JPanel();
JPanel whitePanel = new JPanel();
JPanel grayPanel = new JPanel();
JPanel blackPanel = new JPanel();
JPanel purplePanel = new JPanel();
redPanel.setTransferHandler(new PanelTransferHandler());
greenPanel.setTransferHandler(new PanelTransferHandler());
bluePanel.setTransferHandler(new PanelTransferHandler());
yellowPanel.setTransferHandler(new PanelTransferHandler());
whitePanel.setTransferHandler(new PanelTransferHandler());
grayPanel.setTransferHandler(new PanelTransferHandler());
blackPanel.setTransferHandler(new PanelTransferHandler());
purplePanel.setTransferHandler(new PanelTransferHandler());
redPanel.addMouseListener(new PanelMouseAdapter());
greenPanel.addMouseListener(new PanelMouseAdapter());
bluePanel.addMouseListener(new PanelMouseAdapter());
yellowPanel.addMouseListener(new PanelMouseAdapter());
whitePanel.addMouseListener(new PanelMouseAdapter());
grayPanel.addMouseListener(new PanelMouseAdapter());
blackPanel.addMouseListener(new PanelMouseAdapter());
purplePanel.addMouseListener(new PanelMouseAdapter());
redPanel.setName("Красный");
greenPanel.setName("Зелёный");
bluePanel.setName("Синий");
yellowPanel.setName("Жёлтый");
whitePanel.setName("Белый");
grayPanel.setName("Серый");
blackPanel.setName("Чёрный");
purplePanel.setName("Фиолетовый");
pictureBox.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 {
int speed = ((Number)speedSpinner.getValue()).intValue();
int weight = ((Number)weightSpinner.getValue()).intValue();
int blocksNumber = ((Number)blocksNumberSpinner.getValue()).intValue();
switch ((String)support.getTransferable().getTransferData(DataFlavor.stringFlavor)) {
case "Простой" -> {
drawingShip = new DrawingShip(speed, weight, Color.WHITE,
pictureBoxWidth, pictureBoxHeight, 0, blocksNumber);
drawingShip.setBlocksNumber(blocksNumber);
}
case "Продвинутый" -> {
drawingShip = new DrawingBattleship(speed, weight, Color.WHITE, Color.BLACK,
checkBoxTurret.isSelected(), checkBoxRocketLauncher.isSelected(),
pictureBoxWidth, pictureBoxHeight, 0, blocksNumber);
drawingShip.setBlocksNumber(blocksNumber);
}
}
drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getShipWidth() / 2,
pictureBoxHeight / 2 - drawingShip.getShipHeight() / 2);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
IDrawBlocksLabel.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(IDrawBlocksTransferable.IDrawBlocksDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDrawBlocks obj = (IDrawBlocks) support.getTransferable().getTransferData(IDrawBlocksTransferable.IDrawBlocksDataFlavor);
obj.setNumber(((Number)blocksNumberSpinner.getValue()).intValue());
if (drawingShip == null)
return false;
drawingShip.setDrawingBlocks(obj);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
colorLabel.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 (drawingShip == null)
return false;
drawingShip.setBodyColor(color);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
additionalColorLabel.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 (drawingShip == null || !(drawingShip instanceof DrawingBattleship))
return false;
((DrawingBattleship) drawingShip).setAdditionalColor(color);
pictureBox.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
addButton.setBounds(555,250,94,29);
cancelButton.setBounds(679,250,94,29);
pictureBox.setBounds(555, 65, pictureBoxWidth, pictureBoxHeight);
colorLabel.setBounds(555, 20, 70, 33);
additionalColorLabel.setBounds(629, 20, 70, 33);
IDrawBlocksLabel.setBounds(703, 20, 70, 33);
checkBoxTurret.setBounds(6, 132, 159, 24);
checkBoxRocketLauncher.setBounds(6, 162, 145, 24);
simpleLabel.setBounds(171,169, 120, 50);
advancedLabel.setBounds(297,169, 120, 50);
blocks.setBounds(171,229, 120, 50);
roundBlocks.setBounds(297,229, 120, 50);
crossBlocks.setBounds(423,229, 120, 50);
colorPanel.setBounds(171, 23, 372,143);
speedSpinner.setBounds(6, 46, 150, 27);
speedLabel.setBounds(6, 23, 73, 20);
weightSpinner.setBounds(6, 99, 150, 27);
weightLabel.setBounds(6, 76, 33, 20);
blocksNumberSpinner.setBounds(6, 200, 150, 27);
redPanel.setBackground(Color.RED);
greenPanel.setBackground(Color.GREEN);
bluePanel.setBackground(Color.BLUE);
yellowPanel.setBackground(Color.YELLOW);
whitePanel.setBackground(Color.WHITE);
grayPanel.setBackground(Color.GRAY);
blackPanel.setBackground(Color.BLACK);
purplePanel.setBackground(Color.MAGENTA);
colorPanel.setLayout(new GridLayout(2, 4, 26, 10));
colorPanel.add(redPanel);
colorPanel.add(greenPanel);
colorPanel.add(bluePanel);
colorPanel.add(yellowPanel);
colorPanel.add(whitePanel);
colorPanel.add(grayPanel);
colorPanel.add(blackPanel);
colorPanel.add(purplePanel);
add(colorLabel);
add(additionalColorLabel);
add(IDrawBlocksLabel);
setLayout(null);
setSize(818, 350);
add(speedLabel);
add(speedSpinner);
add(weightLabel);
add(weightSpinner);
add(simpleLabel);
add(advancedLabel);
add(checkBoxTurret);
add(checkBoxRocketLauncher);
add(pictureBox);
add(addButton);
add(cancelButton);
add(blocksNumberSpinner);
add(colorPanel);
add(blocks);
add(roundBlocks);
add(crossBlocks);
}
}

View File

@ -8,8 +8,9 @@ import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Objects;
import drawing_objects.DrawingShip;
import generics.ShipsGenericStorage;
import drawing_objects.*;
import generics.*;
import movement_strategy.*;
public class FrameShipsCollection extends JFrame {
private final ShipsGenericStorage storage;
@ -143,29 +144,24 @@ public class FrameShipsCollection extends JFrame {
reloadObjects();
}
private void buttonAddShipClick() {
if (listStorages.getSelectedIndex() == -1)
if(listStorages.getSelectedIndex() == -1) {
return;
var obj = storage.getSet(listStorages.getSelectedValue());
if (obj == null)
return;
FrameBattleship form;
try {
form = new FrameBattleship();
} catch (IOException e) {
throw new RuntimeException(e);
}
form.selectShipButton.addActionListener(e->{
form.select();
DrawingShip ship = form.getSelectedShip();
form.dispose();
if (ship != null && obj.insert(ship)) {
JOptionPane.showMessageDialog(this, "Объект добавлен");
ShipsGenericCollection<DrawingShip, DrawingObjectShip> drawingShips = storage.getSet(listStorages.getSelectedValue());
FrameShipConfig frameShipConfig = new FrameShipConfig();
frameShipConfig.addButton.addActionListener(e -> {
if (drawingShips.insert(frameShipConfig.drawingShip)) {
frameShipConfig.dispose();
frameShipConfig.drawingShip.pictureWidth = pictureBoxCollection.getWidth();
frameShipConfig.drawingShip.pictureHeight = pictureBoxCollection.getHeight();
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
pictureBoxCollection.repaint();
}
else {
JOptionPane.showMessageDialog(this, "Не удалось добавить объект");
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
});
frameShipConfig.cancelButton.addActionListener(e -> frameShipConfig.dispose());
}
private void buttonRemoveShipClick(){
if (listStorages.getSelectedIndex() == -1 || Objects.equals(textFieldNumber.getText(), "") || textFieldNumber.getText() == null)
@ -190,12 +186,12 @@ public class FrameShipsCollection extends JFrame {
private void buttonTrashClick(){
if (trashCollection.size() == 0)
return;
FrameBattleship form;
FrameBattleship Frame;
try {
form = new FrameBattleship();
Frame = new FrameBattleship();
} catch (IOException e) {
throw new RuntimeException(e);
}
form.setShip(trashCollection.pop());
Frame.setShip(trashCollection.pop());
}
}

View File

@ -37,7 +37,7 @@ public class HardFrame extends JFrame {
}while(check);
buttonMakeObject.addActionListener(e -> {
DrawingShip drawingShip = generic.makeObject();
drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getWidth()/2, pictureBoxHeight / 2 - drawingShip.getHeight()/2);
drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getShipWidth()/2, pictureBoxHeight / 2 - drawingShip.getShipHeight()/2);
drawing = drawingShip;
draw();
});

View File

@ -13,7 +13,7 @@ public class DrawingObjectShip implements IMoveableObject{
if(drawingShip == null || drawingShip.getEntityShip() == null)
return null;
return new ObjectParameters(drawingShip.getPosX(), drawingShip.getPosY(),
drawingShip.getWidth(), drawingShip.getHeight());
drawingShip.getShipWidth(), drawingShip.getShipHeight());
}
@Override
public int getStep(){