Lab1 Done
This commit is contained in:
parent
2822d90d4e
commit
dd894ab3ef
1
.idea/.name
Normal file
1
.idea/.name
Normal file
@ -0,0 +1 @@
|
||||
Main.java
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
BIN
images/down.png
Normal file
BIN
images/down.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 967 B |
BIN
images/left.png
Normal file
BIN
images/left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 970 B |
BIN
images/right.png
Normal file
BIN
images/right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 989 B |
BIN
images/up.png
Normal file
BIN
images/up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 957 B |
6
src/DirectionType.java
Normal file
6
src/DirectionType.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum DirectionType {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
141
src/DrawingAirBomber.java
Normal file
141
src/DrawingAirBomber.java
Normal file
@ -0,0 +1,141 @@
|
||||
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 blocksNumber)
|
||||
{
|
||||
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(blocksNumber);
|
||||
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.drawBlocks(g, _startPosX, _startPosY);
|
||||
}
|
||||
}
|
31
src/DrawingEngines.java
Normal file
31
src/DrawingEngines.java
Normal file
@ -0,0 +1,31 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingEngines {
|
||||
private EngineNumber number;
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = EngineNumber.TWO;
|
||||
if(x == 4)
|
||||
number = EngineNumber.FOUR;
|
||||
if(x >= 6)
|
||||
number = EngineNumber.SIX;
|
||||
}
|
||||
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillRect(_startX+70, _startY+20, 20, 15);
|
||||
graphics2D.fillOval(_startX+80, _startY+20, 20, 15);
|
||||
graphics2D.fillRect(_startX+70, _startY+125, 20, 15);
|
||||
graphics2D.fillOval(_startX+80, _startY+125, 20, 15);
|
||||
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
|
||||
graphics2D.fillRect(_startX+70, _startY+40, 20, 15);
|
||||
graphics2D.fillOval(_startX+80, _startY+40, 20, 15);
|
||||
graphics2D.fillRect(_startX+70, _startY+105, 20, 15);
|
||||
graphics2D.fillOval(_startX+80, _startY+105, 20, 15);
|
||||
}
|
||||
if (number == EngineNumber.SIX){
|
||||
graphics2D.fillRect(_startX+130, _startY+50, 25, 15);
|
||||
graphics2D.fillOval(_startX+145, _startY+50, 20, 15);
|
||||
graphics2D.fillRect(_startX+130, _startY+95, 25, 15);
|
||||
graphics2D.fillOval(_startX+145, _startY+95, 20, 15);
|
||||
}
|
||||
}
|
||||
}
|
5
src/EngineNumber.java
Normal file
5
src/EngineNumber.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum EngineNumber {
|
||||
TWO,
|
||||
FOUR,
|
||||
SIX
|
||||
}
|
39
src/EntityAirBomber.java
Normal file
39
src/EntityAirBomber.java
Normal file
@ -0,0 +1,39 @@
|
||||
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;
|
||||
}
|
||||
}
|
107
src/FrameAirBomber.java
Normal file
107
src/FrameAirBomber.java
Normal file
@ -0,0 +1,107 @@
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
public class Main {
|
||||
public static void main(String[] args){
|
||||
import java.io.IOException;
|
||||
|
||||
}
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException { new FrameAirBomber(); }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user