PIbd-21. Kryukov A.I. Lab work 02 hard #2

Closed
SooNooClose wants to merge 3 commits from lab_2 into lab_1
8 changed files with 143 additions and 12 deletions
Showing only changes of commit aad0dc3598 - Show all commits

View File

@ -54,4 +54,4 @@ public class DrawningExcavator extends DrawningTracktor {
g.drawRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10);
}
}
}
}

View File

@ -187,4 +187,4 @@ public class DrawningTracktor {
public int[] getCurrentPosition(){
return new int[]{_startPosX,_startPosX + _tracktorWidth - 1, _startPosY, _startPosY - _tracktorHeight -1};
}
}
}

View File

@ -25,4 +25,4 @@ public class EntityExcavator extends EntityTracktor{
public boolean getSupports(){
return supports;
}
}
}

View File

@ -21,14 +21,25 @@
<grid row="1" column="2" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="2fbf8" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="2fbf8" binding="pictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="8" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
<children>
<hspacer id="dc061">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="15753">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
<component id="5a9f4" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
@ -86,6 +97,29 @@
<text value="Модификация"/>
</properties>
</component>
<component id="f577" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value=""/>
<item value="0"/>
<item value="1"/>
</model>
</properties>
</component>
<component id="a453" class="javax.swing.JButton" binding="buttonMakeStep">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
</children>
</grid>
<inspectionSuppressions>
<suppress inspection="NoLabelFor"/>
</inspectionSuppressions>
</form>

View File

@ -7,17 +7,17 @@ import java.util.Random;
public class FormTracktor extends JFrame {
private JPanel ContentPanel;
private JButton buttonCreate;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
private JButton buttonLeft;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonUp;
private JPanel pictureBox;
private JButton buttonCreateModif;
private JComboBox comboBoxStrategy;
private JButton buttonMakeStep;
private AbstractStrategy abstractStrategy;
private DrawningTracktor _tracktor;
private DrawningExcavator _excavator;
public FormTracktor(){
setTitle("Трактор");
@ -44,7 +44,7 @@ public class FormTracktor extends JFrame {
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
rnd.nextInt(3,8),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
new Color(rnd.nextInt(0,256),rnd.nextInt(0,256),rnd.nextInt(0,256)),
rnd.nextBoolean(),
rnd.nextBoolean()
);
@ -79,6 +79,7 @@ public class FormTracktor extends JFrame {
}
});
pictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
@ -89,6 +90,33 @@ public class FormTracktor extends JFrame {
}
}
});
buttonMakeStep.addActionListener(e -> {
if (_tracktor == null ) {
return;
}
if(comboBoxStrategy.isEditable()) {
abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()){
case 0 -> new MoveToCenter();
case 1 -> new MoveToBorder();
default -> null;
};
if(abstractStrategy == null){
return;
}
abstractStrategy.setData(new DrawningObjectExcavator(_excavator),this.getWidth(),this.getHeight());
}
if(abstractStrategy == null){
return;
}
comboBoxStrategy.setEditable(false);
abstractStrategy.makeStep();
repaint();
if(abstractStrategy.getStatus() == Status.Finish){
comboBoxStrategy.setEditable(true);
abstractStrategy = null;
}
});
}
private void setData() {

27
src/MoveToBorder.java Normal file
View File

@ -0,0 +1,27 @@
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean isTargetDestination(){
var objParams = getObjectParameters();
if (objParams == null) {
return false;
}
return objParams.rightBorder() <= getFieldWidth() &&
objParams.rightBorder() + getStep() >= getFieldWidth() &&
objParams.downBorder() <= getFieldHeight() &&
objParams.downBorder() +getStep() >= getFieldHeight();
}
@Override
protected void moveToTarget(){
var objParams = getObjectParameters();
if (objParams == null)
return;
var diffX = objParams.objectMiddleHorizontal() - getFieldWidth();
if(Math.abs(diffX) > getStep())
if(diffX < 0)
moveRight();
var diffY = objParams.objectMiddleVertical() - getFieldHeight();
if (Math.abs(diffY) > getStep())
if(diffY < 0)
moveDown();
}
}

35
src/MoveToCenter.java Normal file
View File

@ -0,0 +1,35 @@
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean isTargetDestination(){
var objParams = getObjectParameters();
if(objParams == null){
return false;
}
return objParams.objectMiddleHorizontal() <= getFieldWidth() / 2 &&
objParams.objectMiddleHorizontal() + getStep() <= getFieldWidth() / 2 &&
objParams.objectMiddleVertical() <= getFieldHeight() / 2 &&
objParams.objectMiddleVertical() + getStep() <= getFieldHeight() / 2;
}
@Override
protected void moveToTarget(){
var objParams = getObjectParameters();
if(objParams == null)
return;
var diffX = objParams.objectMiddleHorizontal() - getFieldWidth() / 2;
if(Math.abs(diffX) > getStep()) {
if (diffX > 0)
moveLeft();
else
moveRight();
}
var diffY = objParams.objectMiddleVertical() + getFieldHeight() / 2;
if (Math.abs(diffY) > getStep()){
if (diffY > 0)
moveUp();
else
moveDown();
}
}
}

View File

@ -6,15 +6,22 @@ public class ObjectParameters {
public int leftBorder(){
return x;
}
public int topBorde(){
public int topBorder(){
return y;
}
public int rightBorder(){
return x+width/2;
return x + width;
}
public int downBorder(){
return y + height;
}
public int objectMiddleHorizontal(){
return x+width/2;
}
public int objectMiddleVertical(){
return y + height/2;
}
public ObjectParameters(int x, int y, int width,int height){
this.x = x;
this.y = y;