лабочка

This commit is contained in:
Extrimal 2023-12-24 00:41:18 +03:00
parent 1a2adf6e6b
commit 2af67567a2
7 changed files with 289 additions and 3 deletions

View File

@ -0,0 +1,6 @@
public enum DirectionType {
UP,
DOWN,
LEFT,
RIGHT
}

View File

@ -0,0 +1,102 @@
import javax.swing.*;
import java.awt.*;
public class DrawingAircraftCarrier extends JPanel {
private EntityAircraftCarrier entityAircraftCarrier;
public EntityAircraftCarrier getEntityAircraftCarrier(){
return entityAircraftCarrier;
}
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int _ShipWidth = 164;
private final int _ShipHeight = 40;
private DrawingImprovements drawingImprovements;
public boolean init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean Cabin, boolean Runway, int width, int height, int improvementsNumber)
{
if (_ShipWidth > width || _ShipHeight > height)
return false;
_pictureWidth = width;
_pictureHeight = height;
entityAircraftCarrier = new EntityAircraftCarrier();
entityAircraftCarrier.init(speed, weight, bodyColor, additionalColor,
Cabin, Runway);
drawingImprovements = new DrawingImprovements();
drawingImprovements.setNumber(improvementsNumber);
return true;
}
public void setPosition(int x, int y)
{
if (_startPosX > (_pictureWidth - _ShipWidth) || _startPosY>(_pictureHeight - _ShipHeight) || x <= 0 || y <= 0)
x = y = 2;
_startPosX = x;
_startPosY = y;
}
public void moveTransport(DirectionType direction)
{
if (entityAircraftCarrier == null)
return;
int step = entityAircraftCarrier.step.get().intValue();
switch (direction)
{
case LEFT:
if (_startPosX - step > 0)
_startPosX -= step;
break;
case UP:
if (_startPosY - step > 0)
_startPosY -= step;
break;
case RIGHT:
if (_startPosX + _ShipWidth + step < _pictureWidth)
_startPosX += step;
break;
case DOWN:
if (_startPosY + _ShipHeight + step < _pictureHeight)
_startPosY += step;
break;
}
}
public void drawTransport(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
if (entityAircraftCarrier == null)
return;
Color penColor = Color.BLACK;
Color bodyColor = entityAircraftCarrier.getBodyColor();
Color additionalColor = entityAircraftCarrier.getAdditionalColor();
g.setColor(penColor);
g.drawLine(_startPosX+4, _startPosY, _startPosX + 124, _startPosY);
g.drawLine(_startPosX+124, _startPosY, _startPosX + 164, _startPosY+20);
g.drawLine(_startPosX + 164, _startPosY+20, _startPosX + 124, _startPosY + 40);
g.drawLine(_startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40);
g.drawLine(_startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40);
g.drawLine(_startPosX+4, _startPosY + 40, _startPosX + 4, _startPosY);
g.setColor(bodyColor);
g.fillRect(_startPosX, _startPosY + 6, 4, 12);
g.fillRect(_startPosX, _startPosY + 24, 4, 12);
g.setColor(penColor);
g.drawOval(_startPosX + 115, _startPosY + 13, 14, 14);
g.drawRect(_startPosX + 63, _startPosY + 13, 27, 14);
g.drawRect(_startPosX + 90, _startPosY + 9, 14, 22);
if(entityAircraftCarrier.getRunway()){
g.setColor(additionalColor);
g.fillRect(_startPosX + 4, _startPosY + 13, 59, 14);
g.setColor(Color.WHITE);
g.drawLine(_startPosX + 62, _startPosY + 19, _startPosX + 52, _startPosY + 19);
g.drawLine(_startPosX + 47, _startPosY + 19, _startPosX + 37, _startPosY + 19);
g.drawLine(_startPosX + 32, _startPosY + 19, _startPosX + 22, _startPosY + 19);
g.drawLine(_startPosX + 17, _startPosY + 19, _startPosX + 7, _startPosY + 19);
}
if (entityAircraftCarrier.getCabin())
{
g.setColor(additionalColor);
g.fillRect(_startPosX + 90, _startPosY, 15, 9);
}
g.setColor(bodyColor);
drawingImprovements.drawEngines(g, _startPosX, _startPosY);
}
}

