Kharlamov A.A. Lab Work 02 Hard #2

Closed
Kharlamov wants to merge 4 commits from Lab02 into Lab01
Showing only changes of commit 54a450c029 - Show all commits

View File

@ -1,141 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Random;
public class FormStormtrooper extends JComponent {
private DrawingStormtrooper _stormtrooper;
public static void main(String[] args) {
FormStormtrooper formStormtrooper = new FormStormtrooper();
}
public FormStormtrooper() {
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(_stormtrooper != null) _stormtrooper.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 GridBagLayout());
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();
_stormtrooper = new DrawingStormtrooper();
_stormtrooper.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
rnd.nextInt(0, 256)), countBlocks[rnd.nextInt(0, 3)]);
_stormtrooper.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 80);
speedLabel.setText("Скорость: " + _stormtrooper.Stormtrooper.getSpeed());
weightLabel.setText("Вес: " + (int)_stormtrooper.Stormtrooper.getWeight());
colorLabel.setText("Цвет: " + _stormtrooper.Stormtrooper.getBodyColor().getRed() + " " + _stormtrooper.Stormtrooper.getBodyColor().getGreen() + " " + _stormtrooper.Stormtrooper.getBodyColor().getBlue() );
repaint();
});
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
statusPanel.add(createButton,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
statusPanel.add(speedLabel,c);
c.gridx = 2;
c.gridy = 1;
statusPanel.add(weightLabel,c);
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(colorLabel,c);
JButton upButton = new JButton("");
JButton rightButton = new JButton("");
JButton leftButton = new JButton("");
JButton downButton = new JButton("");
upButton.addActionListener(e -> {
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.UP);
repaint();
});
rightButton.addActionListener(e -> {
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.RIGHT);
repaint();
});
leftButton.addActionListener(e -> {
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.LEFT);
repaint();
});
downButton.addActionListener(e -> {
if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.DOWN);
repaint();
});
c.fill = GridBagConstraints.NONE;
c.gridx = 8;
c.gridy = 0;
c.gridwidth = 1;
statusPanel.add(upButton,c);
c.gridx = 7;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(leftButton,c);
c.gridx = 9;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(rightButton,c);
c.gridx = 8;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(downButton,c);
form.getContentPane().add(this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (_stormtrooper != null) _stormtrooper.DrawTransport(g2);
super.repaint();
}
}