Перенес первую часть лабораторной

This commit is contained in:
pyzhov.egor 2024-03-10 20:28:14 +04:00
parent a27cb59ee0
commit f1f7561301
7 changed files with 280 additions and 198 deletions

View File

@ -0,0 +1,132 @@
import Entities.*;
import java.awt.*;
import java.util.Random;
public class DrawningBoat {
private EntityBoat entityBoat;
public EntityBoat getEntityBoat() {
return entityBoat;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
protected Integer _startPosX;
protected Integer _startPosY;
private int _drawingBoatWidth = 107;
private int _drawingBoatHeight = 55;
public DrawningCatamaranPaddle _drawingCatamaranPaddle;
public DrawningBoat(int speed, float weight, Color bodyColor) {
entityBoat = new EntityBoat(speed, weight, bodyColor);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
_pictureHeight = null;
_drawingCatamaranPaddle = new DrawningCatamaranPaddle();
Random random = new Random();
int paddlesCount = random.nextInt(1,4);
_drawingCatamaranPaddle.setEnumNumber(paddlesCount);
}
protected DrawningBoat(int speed, float weight, Color bodyColor, int boatWidth, int boatHeight) {
this(speed, weight, bodyColor);
_drawingBoatHeight = boatHeight;
_drawingBoatWidth = boatWidth;
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;
_startPosX = x;
_startPosY = y;
if (_drawingBoatWidth + x > _pictureWidth || x < 0) {
_startPosX = 0;
}
if (_drawingBoatHeight + y > _pictureHeight || y < 0) {
_startPosY = 0;
}
}
public boolean setPictureSize(int width, int height) {
if (_drawingBoatHeight > height || _drawingBoatWidth > width)
return false;
_pictureHeight = height;
_pictureWidth = width;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX + _drawingBoatWidth > width)
_startPosX = width - _drawingBoatWidth;
if (_startPosY + _drawingBoatHeight > height)
_startPosY = height - _drawingBoatHeight;
}
return true;
}
public boolean moveTransport(DirectionType direction) {
if (entityBoat == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case Left:
if (_startPosX - entityBoat.Step() > 0)
_startPosX -= (int) entityBoat.Step();
return true;
case Up:
if (_startPosY - entityBoat.Step() > 0)
_startPosY -= (int) entityBoat.Step();
return true;
case Right:
if (_startPosX + entityBoat.Step() < _pictureWidth - _drawingBoatWidth)
_startPosX += (int) entityBoat.Step();
return true;
case Down:
if (_startPosY + entityBoat.Step() < _pictureHeight - _drawingBoatHeight)
_startPosY += (int) entityBoat.Step();
return true;
default:
return false;
}
}
public void drawBoat(Graphics g) {
if (entityBoat == null || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
Point[] boatBorders = new Point[]{
new Point(_startPosX + 10, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 20),
new Point(_startPosX + 107, _startPosY + 40),
new Point(_startPosX + 80, _startPosY + 55),
new Point(_startPosX + 10, _startPosY + 55)
};
Polygon catamaranPolygon = new Polygon();
for (Point point : boatBorders)
catamaranPolygon.addPoint(point.x, point.y);
g2d.setColor(Color.BLACK);
g2d.draw(catamaranPolygon);
g2d.setColor(entityBoat.getBodyColor());
g2d.fill(catamaranPolygon);
int ellipseX = _startPosX + 17;
int ellipseY = _startPosY + 24;
int ellipseWidth = 65;
int ellipseHeight = 27;
g2d.setColor(Color.BLACK);
g2d.drawOval(ellipseX, ellipseY, ellipseWidth, ellipseHeight);
g2d.setColor(Color.WHITE);
g2d.fillOval(ellipseX, ellipseY, ellipseWidth, ellipseHeight);
_drawingCatamaranPaddle.drawCatamaranPaddles(g2d, entityBoat.getBodyColor(), _startPosX, _startPosY);
}
}

