This commit is contained in:
Milana Ievlewa 2023-12-18 18:04:37 +03:00
parent 7506645caf
commit d01731c765
12 changed files with 491 additions and 0 deletions

BIN
Resources/arrowDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 415 B

BIN
Resources/arrowLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 411 B

BIN
Resources/arrowRight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 352 B

BIN
Resources/arrowUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 B

11
src/DirectionType.java Normal file
View File

@ -0,0 +1,11 @@
package src;
public enum DirectionType {
Up,
Down,
Left,
Right
}

59
src/DrawingOars.java Normal file
View File

@ -0,0 +1,59 @@
package src;
import javax.swing.*;
import java.awt.*;
public class DrawingOars extends JComponent {
private OarsNumber oarsNumber;
public void setOarNumber(int number) {
switch (number) {
case 1:
oarsNumber = oarsNumber.One;
break;
case 2:
oarsNumber = oarsNumber.Two;
break;
case 3:
oarsNumber = oarsNumber.Three;
break;
default: oarsNumber = oarsNumber.Two;
}
}
public void drawOars(Graphics gr, int _startXCoordOar, int _startYCoordOar, Color pen) {
super.paintComponent(gr);
Graphics2D g=(Graphics2D)gr;
_startXCoordOar += 40;
_startYCoordOar += 33;
System.out.println("oarsNum(drawOars): "+ oarsNumber);
switch (oarsNumber) {
case One:
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
break;
case Two:
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
paintPaddle(g, _startXCoordOar -10, _startYCoordOar -41, _startYCoordOar -51,pen);
break;
case Three:
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
paintPaddle(g, _startXCoordOar -10, _startYCoordOar -41, _startYCoordOar -51,pen);
paintPaddle(g, _startXCoordOar -20, _startYCoordOar, _startYCoordOar + 16,pen);
break;
}
}
protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1,int _startPosY2, Color pen) {
try {
g.setPaint(pen);
} catch (Exception e) {
g.setPaint(Color.black);
}
g.fillRect(_startPosX, _startPosY1, 4, 17);
g.fillOval(_startPosX - 1, _startPosY2, 6, 10);
g.setPaint(Color.black);
g.drawRect(_startPosX, _startPosY1, 4, 17);
g.drawOval(_startPosX - 1, _startPosY2, 6, 10);
}
}

157
src/DrawingSpeedBoat.java Normal file
View File

