Первая усложненная лабораторная работа.

This commit is contained in:
ksenianeva 2022-12-21 19:25:39 +04:00
parent 363ba5db2a
commit ead0ac0602
12 changed files with 623 additions and 5 deletions

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

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

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

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

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

View File

@ -23,13 +23,164 @@
<Layout> <Layout>
<DimensionLayout dim="0"> <DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="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="labelAmountOfDeks" 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> </Group>
</DimensionLayout> </DimensionLayout>
<DimensionLayout dim="1"> <DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0"> <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="labelAmountOfDeks" 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> </Group>
</DimensionLayout> </DimensionLayout>
</Layout> </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="labelAmountOfDeks">
<Properties>
<Property name="text" type="java.lang.String" value="&#x41a;&#x43e;&#x43b;&#x438;&#x447;&#x435;&#x441;&#x442;&#x432;&#x43e; &#x43f;&#x430;&#x43b;&#x443;&#x431;"/>
</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="&#x41e;&#x434;&#x43d;&#x430;"/>
<StringItem index="1" value="&#x414;&#x432;&#x435;"/>
<StringItem index="2" value="&#x422;&#x440;&#x438;"/>
</StringArray>
</Property>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
</AuxValues>
</Component>
<Component class="javax.swing.JButton" name="buttonCreate">
<Properties>
<Property name="text" type="java.lang.String" value="&#x421;&#x43e;&#x437;&#x434;&#x430;&#x442;&#x44c;"/>
</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="&#x421;&#x43a;&#x43e;&#x440;&#x43e;&#x441;&#x442;&#x44c;: &#x412;&#x435;&#x441;: &#x426;&#x432;&#x435;&#x442;: &#x41f;&#x430;&#x43b;&#x443;&#x431;&#x44b;:"/>
</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="actionCommand" type="java.lang.String" value=""/>
<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> </Form>

View File

@ -3,6 +3,9 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/ */
package containership; 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() { public JFrame_Ship() {
initComponents(); initComponents();
} }
private DrawingShip _ship;
/** /**
* This method is called from within the constructor to initialize the form. * 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 * 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 {
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void 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); 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()); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout); getContentPane().setLayout(layout);
layout.setHorizontalGroup( layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 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.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 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(); pack();
}// </editor-fold>//GEN-END:initComponents }// </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 * @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 // 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 labelAmountOfDeks;
private javax.swing.JLabel statusLabel;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB