Compare commits

..

1 Commits

Author SHA1 Message Date
GokaPek
f654883233 Лаба 1 2023-10-25 14:10:54 +04:00
11 changed files with 466 additions and 0 deletions

BIN
Arrows/down-arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 281 B

BIN
Arrows/left-arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 265 B

BIN
Arrows/right-arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 B

BIN
Arrows/upper-arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 B

13
DirectionType.java Normal file
View File

@ -0,0 +1,13 @@
public enum DirectionType
{
Up(1),
Down(2),
Left(3),
Right(4);
private final int DirectionTypeCode;
private DirectionType(int DirectionTypeCode){
this.DirectionTypeCode = DirectionTypeCode;
}
}

65
DrawingRollers.java Normal file
View File

@ -0,0 +1,65 @@
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class DrawingRollers {
Graphics2D g2d;
int startPosX;
int startPosY;
Color color;
private NumbeRollers _numbeRollers;
public void setNumRollers(int numRollers) {
if (numRollers < 4 || numRollers > 6 ){
_numbeRollers = NumbeRollers.fromNumberToEnum(numRollers);
return;
}
_numbeRollers = NumbeRollers.fromNumberToEnum(numRollers);
}
public DrawingRollers(Graphics2D g2d, int startPosX, int startPosY, Color color, int numRollers){
this.color = color;
this.g2d = g2d;
this.startPosX = startPosX;
this.startPosY = startPosY;
setNumRollers(numRollers);
}
public void Draw(){
g2d.setPaint(Color.BLACK);
Ellipse2D ellipse7 = new Ellipse2D.Double(startPosX + 10, startPosY + 55, 120, 20);
Ellipse2D ellipse1 = new Ellipse2D.Double(startPosX + 5, startPosY + 50, 20, 20);
Ellipse2D ellipse2 = new Ellipse2D.Double(startPosX + 30, startPosY + 50, 20, 20);
Ellipse2D ellipse3 = new Ellipse2D.Double(startPosX + 55, startPosY + 50, 20, 20);
Ellipse2D ellipse4 = new Ellipse2D.Double(startPosX + 80, startPosY + 50, 20, 20);
Ellipse2D ellipse5 = new Ellipse2D.Double(startPosX + 105, startPosY + 50, 20, 20);
Ellipse2D ellipse6 = new Ellipse2D.Double(startPosX + 125, startPosY + 60, 10, 10);
g2d.setPaint(color);
g2d.draw(ellipse7);
switch (_numbeRollers) {
case Min:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
break;
case Mid:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
g2d.fill(ellipse5);
break;
case Max:
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
g2d.fill(ellipse5);
g2d.fill(ellipse6);
break;
}
}
}

161
DrawingSPAU.java Normal file
View File

