Lab3 Done+

This commit is contained in:
Artyom_Yashin 2023-11-10 00:11:28 +04:00
parent 385c55967a
commit 7739545975
3 changed files with 0 additions and 290 deletions

View File

@ -1,141 +0,0 @@
import javax.swing.*;
import java.awt.*;
public class DrawingAirBomber extends JPanel {
private EntityAirBomber entityAirBomber;
public EntityAirBomber getEntityAirBomber(){
return entityAirBomber;
}
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int PLANE_WIDTH = 160;
private final int PLANE_HEIGHT = 160;
private DrawingEngines drawingEngines;
public boolean init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean bombs, boolean fuel, int width, int height, int enginesNumber)
{
if (PLANE_WIDTH > width || PLANE_HEIGHT > height)
return false;
_pictureWidth = width;
_pictureHeight = height;
entityAirBomber = new EntityAirBomber();
entityAirBomber.init(speed, weight, bodyColor, additionalColor,
bombs, fuel);
drawingEngines = new DrawingEngines();
drawingEngines.setNumber(enginesNumber);
return true;
}
public void setPosition(int x, int y)
{
if (x < 0 || y < 0 || x + PLANE_WIDTH >= _pictureWidth || y + PLANE_HEIGHT >= _pictureHeight)
x = y = 2;
_startPosX = x;
_startPosY = y;
}
public void moveTransport(DirectionType direction)
{
if (entityAirBomber == null)
return;
switch (direction)
{
//влево
case LEFT:
if (_startPosX - entityAirBomber.step.get().intValue() > 0)
_startPosX -= entityAirBomber.step.get().intValue();
break;
//вверх
case UP:
if (_startPosY - entityAirBomber.step.get().intValue() > 0)
_startPosY -= entityAirBomber.step.get().intValue();
break;
// вправо
case RIGHT:
if (_startPosX + PLANE_WIDTH + entityAirBomber.step.get().intValue() < _pictureWidth)
_startPosX += entityAirBomber.step.get().intValue();
break;
//вниз
case DOWN:
if (_startPosY + PLANE_HEIGHT + entityAirBomber.step.get().intValue() < _pictureHeight)
_startPosY += entityAirBomber.step.get().intValue();
break;
}
}
public void drawTransport(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
if (entityAirBomber == null)
return;
BasicStroke pen = new BasicStroke(2);
Color penColor = Color.BLACK;
Color bodyColor = entityAirBomber.getBodyColor();
Color additionalColor = entityAirBomber.getAdditionalColor();
g.setStroke(pen);
g.setColor(bodyColor);
//фюзеляж
g.fillRect( _startPosX + 20, _startPosY + 70, 140, 20);
//кабина
int[] pointX = new int[]{ _startPosX, _startPosX+20, _startPosX+20};
int[] pointY = new int[]{ _startPosY + 80, _startPosY+70, _startPosY+90};
g.setColor(Color.BLUE);
g.fillPolygon(pointX, pointY, 3);
//границы самолета
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 3);
g.drawRect(_startPosX + 20, _startPosY + 70, 140, 20);
//Крылья
pointX = new int[] {_startPosX+70, _startPosX+70, _startPosX + 90, _startPosX + 100};
pointY = new int[] { _startPosY+70, _startPosY, _startPosY, _startPosY+70};
g.setColor(bodyColor);
g.fillPolygon(pointX, pointY, 4);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 4);
pointX = new int[] {_startPosX+70, _startPosX+70, _startPosX + 90, _startPosX + 100};
pointY = new int[] { _startPosY+90, _startPosY+160, _startPosY+160, _startPosY+90};
g.setColor(bodyColor);
g.fillPolygon(pointX, pointY, 4);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 4);
pointX = new int[] {_startPosX+130, _startPosX+130, _startPosX + 160, _startPosX + 160};
pointY = new int[] { _startPosY+70, _startPosY+50, _startPosY+30, _startPosY+70};
g.setColor(bodyColor);
g.fillPolygon(pointX, pointY, 4);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 4);
pointX = new int[] {_startPosX+130, _startPosX+130, _startPosX + 160, _startPosX + 160};
pointY = new int[] { _startPosY+90, _startPosY+110, _startPosY+130, _startPosY+90};
g.setColor(bodyColor);
g.fillPolygon(pointX, pointY, 4);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 4);
// топливо
if (entityAirBomber.getFuel())
{
g.setColor(additionalColor);
g.fillOval(_startPosX + 60, _startPosY - 1, 40, 10);
g.fillOval(_startPosX + 60, _startPosY + 150, 40, 10);
g.setColor(penColor);
g.drawOval(_startPosX + 60, _startPosY - 1, 40, 10);
g.drawOval(_startPosX + 60, _startPosY + 150, 40, 10);
}
//бомбы
if (entityAirBomber.getBombs())
{
pointX = new int[]{_startPosX+50, _startPosX+70, _startPosX+80, _startPosX+90, _startPosX+90, _startPosX+80, _startPosX+70, _startPosX+50};
pointY = new int[]{_startPosY+75, _startPosY+75, _startPosY+80, _startPosY+75, _startPosY+85, _startPosY+80, _startPosY+85, _startPosY+85};
g.setColor(additionalColor);
g.fillPolygon(pointX, pointY, 8);
g.setColor(penColor);
g.drawPolygon(pointX, pointY, 8);
pointX = new int[]{_startPosX+100, _startPosX+120, _startPosX+130, _startPosX+140, _startPosX+140, _startPosX+130, _startPosX+120, _startPosX+100};
pointY = new int[]{_startPosY+75, _startPosY+75, _startPosY+80, _startPosY+75, _startPosY+85, _startPosY+80, _startPosY+85, _startPosY+85};
g.setColor(additionalColor);
g.fillPolygon(pointX, pointY, 8);
g.setColor(penColor);
g.drawPolygon(pointX, pointY,8);
}
drawingEngines.drawEngines(g, _startPosX, _startPosY);
}
}