View File

@ -0,0 +1,25 @@
import java.awt.*;
public class DrawingImprovements {
private NumberImprovement number;
public void setNumber(int x){
if(x <= 2)
number = NumberImprovement.TWO;
if(x == 4)
number = NumberImprovement.FOUR;
if(x >= 6)
number = NumberImprovement.SIX;
}
public void drawEngines(Graphics2D g, int _startPosX, int _startPosY){
g.fillRect(_startPosX+5, _startPosY+1, 15, 12);
g.fillRect(_startPosX+5, _startPosY+27, 15, 13);
if (number == NumberImprovement.FOUR || number == NumberImprovement.SIX){
g.fillRect(_startPosX+26, _startPosY+1, 15, 12);
g.fillRect(_startPosX+26, _startPosY+27, 15, 13);
}
if (number == NumberImprovement.SIX){
g.fillRect(_startPosX+48, _startPosY+1, 15, 12);
g.fillRect(_startPosX+48, _startPosY+27, 15, 13);
}
}
}

View File

@ -0,0 +1,39 @@
import java.awt.*;
import java.util.function.Supplier;
public class EntityAircraftCarrier {
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 Cabin;
public boolean getCabin() {
return Cabin;
}
private boolean Runway;
public boolean getRunway() {
return Runway;
}
public Supplier<Double> step = () -> (double) speed * 100 / weight;
public void init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean Cabin, boolean Runway) {
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
this.additionalColor = additionalColor;
this.Cabin = Cabin;
this.Runway = Runway;
}
}

View File

@ -0,0 +1,108 @@
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 FrameAircraftCarrier extends JFrame {
private DrawingAircraftCarrier drawingAircraftCarrier;
private final JComponent pictureBox;
public FrameAircraftCarrier() throws IOException {
super("Военный корабль");
setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBox = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingAircraftCarrier!= null) drawingAircraftCarrier.drawTransport(graphics2D);
super.repaint();
}
};
JButton createButton = new JButton("Создать");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\right.png"))));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\left.png"))));
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\up.png"))));
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\down.png"))));
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
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 panelAircraftCarrier = 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);
panelAircraftCarrier.add(rightPanel, BorderLayout.EAST);
panelAircraftCarrier.add(createPanel, BorderLayout.WEST);
add(panelAircraftCarrier,BorderLayout.CENTER);
setVisible(true);
}
private void buttonCreateClick() {
Random random = new Random();
drawingAircraftCarrier = new DrawingAircraftCarrier();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingAircraftCarrier.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);
drawingAircraftCarrier.setPosition(random.nextInt(90) + 5, random.nextInt(90) + 5);
draw();
}
private void buttonMoveClick(ActionEvent event) {
if(drawingAircraftCarrier == null || drawingAircraftCarrier.getEntityAircraftCarrier() == null)
return;
switch (event.getActionCommand())
{
case "left":
drawingAircraftCarrier.moveTransport(DirectionType.LEFT);
break;
case "right":
drawingAircraftCarrier.moveTransport(DirectionType.RIGHT);
break;
case "up":
drawingAircraftCarrier.moveTransport(DirectionType.UP);
break;
case "down":
drawingAircraftCarrier.moveTransport(DirectionType.DOWN);
break;
}
draw();
}
private void draw() {
if (drawingAircraftCarrier == null)
return;
pictureBox.repaint();
}
}

View File

@ -1,6 +1,7 @@
import java.io.IOException;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) throws IOException{
System.out.println("Hello, World!"); new FrameAircraftCarrier();
System.out.println("Hello, World!2");
} }
} }

View File

@ -0,0 +1,5 @@
public enum NumberImprovement {
TWO,
FOUR,
SIX
}