подготовка к пулу ч.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;
switch (direction) {
//влево
case LEFT:
case LEFT -> {
if (startPosX - entityBattleship.step.get().intValue() > 0)
startPosX -= entityBattleship.step.get().intValue();
break;
}
//вверх
case UP:
case UP -> {
if (startPosY - entityBattleship.step.get().intValue() > 0)
startPosY -= entityBattleship.step.get().intValue();
break;
}
// вправо
case RIGHT:
case RIGHT -> {
if (startPosX + SHIP_WIDTH + entityBattleship.step.get().intValue() < pictureWidth)
startPosX += entityBattleship.step.get().intValue();
break;
}
//вниз
case DOWN:
case DOWN -> {
if (startPosY + SHIP_HEIGHT + entityBattleship.step.get().intValue() < pictureHeight)
startPosY += entityBattleship.step.get().intValue();
break;
}
}
}
public void drawTransport(Graphics2D graphics2D) {

View File

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