View File

@ -1,39 +0,0 @@
import java.awt.*;
import java.util.function.Supplier;
public class EntityAirBomber {
private int speed;
public int getSpeed(){
return speed;
}
private double weight;
public double getWeight(){
return weight;
}
private Color bodyColor;
public Color getBodyColor(){
return bodyColor;
}
private Color additionalColor;
public Color getAdditionalColor(){
return additionalColor;
}
private boolean isFuel;
public boolean getFuel() {
return isFuel;
}
private boolean isBombs;
public boolean getBombs() {
return isBombs;
}
public Supplier<Double> step = () -> (double) speed * 100 / weight;
public void init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean isFuel, boolean isBombs) {
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
this.additionalColor = additionalColor;
this.isFuel = isFuel;
this.isBombs = isBombs;
}
}

View File

@ -1,110 +0,0 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class FrameAirBomber extends JFrame {
private DrawingAirBomber drawingAirBomber;
private final JComponent pictureBox;
public FrameAirBomber() throws IOException {
super("Бомбардировщик");
setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//components initialisation
pictureBox = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingAirBomber != null) drawingAirBomber.drawTransport(graphics2D);
super.repaint();
}
};
JButton createButton = new JButton("Создать");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png"))));
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
//ActionListeners and ActionCommand addition
createButton.addActionListener(e -> buttonCreateClick());
rightButton.setActionCommand("right");
rightButton.addActionListener(this::buttonMoveClick);
leftButton.setActionCommand("left");
leftButton.addActionListener(this::buttonMoveClick);
upButton.setActionCommand("up");
upButton.addActionListener(this::buttonMoveClick);
downButton.setActionCommand("down");
downButton.addActionListener(this::buttonMoveClick);
//component addition
setLayout(new BorderLayout());
JPanel panelBattleship = new JPanel(new BorderLayout());
JPanel createPanel = new JPanel(new BorderLayout());
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
createPanel.add(createButton, BorderLayout.SOUTH);
JPanel movementPanel = new JPanel(new GridBagLayout());
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(movementPanel, BorderLayout.SOUTH);
rightButton.setPreferredSize(new Dimension(30,30));
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 2;
constraints.gridy = 1;
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
movementPanel.add(rightButton, constraints);
leftButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 0;
constraints.gridy = 1;
movementPanel.add(leftButton, constraints);
upButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 1;
constraints.gridy = 0;
movementPanel.add(upButton, constraints);
downButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 1;
constraints.gridy = 1;
movementPanel.add(downButton, constraints);
add(pictureBox);
panelBattleship.add(rightPanel, BorderLayout.EAST);
panelBattleship.add(createPanel, BorderLayout.WEST);
add(panelBattleship,BorderLayout.CENTER);
setVisible(true);
}
private void buttonCreateClick() {
Random random = new Random();
drawingAirBomber = new DrawingAirBomber();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingAirBomber.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(),
(random.nextInt(3)+1)*2);
drawingAirBomber.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonMoveClick(ActionEvent event) {
if(drawingAirBomber == null || drawingAirBomber.getEntityAirBomber() == null)
return;
switch (event.getActionCommand())
{
case "left":
drawingAirBomber.moveTransport(DirectionType.LEFT);
break;
case "right":
drawingAirBomber.moveTransport(DirectionType.RIGHT);
break;
case "up":
drawingAirBomber.moveTransport(DirectionType.UP);
break;
case "down":
drawingAirBomber.moveTransport(DirectionType.DOWN);
break;
}
draw();
}
private void draw() {
if (drawingAirBomber == null)
return;
pictureBox.repaint();
}
}