PIbd-23_Nasyrov_A_G_Lab1_hard #1

Closed
gaillard wants to merge 2 commits from lab1_hard into main
6 changed files with 373 additions and 0 deletions

6
src/DirectionType.java Normal file
View File

@ -0,0 +1,6 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,171 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningAirplaneWithRadar {
JPanel AirplaneWithRadarPanel;
private EntityAirplaneWithRadar EntityAirplaneWithRadar;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX = 0;
private int _startPosY = 0;
private int _airplaneWidth = 200;
private int _airplaneHeight = 78;
private DrawningIlluminators DrawningIlluminators;
public EntityAirplaneWithRadar EntityAirplaneWithRadar(){
return EntityAirplaneWithRadar;
}
public boolean Init(int speed, double weight, Color bodyColor, Color illuminatorColor, Color additionalColor, int illuminatorNumb,
int width, int height, boolean radar, boolean dopBak, JPanel airplaneWithRadarPanel){
if(width <= _airplaneWidth || height <= _airplaneHeight)
return false;
_startPosY=0;
_startPosX = 0;
airplaneWithRadarPanel.setSize(width, height);
AirplaneWithRadarPanel = airplaneWithRadarPanel;
airplaneWithRadarPanel.paint(AirplaneWithRadarPanel.getGraphics());
_pictureWidth = width;
_pictureHeight = height;
EntityAirplaneWithRadar = new EntityAirplaneWithRadar();
EntityAirplaneWithRadar.Init(speed, weight, bodyColor, illuminatorColor, additionalColor, illuminatorNumb, radar, dopBak);
DrawningIlluminators = new DrawningIlluminators();
DrawningIlluminators.Init(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY,illuminatorColor,airplaneWithRadarPanel);
Random rand = new Random();
DrawningIlluminators.ChangeIlluminatorNumb(rand.nextInt(1, 5));
return true;
}
public void SetPosition(int x, int y){
if (x < 0 || y < 0 || x + _airplaneWidth >= _pictureWidth || y + _airplaneHeight >= _pictureHeight)
{
x = y = 10;
_startPosX = x;
_startPosY = y;
}
_startPosX = x;
_startPosY = y;
DrawningIlluminators.CurX = _startPosX;
DrawningIlluminators.CurY = _startPosY;
}
public void MoveTransport(DirectionType direction){
if (EntityAirplaneWithRadar == null)
return;
AirplaneWithRadarPanel.paint(AirplaneWithRadarPanel.getGraphics());
switch (direction)
{
case Left:
if (_startPosX - EntityAirplaneWithRadar.Step() >= 0)
_startPosX -= (int)EntityAirplaneWithRadar.Step();
else
_startPosX = 0;
break;
case Up:
if (_startPosY - EntityAirplaneWithRadar.Step() >= 0)
_startPosY -= (int)EntityAirplaneWithRadar.Step();
else
_startPosY = 0;
break;
case Right:
if (_startPosX + EntityAirplaneWithRadar.Step() + _airplaneWidth < _pictureWidth)
_startPosX += (int)EntityAirplaneWithRadar.Step();
else
_startPosX = _pictureWidth - _airplaneWidth;
break;
case Down:
if (_startPosY + EntityAirplaneWithRadar.Step() + _airplaneHeight < _pictureHeight)
_startPosY += (int)EntityAirplaneWithRadar.Step();
else
_startPosY = _pictureHeight - _airplaneHeight;
break;
}
DrawningIlluminators.CurX = _startPosX;
DrawningIlluminators.CurY = _startPosY;
}
public void DrawAirplaneWithRadar(){
Graphics2D g2d = (Graphics2D)AirplaneWithRadarPanel.getGraphics();
if (EntityAirplaneWithRadar == null)
return;
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(3));
// корпус
g2d.setColor(EntityAirplaneWithRadar.BodyColor());
g2d.fillOval(_startPosX, _startPosY + 25, 180, 30);
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX, _startPosY + 25, 180, 30);
// крыло
g2d.setColor(Color.BLACK);
g2d.fillOval(_startPosX + 70, _startPosY + 35, 80, 10);
// стекла
g2d.setColor(Color.BLUE);
int[] curvePointsX =
{
_startPosX + 170,
_startPosX + 200,
_startPosX + 170,
};
int[] curvePointsY =
{
_startPosY + 30,
_startPosY + 40,
_startPosY + 50,
};
g2d.fillPolygon(curvePointsX,curvePointsY,curvePointsY.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(curvePointsX,curvePointsY,curvePointsY.length);
g2d.drawLine(_startPosX + 170, _startPosY + 40, _startPosX + 200, _startPosY + 40);
// хвост
g2d.setColor(Color.BLUE);
int[] curvePoints2x =
{
_startPosX,
_startPosX,
_startPosX + 30,
};
int[] curvePoints2y =
{
_startPosY + 35,
_startPosY + 5,
_startPosY + 35,
};
g2d.fillPolygon(curvePoints2x,curvePoints2y, curvePoints2x.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(curvePoints2x,curvePoints2y, curvePoints2x.length);
// шасси
g2d.setColor(Color.BLACK);
g2d.drawLine(_startPosX + 50, _startPosY + 55, _startPosX + 50, _startPosY + 70);
g2d.drawLine(_startPosX + 150, _startPosY + 51, _startPosX + 150, _startPosY + 70);
g2d.fillOval(_startPosX + 40, _startPosY + 65, 10, 10);
g2d.fillOval(_startPosX + 50, _startPosY + 65, 10, 10);
g2d.fillOval(_startPosX + 145, _startPosY + 65, 10, 10);
//дополнительный бак
if (EntityAirplaneWithRadar().DopBak()) {
g2d.setColor(EntityAirplaneWithRadar.AdditionalColor());
g2d.fillOval( _startPosX, _startPosY + 45, 40, 20);
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX, _startPosY + 45, 40, 20);
}
//радар
if (EntityAirplaneWithRadar.Radar())
{
g2d.setColor(Color.BLACK);
g2d.drawLine(_startPosX + 60, _startPosY + 25, _startPosX + 60, _startPosY + 15);
g2d.drawLine( _startPosX + 60, _startPosY + 15, _startPosX + 67, _startPosY + 11);
g2d.setColor(EntityAirplaneWithRadar.AdditionalColor());
Point point7 = new Point(_startPosX + 60, _startPosY + 15);
Point point8 = new Point(_startPosX + 60, _startPosY + 5);
Point point9 = new Point(_startPosX + 70, _startPosY + 25);
int[] curvePoints3x =
{
_startPosX + 60,
_startPosX + 60,
_startPosX + 70,
};
int[] curvePoints3y =
{
_startPosY + 15,
_startPosY + 5,
_startPosY + 25,
};
g2d.fillPolygon(curvePoints3x, curvePoints3y,curvePoints3x.length);
}
DrawningIlluminators.DrawIlluminators();
}
}

