подготовка к пулу ч.2

This commit is contained in:
Zakharov_Rostislav 2023-10-28 07:45:37 +04:00
parent ee5ebc2b81
commit 3f53243841
2 changed files with 25 additions and 31 deletions

View File

@ -43,25 +43,25 @@ public class DrawingBattleship {
return; return;
switch (direction) { switch (direction) {
//влево //влево
case LEFT: case LEFT -> {
if (startPosX - entityBattleship.step.get().intValue() > 0) if (startPosX - entityBattleship.step.get().intValue() > 0)
startPosX -= entityBattleship.step.get().intValue(); startPosX -= entityBattleship.step.get().intValue();
break; }
//вверх //вверх
case UP: case UP -> {
if (startPosY - entityBattleship.step.get().intValue() > 0) if (startPosY - entityBattleship.step.get().intValue() > 0)
startPosY -= entityBattleship.step.get().intValue(); startPosY -= entityBattleship.step.get().intValue();
break; }
// вправо // вправо
case RIGHT: case RIGHT -> {
if (startPosX + SHIP_WIDTH + entityBattleship.step.get().intValue() < pictureWidth) if (startPosX + SHIP_WIDTH + entityBattleship.step.get().intValue() < pictureWidth)
startPosX += entityBattleship.step.get().intValue(); startPosX += entityBattleship.step.get().intValue();
break; }
//вниз //вниз
case DOWN: case DOWN -> {
if (startPosY + SHIP_HEIGHT + entityBattleship.step.get().intValue() < pictureHeight) if (startPosY + SHIP_HEIGHT + entityBattleship.step.get().intValue() < pictureHeight)
startPosY += entityBattleship.step.get().intValue(); startPosY += entityBattleship.step.get().intValue();
break; }
} }
} }
public void drawTransport(Graphics2D graphics2D) { public void drawTransport(Graphics2D graphics2D) {

View File

@ -30,9 +30,13 @@ public class FrameBattleship extends JFrame {
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
JButton createButton = new JButton("Создать"); JButton createButton = new JButton("Создать");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png")))); JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
rightButton.setPreferredSize(new Dimension(30,30));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png")))); JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
leftButton.setPreferredSize(new Dimension(30,30));
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png")))); JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png"))));
upButton.setPreferredSize(new Dimension(30,30));
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png")))); JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
downButton.setPreferredSize(new Dimension(30,30));
//ActionListeners and ActionCommand addition //ActionListeners and ActionCommand addition
createButton.addActionListener(e -> buttonCreateClick()); createButton.addActionListener(e -> buttonCreateClick());
rightButton.setActionCommand("right"); rightButton.setActionCommand("right");
@ -43,34 +47,33 @@ public class FrameBattleship extends JFrame {
upButton.addActionListener(this::buttonMoveClick); upButton.addActionListener(this::buttonMoveClick);
downButton.setActionCommand("down"); downButton.setActionCommand("down");
downButton.addActionListener(this::buttonMoveClick); downButton.addActionListener(this::buttonMoveClick);
//component addition //panels and constraints initialisation
setLayout(new BorderLayout());
JPanel panelBattleship = new JPanel(new BorderLayout()); JPanel panelBattleship = new JPanel(new BorderLayout());
JPanel createPanel = new JPanel(new BorderLayout()); JPanel createPanel = new JPanel(new BorderLayout());
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
createPanel.add(createButton, BorderLayout.SOUTH);
JPanel movementPanel = new JPanel(new GridBagLayout()); JPanel movementPanel = new JPanel(new GridBagLayout());
JPanel rightPanel = new JPanel(new BorderLayout()); JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(movementPanel, BorderLayout.SOUTH);
rightButton.setPreferredSize(new Dimension(30,30));
GridBagConstraints constraints = new GridBagConstraints(); GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
//addition to createPanel
createPanel.add(createButton, BorderLayout.SOUTH);
//addition to movementPanel
constraints.gridx = 2; constraints.gridx = 2;
constraints.gridy = 1; constraints.gridy = 1;
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
movementPanel.add(rightButton, constraints); movementPanel.add(rightButton, constraints);
leftButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 0; constraints.gridx = 0;
constraints.gridy = 1; constraints.gridy = 1;
movementPanel.add(leftButton, constraints); movementPanel.add(leftButton, constraints);
upButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 1; constraints.gridx = 1;
constraints.gridy = 0; constraints.gridy = 0;
movementPanel.add(upButton, constraints); movementPanel.add(upButton, constraints);
downButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 1; constraints.gridx = 1;
constraints.gridy = 1; constraints.gridy = 1;
movementPanel.add(downButton, constraints); movementPanel.add(downButton, constraints);
//addition to frame
setLayout(new BorderLayout());
add(pictureBox); add(pictureBox);
rightPanel.add(movementPanel, BorderLayout.SOUTH);
panelBattleship.add(rightPanel, BorderLayout.EAST); panelBattleship.add(rightPanel, BorderLayout.EAST);
panelBattleship.add(createPanel, BorderLayout.WEST); panelBattleship.add(createPanel, BorderLayout.WEST);
add(panelBattleship,BorderLayout.CENTER); add(panelBattleship,BorderLayout.CENTER);
@ -88,20 +91,11 @@ public class FrameBattleship extends JFrame {
private void buttonMoveClick(ActionEvent event) { private void buttonMoveClick(ActionEvent event) {
if(drawingBattleship == null || drawingBattleship.getEntityBattleship() == null) if(drawingBattleship == null || drawingBattleship.getEntityBattleship() == null)
return; return;
switch (event.getActionCommand()) switch (event.getActionCommand()) {
{ case "left" -> drawingBattleship.moveTransport(DirectionType.LEFT);
case "left": case "right" -> drawingBattleship.moveTransport(DirectionType.RIGHT);
drawingBattleship.moveTransport(DirectionType.LEFT); case "up" -> drawingBattleship.moveTransport(DirectionType.UP);
break; case "down" -> drawingBattleship.moveTransport(DirectionType.DOWN);
case "right":
drawingBattleship.moveTransport(DirectionType.RIGHT);
break;
case "up":
drawingBattleship.moveTransport(DirectionType.UP);
break;
case "down":
drawingBattleship.moveTransport(DirectionType.DOWN);
break;
} }
draw(); draw();
} }