@ -0,0 +1,161 @@
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
public class DrawingSPAU {
private EntitySPAU EntitySPAU;
private int pictureWidth;
private int pictureHeight;
private int startPosX;
private int startPosY;
private int _numbeRollers;
private final int carWidth = 135;
private final int carHeight = 75;
public void setEntitySPAU(EntitySPAU entitySPAU) {
this.EntitySPAU = entitySPAU;
}
public boolean Init(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height)
{
this._numbeRollers = _numbeRollers;
this.pictureWidth = width;
this.pictureHeight = height;
if (this.carHeight >= height)
{
return false;
}
if (this.carWidth >= width)
{
return false;
}
EntitySPAU = new EntitySPAU();
EntitySPAU.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
return true;
}
public void SetPosition(int x, int y)
{
if (x < 0 || x > this.pictureWidth - this.carWidth)
{
x = 0;
}
if (y < 0 || y > this.pictureHeight - this.carHeight)
{
y = 0;
}
this.startPosX = x;
this.startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (EntitySPAU == null)
{
return;
}
switch (direction)
{
//влево
case Left:
if (this.startPosX - EntitySPAU.Step > 0)
{
this.startPosX -= (int)EntitySPAU.Step;
}
break;
//вверх
case Up:
if (this.startPosY - EntitySPAU.Step > 0)
{
this.startPosY -= (int)EntitySPAU.Step;
}
break;
// вправо
case Right:
if (this.startPosX + EntitySPAU.Step < this.pictureWidth - this.carWidth)
{
this.startPosX += (int)EntitySPAU.Step;
}
break;
//вниз
case Down:
if (this.startPosY + EntitySPAU.Step < this.pictureHeight - this.carHeight)
{
this.startPosY += (int)EntitySPAU.Step;
}
break;
}
}
public void DrawTransport(Graphics g) {
if (EntitySPAU == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
BasicStroke penBlack = new BasicStroke(1);
Color additionalColor = EntitySPAU.AdditionalColor;
Color bodyColor = EntitySPAU.BodyColor;
boolean hasBodyKit = EntitySPAU.BodyKit;
Path2D.Double path = new Path2D.Double();
// Обвесы
if (hasBodyKit) {
path.moveTo(startPosX + 15, startPosY + 20);
path.lineTo(startPosX + 35, startPosY + 20);
path.lineTo(startPosX + 35, startPosY + 60);
path.lineTo(startPosX + 15, startPosY + 60);
path.closePath();
g2d.setPaint(additionalColor);
g2d.fill(path);
g2d.setPaint(Color.BLACK);
g2d.setStroke(penBlack);
g2d.draw(path);
path.reset();
Line2D line = new Line2D.Double(startPosX + 5, startPosY + 20, startPosX + 15, startPosY + 25);
g2d.draw(line);
}
//гусеницы
DrawingRollers Rollers = new DrawingRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers);
Rollers.Draw();
// пушка
path.moveTo(startPosX + 35, startPosY + 40);
path.lineTo(startPosX + 40, startPosY + 45);
path.lineTo(startPosX + 135, startPosY + 5);
path.lineTo(startPosX + 130, startPosY + 0);
path.closePath();
g2d.setPaint(bodyColor);
g2d.fill(path);
g2d.draw(path);
path.reset();
// корпус
path.moveTo(startPosX, startPosY + 60);
path.lineTo(startPosX + 10, startPosY + 30);
path.lineTo(startPosX + 130, startPosY + 30);
path.lineTo(startPosX + 135, startPosY + 60);
path.closePath();
g2d.setPaint(bodyColor);
g2d.fill(path);
g2d.draw(path);
path.reset();
// башня
path.moveTo(startPosX + 40, startPosY + 30);
path.lineTo(startPosX + 45, startPosY + 15);
path.lineTo(startPosX + 70, startPosY + 15);
path.lineTo(startPosX + 75, startPosY + 30);
path.closePath();
g2d.setPaint(bodyColor);
g2d.fill(path);
g2d.draw(path);
}
}

24
EntitySPAU.java Normal file
View File

@ -0,0 +1,24 @@
import java.awt.Color;
public class EntitySPAU {
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean BodyKit;
public boolean Wing;
public boolean SportLine;
public double Step = (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine) {
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
BodyKit = bodyKit;
Wing = wing;
SportLine = sportLine;
}
}

164
Form.java Normal file
View File

