Compare commits

...

1 Commits
main ... Lab1

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

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;
}
}
}

View File

@ -1,5 +1,4 @@
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
@ -9,14 +8,16 @@ public class DrawingSPAU {
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 speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height)
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)
@ -117,22 +118,10 @@ public class DrawingSPAU {
Line2D line = new Line2D.Double(startPosX + 5, startPosY + 20, startPosX + 15, startPosY + 25);
g2d.draw(line);
}
// Гусеницы
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);
g2d.setPaint(Color.BLACK);
g2d.fill(ellipse1);
g2d.fill(ellipse2);
g2d.fill(ellipse3);
g2d.fill(ellipse4);
g2d.fill(ellipse5);
Ellipse2D ellipse6 = new Ellipse2D.Double(startPosX + 10, startPosY + 55, 113, 20);
g2d.draw(ellipse6);
//гусеницы
DrawingRollers Rollers = new DrawingRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers);
Rollers.Draw();
// пушка
path.moveTo(startPosX + 35, startPosY + 40);

View File

@ -15,17 +15,18 @@ public class Form extends JFrame {
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);
BufferedImage bmp = new BufferedImage(pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
drawingSPAU.DrawTransport(gr);
ImageIcon imageIcon = new ImageIcon(bmp);
@ -35,12 +36,15 @@ public class Form extends JFrame {
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);
@ -48,7 +52,13 @@ public class Form extends JFrame {
pictureBoxSPAU.setBounds(0, 0, 882, 453);
mainPanel.add(pictureBoxSPAU);
buttonCreate.setBounds(22, 410, 82, 29);
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) {
@ -106,19 +116,28 @@ public class Form extends JFrame {
}
protected void buttonCreateActionPerformed(ActionEvent e) {
Random random = new Random();
drawingSPAU = new DrawingSPAU();
drawingSPAU.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(),
random.nextBoolean(),
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
drawingSPAU.SetPosition(random.nextInt(90) + 10,
random.nextInt(90) + 10);
Draw();
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) {

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;
}
}