lab2 second stage

This commit is contained in:
Артём Алейкин 2022-12-13 21:37:45 +04:00
parent 30470e240f
commit a1971e880a
6 changed files with 183 additions and 23 deletions

View File

@ -4,17 +4,15 @@ import java.util.*;
public class DrawningBomber extends JPanel public class DrawningBomber extends JPanel
{ {
private EntityAirBomber AirBomber; public EntityAirBomber AirBomber;
public EntityAirBomber getAirBomber() { return AirBomber; } public EntityAirBomber getAirBomber() { return AirBomber; }
private float _startPosX; public float _startPosX;
private float _startPosY; public float _startPosY;
private int _pictureWidth = 0; private int _pictureWidth = 0;
private int _pictureHeight = 0; private int _pictureHeight = 0;
private final int _airBomberWidth = 100; private int _airBomberWidth = 100;
private final int _airBomberHeight = 100; private int _airBomberHeight = 100;
private int NumEngines = 1;
private DrawEngines drawEngines = new DrawEngines(); private DrawEngines drawEngines = new DrawEngines();
public DrawningBomber(int speed, float weight, Color bodyColor) public DrawningBomber(int speed, float weight, Color bodyColor)
@ -24,6 +22,13 @@ public class DrawningBomber extends JPanel
drawEngines.SetNumEngines(random.nextInt(1, 4)); drawEngines.SetNumEngines(random.nextInt(1, 4));
} }
protected DrawningBomber(int speed, float weight, Color bodyColor, int airBomberWidth, int airBomberHeight)
{
this(speed, weight, bodyColor);
_airBomberWidth = airBomberWidth;
_airBomberHeight = airBomberHeight;
}
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
{ {
if (x + _airBomberWidth > width || x < 0) return; if (x + _airBomberWidth > width || x < 0) return;
@ -69,17 +74,8 @@ public class DrawningBomber extends JPanel
} }
} }
public void DrawTransport()
{
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0)
{
return;
}
repaint();
}
@Override public void DrawTransport(Graphics g)
public void paintComponent(Graphics g)
{ {
if (getAirBomber() == null || _startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0) if (getAirBomber() == null || _startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0)
{ {
@ -167,4 +163,9 @@ public class DrawningBomber extends JPanel
_startPosY = _pictureHeight - _airBomberHeight; _startPosY = _pictureHeight - _airBomberHeight;
} }
} }
public float[] GetCurrentPosition()
{
return new float[]{_startPosX, _startPosX + _airBomberWidth, _startPosY, _startPosY + _airBomberHeight};
}
} }

38
DrawningObjectBomber.java Normal file
View File

@ -0,0 +1,38 @@
import java.awt.*;
public class DrawningObjectBomber implements IDrawningObject
{
private DrawningBomber _airBomber = null;
public DrawningObjectBomber(DrawningBomber airBomber)
{
_airBomber = airBomber;
}
@Override
public float getStep() {
if (_airBomber == null || _airBomber.AirBomber == null) return 0;
return _airBomber.AirBomber.GetStep();
}
@Override
public void SetObject(int x, int y, int width, int height) {
_airBomber.SetPosition(x, y, width, height);
}
@Override
public void MoveObject(Direction direction) {
_airBomber.MoveTransport(direction);
}
@Override
public void DrawningObject(Graphics g) {
_airBomber.DrawTransport(g);
}
@Override
public float[] GetCurrentPosition() {
if (_airBomber == null || _airBomber.AirBomber == null) return null;
return _airBomber.GetCurrentPosition();
}
}

48
DrawningWarplane.java Normal file
View File

