diff --git a/ContainerShip/src/containership/Decks.java b/ContainerShip/src/containership/Decks.java new file mode 100644 index 0000000..6105f0b --- /dev/null +++ b/ContainerShip/src/containership/Decks.java @@ -0,0 +1,15 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; + +/** + * + * @author ateks + */ +public enum Decks { + ONE, + TWO, + THREE +} diff --git a/ContainerShip/src/containership/Direction.java b/ContainerShip/src/containership/Direction.java new file mode 100644 index 0000000..31e1a60 --- /dev/null +++ b/ContainerShip/src/containership/Direction.java @@ -0,0 +1,16 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; + +/** + * + * @author ateks + */ +public enum Direction { + UP, + DOWN, + LEFT, + RIGHT +} \ No newline at end of file diff --git a/ContainerShip/src/containership/DownArrow.png b/ContainerShip/src/containership/DownArrow.png new file mode 100644 index 0000000..dab78c8 Binary files /dev/null and b/ContainerShip/src/containership/DownArrow.png differ diff --git a/ContainerShip/src/containership/DrawingDecks.java b/ContainerShip/src/containership/DrawingDecks.java new file mode 100644 index 0000000..65086b6 --- /dev/null +++ b/ContainerShip/src/containership/DrawingDecks.java @@ -0,0 +1,93 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; + +import java.awt.Graphics2D; +import java.awt.Color; + +/** + * + * @author ateks + */ +public class DrawingDecks { + + private Decks decks = null; + private int numberOfDecks; + + public int getNumberOfDecks() { + return numberOfDecks; + } + + public void setNumberOfDecks(int numberOfDecks) { + if (numberOfDecks != 1 && numberOfDecks != 2 && numberOfDecks != 3) { + return; + } + this.numberOfDecks = numberOfDecks; + switch (this.numberOfDecks) { + case 1: + decks = Decks.ONE; + break; + case 2: + decks = Decks.TWO; + break; + case 3: + decks = Decks.THREE; + break; + } + } + + public void drawBottomDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + int[] curcuitY + = { + _startPosY + 30, + _startPosY + 30, + _startPosY + 55, + _startPosY + 55, + _startPosY + 30,}; + int[] curcuitX + = { + _startPosX + 100, + _startPosX + 135, + _startPosX + 115, + _startPosX + 20, + _startPosX,}; + g.setColor(bodyColor); + g.fillPolygon(curcuitX, curcuitY, curcuitX.length); + g.setColor(Color.BLACK); + g.drawPolygon(curcuitX, curcuitY, curcuitX.length); + } + + public void drawMiddleDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + g.setColor(bodyColor); + g.fillRect(_startPosX+30, _startPosY+15, 70, 15); + g.setColor(Color.BLACK); + g.drawRect(_startPosX+30, _startPosY+15, 70, 15 ); + } + + public void drawUpperDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + g.setColor(bodyColor); + g.fillRect(_startPosX+50, _startPosY, 45, 15 ); + g.setColor(Color.BLACK); + g.drawRect(_startPosX+50, _startPosY, 45, 15 ); + + } + + + public void drawDecks(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + if (decks == null) { + return; + } + switch (decks) { + case THREE: + drawUpperDeck(g, _startPosX, _startPosY, bodyColor); + case TWO: + drawMiddleDeck(g, _startPosX, _startPosY, bodyColor); + case ONE: + drawBottomDeck(g, _startPosX, _startPosY, bodyColor); + break; + } + + } +} diff --git a/ContainerShip/src/containership/DrawingShip.java b/ContainerShip/src/containership/DrawingShip.java new file mode 100644 index 0000000..e9a2fdb --- /dev/null +++ b/ContainerShip/src/containership/DrawingShip.java @@ -0,0 +1,112 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; +import java.awt.*; +import java.util.Random; +/** + * + * @author ateks + */ +public class DrawingShip { + private EntityShip Ship; + //отрисовка палуб + public DrawingDecks drawingDecks; + public float _startPosX; + public float _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private static final int _shipWidth = 135; + private static final int _shipHeight = 40; + + public void Init(int speed, float weight, Color bodyColor, int numberOfDeks){ + Ship = new EntityShip(); + // мметод прорисовки палуб + drawingDecks = new DrawingDecks(); + Ship.Init(speed, weight, bodyColor); + drawingDecks.setNumberOfDecks(numberOfDeks);/////// палубы + } + + public EntityShip getShip() { + return Ship; + } + + public void SetPosition(int x, int y, int width, int height){ + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + Random rd = new Random(); + _startPosX = x + _shipWidth >= _pictureWidth ? rd.nextInt(0, _pictureWidth - 1 - _shipWidth) : x; + _startPosY = y + _shipHeight >= _pictureHeight ? rd.nextInt(0, _pictureHeight - 1 - _shipHeight) : y; + } + + public void MoveTransport(Direction direction) + { + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + switch (direction) + { + case RIGHT: + if (_startPosX + _shipWidth + Ship.Step < _pictureWidth) + { + _startPosX += Ship.Step; + } + break; + case LEFT: + if (_startPosX - Ship.Step > 0) + { + _startPosX -= Ship.Step; + } + break; + case UP: + if (_startPosY - Ship.Step > 0) + { + _startPosY -= Ship.Step; + } + break; + case DOWN: + if (_startPosY + _shipHeight + Ship.Step < _pictureHeight-13) + { + _startPosY += Ship.Step; + } + break; + } + } + + public void DrawTransport(Graphics2D g) + { + if (_startPosX < 0 || _startPosY < 0 + || _pictureHeight == null || _pictureWidth == null) + { + return; + } + int _startPosXInt = (int)_startPosX; + int _startPosYInt = (int)_startPosY; + drawingDecks.drawDecks(g, _startPosXInt, _startPosYInt, Ship.getBodyColor()); + } + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _shipWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _shipWidth; + } + if (_startPosY + _shipHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _shipHeight; + } + } +} diff --git a/ContainerShip/src/containership/EntityShip.java b/ContainerShip/src/containership/EntityShip.java new file mode 100644 index 0000000..dbc2590 --- /dev/null +++ b/ContainerShip/src/containership/EntityShip.java @@ -0,0 +1,42 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; +import java.awt.Color; +import java.util.Random; +/** + * + * @author ateks + */ +public class EntityShip { + private int Speed; + + private float Weight; + + private Color BodyColor; + + public float Step; + + public int getSpeed() { + return Speed; + } + + public float getWeight() { + return Weight; + } + + public Color getBodyColor() { + return BodyColor; + } + + + + 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/ContainerShip/src/containership/JFrame_Ship.form b/ContainerShip/src/containership/JFrame_Ship.form index 769eecd..772a7aa 100644 --- a/ContainerShip/src/containership/JFrame_Ship.form +++ b/ContainerShip/src/containership/JFrame_Ship.form @@ -23,13 +23,164 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ContainerShip/src/containership/JFrame_Ship.java b/ContainerShip/src/containership/JFrame_Ship.java index 92350be..4652f93 100644 --- a/ContainerShip/src/containership/JFrame_Ship.java +++ b/ContainerShip/src/containership/JFrame_Ship.java @@ -3,6 +3,9 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template */ package containership; +import java.awt.*; +import javax.swing.*; +import java.util.Random; /** * @@ -16,7 +19,7 @@ public class JFrame_Ship extends javax.swing.JFrame { public JFrame_Ship() { initComponents(); } - + private DrawingShip _ship; /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -26,22 +29,170 @@ public class JFrame_Ship extends javax.swing.JFrame { // //GEN-BEGIN:initComponents private void initComponents() { + ShipCanvas = new containership.MyCanvas(); + labelAmountOfDeks = new javax.swing.JLabel(); + comboBoxAmountOfDecks = new javax.swing.JComboBox<>(); + buttonCreate = new javax.swing.JButton(); + statusLabel = new javax.swing.JLabel(); + buttonLeft = new javax.swing.JButton(); + buttonDown = new javax.swing.JButton(); + buttonRight = new javax.swing.JButton(); + buttonUp = new javax.swing.JButton(); + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + ShipCanvas.addComponentListener(new java.awt.event.ComponentAdapter() { + public void componentResized(java.awt.event.ComponentEvent evt) { + ShipCanvasComponentResized(evt); + } + }); + + labelAmountOfDeks.setText("Количество палуб"); + + comboBoxAmountOfDecks.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Одна", "Две", "Три" })); + + buttonCreate.setText("Создать"); + buttonCreate.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonCreateActionPerformed(evt); + } + }); + + statusLabel.setText("Скорость: Вес: Цвет: Палубы:"); + + buttonLeft.setIcon(new javax.swing.ImageIcon(getClass().getResource("/containership/LeftArrow.png"))); // NOI18N + buttonLeft.setName("buttonLeft"); // NOI18N + buttonLeft.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + buttonDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/containership/DownArrow.png"))); // NOI18N + buttonDown.setName("buttonDown"); // NOI18N + buttonDown.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + buttonRight.setIcon(new javax.swing.ImageIcon(getClass().getResource("/containership/Rightarrow.png"))); // NOI18N + buttonRight.setActionCommand(""); + buttonRight.setName("buttonRight"); // NOI18N + buttonRight.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + buttonUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/containership/UpArrow.png"))); // NOI18N + buttonUp.setToolTipText(""); + buttonUp.setName("buttonUp"); // NOI18N + buttonUp.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 400, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(buttonCreate) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 381, Short.MAX_VALUE) + .addComponent(buttonLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(labelAmountOfDeks) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboBoxAmountOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(buttonDown, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(buttonRight, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(buttonUp, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(23, 23, 23)) + .addGroup(layout.createSequentialGroup() + .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap()) + .addComponent(ShipCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 300, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(ShipCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(labelAmountOfDeks) + .addComponent(comboBoxAmountOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(buttonCreate)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(buttonUp, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(buttonLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(buttonDown, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(buttonRight, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))) + .addGap(18, 18, 18) + .addComponent(statusLabel) + .addContainerGap()) ); + labelAmountOfDeks.getAccessibleContext().setAccessibleName(""); + pack(); }// //GEN-END:initComponents + private void buttonCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCreateActionPerformed + Random rnd = new Random(); + _ship = new DrawingShip(); + _ship.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), (comboBoxAmountOfDecks.getSelectedIndex() + 1)); + _ship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), ShipCanvas.getWidth(), ShipCanvas.getHeight()); + statusLabel.setText("Скорость: " + _ship.getShip().getSpeed() + " Вес: " + (int) _ship.getShip().getWeight() + " Цвет: " + _ship.getShip().getBodyColor() + " Палубы: " + _ship.drawingDecks.getNumberOfDecks()); + ShipCanvas.setShip(_ship); + Draw(); + }//GEN-LAST:event_buttonCreateActionPerformed + + private void moveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveButtonActionPerformed + if (_ship == null) return; + String name = ((JButton) evt.getSource()).getName(); + switch (name) + { + case "buttonUp": + _ship.MoveTransport(Direction.UP); + break; + case "buttonDown": + _ship.MoveTransport(Direction.DOWN); + break; + case "buttonLeft": + _ship.MoveTransport(Direction.LEFT); + break; + case "buttonRight": + _ship.MoveTransport(Direction.RIGHT); + break; + } + Draw(); + }//GEN-LAST:event_moveButtonActionPerformed + + private void ShipCanvasComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_ShipCanvasComponentResized + if (_ship == null) return; + _ship.ChangeBorders(ShipCanvas.getWidth(), ShipCanvas.getHeight()); + }//GEN-LAST:event_ShipCanvasComponentResized + + private void Draw(){ + ShipCanvas.repaint(); + } /** * @param args the command line arguments */ @@ -78,5 +229,14 @@ public class JFrame_Ship extends javax.swing.JFrame { } // Variables declaration - do not modify//GEN-BEGIN:variables + private containership.MyCanvas ShipCanvas; + private javax.swing.JButton buttonCreate; + private javax.swing.JButton buttonDown; + private javax.swing.JButton buttonLeft; + private javax.swing.JButton buttonRight; + private javax.swing.JButton buttonUp; + private javax.swing.JComboBox comboBoxAmountOfDecks; + private javax.swing.JLabel labelAmountOfDeks; + private javax.swing.JLabel statusLabel; // End of variables declaration//GEN-END:variables } diff --git a/ContainerShip/src/containership/LeftArrow.png b/ContainerShip/src/containership/LeftArrow.png new file mode 100644 index 0000000..3e14493 Binary files /dev/null and b/ContainerShip/src/containership/LeftArrow.png differ diff --git a/ContainerShip/src/containership/MyCanvas.java b/ContainerShip/src/containership/MyCanvas.java new file mode 100644 index 0000000..7d95be1 --- /dev/null +++ b/ContainerShip/src/containership/MyCanvas.java @@ -0,0 +1,29 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; +import javax.swing.*; +import java.awt.*; +/** + * + * @author ateks + */ +public class MyCanvas extends JComponent { + public MyCanvas(){ + super(); + } + + private DrawingShip _ship = null; + + public void setShip(DrawingShip _ship) { + this._ship = _ship; + } + + @Override + public void paintComponent(Graphics g){ + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + if (_ship != null) _ship.DrawTransport(g2d); + } +} diff --git a/ContainerShip/src/containership/Rightarrow.png b/ContainerShip/src/containership/Rightarrow.png new file mode 100644 index 0000000..17163f7 Binary files /dev/null and b/ContainerShip/src/containership/Rightarrow.png differ diff --git a/ContainerShip/src/containership/UpArrow.png b/ContainerShip/src/containership/UpArrow.png new file mode 100644 index 0000000..9ae59e1 Binary files /dev/null and b/ContainerShip/src/containership/UpArrow.png differ