@ -0,0 +1,157 @@
package src;
import javax.swing.*;
import java.awt.*;
public class DrawingSpeedBoat extends JComponent {
private EntitySpeedBoat entitySpeedBoat;
public EntitySpeedBoat getEntitySpeedBoat() {
return entitySpeedBoat;
}
private DrawingOars drawingOars;
private int pictureWidth;
private int pictureHeight;
private int startPosX;
private int startPosY;
private int boatWidth = 75;
private int boatHeight = 70;
public boolean Init(int speed, double weight, Color mainColor, Color secondColor, boolean isMotor,
boolean isProtectedGLass, int width, int height, int oarsNumber) {
if (width < boatWidth || height < boatHeight) { return false; }
pictureWidth = width;
pictureHeight = height;
entitySpeedBoat = new EntitySpeedBoat();
entitySpeedBoat.Init(speed, weight, mainColor, secondColor,
isMotor, isProtectedGLass);
drawingOars = new DrawingOars();
drawingOars.setOarNumber(oarsNumber);
System.out.println("oarsNum(DrSpeedBoat): "+oarsNumber);
return true;
}
public void SetPosition(int x, int y) {
if (x < 0 || x + boatWidth > pictureWidth) { x = 0; }
if (y < 0 || y + boatHeight > pictureHeight) { y = 0; }
startPosX = x;
startPosY = y;
}
public void MoveTransport(DirectionType direction) {
if (entitySpeedBoat == null) {
return;
}
switch (direction) {
//влево
case Left:
if (startPosX - entitySpeedBoat.getStep() > 0)
{
startPosX -= (int) entitySpeedBoat.getStep();
}
break;
//вверх
case Up:
if (startPosY - entitySpeedBoat.getStep() > 0)
{
startPosY -= (int) entitySpeedBoat.getStep();
}
break;
//вправо
case Right:
if (startPosX + boatWidth + entitySpeedBoat.getStep() < pictureWidth)
{
startPosX += (int) entitySpeedBoat.getStep();
}
break;
//вниз
case Down:
if (startPosY + boatHeight + entitySpeedBoat.getStep() < pictureHeight)
{
startPosY += (int) entitySpeedBoat.getStep();
}
break;
}
}
public void DrawTransport(Graphics2D g)
{
if (entitySpeedBoat == null) {
return;
}
super.paintComponent(g);
if (startPosX < 0 || startPosY < 0
|| pictureHeight == 0 || pictureWidth == 0)
{
return;
}
Color pen = new Color(0, 0, 0);
//границы лодки
int [] pointsX = new int[]{(startPosX + 15),(startPosX + 65),(startPosX + 85),(startPosX + 65),(startPosX + 15)};
int [] pointsY = new int[]{(startPosY),(startPosY),(startPosY + 20),(startPosY + 40),(startPosY+40)};
try { pen = entitySpeedBoat.getMainColor(); }
catch (Exception e) {}
g.setPaint(pen);
g.fillPolygon(pointsX,pointsY,5);
g.setPaint(Color.black);
g.drawPolygon(pointsX,pointsY,5);
g.setPaint(Color.orange);
g.fillOval((startPosX + 25),(int)( startPosY + 7.5), 40, 25);
g.setPaint(Color.black);
g.drawOval((startPosX + 25),(int)( startPosY + 7.5), 40, 25);
if(entitySpeedBoat.get_isMotor()){
Color addPen = new Color(200, 200, 0);
//Motor
int [] pointXMotor = new int[]{(startPosX),(startPosX + 15),(startPosX + 15),(startPosX)};
int [] pointYMotor = new int[]{(startPosY + 10),(startPosY + 10),(startPosY + 30),(startPosY + 30)};
try { addPen = entitySpeedBoat.getsecondColor(); }
catch(Exception e) {}
g.setPaint(addPen);
g.fillPolygon(pointXMotor, pointYMotor, 4);
g.setPaint(Color.black);
g.drawPolygon(pointXMotor, pointYMotor, 4);
}
if(entitySpeedBoat.get_isProtectedGlass()){
Color addPen = new Color(200, 200, 0);
//protectedGlass
int [] pointXGlass = new int[]{(startPosX + 55),(startPosX + 70),(startPosX + 70),(startPosX + 55)};
int [] pointYGlass = new int[]{(startPosY + 10),(startPosY + 6),(startPosY + 34),(startPosY + 30)};
try { addPen = entitySpeedBoat.getsecondColor(); }
catch(Exception e) {}
g.setPaint(addPen);
g.fillPolygon(pointXGlass, pointYGlass, 4);
g.setPaint(Color.black);
g.drawPolygon(pointXGlass, pointYGlass, 4);
}
try { pen = entitySpeedBoat.getsecondColor(); }
catch (Exception e) {}
drawingOars.drawOars(g, startPosX, startPosY, pen);
super.repaint();
}
}

55
src/EntitySpeedBoat.java Normal file
View File

@ -0,0 +1,55 @@
package src;
import java.awt.*;
public class EntitySpeedBoat {
private int speed;
public int getSpeed() {
return speed;
}
private double weight;
public double getWeight() {
return weight;
}
private Color mainColor;
public Color getMainColor() {
return mainColor;
}
private Color secondColor;
public Color getsecondColor() {
return secondColor;
}
private boolean isMotor;
public boolean get_isMotor() {
return isMotor;
}
private boolean isProtectedGlass;
public boolean get_isProtectedGlass() {
return isProtectedGlass;
}
public double getStep() {
return (double)speed * 20 / weight;
}
public void Init(int speed, double weight, Color mainColor, Color secondColor, boolean isMotor, boolean isProtectedGlass) {
this.speed = speed;
this.weight = weight;
this.mainColor = mainColor;
this.secondColor = secondColor;
this.isMotor = isMotor;
this.isProtectedGlass = isProtectedGlass;
}
}

20
src/FrameSpeedBoat.java Normal file
View File