View File

@ -1,94 +1,20 @@
import Entities.*;
import java.awt.*;
import java.util.Random;
public class DrawningCatamaran {
private EntityCatamaran entityCatamaran;
public EntityCatamaran getEntityCatamaran() {
return entityCatamaran;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
private Integer _startPosX;
private Integer _startPosY;
private final int _drawingCatamaranWidth = 120;
private final int _drawingCatamaranHeight = 70;
public DrawningCatamaranPaddle _drawingCatamaranPaddle;
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean floaters, boolean sail) {
entityCatamaran = new EntityCatamaran();
entityCatamaran.Init(speed, weight, bodyColor, additionalColor, floaters, sail);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
_pictureHeight = null;
_drawingCatamaranPaddle = new DrawningCatamaranPaddle();
Random random = new Random();
int paddlesCount = random.nextInt(1,4);
_drawingCatamaranPaddle.setEnumNumber(paddlesCount);
public class DrawningCatamaran extends DrawningBoat {
EntityCatamaran EntityBoat;
public DrawningCatamaran(int speed, float weight, Color bodyColor, Color additionalColor, boolean sail, boolean floaters) {
super(speed, weight, bodyColor, 110, 58);
EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, floaters, sail);
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;
_startPosX = x;
_startPosY = y;
if (_drawingCatamaranWidth + x > _pictureWidth || x < 0) {
_startPosX = 0;
}
if (_drawingCatamaranHeight + y > _pictureHeight || y < 0) {
_startPosY = 0;
}
}
public boolean setPictureSize(int width, int height) {
if (_drawingCatamaranHeight > height || _drawingCatamaranWidth > width)
return false;
_pictureHeight = height;
_pictureWidth = width;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX + _drawingCatamaranWidth > width)
_startPosX = width - _drawingCatamaranWidth;
if (_startPosY + _drawingCatamaranHeight > height)
_startPosY = height - _drawingCatamaranHeight;
}
return true;
}
public boolean moveTransport(DirectionType direction) {
if (entityCatamaran == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case Left:
if (_startPosX - entityCatamaran.Step() > 0)
_startPosX -= (int) entityCatamaran.Step();
return true;
case Up:
if (_startPosY - entityCatamaran.Step() > 0)
_startPosY -= (int) entityCatamaran.Step();
return true;
case Right:
if (_startPosX + entityCatamaran.Step() < _pictureWidth - _drawingCatamaranWidth)
_startPosX += (int) entityCatamaran.Step();
return true;
case Down:
if (_startPosY + entityCatamaran.Step() < _pictureHeight - _drawingCatamaranHeight)
_startPosY += (int) entityCatamaran.Step();
return true;
default:
return false;
}
}
private void drawFloater(int y0, Color additionalColor, Graphics g2d)
{
g2d.setColor(additionalColor);
@ -108,55 +34,30 @@ public class DrawningCatamaran {
g2d.fillPolygon(floaterPolygon);
g2d.drawPolygon(floaterPolygon);
}
@Override
public void drawBoat(Graphics g) {
public void drawCatamaran(Graphics g) {
if (entityCatamaran == null || _startPosX == null || _startPosY == null) {
if (EntityBoat == null || !(EntityBoat instanceof EntityCatamaran) || _startPosX == null || _startPosY == null) {
return;
}
super.drawBoat(g);
Graphics2D g2d = (Graphics2D) g;
Point[] catamaranBorders = new Point[] {
new Point(_startPosX + 10, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 20),
new Point(_startPosX + 107, _startPosY + 40),
new Point(_startPosX + 80, _startPosY + 55),
new Point(_startPosX + 10, _startPosY + 55)
};
Polygon catamaranPolygon = new Polygon();
for(Point point : catamaranBorders)
catamaranPolygon.addPoint(point.x, point.y);
g2d.setColor(Color.BLACK);
g2d.draw(catamaranPolygon);
g2d.setColor(entityCatamaran.getBodyColor());
g2d.fill(catamaranPolygon);
int ellipseX = _startPosX + 17;
int ellipseY = _startPosY + 24;
int ellipseWidth = 65;
int ellipseHeight = 27;
g2d.setColor(Color.BLACK);
g2d.drawOval(ellipseX, ellipseY, ellipseWidth, ellipseHeight);
g2d.setColor(Color.WHITE);
g2d.fillOval(ellipseX, ellipseY, ellipseWidth, ellipseHeight);
if (entityCatamaran.getFloaters()) {
if (EntityBoat.getFloaters()) {
g2d.setColor(Color.BLACK);
g2d.drawLine(_startPosX+20, _startPosY+20, _startPosX + 20, _startPosY+17);
g2d.drawLine(_startPosX+80, _startPosY+20, _startPosX + 80, _startPosY+17);
g2d.drawLine(_startPosX+20, _startPosY+55, _startPosX + 20, _startPosY+58);
g2d.drawLine(_startPosX+80, _startPosY+55, _startPosX + 80, _startPosY+58);
drawFloater(11, entityCatamaran.getAdditionalColor(), g2d);
drawFloater(58, entityCatamaran.getAdditionalColor(), g2d);
drawFloater(11, EntityBoat.getAdditionalColor(), g2d);
drawFloater(58, EntityBoat.getAdditionalColor(), g2d);
}
_drawingCatamaranPaddle.drawCatamaranPaddles(g, entityCatamaran.getBodyColor(), _startPosX, _startPosY);
if (entityCatamaran.getSail()) {
if (EntityBoat.getSail()) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(1));
Point[] sail = new Point[] {
Point[] sail = new Point[] {
new Point(_startPosX + 50, _startPosY + 45),
new Point(_startPosX + 35, _startPosY + 39),
new Point(_startPosX + 35, _startPosY + 24),
@ -168,7 +69,7 @@ public class DrawningCatamaran {
sailPolygon.addPoint(point.x, point.y);
}
g2d.drawPolygon(sailPolygon);
g2d.setColor(entityCatamaran.getAdditionalColor());
g2d.setColor(EntityBoat.getAdditionalColor());
g2d.fillPolygon(sailPolygon);

View File

@ -0,0 +1,27 @@
package Entities;
import java.awt.*;
public class EntityBoat {
private int Speed;
public int getSpeed() {
return Speed;
}
private double Weight;
public double getWeight() {
return Weight;
}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
public double Step() {
return Speed*100/Weight;
}
public EntityBoat(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,24 @@
package Entities;
import java.awt.*;
public class EntityCatamaran extends EntityBoat {
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
private boolean Sail;
public boolean getSail() {
return Sail;
}
private boolean Floaters;
public boolean getFloaters() {
return Floaters;
}
public EntityCatamaran(int speed, double weight, Color bodyColor ,Color additionalColor, boolean sail, boolean floaters) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Sail = sail;
Floaters = floaters;
}
}

View File

@ -1,41 +0,0 @@
import java.awt.*;
public class EntityCatamaran {
private int Speed;
public int getSpeed() {
return Speed;
}
private double Weight;
public double getWeight() {
return Weight;
}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
public double Step() {
return Speed*100/Weight;
}
private boolean Sail;
public boolean getSail() {
return Sail;
}
private boolean Floaters;
public boolean getFloaters() {
return Floaters;
}
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean sail, boolean floaters) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Sail = sail;
Floaters = floaters;
}
}

View File

@ -16,19 +16,6 @@
<properties/>
<border type="none"/>
<children>
<component id="a8c2" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="2" column="0" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<vspacer id="7b40c">
<constraints>
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="3dc0" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
@ -42,11 +29,6 @@
<text value=""/>
</properties>
</component>
<hspacer id="abaeb">
<constraints>
<grid row="2" column="1" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="ccb2a" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
@ -64,7 +46,7 @@
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<preferred-size width="47" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
@ -86,6 +68,42 @@
<text value=""/>
</properties>
</component>
<vspacer id="7b40c">
<constraints>
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<preferred-size width="88" height="14"/>
</grid>
</constraints>
</vspacer>
<hspacer id="e0b21">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="a8c2" class="javax.swing.JButton" binding="buttonCreateCatamaran">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать катамаран"/>
</properties>
</component>
<component id="79b3e" class="javax.swing.JButton" binding="buttonCreateBoat">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать лодку"/>
</properties>
</component>
</children>
</grid>
</children>

View File

@ -7,16 +7,38 @@ import java.util.Random;
import java.util.List;
public class FormCatamaran extends JFrame {
protected DrawningCatamaran _drawningCatamaran = new DrawningCatamaran();
protected DrawningBoat _drawningBoat;
JPanel PanelWrapper;
private JPanel PictureBox;
private JButton buttonCreate;
private JButton buttonCreateCatamaran;
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonCreateBoat;
private List<JComponent> controls;
private void createObject(String type) {
Random random = new Random();
switch (type) {
case "DrawningBoat":
_drawningBoat = new DrawningBoat(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
break;
case "DrawningCatamaran":
_drawningBoat = new DrawningCatamaran(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean());
break;
default:
return;
}
_drawningBoat.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
_drawningBoat.setPosition(random.nextInt(25, 100),
random.nextInt(25, 100));
Draw();
}
public FormCatamaran() {
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
@ -25,25 +47,22 @@ public class FormCatamaran extends JFrame {
InitializeControlsRepaintList();
buttonCreate.addActionListener(new ActionListener() {
buttonCreateCatamaran.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCatamaran = new DrawningCatamaran();
Random random = new Random();
createObject("DrawningCatamaran");
_drawningCatamaran.Init(random.nextInt(30, 100),
random.nextInt(100, 500),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean() );
_drawningCatamaran.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
_drawningCatamaran.setPosition(random.nextInt(25, 100),
random.nextInt(25, 100));
Draw();
}
});
buttonCreateBoat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createObject("DrawningBoat");
}
});
ActionListener buttonMoveClickedListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -52,19 +71,19 @@ public class FormCatamaran extends JFrame {
switch (buttonName) {
case "buttonUp": {
result = _drawningCatamaran.moveTransport(DirectionType.Up);
result = _drawningBoat.moveTransport(DirectionType.Up);
}
break;
case "buttonDown": {
result = _drawningCatamaran.moveTransport(DirectionType.Down);
result = _drawningBoat.moveTransport(DirectionType.Down);
}
break;
case "buttonLeft": {
result = _drawningCatamaran.moveTransport(DirectionType.Left);
result = _drawningBoat.moveTransport(DirectionType.Left);
}
break;
case "buttonRight": {
result = _drawningCatamaran.moveTransport(DirectionType.Right);
result = _drawningBoat.moveTransport(DirectionType.Right);
}
break;
@ -79,9 +98,10 @@ public class FormCatamaran extends JFrame {
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonUp.addActionListener(buttonMoveClickedListener);
}
private void Draw() {
if (_drawningCatamaran.getEntityCatamaran() == null)
if (_drawningBoat.getEntityBoat() == null)
return;
if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) {
return;
@ -89,7 +109,7 @@ public class FormCatamaran extends JFrame {
Graphics g = PictureBox.getGraphics();
g.setColor(PictureBox.getBackground());
g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight());
_drawningCatamaran.drawCatamaran(g);
_drawningBoat.drawBoat(g);
RepaintControls();
@ -103,7 +123,8 @@ public class FormCatamaran extends JFrame {
private void InitializeControlsRepaintList() {
controls = new LinkedList<>();
controls.add(buttonCreate);
controls.add(buttonCreateCatamaran);
controls.add(buttonCreateBoat);
controls.add(buttonUp);
controls.add(buttonDown);
controls.add(buttonLeft);