Compare commits

..

7 Commits
main ... lab1

11 changed files with 355 additions and 0 deletions

BIN
image/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 B

BIN
image/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 B

BIN
image/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 257 B

BIN
image/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 B

7
src/Main.java Normal file
View File

@ -0,0 +1,7 @@
import frames.FrameLainer;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException { new FrameLainer(); }
}

View File

@ -0,0 +1,7 @@
package drawing_objects;
public enum DecksNumber {
ONE,
TWO,
THREE
}

View File

@ -0,0 +1,8 @@
package drawing_objects;
public enum DirectionType {
UP,
DOWN,
LEFT,
RIGHT
}

View File

@ -0,0 +1,45 @@
package drawing_objects;
import java.awt.*;
public class DrawingDecks {
private DecksNumber number;
public void setNumber(int x){
switch (x){
case 1:
number = DecksNumber.ONE;
break;
case 2:
number = DecksNumber.TWO;
break;
case 3:
number = DecksNumber.THREE;
break;
default:
number = DecksNumber.ONE;
break;
}
}
public void drawAddDecks(Graphics2D graphics2D, int _startX, int _startY, int lainerwidth, Color color){
//прописать логику отрисовки палуб
int limit = 0;
if(number == DecksNumber.ONE){
limit = 1;
}
else if(number == DecksNumber.TWO){
limit = 2;
}
if(number == DecksNumber.THREE){
limit = 3;
}
BasicStroke pen = new BasicStroke(2);
graphics2D.setStroke(pen);
for(int i = 0; i < limit; i++) {
graphics2D.setPaint(Color.BLACK);
graphics2D.drawRect(_startX + i * 3, _startY - i * 10, lainerwidth - 5 * i -20, 8);
graphics2D.setPaint(color);
graphics2D.fillRect(_startX + i * 3, _startY - i * 10, lainerwidth - 5 * i -20, 9);
}
}
}

View File

@ -0,0 +1,129 @@
package drawing_objects;
import entities.EntityLainer;
import java.awt.*;
public class DrawingLainer {
private EntityLainer entityLainer;
public EntityLainer getEntityLainer() {
return entityLainer;
}
private int pictureWidth;
private int pictureHeight;
private int startPosX;
private int startPosY;
private final int LAINER_WIDTH = 110;
public int getShipWidth() {return LAINER_WIDTH;}
private final int LAINER_HEIGHT = 70;
public int getShipHeight() {return LAINER_HEIGHT;}
private DrawingDecks drawingDecks;
public boolean init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean pool, boolean decks, int width, int height, int decksNumber) {
if (width < LAINER_WIDTH || height < LAINER_HEIGHT)
return false;
pictureWidth = width;
pictureHeight = height;
entityLainer = new EntityLainer();
entityLainer.init(speed, weight, bodyColor, additionalColor, pool, decks);
drawingDecks = new DrawingDecks();
drawingDecks.setNumber(decksNumber);
return true;
}
public void setPosition(int x, int y) {
if (x < 0 || y < 0 || x + LAINER_WIDTH > pictureWidth || y + LAINER_HEIGHT > pictureHeight) {
x = 0;
y = 0;
}
startPosX = x;
startPosY = y;
}
public void moveTransport(DirectionType direction) {
if (entityLainer == null)
return;
switch (direction) {
//влево
case LEFT :
if (startPosX - (int)entityLainer.getStep() > 0) {
startPosX -= (int) entityLainer.getStep();
}
else{
startPosX = 1;
}
break;
//вверх
case UP :
if (startPosY - (int)entityLainer.getStep() > 0) {
startPosY -= (int) entityLainer.getStep();
}
else{
startPosY = 1;
}
break;
// вправо
case RIGHT:
if (startPosX + LAINER_WIDTH + (int)entityLainer.getStep() < pictureWidth) {
startPosX += (int) entityLainer.getStep();
}
else{
startPosX = pictureWidth - LAINER_WIDTH;
}
break;
//вниз
case DOWN :
if (startPosY + LAINER_HEIGHT + (int)entityLainer.getStep() < pictureHeight) {
startPosY += (int) entityLainer.getStep();
}
else{
startPosY = pictureHeight - LAINER_HEIGHT - 1;
}
break;
}
}
public void drawTransport(Graphics2D graphics2D) {
if (entityLainer == null)
return;
BasicStroke pen = new BasicStroke(2);
graphics2D.setStroke(pen);
Color bodyColor = entityLainer.getBodyColor();
Color additionalColor = entityLainer.getAdditionalColor();
//корпус
graphics2D.setPaint(Color.BLUE);
int[] pointsX = { startPosX, startPosX + LAINER_WIDTH, startPosX + 80, startPosX + 20 };
int[] pointsY = { startPosY +40, startPosY + 40, startPosY + 70, startPosY + 70 };
Polygon pol = new Polygon(pointsX, pointsY, 4);
graphics2D.fillPolygon(pol);
//1 палуба
int newposX = startPosX+5;
int newposY = startPosY+30;
graphics2D.setPaint(bodyColor);
graphics2D.fillRect(newposX, newposY, LAINER_WIDTH - 10, 10);
newposX+=2;
newposY-=10;
//доп палубы
if (drawingDecks != null){
drawingDecks.drawAddDecks(graphics2D, newposX, newposY, LAINER_WIDTH, additionalColor);
}
for (int i = 1; i < 5; i++)
{
graphics2D.setPaint(Color.cyan);
graphics2D.fillOval(startPosX + i * 16, startPosY+ 28, 12, 12);
}
//бассейн
if (entityLainer.getPool()) {
graphics2D.setPaint(Color.cyan);
graphics2D.fillOval(startPosX + 15, startPosY + 35, 35, 12);
graphics2D.setPaint(Color.DARK_GRAY);
graphics2D.fillRect(startPosX + 15, startPosY + 30, 35, 20);
//лестница
graphics2D.setPaint(Color.BLACK);
graphics2D.setStroke(pen);
graphics2D.drawLine(startPosX + 18, startPosY + 35, startPosX + 18, startPosY + 70);
graphics2D.drawLine(startPosX + 30, startPosY + 35, startPosX + 30, startPosY + 70);
for(int i = 0; i < 4; i++)
{
graphics2D.drawLine(startPosX + 18, startPosY + 35 + i*10, startPosX + 30, startPosY + 35 + i * 10);
}
}
}
}