@ -0,0 +1,164 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Form extends JFrame {
private JPanel mainPanel;
private JButton buttonCreate;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private JLabel pictureBoxSPAU;
private DrawingSPAU drawingSPAU;
private JTextField wheelsTextField;
private JLabel wheelsLabel;
public Form() {
initComponents();
}
private void Draw() {
if (drawingSPAU == null) {
return;
}
BufferedImage bmp = new BufferedImage(pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
drawingSPAU.DrawTransport(gr);
ImageIcon imageIcon = new ImageIcon(bmp);
pictureBoxSPAU.setIcon(imageIcon);
}
private void initComponents() {
mainPanel = new JPanel();
buttonCreate = new JButton("Создать");
buttonUp = new JButton(new ImageIcon("resources/upper_arrow.png"));
buttonDown = new JButton(new ImageIcon("resources/down_arrow.png"));
buttonLeft = new JButton(new ImageIcon("resources/left_arrow.png"));
buttonRight = new JButton(new ImageIcon("resources/right_arrow.png"));
pictureBoxSPAU = new JLabel();
wheelsLabel = new JLabel("Количество колёс:");
wheelsTextField = new JTextField();
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(null);
pictureBoxSPAU.setBounds(0, 0, 882, 453);
mainPanel.add(pictureBoxSPAU);
wheelsLabel.setBounds(22, 410, 150, 29);
mainPanel.add(wheelsLabel);
wheelsTextField.setBounds(150, 410, 80, 29);
mainPanel.add(wheelsTextField);
buttonCreate.setBounds(250, 410, 100, 29);
mainPanel.add(buttonCreate);
buttonCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonCreateActionPerformed(e);
}
});
buttonUp.setBounds(804, 336, 30, 30);
mainPanel.add(buttonUp);
ImageIcon iconUp = new ImageIcon("Arrows/upper-arrow.png");
buttonUp.setIcon(iconUp);
buttonUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(buttonUp, e);
}
});
buttonDown.setBounds(804, 409, 30, 30);
mainPanel.add(buttonDown);
ImageIcon iconDown = new ImageIcon("Arrows/down-arrow.png");
buttonDown.setIcon(iconDown);
buttonDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(buttonDown, e);
}
});
buttonLeft.setBounds(768, 372, 30, 30);
mainPanel.add(buttonLeft);
ImageIcon iconLeft = new ImageIcon("Arrows/left-arrow.png");
buttonLeft.setIcon(iconLeft);
buttonLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(buttonLeft, e);
}
});
buttonRight.setBounds(840, 372, 30, 30);
mainPanel.add(buttonRight);
ImageIcon iconRight = new ImageIcon("Arrows/right-arrow.png");
buttonRight.setIcon(iconRight);
buttonRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(buttonRight, e);
}
});
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
setPreferredSize(new Dimension(882, 453));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
protected void buttonCreateActionPerformed(ActionEvent e) {
int wheelCount;
try {
wheelCount = Integer.parseInt(wheelsTextField.getText());
} catch (NumberFormatException ex) {
wheelCount = 0;
}
if (wheelCount > 0) {
Random random = new Random();
drawingSPAU = new DrawingSPAU();
drawingSPAU.Init(wheelCount, 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(),
random.nextBoolean(),
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
drawingSPAU.SetPosition(random.nextInt(90) + 10,
random.nextInt(90) + 10);
Draw();
}
}
protected void buttonMoveActionPerformed(Object sender, ActionEvent e) {
if (drawingSPAU == null) {
return;
}
String name = (sender instanceof JButton) ? ((JButton) sender).getName() : "";
switch (name) {
case "buttonUp":
drawingSPAU.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
drawingSPAU.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
drawingSPAU.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
drawingSPAU.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
}

28
NumbeRollers.java Normal file
View File

@ -0,0 +1,28 @@
public enum NumbeRollers {
Min(4),
Mid(5),
Max(6);
private final int NumbeRollersCode;
private NumbeRollers(int NumbeRollersCode) {
this.NumbeRollersCode = NumbeRollersCode;
}
public int getNumbeRollersCode() {
return this.NumbeRollersCode;
}
public static NumbeRollers fromNumberToEnum(int number) {
try{
for (NumbeRollers numbeRoller : NumbeRollers.values()) {
if (numbeRoller.getNumbeRollersCode() == number) {
return numbeRoller;
}
}
}catch(NumberFormatException e){
}
return Min;
}
}

View File

@ -0,0 +1,11 @@
import javax.swing.*;
public class SelfPropelledArtilleryUnit {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Form form = new Form();
form.setSize(900, 500);
form.setVisible(true);
});
}
}