View File

@ -0,0 +1,57 @@
import javax.swing.*;
import java.awt.*;
public class DrawningIlluminators {
JPanel AirplaneWithRadarPanel;
private NumberType IlluminatorNumb;
private Color IlluminatorColor;
private int Width, Height;
public int CurX, CurY;
boolean Init(int width, int height, int curX, int curY, Color illuminatorColor, JPanel airplaneWithRadarPanel){
Width = width;
Height = height;
CurX = curX;
CurY = curY;
IlluminatorColor = illuminatorColor;
AirplaneWithRadarPanel = airplaneWithRadarPanel;
return true;
}
public void ChangeIlluminatorNumb(int x){
if(x <= 2)
IlluminatorNumb = NumberType.Ten;
if(x == 3)
IlluminatorNumb = NumberType.Twenty;
if(x >= 4)
IlluminatorNumb = NumberType.Thirty;
}
public NumberType IlluminatorNumb(){
return IlluminatorNumb;
}
public void DrawIlluminators(){
Graphics2D g2d = (Graphics2D)AirplaneWithRadarPanel.getGraphics();
g2d.setColor(IlluminatorColor);
int x = CurX;
int y = CurY;
for( int i =0; i<10;i++){
g2d.fillOval( x+34 , y+29 , 5, 5);
x+=7;
}
//20 иллюминаторов
if (IlluminatorNumb == NumberType.Twenty || IlluminatorNumb == NumberType.Thirty)
{
x = CurX;
for( int i =0; i<10;i++){
g2d.fillOval( x+34 , y+37 , 5, 5);
x+=7;
}
}
//30 иллюминаторов
if (IlluminatorNumb == NumberType.Thirty)
{
x = CurX;
for( int i =0; i<10;i++){
g2d.fillOval( x+34 , y+45 , 5, 5);
x+=7;
}
}
}
}

View File

