Pibd-23_Zhelovanov_D.Y._Bat.../FormBattleship.java

130 lines
5.2 KiB
Java
Raw Normal View History

2022-10-24 20:59:27 +03:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2022-10-24 20:59:27 +03:00
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Random;
public class FormBattleship extends JComponent {
private DrawningBattleship _battleship;
private EntityBattleship _entityBattleship;
public static void main(String[] args) {
2022-12-06 16:23:28 +03:00
FormMap formMap = new FormMap();
2022-10-24 20:59:27 +03:00
}
public FormBattleship() {
JFrame form = new JFrame("Военный корабль");
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setSize(800, 500);
form.setVisible(true);
form.setLocationRelativeTo(null);
form.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
if(_battleship != null) _battleship.ChangeBorders(getWidth(), getHeight());
repaint();
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
});
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new FlowLayout());
setLayout(new BorderLayout());
form.add(statusPanel, BorderLayout.SOUTH);
Label speedLabel = new Label("Скорость: ");
Label weightLabel = new Label("Вес: ");
Label colorLabel = new Label("Цвет: ");
JButton createButton = new JButton("Создать");
createButton.addActionListener(e -> {
int[] countBlocks = {2, 4, 6};
Random rnd = new Random();
2022-12-06 16:23:28 +03:00
_battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
2022-12-06 16:23:28 +03:00
rnd.nextInt(0, 256)));
_battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 75);
speedLabel.setText("Скорость: " + _battleship.Battleship.GetSpeed());
weightLabel.setText("Вес: " + (int)_battleship.Battleship.GetWeight());
colorLabel.setText("Цвет: " + _battleship.Battleship.GetBodyColor().getRed() + " " +
_battleship.Battleship.GetBodyColor().getGreen() + " " + _battleship.Battleship.GetBodyColor().getBlue() );
repaint();
});
2022-12-06 16:23:28 +03:00
JButton modifiedButton = new JButton("Модификация");
modifiedButton.addActionListener(e -> {
Random rnd = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
_battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
colorFirst,
colorSecond,
rnd.nextBoolean(),
rnd.nextBoolean());
_battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75);
// speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed());
// weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight());
// colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() );
repaint();
});
statusPanel.add(createButton);
2022-12-06 16:23:28 +03:00
statusPanel.add(modifiedButton);
statusPanel.add(speedLabel);
statusPanel.add(weightLabel);
statusPanel.add(colorLabel);
JButton upButton = new JButton("");
JButton rightButton = new JButton("");
JButton leftButton = new JButton("");
JButton downButton = new JButton("");
upButton.addActionListener(e -> {
if (_battleship != null) _battleship.MoveTransport(Direction.Up);
repaint();
});
rightButton.addActionListener(e -> {
if (_battleship != null) _battleship.MoveTransport(Direction.Right);
repaint();
});
leftButton.addActionListener(e -> {
if (_battleship != null) _battleship.MoveTransport(Direction.Left);
repaint();
});
downButton.addActionListener(e -> {
if (_battleship != null) _battleship.MoveTransport(Direction.Down);
repaint();
});
statusPanel.add(leftButton);
statusPanel.add(upButton);
statusPanel.add(rightButton);
statusPanel.add(downButton);
form.getContentPane().add(this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (_battleship != null) _battleship.DrawTransport(g2);
super.repaint();
2022-10-24 18:25:26 +03:00
}
2022-10-24 18:25:26 +03:00
}