@ -0,0 +1,20 @@
package src;
import javax.swing.*;
public class FrameSpeedBoat extends JFrame {
private PictureBox pictureBox;
public FrameSpeedBoat() {
super("SpeedBoat");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBox = new PictureBox();
add(pictureBox);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

8
src/Main.java Normal file
View File

@ -0,0 +1,8 @@
package src;
public class Main {
public static void main(String[] args) {
FrameSpeedBoat frameSpeedBoat = new FrameSpeedBoat();
}
}

7
src/OarsNumber.java Normal file
View File

@ -0,0 +1,7 @@
package src;
public enum OarsNumber {
One,
Two,
Three
}

174
src/PictureBox.java Normal file
View File

@ -0,0 +1,174 @@
package src;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
public class PictureBox extends JPanel {
private DrawingSpeedBoat drawingSpeedBoat;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonCreateSpeedBoat;
private JPanel buttonsPanel;
private JPanel buttonsMovePanel;
private JPanel paddingPanel;
public PictureBox() {
setLayout(new BorderLayout());
buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.setOpaque(false);
buttonCreateSpeedBoat = new JButton("Create");
buttonCreateSpeedBoat.setFocusable(false);
buttonCreateSpeedBoat.setFont(new Font("Segoe UI", Font.PLAIN, 12));
buttonCreateSpeedBoat.setBackground(Color.LIGHT_GRAY);
buttonCreateSpeedBoat.setPreferredSize(new Dimension(75, 23));
buttonCreateSpeedBoat.addActionListener (e -> {
Random random = new Random();
drawingSpeedBoat = new DrawingSpeedBoat();
int oars = random.nextInt(1, 4);
System.out.println("OARS: "+oars);
drawingSpeedBoat.Init(random.nextInt(50, 150),
random.nextInt(40, 120),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
random.nextBoolean(), random.nextBoolean(), this.getWidth(), this.getHeight(), oars);
drawingSpeedBoat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
});
ActionListener buttonMoveListener = e -> {
if (drawingSpeedBoat == null)
{
return;
}
String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) {
case ("buttonUp"):
drawingSpeedBoat.MoveTransport(DirectionType.Up);
break;
case ("buttonDown"):
drawingSpeedBoat.MoveTransport(DirectionType.Down);
break;
case ("buttonLeft"):
drawingSpeedBoat.MoveTransport(DirectionType.Left);
break;
case ("buttonRight"):
drawingSpeedBoat.MoveTransport(DirectionType.Right);
break;
}
repaint();
};
buttonLeft = new JButton();
buttonLeft.setName("buttonLeft");
buttonLeft.setFocusable(false);
buttonLeft.setPreferredSize(new Dimension(30, 30));
buttonLeft.setIcon(new ImageIcon("Resources/arrowLeft.png"));
buttonLeft.addActionListener(buttonMoveListener);
buttonRight = new JButton();
buttonRight.setName("buttonRight");
buttonRight.setFocusable(false);
buttonRight.setPreferredSize(new Dimension(30, 30));
buttonRight.setIcon(new ImageIcon("Resources/arrowRight.png"));
buttonRight.addActionListener(buttonMoveListener);
buttonDown = new JButton();
buttonDown.setName("buttonDown");
buttonDown.setFocusable(false);
buttonDown.setPreferredSize(new Dimension(30, 30));
buttonDown.setIcon(new ImageIcon("Resources/arrowDown.png"));
buttonDown.addActionListener(buttonMoveListener);
buttonUp = new JButton();
buttonUp.setName("buttonUp");
buttonUp.setFocusable(false);
buttonUp.setPreferredSize(new Dimension(30, 30));
buttonUp.setIcon(new ImageIcon("Resources/arrowUp.png"));
buttonUp.addActionListener(buttonMoveListener);
buttonsMovePanel = new JPanel();
buttonsMovePanel.setLayout(new GridBagLayout());
buttonsMovePanel.setOpaque(false);
GridBagConstraints constrains = new GridBagConstraints();
constrains.insets = new Insets(5,5,5,5);
constrains.gridx = 0;
constrains.gridy = 0;
buttonsMovePanel.add(Box.createHorizontalStrut(30), constrains);
constrains.gridx = 1;
constrains.gridy = 0;
buttonsMovePanel.add(buttonUp, constrains);
constrains.gridx = 2;
constrains.gridy = 0;
buttonsMovePanel.add(Box.createHorizontalStrut(30), constrains);
constrains.gridx = 0;
constrains.gridy = 1;
buttonsMovePanel.add(buttonLeft, constrains);
constrains.gridx = 1;
constrains.gridy = 1;
buttonsMovePanel.add(buttonDown, constrains);
constrains.gridx = 2;
constrains.gridy = 1;
buttonsMovePanel.add(buttonRight, constrains);
paddingPanel = new JPanel();
paddingPanel.setLayout(new BoxLayout(paddingPanel, BoxLayout.Y_AXIS));
paddingPanel.setOpaque(false);
paddingPanel.add(buttonsMovePanel);
paddingPanel.add(Box.createVerticalStrut(15));
buttonsPanel.add(Box.createHorizontalStrut(20));
buttonsPanel.add(buttonCreateSpeedBoat);
buttonsPanel.add(Box.createHorizontalStrut(650));
buttonsPanel.add(paddingPanel);
add(buttonsPanel, BorderLayout.SOUTH);
setPreferredSize(new Dimension(900, 500));
}
@Override
protected void paintComponent(Graphics g) {
if (drawingSpeedBoat == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawingSpeedBoat.DrawTransport(g2d);
}
}