@ -0,0 +1,43 @@
import java.awt.*;
public class EntityAirplaneWithRadar {
private int Speed, IlluminatorNumb;
private double Weight, Step;
private Color BodyColor, AdditionalColor, IlluminatorColor;
private boolean Radar;
private boolean DopBak;
public int Speed(){
return Speed;
}
public int IlluminatorNumb(){
return IlluminatorNumb;
}
public double Weight(){
return Weight;
}
public double Step(){
return Step;
}
public Color BodyColor(){
return BodyColor;
}
public Color AdditionalColor(){
return AdditionalColor;
}
public Color IlluminatorColor(){
return IlluminatorColor;
}
public boolean Radar(){return Radar;}
public boolean DopBak(){return DopBak;}
public void Init(int speed, double weight, Color bodyColor, Color illuminatorColor, Color additionalColor, int illuminatorNumb,
boolean radar, boolean dopBak ){
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
IlluminatorColor=illuminatorColor;
AdditionalColor = additionalColor;
IlluminatorNumb = illuminatorNumb;
Radar = radar;
DopBak = dopBak;
}
}

91
src/Main.java Normal file
View File

@ -0,0 +1,91 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame AirplaneWithRadarFrame = new JFrame();
JPanel AirplaneWithRadarPanel = new JPanel();
AirplaneWithRadarFrame.setLayout(new BorderLayout());
AirplaneWithRadarFrame.setSize(900, 500);
AirplaneWithRadarFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AirplaneWithRadarFrame.setLayout(new BorderLayout(1,1));
AirplaneWithRadarFrame.setResizable(false);
DrawningAirplaneWithRadar DrawningEntityAirplaneWithRadar = new DrawningAirplaneWithRadar();
AirplaneWithRadarPanel.setLayout(null);
BufferedImage RightIcon = ImageIO.read(new File("RightButton.png"));
BufferedImage LeftIcon = ImageIO.read(new File("LeftButton.png"));
BufferedImage UpIcon = ImageIO.read(new File("UpButton.png"));
BufferedImage DownIcon = ImageIO.read(new File("DownButton.png"));
JButton RightButton = new JButton(new ImageIcon(RightIcon));
JButton LeftButton = new JButton(new ImageIcon(LeftIcon));
JButton UpButton = new JButton(new ImageIcon(UpIcon));
JButton DownButton = new JButton(new ImageIcon(DownIcon));
JButton CreateButton = new JButton();
CreateButton.setText("Создать");
CreateButton.setBounds(12, 401, 90, 40);
RightButton.setBounds(840,411,30,30);
LeftButton.setBounds(768,411,30,30);
UpButton.setBounds(804,375,30,30);
DownButton.setBounds(804,411,30,30);
AirplaneWithRadarPanel.add(CreateButton);
AirplaneWithRadarPanel.add(RightButton);
AirplaneWithRadarPanel.add(LeftButton);
AirplaneWithRadarPanel.add(UpButton);
AirplaneWithRadarPanel.add(DownButton);
AirplaneWithRadarFrame.add(AirplaneWithRadarPanel, BorderLayout.CENTER);
Random random = new Random();
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningEntityAirplaneWithRadar.Init(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
random.nextInt(1, 4)*10,
AirplaneWithRadarPanel.getWidth(), AirplaneWithRadarPanel.getHeight(), random.nextBoolean(), random.nextBoolean(), AirplaneWithRadarPanel);
DrawningEntityAirplaneWithRadar.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningEntityAirplaneWithRadar.EntityAirplaneWithRadar() == null)
return;
DrawningEntityAirplaneWithRadar.MoveTransport(DirectionType.Right);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningEntityAirplaneWithRadar.EntityAirplaneWithRadar() == null)
return;
DrawningEntityAirplaneWithRadar.MoveTransport(DirectionType.Left);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningEntityAirplaneWithRadar.EntityAirplaneWithRadar() == null)
return;
DrawningEntityAirplaneWithRadar.MoveTransport(DirectionType.Up);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningEntityAirplaneWithRadar.EntityAirplaneWithRadar() == null)
return;
DrawningEntityAirplaneWithRadar.MoveTransport(DirectionType.Down);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
AirplaneWithRadarFrame.setVisible(true);
}
}

5
src/NumberType.java Normal file
View File

@ -0,0 +1,5 @@
public enum NumberType {
Ten,
Twenty,
Thirty
}