Compare commits
2 Commits
main
...
Lab_Work01
Author | SHA1 | Date | |
---|---|---|---|
|
af8b91a1a4 | ||
|
ead0ac0602 |
15
ContainerShip/src/containership/Decks.java
Normal file
15
ContainerShip/src/containership/Decks.java
Normal file
@ -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
|
||||
}
|
16
ContainerShip/src/containership/Direction.java
Normal file
16
ContainerShip/src/containership/Direction.java
Normal file
@ -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
|
||||
}
|
BIN
ContainerShip/src/containership/DownArrow.png
Normal file
BIN
ContainerShip/src/containership/DownArrow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
93
ContainerShip/src/containership/DrawingDecks.java
Normal file
93
ContainerShip/src/containership/DrawingDecks.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
112
ContainerShip/src/containership/DrawingShip.java
Normal file
112
ContainerShip/src/containership/DrawingShip.java
Normal file
@ -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 numberOfDecks){
|
||||
Ship = new EntityShip();
|
||||
// мметод прорисовки палуб
|
||||
drawingDecks = new DrawingDecks();
|
||||
Ship.Init(speed, weight, bodyColor);
|
||||
drawingDecks.setNumberOfDecks(numberOfDecks);/////// палубы
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
42
ContainerShip/src/containership/EntityShip.java
Normal file
42
ContainerShip/src/containership/EntityShip.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -23,13 +23,163 @@
|
||||
<Layout>
|
||||
<DimensionLayout dim="0">
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<EmptySpace min="0" pref="400" max="32767" attributes="0"/>
|
||||
<Group type="102" alignment="0" attributes="0">
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Group type="102" alignment="0" attributes="0">
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Group type="102" attributes="0">
|
||||
<Component id="buttonCreate" min="-2" max="-2" attributes="0"/>
|
||||
<EmptySpace pref="381" max="32767" attributes="0"/>
|
||||
<Component id="buttonLeft" min="-2" pref="35" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<Group type="102" alignment="0" attributes="0">
|
||||
<Component id="labelAmountOfDecks" min="-2" max="-2" attributes="0"/>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Component id="comboBoxAmountOfDecks" min="-2" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
</Group>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Group type="102" attributes="0">
|
||||
<Component id="buttonDown" min="-2" pref="35" max="-2" attributes="0"/>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Component id="buttonRight" min="-2" pref="35" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<Component id="buttonUp" min="-2" pref="35" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<EmptySpace min="-2" pref="23" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<Group type="102" alignment="0" attributes="0">
|
||||
<Component id="statusLabel" max="32767" attributes="0"/>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<Component id="ShipCanvas" alignment="0" max="32767" attributes="0"/>
|
||||
</Group>
|
||||
</Group>
|
||||
</Group>
|
||||
</DimensionLayout>
|
||||
<DimensionLayout dim="1">
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<EmptySpace min="0" pref="300" max="32767" attributes="0"/>
|
||||
<Group type="102" alignment="0" attributes="0">
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Component id="ShipCanvas" min="-2" pref="316" max="-2" attributes="0"/>
|
||||
<EmptySpace min="-2" max="-2" attributes="0"/>
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Group type="102" alignment="1" attributes="0">
|
||||
<Group type="103" groupAlignment="3" attributes="0">
|
||||
<Component id="labelAmountOfDecks" alignment="3" min="-2" max="-2" attributes="0"/>
|
||||
<Component id="comboBoxAmountOfDecks" alignment="3" min="-2" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<EmptySpace type="unrelated" max="-2" attributes="0"/>
|
||||
<Component id="buttonCreate" min="-2" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<Group type="102" alignment="1" attributes="0">
|
||||
<Component id="buttonUp" min="-2" pref="35" max="-2" attributes="0"/>
|
||||
<EmptySpace max="32767" attributes="0"/>
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Component id="buttonLeft" min="-2" pref="35" max="-2" attributes="0"/>
|
||||
<Component id="buttonDown" min="-2" pref="35" max="-2" attributes="0"/>
|
||||
<Component id="buttonRight" min="-2" pref="35" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
</Group>
|
||||
</Group>
|
||||
<EmptySpace type="separate" max="-2" attributes="0"/>
|
||||
<Component id="statusLabel" min="-2" max="-2" attributes="0"/>
|
||||
<EmptySpace min="-2" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
</Group>
|
||||
</DimensionLayout>
|
||||
</Layout>
|
||||
<SubComponents>
|
||||
<Container class="containership.MyCanvas" name="ShipCanvas">
|
||||
<Events>
|
||||
<EventHandler event="componentResized" listener="java.awt.event.ComponentListener" parameters="java.awt.event.ComponentEvent" handler="ShipCanvasComponentResized"/>
|
||||
</Events>
|
||||
|
||||
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout">
|
||||
<Property name="useNullLayout" type="boolean" value="true"/>
|
||||
</Layout>
|
||||
</Container>
|
||||
<Component class="javax.swing.JLabel" name="labelAmountOfDecks">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" value="Количество палуб"/>
|
||||
</Properties>
|
||||
<AccessibilityProperties>
|
||||
<Property name="AccessibleContext.accessibleName" type="java.lang.String" value=""/>
|
||||
</AccessibilityProperties>
|
||||
</Component>
|
||||
<Component class="javax.swing.JComboBox" name="comboBoxAmountOfDecks">
|
||||
<Properties>
|
||||
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
|
||||
<StringArray count="3">
|
||||
<StringItem index="0" value="Одна"/>
|
||||
<StringItem index="1" value="Две"/>
|
||||
<StringItem index="2" value="Три"/>
|
||||
</StringArray>
|
||||
</Property>
|
||||
</Properties>
|
||||
<AuxValues>
|
||||
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="<String>"/>
|
||||
</AuxValues>
|
||||
</Component>
|
||||
<Component class="javax.swing.JButton" name="buttonCreate">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" value="Создать"/>
|
||||
</Properties>
|
||||
<Events>
|
||||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="buttonCreateActionPerformed"/>
|
||||
</Events>
|
||||
</Component>
|
||||
<Component class="javax.swing.JLabel" name="statusLabel">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" value="Скорость: Вес: Цвет: Палубы:"/>
|
||||
</Properties>
|
||||
</Component>
|
||||
<Component class="javax.swing.JButton" name="buttonLeft">
|
||||
<Properties>
|
||||
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
|
||||
<Image iconType="3" name="/containership/LeftArrow.png"/>
|
||||
</Property>
|
||||
<Property name="name" type="java.lang.String" value="buttonLeft" noResource="true"/>
|
||||
</Properties>
|
||||
<Events>
|
||||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="moveButtonActionPerformed"/>
|
||||
</Events>
|
||||
</Component>
|
||||
<Component class="javax.swing.JButton" name="buttonDown">
|
||||
<Properties>
|
||||
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
|
||||
<Image iconType="3" name="/containership/DownArrow.png"/>
|
||||
</Property>
|
||||
<Property name="name" type="java.lang.String" value="buttonDown" noResource="true"/>
|
||||
</Properties>
|
||||
<Events>
|
||||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="moveButtonActionPerformed"/>
|
||||
</Events>
|
||||
</Component>
|
||||
<Component class="javax.swing.JButton" name="buttonRight">
|
||||
<Properties>
|
||||
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
|
||||
<Image iconType="3" name="/containership/Rightarrow.png"/>
|
||||
</Property>
|
||||
<Property name="name" type="java.lang.String" value="buttonRight" noResource="true"/>
|
||||
</Properties>
|
||||
<Events>
|
||||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="moveButtonActionPerformed"/>
|
||||
</Events>
|
||||
</Component>
|
||||
<Component class="javax.swing.JButton" name="buttonUp">
|
||||
<Properties>
|
||||
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
|
||||
<Image iconType="3" name="/containership/UpArrow.png"/>
|
||||
</Property>
|
||||
<Property name="toolTipText" type="java.lang.String" value=""/>
|
||||
<Property name="name" type="java.lang.String" value="buttonUp" noResource="true"/>
|
||||
</Properties>
|
||||
<Events>
|
||||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="moveButtonActionPerformed"/>
|
||||
</Events>
|
||||
</Component>
|
||||
</SubComponents>
|
||||
</Form>
|
||||
|
@ -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,169 @@ public class JFrame_Ship extends javax.swing.JFrame {
|
||||
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
|
||||
private void initComponents() {
|
||||
|
||||
ShipCanvas = new containership.MyCanvas();
|
||||
labelAmountOfDecks = 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);
|
||||
}
|
||||
});
|
||||
|
||||
labelAmountOfDecks.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.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(labelAmountOfDecks)
|
||||
.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(labelAmountOfDecks)
|
||||
.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())
|
||||
);
|
||||
|
||||
labelAmountOfDecks.getAccessibleContext().setAccessibleName("");
|
||||
|
||||
pack();
|
||||
}// </editor-fold>//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 +228,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<String> comboBoxAmountOfDecks;
|
||||
private javax.swing.JLabel labelAmountOfDecks;
|
||||
private javax.swing.JLabel statusLabel;
|
||||
// End of variables declaration//GEN-END:variables
|
||||
}
|
||||
|
BIN
ContainerShip/src/containership/LeftArrow.png
Normal file
BIN
ContainerShip/src/containership/LeftArrow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
29
ContainerShip/src/containership/MyCanvas.java
Normal file
29
ContainerShip/src/containership/MyCanvas.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
BIN
ContainerShip/src/containership/Rightarrow.png
Normal file
BIN
ContainerShip/src/containership/Rightarrow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
BIN
ContainerShip/src/containership/UpArrow.png
Normal file
BIN
ContainerShip/src/containership/UpArrow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
Loading…
Reference in New Issue
Block a user