@ -0,0 +1,48 @@
import java.awt.*;
public class DrawningWarplane extends DrawningBomber
{
public DrawningWarplane(int speed, float weight, Color bodyColor, Color dopColor, boolean engines, boolean weapons)
{
super(speed, weight, bodyColor, 100, 100);
AirBomber = new EntityWarplane(speed, weight, bodyColor, dopColor, engines, weapons);
}
@Override
public void DrawTransport(Graphics g)
{
if (!(AirBomber instanceof EntityWarplane warplane))
{
return;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.setColor(warplane.GetDopColor());
if (warplane.GetEngines())
{
g2d.fillOval((int)_startPosX + 50, (int)_startPosY + 15, 30, 15);
g2d.fillOval( (int)_startPosX + 45, (int)_startPosY + 35, 35, 15);
g2d.fillOval( (int)_startPosX + 45, (int)_startPosY + 70, 35, 15);
g2d.fillOval( (int)_startPosX + 50, (int)_startPosY + 90, 30, 15);
g2d.drawOval((int)_startPosX + 50, (int)_startPosY + 15, 30, 15);
g2d.drawOval((int)_startPosX + 45, (int)_startPosY + 35, 35, 15);
g2d.drawOval((int)_startPosX + 45, (int)_startPosY + 70, 35, 15);
g2d.drawOval((int)_startPosX + 50, (int)_startPosY + 90, 30, 15);
}
_startPosY += 25;
super.DrawTransport(g);
_startPosY -= 25;
if (warplane.GetWeapons())
{
g2d.fillRect((int)_startPosX + 40,(int) _startPosY, 30, 5);
g2d.fillRect( (int)_startPosX + 40, (int)_startPosY + 115, 30, 5);
g2d.drawRect((int)_startPosX + 40, (int)_startPosY, 30, 5);
g2d.drawRect((int)_startPosX + 40, (int)_startPosY + 115, 30, 5);
}
}
}

22
EntityWarplane.java Normal file
View File

@ -0,0 +1,22 @@
import java.awt.*;
public class EntityWarplane extends EntityAirBomber
{
private Color DopColor;
private boolean Engines;
private boolean Weapons;
public EntityWarplane(int speed, float weight, Color bodyColor, Color dopColor, boolean engines, boolean weapons)
{
super(speed, weight, bodyColor);
DopColor = dopColor;
Engines = engines;
Weapons = weapons;
}
public Color GetDopColor(){return DopColor;}
public boolean GetEngines(){return Engines;}
public boolean GetWeapons(){return Weapons;}
}

View File

@ -2,6 +2,7 @@ import javax.imageio.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.awt.image.*;
import java.util.*; import java.util.*;
public class FormAirBomber { public class FormAirBomber {
@ -15,6 +16,8 @@ public class FormAirBomber {
private JButton buttonLeft; private JButton buttonLeft;
private JButton buttonUp; private JButton buttonUp;
private JButton buttonDown; private JButton buttonDown;
private JPanel GraphicsOutput;
private JButton buttonCreateModif;
private JLabel JLabelSpeed = new JLabel(); private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel(); private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel(); private JLabel JLabelColor = new JLabel();
@ -22,7 +25,27 @@ public class FormAirBomber {
private void Draw() private void Draw()
{ {
if (pictureBomber.getAirBomber() == null) return; if (pictureBomber.getAirBomber() == null) return;
pictureBomber.DrawTransport(); GraphicsOutput.removeAll();
BufferedImage bmp = new BufferedImage(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = bmp.getGraphics();
g.setColor(new Color(238, 238, 238));
g.fillRect(0, 0, GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
pictureBomber.DrawTransport(g);
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(bmp));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
//validate();
}
private void SetData()
{
Random random = new Random();
pictureBomber.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
JLabelSpeed.setText("орость: " + pictureBomber.getAirBomber().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + pictureBomber.getAirBomber().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + pictureBomber.getAirBomber().GetColor() + " "));
} }
private void ButtonMove_Click(String name) private void ButtonMove_Click(String name)
@ -72,10 +95,7 @@ public class FormAirBomber {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
Random random = new Random(); Random random = new Random();
pictureBomber = new DrawningBomber(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); pictureBomber = new DrawningBomber(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
pictureBomber.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBomber.getWidth(), pictureBomber.getHeight()); SetData();
JLabelSpeed.setText("орость: " + pictureBomber.getAirBomber().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + pictureBomber.getAirBomber().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + pictureBomber.getAirBomber().GetColor() + " "));
Draw(); Draw();
} }
}); });
@ -103,13 +123,35 @@ public class FormAirBomber {
ButtonMove_Click("buttonRight"); ButtonMove_Click("buttonRight");
} }
}); });
pictureBomber.addComponentListener(new ComponentAdapter() { /*pictureBomber.addComponentListener(new ComponentAdapter() {
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
super.componentResized(e); super.componentResized(e);
pictureBomber.ChangeBorders(pictureBomber.getWidth(), pictureBomber.getHeight()); pictureBomber.ChangeBorders(pictureBomber.getWidth(), pictureBomber.getHeight());
Draw(); Draw();
} }
});*/
GraphicsOutput.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
if (pictureBomber == null || pictureBomber.getAirBomber() == null) return;
pictureBomber.ChangeBorders(GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
GraphicsOutput.revalidate();
Draw();
}
});
buttonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
pictureBomber = new DrawningWarplane(random.nextInt(100, 300), random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextBoolean(), random.nextBoolean());
SetData();
Draw();
}
}); });
} }
} }

9
IDrawningObject.java Normal file
View File

@ -0,0 +1,9 @@
import java.awt.*;
public interface IDrawningObject {
float getStep();
void SetObject(int x, int y, int width, int height);
void MoveObject(Direction direction);
void DrawningObject(Graphics g);
float[] GetCurrentPosition();
}