View File

@ -0,0 +1,41 @@
package entities;
import java.awt.*;
import java.util.function.Supplier;
public class EntityLainer {
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;
}
private boolean isPool;
public boolean getPool() {
return isPool;
}
private boolean isMultipleDecks;
public boolean getDeks(){return isMultipleDecks;}
public double step;
public double getStep() {return step;}
public void init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean pool, boolean decks) {
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
this.additionalColor = additionalColor;
this.isPool = pool;
this.isMultipleDecks = decks;
step = speed * 100 / weight;
}
}

118
src/frames/FrameLainer.java Normal file
View File

@ -0,0 +1,118 @@
package frames;
import drawing_objects.DrawingLainer;
import drawing_objects.DirectionType;
import drawing_objects.DrawingLainer;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class FrameLainer extends JFrame {
private DrawingLainer drawingLainer;
private JComponent pictureBox;
public FrameLainer() throws IOException {
super("лайнер");
setSize(new Dimension(900,480));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBox = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingLainer != null) drawingLainer.drawTransport(graphics2D);
super.repaint();
}
};
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
JButton createButton = new JButton("Создать");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
rightButton.setPreferredSize(new Dimension(30,30));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
leftButton.setPreferredSize(new Dimension(30,30));
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png"))));
upButton.setPreferredSize(new Dimension(30,30));
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
downButton.setPreferredSize(new Dimension(30,30));
createButton.addActionListener(e -> buttonCreateClick());
rightButton.setActionCommand("right");
rightButton.addActionListener(this::buttonMoveClick);
leftButton.setActionCommand("left");
leftButton.addActionListener(this::buttonMoveClick);
upButton.setActionCommand("up");
upButton.addActionListener(this::buttonMoveClick);
downButton.setActionCommand("down");
downButton.addActionListener(this::buttonMoveClick);
JPanel panelLainer = new JPanel(new BorderLayout());
JPanel createPanel = new JPanel(new BorderLayout());
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
JPanel movementPanel = new JPanel(new GridBagLayout());
JPanel rightPanel = new JPanel(new BorderLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
createPanel.add(createButton, BorderLayout.SOUTH);
constraints.gridx = 2;
constraints.gridy = 1;
movementPanel.add(rightButton, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
movementPanel.add(leftButton, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
movementPanel.add(upButton, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
movementPanel.add(downButton, constraints);
setLayout(new BorderLayout());
add(pictureBox);
rightPanel.add(movementPanel, BorderLayout.SOUTH);
panelLainer.add(rightPanel, BorderLayout.EAST);
panelLainer.add(createPanel, BorderLayout.WEST);
add(panelLainer,BorderLayout.CENTER);
setVisible(true);
}
private void buttonCreateClick() {
Random random = new Random();
drawingLainer = new DrawingLainer();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingLainer.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, 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(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(4));
drawingLainer.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonMoveClick(ActionEvent event) {
if(drawingLainer == null || drawingLainer.getEntityLainer() == null)
return;
switch (event.getActionCommand()) {
case "left" :
drawingLainer.moveTransport(DirectionType.LEFT);
break;
case "right" :
drawingLainer.moveTransport(DirectionType.RIGHT);
break;
case "up" :
drawingLainer.moveTransport(DirectionType.UP);
break;
case "down" :
drawingLainer.moveTransport(DirectionType.DOWN);
break;
}
draw();
}
private void draw() {
if (drawingLainer == null)
{
return;
}
pictureBox.repaint();
}
}