файлы лабораторной

This commit is contained in:
Ivan_Starostin 2023-12-22 20:13:55 +04:00
parent d7792f53c3
commit 59c5a52675

View File

@ -13,7 +13,7 @@ import java.io.IOException;
import java.util.Random; import java.util.Random;
public class FrameLainer extends JFrame { public class FrameLainer extends JFrame {
private DrawingLainer drawingBattleship; private DrawingLainer drawingLainer;
private JComponent pictureBox; private JComponent pictureBox;
public FrameLainer() throws IOException { public FrameLainer() throws IOException {
super("лайнер"); super("лайнер");
@ -24,7 +24,7 @@ public class FrameLainer extends JFrame {
public void paintComponent(Graphics graphics){ public void paintComponent(Graphics graphics){
super.paintComponent(graphics); super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics; Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingBattleship != null) drawingBattleship.drawTransport(graphics2D); if (drawingLainer != null) drawingLainer.drawTransport(graphics2D);
super.repaint(); super.repaint();
} }
}; };
@ -49,7 +49,7 @@ public class FrameLainer extends JFrame {
downButton.setActionCommand("down"); downButton.setActionCommand("down");
downButton.addActionListener(this::buttonMoveClick); downButton.addActionListener(this::buttonMoveClick);
JPanel panelBattleship = new JPanel(new BorderLayout()); JPanel panelLainer = 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));
JPanel movementPanel = new JPanel(new GridBagLayout()); JPanel movementPanel = new JPanel(new GridBagLayout());
@ -75,41 +75,41 @@ public class FrameLainer extends JFrame {
setLayout(new BorderLayout()); setLayout(new BorderLayout());
add(pictureBox); add(pictureBox);
rightPanel.add(movementPanel, BorderLayout.SOUTH); rightPanel.add(movementPanel, BorderLayout.SOUTH);
panelBattleship.add(rightPanel, BorderLayout.EAST); panelLainer.add(rightPanel, BorderLayout.EAST);
panelBattleship.add(createPanel, BorderLayout.WEST); panelLainer.add(createPanel, BorderLayout.WEST);
add(panelBattleship,BorderLayout.CENTER); add(panelLainer,BorderLayout.CENTER);
setVisible(true); setVisible(true);
} }
private void buttonCreateClick() { private void buttonCreateClick() {
Random random = new Random(); Random random = new Random();
drawingBattleship = new DrawingLainer(); drawingLainer = new DrawingLainer();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingBattleship.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), drawingLainer.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(4)); new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(4));
drawingBattleship.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); drawingLainer.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw(); draw();
} }
private void buttonMoveClick(ActionEvent event) { private void buttonMoveClick(ActionEvent event) {
if(drawingBattleship == null || drawingBattleship.getEntityLainer() == null) if(drawingLainer == null || drawingLainer.getEntityLainer() == null)
return; return;
switch (event.getActionCommand()) { switch (event.getActionCommand()) {
case "left" : case "left" :
drawingBattleship.moveTransport(DirectionType.LEFT); drawingLainer.moveTransport(DirectionType.LEFT);
break; break;
case "right" : case "right" :
drawingBattleship.moveTransport(DirectionType.RIGHT); drawingLainer.moveTransport(DirectionType.RIGHT);
break; break;
case "up" : case "up" :
drawingBattleship.moveTransport(DirectionType.UP); drawingLainer.moveTransport(DirectionType.UP);
break; break;
case "down" : case "down" :
drawingBattleship.moveTransport(DirectionType.DOWN); drawingLainer.moveTransport(DirectionType.DOWN);
break; break;
} }
draw(); draw();
} }
private void draw() { private void draw() {
if (drawingBattleship == null) if (drawingLainer == null)
{ {
return; return;
} }