файлы лабораторной
This commit is contained in:
parent
6cb8323d1b
commit
d52af36e71
118
src/frames/FrameLainer.java
Normal file
118
src/frames/FrameLainer.java
Normal file
@ -0,0 +1,118 @@
|
||||
package frames;
|
||||
|
||||
import drawing_objects.DrawingLainer;
|
||||
import drawing_objects.DirectionType;
|
||||
import drawing_objects.DrawingLainer;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
public class FrameLainer extends JFrame {
|
||||
private DrawingLainer drawingBattleship;
|
||||
private JComponent pictureBox;
|
||||
public FrameLainer() throws IOException {
|
||||
super("лайнер");
|
||||
setSize(new Dimension(900,480));
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
pictureBox = new JComponent(){
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (drawingBattleship != null) drawingBattleship.drawTransport(graphics2D);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
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));
|
||||
|
||||
createButton.addActionListener(e -> buttonCreateClick());
|
||||
rightButton.setActionCommand("right");
|
||||
rightButton.addActionListener(this::buttonMoveClick);
|
||||
leftButton.setActionCommand("left");
|
||||
leftButton.addActionListener(this::buttonMoveClick);
|
||||
upButton.setActionCommand("up");
|
||||
upButton.addActionListener(this::buttonMoveClick);
|
||||
downButton.setActionCommand("down");
|
||||
downButton.addActionListener(this::buttonMoveClick);
|
||||
|
||||
JPanel panelBattleship = new JPanel(new BorderLayout());
|
||||
JPanel createPanel = new JPanel(new BorderLayout());
|
||||
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
|
||||
JPanel movementPanel = new JPanel(new GridBagLayout());
|
||||
JPanel rightPanel = new JPanel(new BorderLayout());
|
||||
GridBagConstraints constraints = new GridBagConstraints();
|
||||
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
|
||||
|
||||
createPanel.add(createButton, BorderLayout.SOUTH);
|
||||
|
||||
constraints.gridx = 2;
|
||||
constraints.gridy = 1;
|
||||
movementPanel.add(rightButton, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 1;
|
||||
movementPanel.add(leftButton, constraints);
|
||||
constraints.gridx = 1;
|
||||
constraints.gridy = 0;
|
||||
movementPanel.add(upButton, constraints);
|
||||
constraints.gridx = 1;
|
||||
constraints.gridy = 1;
|
||||
movementPanel.add(downButton, constraints);
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
add(pictureBox);
|
||||
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
||||
panelBattleship.add(rightPanel, BorderLayout.EAST);
|
||||
panelBattleship.add(createPanel, BorderLayout.WEST);
|
||||
add(panelBattleship,BorderLayout.CENTER);
|
||||
setVisible(true);
|
||||
}
|
||||
private void buttonCreateClick() {
|
||||
Random random = new Random();
|
||||
drawingBattleship = new DrawingLainer();
|
||||
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)),
|
||||
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);
|
||||
draw();
|
||||
}
|
||||
private void buttonMoveClick(ActionEvent event) {
|
||||
if(drawingBattleship == null || drawingBattleship.getEntityLainer() == 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;
|
||||
}
|
||||
draw();
|
||||
}
|
||||
private void draw() {
|
||||
if (drawingBattleship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBox.repaint();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user