diff --git a/ArmoredVehicle/src/ArmoredVehicleEntity.java b/ArmoredVehicle/src/ArmoredVehicleEntity.java new file mode 100644 index 0000000..8882deb --- /dev/null +++ b/ArmoredVehicle/src/ArmoredVehicleEntity.java @@ -0,0 +1,36 @@ + +import java.awt.Color; +import java.util.Random; +class ArmoredVehicleEntity { + /// + /// Скорость + /// + public int Speed; + /// + /// Вес + /// + public float Weight; + /// + /// Цвет + /// + public Color BodyColor; + /// + /// Шаг перемещения + /// + public float Step; + /// + /// Инициализация полей объекта-класса + /// + /// + /// + /// + /// + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; + Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight; + Step = Speed * 100 / Weight; + BodyColor = bodyColor; + } +} diff --git a/ArmoredVehicle/src/CountRollers.java b/ArmoredVehicle/src/CountRollers.java new file mode 100644 index 0000000..753d9c5 --- /dev/null +++ b/ArmoredVehicle/src/CountRollers.java @@ -0,0 +1,6 @@ + +public enum CountRollers { + Four, + Five, + Six +} diff --git a/ArmoredVehicle/src/Direction.java b/ArmoredVehicle/src/Direction.java new file mode 100644 index 0000000..fc59abc --- /dev/null +++ b/ArmoredVehicle/src/Direction.java @@ -0,0 +1,8 @@ + +public enum Direction { + + Up, + Down, + Left, + Right; +} diff --git a/ArmoredVehicle/src/DrawingArmoredVehicle.java b/ArmoredVehicle/src/DrawingArmoredVehicle.java new file mode 100644 index 0000000..10b0da8 --- /dev/null +++ b/ArmoredVehicle/src/DrawingArmoredVehicle.java @@ -0,0 +1,198 @@ + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.util.Random; +import javax.swing.JPanel; + +public class DrawingArmoredVehicle extends JPanel{ + + /// + /// Класс-сущность + /// + public ArmoredVehicleEntity ArmoredVehicle; + /// + /// Левая координата отрисовки + /// + private float _startPosX; + /// + /// Верхняя кооридната отрисовки + /// + private float _startPosY; + /// + /// Ширина окна отрисовки + /// + private int _pictureWidth = 0; + /// + /// Высота окна отрисовки + /// + private int _pictureHeight = 0; + /// + /// Ширина отрисовки + /// + private int _ArmoredVehicleWidth = 210; + /// + /// Высота отрисовки + /// + private int _ArmoredVehicleHeight = 50; + /// + /// Инициализация свойств + /// + + private Roller rolls = new Roller();; + + Random rnd = new Random(); + public int Count; + /// Скорость + /// Вес + /// Цвет + public void Init(int speed, float weight, Color bodyColor) + { + ArmoredVehicle = new ArmoredVehicleEntity(); + ArmoredVehicle.Init(speed, weight, bodyColor); + Count = 6 - rnd.nextInt(0, 3); + } + + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + /// Ширина картинки + /// Высота картинки + public void SetPosition(int x, int y, int width, int height) + { + + if(x > 0 && x < width) + { + _startPosX = x; + } + else + { + _startPosX = 0; + } + + if(y > 20 && y < height) + { + _startPosY = y; + } + else + { + _startPosY = 0; + } + + + if(width >= _ArmoredVehicleWidth && height >= _ArmoredVehicleHeight) + { + _pictureWidth = width; + _pictureHeight = height; + + } + repaint(); + } + /// + /// Изменение направления пермещения + /// + /// Направление + public void MoveTransport(Direction direction) + { + if (_pictureWidth == 0 || _pictureHeight == 0) + { + return; + } + switch (direction) + { + // вправо + case Right: + if (_startPosX + _ArmoredVehicleWidth + ArmoredVehicle.Step < _pictureWidth) + { + _startPosX += ArmoredVehicle.Step; + } + break; + //влево + case Left: + if (_startPosX - ArmoredVehicle.Step > 0) + { + _startPosX -= ArmoredVehicle.Step; + } + break; + //вверх + case Up: + if (_startPosY - ArmoredVehicle.Step > 0) + { + _startPosY -= ArmoredVehicle.Step; + } + break; + //вниз + case Down: + if (_startPosY + _ArmoredVehicleHeight + ArmoredVehicle.Step < _pictureHeight) + { + _startPosY += ArmoredVehicle.Step; + } + break; + } + super.repaint(); + } + + /// + /// Отрисовка + /// + /// + + @Override + public void paint(Graphics gr) { + + if (_startPosX < 0 || _startPosY < 0 + || _pictureHeight==0 || _pictureWidth==0) + { + return; + } + gr.clearRect(0, 0, _pictureHeight+500, _pictureWidth+500); + super.paintComponent(gr); + Graphics2D g = (Graphics2D) gr; + Color color = ArmoredVehicle.BodyColor != null ? + ArmoredVehicle.BodyColor : Color.BLACK; + g.setColor(color); + + + + g.fillRect((int)_startPosX + 50, (int)_startPosY, 100, 40); + g.fillRect((int)_startPosX + 15, (int)_startPosY+20, 200, 20); + + + //контур + g.drawRect((int)_startPosX + 50, (int)_startPosY, 100, 20); + g.drawRect((int)_startPosX + 15, (int)_startPosY+20, 200, 20); + g.drawOval((int)_startPosX + 15, (int)_startPosY + 25, 200, 35); + + rolls.Init(Count, color, _startPosX, _startPosY, _ArmoredVehicleWidth); + rolls.paint(gr); + super.repaint(); + g.dispose(); + } + /// + /// Смена границ формы отрисовки + /// + /// Ширина картинки + /// Высота картинки + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _ArmoredVehicleWidth || _pictureHeight <= _ArmoredVehicleHeight) + { + _pictureWidth = 0; + _pictureHeight = 0; + return; + } + if (_startPosX + _ArmoredVehicleWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _ArmoredVehicleWidth; + } + if (_startPosY + _ArmoredVehicleHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _ArmoredVehicleHeight; + } + super.repaint(); + } +} diff --git a/ArmoredVehicle/src/MainForm.form b/ArmoredVehicle/src/MainForm.form new file mode 100644 index 0000000..3fe309f --- /dev/null +++ b/ArmoredVehicle/src/MainForm.form @@ -0,0 +1,164 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ArmoredVehicle/src/MainForm.java b/ArmoredVehicle/src/MainForm.java new file mode 100644 index 0000000..79d65e1 --- /dev/null +++ b/ArmoredVehicle/src/MainForm.java @@ -0,0 +1,247 @@ + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.util.Random; +import javax.swing.ImageIcon; + +public class MainForm extends javax.swing.JFrame { + + + + private DrawingArmoredVehicle _ArmoredVehicle = new DrawingArmoredVehicle();; + + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + java.awt.GridBagConstraints gridBagConstraints; + + DrawPanel = new javax.swing.JPanel(){ + Graphics2D g2;}; + PropertyText = new java.awt.TextField(); + CreateButton = new java.awt.Button(); + ImageIcon arrowUp = new ImageIcon("C:\\Users\\Alena\\Desktop\\New Folder\\JavaApplication4\\src\\Resources\\arrowUp.jpg"); + UpButton = new java.awt.Button(); + LeftButton = new java.awt.Button(); + DownButton = new java.awt.Button(); + RightButton = new java.awt.Button(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setMinimumSize(new java.awt.Dimension(444, 303)); + setName("MainFrame"); // NOI18N + + DrawPanel.setLayout(new java.awt.GridBagLayout()); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 4; + gridBagConstraints.gridwidth = 2; + gridBagConstraints.ipadx = 322; + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; + gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 0); + DrawPanel.add(PropertyText, gridBagConstraints); + + CreateButton.setActionCommand("Create"); + CreateButton.setLabel("Создать"); + CreateButton.setName("CreateButton"); // NOI18N + CreateButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + CreateButtonMouseClicked(evt); + } + }); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 2; + gridBagConstraints.gridheight = 2; + gridBagConstraints.ipadx = 67; + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; + gridBagConstraints.insets = new java.awt.Insets(13, 0, 0, 0); + DrawPanel.add(CreateButton, gridBagConstraints); + + UpButton.setActionCommand("Up"); + UpButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); + UpButton.setLabel("↑"); + UpButton.setMinimumSize(new java.awt.Dimension(30, 30)); + UpButton.setPreferredSize(new java.awt.Dimension(30, 30)); + UpButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + UpButtonMouseClicked(evt); + } + }); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 4; + gridBagConstraints.gridy = 0; + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; + gridBagConstraints.insets = new java.awt.Insets(160, 0, 0, 0); + DrawPanel.add(UpButton, gridBagConstraints); + + LeftButton.setActionCommand("Left"); + LeftButton.setMinimumSize(new java.awt.Dimension(30, 30)); + LeftButton.setName("LeftButton"); // NOI18N + LeftButton.setPreferredSize(new java.awt.Dimension(30, 30)); + LeftButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + LeftButtonMouseClicked(evt); + } + }); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 1; + gridBagConstraints.gridy = 1; + gridBagConstraints.gridwidth = 3; + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; + gridBagConstraints.insets = new java.awt.Insets(0, 227, 0, 0); + DrawPanel.add(LeftButton, gridBagConstraints); + + DownButton.setActionCommand("Left"); + DownButton.setLabel("↓"); + DownButton.setMinimumSize(new java.awt.Dimension(30, 30)); + DownButton.setName("LeftButton"); // NOI18N + DownButton.setPreferredSize(new java.awt.Dimension(30, 30)); + DownButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + DownButtonMouseClicked(evt); + } + }); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 4; + gridBagConstraints.gridy = 2; + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; + DrawPanel.add(DownButton, gridBagConstraints); + + RightButton.setActionCommand("Right"); + RightButton.setLabel("→"); + RightButton.setMinimumSize(new java.awt.Dimension(30, 30)); + RightButton.setName("RightButton"); // NOI18N + RightButton.setPreferredSize(new java.awt.Dimension(30, 30)); + RightButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + RightButtonMouseClicked(evt); + } + }); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 5; + gridBagConstraints.gridy = 1; + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; + gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 32); + DrawPanel.add(RightButton, gridBagConstraints); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 444, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(DrawPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE))) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 309, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(DrawPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap())) + ); + + pack(); + }// //GEN-END:initComponents + + public MainForm() { + initComponents(); + LeftButton.setLabel("<"); + RightButton.setLabel(">"); + UpButton.setLabel("/\\"); + DownButton.setLabel("\\/"); + } + private void CreateButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CreateButtonMouseClicked + Random rnd = new Random(); + _ArmoredVehicle.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); + _ArmoredVehicle.SetPosition(rnd.nextInt(0, 100), rnd.nextInt(25, 200), + DrawPanel.getWidth(), DrawPanel.getHeight()); + PropertyText.setText("Скорость: " + _ArmoredVehicle.ArmoredVehicle.Speed + + " Вес: "+_ArmoredVehicle.ArmoredVehicle.Weight + + " Цвет: "+_ArmoredVehicle.ArmoredVehicle.BodyColor.getRGB()+ + " Катков: "+_ArmoredVehicle.Count); + Draw(); + + }//GEN-LAST:event_CreateButtonMouseClicked + + public void Move(String name) + { + switch (name) + { + case "UpButton" -> _ArmoredVehicle.MoveTransport(Direction.Up); + case "DownButton" -> _ArmoredVehicle.MoveTransport(Direction.Down); + case "LeftButton" -> _ArmoredVehicle.MoveTransport(Direction.Left); + case "RightButton" -> _ArmoredVehicle.MoveTransport(Direction.Right); + } + + Draw(); + } + private void UpButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UpButtonMouseClicked + Move("UpButton"); + }//GEN-LAST:event_UpButtonMouseClicked + + private void LeftButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LeftButtonMouseClicked + Move("LeftButton"); + }//GEN-LAST:event_LeftButtonMouseClicked + + private void RightButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_RightButtonMouseClicked + Move("RightButton"); + }//GEN-LAST:event_RightButtonMouseClicked + + private void DownButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_DownButtonMouseClicked + Move("DownButton"); + }//GEN-LAST:event_DownButtonMouseClicked + + public static void main(String args[]) { + + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new MainForm().setVisible(true); + } + }); + } + + private void Draw() + { + Graphics gr = getGraphics(); + _ArmoredVehicle.paint(gr); + gr.dispose(); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private java.awt.Button CreateButton; + private java.awt.Button DownButton; + private javax.swing.JPanel DrawPanel; + private java.awt.Button LeftButton; + private java.awt.TextField PropertyText; + private java.awt.Button RightButton; + private java.awt.Button UpButton; + // End of variables declaration//GEN-END:variables + +} diff --git a/ArmoredVehicle/src/Roller.java b/ArmoredVehicle/src/Roller.java new file mode 100644 index 0000000..d7c11f7 --- /dev/null +++ b/ArmoredVehicle/src/Roller.java @@ -0,0 +1,74 @@ + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import javax.swing.JPanel; + +public class Roller extends JPanel{ + /// + /// Количество катоков + /// + public int _count; + /// + /// Цвет + /// + private Color _color; + /// + /// Левая координата отрисовки + /// + private float _startPosX; + /// + /// Верхняя кооридната отрисовки + /// + private float _startPosY; + /// + /// Ширина поля под катки + /// + private float _Width; + /// + /// Перечисление + /// + private CountRollers rolls; + + public void Init(int Count, Color color, float startPosX, float startPosY, float Width) + { + _count = Count; + _color = color; + _startPosX = startPosX; + _startPosY = startPosY; + _Width = Width; + Count(_count); + } + + private void Count(int c) + { + switch (c) + { + case 4 -> rolls = CountRollers.Four; + case 5 -> rolls = CountRollers.Five; + case 6 -> rolls = CountRollers.Six; + } + } + public void paint(Graphics gr) + { + Graphics2D g = (Graphics2D) gr; + Color color = _color != null ? + _color : Color.BLACK; + g.setColor(color); + //g.setColor(Color.BLUE); + int count = 0; + switch (rolls) + { + case Four -> count = 4; + case Five -> count = 5; + case Six -> count = 6; + } + int ras = (int)_Width /count; + + for (int i = 0; i < count; i += 1) + { + g.drawOval((int)_startPosX+ i*ras + 15, (int)_startPosY + 35, 20, 20); + } + g.dispose(); + } +}