Переход на конструкторы и переделанная форма

This commit is contained in:
ArtemEmelyanov 2022-11-28 23:14:15 +04:00
parent ab78753f83
commit 0487aff030
4 changed files with 60 additions and 52 deletions

View File

@ -26,10 +26,9 @@ public class DrawningPlane extends JPanel {
IlluminatorDraw.SetIlluminatorCount(numIllum); IlluminatorDraw.SetIlluminatorCount(numIllum);
} }
public void Init(int speed, float weight, Color bodyColor) public DrawningPlane(int speed, float weight, Color bodyColor)
{ {
Plane = new EntityPlane(); Plane = new EntityPlane(speed, weight, bodyColor);
Plane.Init(speed, weight, bodyColor);
IlluminatorDraw = new DrawningIlluminator(); IlluminatorDraw = new DrawningIlluminator();
SetIlluminator(); SetIlluminator();
} }
@ -79,20 +78,13 @@ public class DrawningPlane extends JPanel {
} }
} }
public void DrawTransport() { public void DrawTransport(Graphics g) {
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return; return;
} }
repaint();
}
@Override
public void paintComponent(Graphics g) {
if (GetPlane() == null) { if (GetPlane() == null) {
return; return;
} }
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
super.paintComponent(g); super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
@ -122,6 +114,7 @@ public class DrawningPlane extends JPanel {
g2d.fillOval(_startPosX - 5, _startPosY + 25, 30, 10); g2d.fillOval(_startPosX - 5, _startPosY + 25, 30, 10);
IlluminatorDraw.DrawIlluminator(g, _startPosX, _startPosY); IlluminatorDraw.DrawIlluminator(g, _startPosX, _startPosY);
repaint();
} }
public void ChangeBorders(int width, int height) public void ChangeBorders(int width, int height)

View File

@ -18,7 +18,7 @@ public class EntityPlane {
} }
public float Step; public float Step;
public void Init(int speed, float weight, Color bodyColor) public EntityPlane(int speed, float weight, Color bodyColor)
{ {
Random rnd = new Random(); Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50,150) : speed; Speed = speed <= 0 ? rnd.nextInt(50,150) : speed;

View File

@ -3,7 +3,7 @@
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="20" y="20" width="500" height="400"/> <xy x="20" y="20" width="513" height="406"/>
</constraints> </constraints>
<properties> <properties>
<opaque value="true"/> <opaque value="true"/>
@ -48,24 +48,6 @@
<text value=""/> <text value=""/>
</properties> </properties>
</component> </component>
<component id="9f55" class="DrawningPlane" binding="PictureBoxPlane">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="fb937" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<toolbar id="e747d" binding="StatusStrip"> <toolbar id="e747d" binding="StatusStrip">
<constraints> <constraints>
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"> <grid row="3" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
@ -80,11 +62,6 @@
<border type="none"/> <border type="none"/>
<children/> <children/>
</toolbar> </toolbar>
<hspacer id="d6bea">
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="d86ff"> <hspacer id="d86ff">
<constraints> <constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
@ -98,6 +75,26 @@
<text value="Создать"/> <text value="Создать"/>
</properties> </properties>
</component> </component>
<grid id="febd4" binding="PictureBox" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="fb937" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children> </children>
</grid> </grid>
</form> </form>

View File

@ -5,24 +5,36 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.util.Random; import java.util.Random;
public class FormPlane { public class FormPlane extends JFrame{
public JPanel Mainpanel; public JPanel Mainpanel;
private JButton ButtonCreate; private JButton ButtonCreate;
private JButton ButtonLeft; private JButton ButtonLeft;
private JButton ButtonUp; private JButton ButtonUp;
private JButton ButtonDown; private JButton ButtonDown;
private JButton ButtonRight; private JButton ButtonRight;
protected DrawningPlane PictureBoxPlane; protected DrawningPlane _plane;
private JPanel PictureBox;
private JToolBar StatusStrip; private JToolBar StatusStrip;
private final JLabel JLabelSpeed = new JLabel(); private final JLabel JLabelSpeed = new JLabel();
private final JLabel JLabelWeight = new JLabel(); private final JLabel JLabelWeight = new JLabel();
private final JLabel JLabelColor = new JLabel(); private final JLabel JLabelColor = new JLabel();
public void Draw() { public void Draw() {
if (PictureBoxPlane.GetPlane() == null) { PictureBox.removeAll();
return; BufferedImage bmp = new BufferedImage(PictureBox.getWidth(), PictureBox.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
if (_plane != null) {
_plane.DrawTransport(gr);
JLabel imageOfPlane = new JLabel();
imageOfPlane.setPreferredSize(PictureBox.getSize());
imageOfPlane.setMinimumSize(new Dimension(1, 1));
imageOfPlane.setIcon(new ImageIcon(bmp));
PictureBox.add(imageOfPlane,BorderLayout.CENTER);
} }
PictureBoxPlane.DrawTransport(); validate();
} }
public FormPlane() { public FormPlane() {
Box LabelBox = Box.createHorizontalBox(); Box LabelBox = Box.createHorizontalBox();
@ -45,35 +57,41 @@ public class FormPlane {
} }
ButtonCreate.addActionListener(e -> { ButtonCreate.addActionListener(e -> {
Random random = new Random(); Random random = new Random();
PictureBoxPlane.Init(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); _plane = new DrawningPlane(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
PictureBoxPlane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); _plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBox.getWidth(), PictureBox.getHeight());
JLabelSpeed.setText("орость: " + PictureBoxPlane.GetPlane().GetSpeed() + " "); JLabelSpeed.setText("орость: " + _plane.GetPlane().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + PictureBoxPlane.GetPlane().GetWeight() + " "); JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + PictureBoxPlane.GetPlane().GetBodyColor() + " ")); JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " "));
Draw(); Draw();
}); });
PictureBoxPlane.addComponentListener(new ComponentAdapter() { PictureBox.addComponentListener(new ComponentAdapter() {
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
if(_plane == null || _plane.GetPlane() == null) return;
super.componentResized(e); super.componentResized(e);
PictureBoxPlane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); _plane.ChangeBorders(PictureBox.getWidth(), PictureBox.getHeight());
PictureBox.revalidate();
Draw(); Draw();
} }
}); });
ButtonUp.addActionListener(e -> { ButtonUp.addActionListener(e -> {
PictureBoxPlane.MoveTransport(Direction.Up); _plane.MoveTransport(Direction.Up);
PictureBox.revalidate();
Draw(); Draw();
}); });
ButtonDown.addActionListener(e -> { ButtonDown.addActionListener(e -> {
PictureBoxPlane.MoveTransport(Direction.Down); _plane.MoveTransport(Direction.Down);
PictureBox.revalidate();
Draw(); Draw();
}); });
ButtonRight.addActionListener(e -> { ButtonRight.addActionListener(e -> {
PictureBoxPlane.MoveTransport(Direction.Right); _plane.MoveTransport(Direction.Right);
PictureBox.revalidate();
Draw(); Draw();
}); });
ButtonLeft.addActionListener(e -> { ButtonLeft.addActionListener(e -> {
PictureBoxPlane.MoveTransport(Direction.Left); _plane.MoveTransport(Direction.Left);
PictureBox.revalidate();
Draw(); Draw();
}); });
} }