Форма

This commit is contained in:
prodigygirl 2022-10-07 19:33:25 +04:00
parent 620e76136b
commit 01959c9850
3 changed files with 229 additions and 1 deletions

105
src/main/java/FormCar.form Normal file
View File

@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormCar">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="617" height="499"/>
</constraints>
<properties>
<background color="-4473925"/>
<foreground color="-4473925"/>
<inheritsPopupMenu value="false"/>
</properties>
<border type="none"/>
<children>
<grid id="cfb06" binding="drawPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="2" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="24" height="376"/>
</grid>
</constraints>
<properties>
<background color="-4473925"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="7c72e" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f0a85" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="12974" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<text value="UP"/>
</properties>
</component>
<component id="c8778" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="DOWN"/>
</properties>
</component>
<component id="43d8e" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="LEFT"/>
<visible value="true"/>
</properties>
</component>
<component id="f4d6e" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="RIGHT"/>
</properties>
</component>
</children>
</grid>
<component id="2cd31" class="javax.swing.JLabel" binding="labelSpeed">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="9ff3" class="javax.swing.JLabel" binding="labelWeight">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="aa5be" class="javax.swing.JLabel" binding="labelColor">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

123
src/main/java/FormCar.java Normal file
View File

@ -0,0 +1,123 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormCar extends JFrame{
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonLeft;
private JPanel mainPanel;
private JPanel drawPanel;
private JButton buttonCreate;
private JLabel labelSpeed;
private JLabel labelWeight;
private JLabel labelColor;
private DrawingArmoredCar armoredCar;
public FormCar() {
super("Бронированная машина");
setBounds(100, 100, 700, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent componentEvent) {
if (armoredCar != null) {
armoredCar.ChangeBorders(drawPanel.getWidth(), drawPanel.getHeight());
repaint();
}
}
});
labelSpeed.setText("Скорость: ");
labelWeight.setText("Вес: ");
labelColor.setText("Цвет: ");
Canvas canvas = new Canvas();
drawPanel.add(canvas);
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Random rnd = new Random();
armoredCar = new DrawingArmoredCar();
armoredCar.Init(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
armoredCar.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90) + 10,
drawPanel.getWidth(), drawPanel.getHeight());
canvas.setArmoredCar(armoredCar);
labelSpeed.setText("Скорость: " + armoredCar.armoredCar.getSpeed());
labelWeight.setText("Вес: " + armoredCar.armoredCar.getWeight());
labelColor.setText("Цвет: " + armoredCar.armoredCar.getWeight());
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (armoredCar != null)
armoredCar.MoveTransport(Direction.Up);
canvas.repaint();
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (armoredCar != null)
armoredCar.MoveTransport(Direction.Down);
canvas.repaint();
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (armoredCar != null)
armoredCar.MoveTransport(Direction.Left);
canvas.repaint();
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (armoredCar != null)
armoredCar.MoveTransport(Direction.Right);
canvas.repaint();
}
});
setContentPane(mainPanel);
setVisible(true);
}
}
class Canvas extends JPanel {
private DrawingArmoredCar armoredCar;
public Canvas () {
super();
}
public void setArmoredCar(DrawingArmoredCar armoredCar) {
this.armoredCar = armoredCar;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (armoredCar != null) {
Graphics2D g2d = (Graphics2D) g;
armoredCar.DrawTransport(g2d);
}
super.repaint();
}
}

View File

@ -3,6 +3,6 @@ import java.awt.*;
public class Program {
public static void main(String[] args) {
new FormCar();
}
}