лаба1

This commit is contained in:
AnnaLioness 2023-09-25 18:40:25 +04:00
parent c924fd9a16
commit 911e8bfdc5
11 changed files with 326 additions and 0 deletions

3
Direction.java Normal file
View File

@ -0,0 +1,3 @@
public enum Direction {
Up, Down, Left, Right;
}

130
DrawingContainerShip.java Normal file
View File

@ -0,0 +1,130 @@
import java.awt.*;
public class DrawingContainerShip {
public EntityContainerShip EntityContainerShip;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _shipWidth = 110;
private int _shipHeight = 65;
private DrawingDecks drawingDecks;
public boolean Init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean crane, boolean container, int deck, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth < _shipWidth || _pictureHeight < _shipHeight)
{
return false;
}
EntityContainerShip = new EntityContainerShip();
EntityContainerShip.Init(speed, weight, bodyColor, additionalColor,
crane, container, deck);
drawingDecks = new DrawingDecks();
drawingDecks.setNumDecks(deck);
return true;
}
public void SetPosition(int x, int y)
{
_startPosX = Math.min(x, _pictureWidth - _shipWidth);
_startPosY = Math.min(y, _pictureHeight - _shipHeight);
}
public void MoveTransport(Direction direction)
{
if (EntityContainerShip == null)
{
return;
}
switch (direction)
{
case Left:
if (_startPosX - EntityContainerShip.Step > 0)
{
_startPosX -= (int)EntityContainerShip.Step;
}
break;
case Up:
if (_startPosY - EntityContainerShip.Step > 0)
{
_startPosY -= (int)EntityContainerShip.Step;
}
break;
case Right:
if (_startPosX + EntityContainerShip.Step + _shipWidth < _pictureWidth)
{
_startPosX += (int)EntityContainerShip.Step;
}
break;
case Down:
if (_startPosY + EntityContainerShip.Step + _shipHeight< _pictureHeight)
{
_startPosY += (int)EntityContainerShip.Step;
}
break;
}
}
public void DrawShip(Graphics2D g)
{
if (EntityContainerShip == null)
{
return;
}
//Pen pen = new Pen(Color.Black);
//Brush adbrush = new SolidBrush(EntityContainerShip.AdditionalColor);
//Brush brBlue = new SolidBrush(Color.Blue);
g.setPaint(Color.BLUE);
// заполнение борта
int x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.fillPolygon(x, y, 5);
//борт корабля контур
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.drawPolyline(_x, _y, 5);
//рисунок на борту
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
//контейнеры
if (EntityContainerShip.Conteiners)
{
g.setPaint(EntityContainerShip.AdditionalColor);
g.fillRect(_startPosX + 50, _startPosY + 55, 35, 10);
g.fillRect(_startPosX + 85, _startPosY + 55, 20, 10);
g.fillRect(_startPosX + 105, _startPosY + 50, 15, 15);
g.fillRect(_startPosX + 50, _startPosY + 45, 15, 10);
g.fillRect(_startPosX + 65, _startPosY + 45, 55, 5);
g.fillRect(_startPosX + 65, _startPosY + 50, 40, 5);
g.setPaint(Color.BLACK);
g.drawRect(_startPosX + 50, _startPosY + 55, 35, 10);
g.drawRect(_startPosX + 85, _startPosY + 55, 20, 10);
g.drawRect(_startPosX + 105, _startPosY + 50, 15, 15);
g.drawRect(_startPosX + 50, _startPosY + 45, 15, 10);
g.drawRect(_startPosX + 65, _startPosY + 45, 55, 5);
g.drawRect(_startPosX + 65, _startPosY + 50, 40, 5);
}
//кран
if (EntityContainerShip.Crane)
{
g.setPaint(EntityContainerShip.AdditionalColor);
g.fillRect(_startPosX + 43, _startPosY+20, 5, 45);
g.fillRect(_startPosX + 47, _startPosY + 30, 20, 3);
g.setPaint(Color.BLACK);
g.drawRect(_startPosX + 43, _startPosY+20, 5, 45);
g.drawRect(_startPosX + 47, _startPosY + 30, 20, 3);
g.drawLine(_startPosX + 47, _startPosY+20, _startPosX + 67, _startPosY + 30);
g.drawLine(_startPosX + 67, _startPosY + 33, _startPosX + 67, _startPosY + 45);
}
drawingDecks.DrawDeck(_startPosX, _startPosY, g);
}
}

39
DrawingDecks.java Normal file
View File

@ -0,0 +1,39 @@
import java.awt.*;
public class DrawingDecks {
private NumberOfDecks numDecks;
public NumberOfDecks getProperty(){
return numDecks;
}
public void setNumDecks(int nDecks){
switch(nDecks){
case 1:
numDecks = NumberOfDecks.Deck_1;
break;
case 2:
numDecks = NumberOfDecks.Deck_2;
break;
case 3:
numDecks = NumberOfDecks.Deck_3;
break;
default:
numDecks = NumberOfDecks.Deck_1;
System.out.println("Что-то пошло не так, количество палуб неверное" + Integer.toString(nDecks) + "сделаем вид, будто она одна");
break;
}
}
public void DrawDeck(int _startPosX, int _startPosY, Graphics2D g){
if(numDecks == NumberOfDecks.Deck_1){
}
if(numDecks == NumberOfDecks.Deck_2){
g.setPaint(Color.BLACK);
g.drawLine(_startPosX + 120, _startPosY + 75, _startPosX+ 55, _startPosY + 75);
}
if(numDecks == NumberOfDecks.Deck_3){
g.setPaint(Color.BLACK);
g.drawLine(_startPosX + 120, _startPosY + 75, _startPosX+ 55, _startPosY + 75);
g.drawLine(_startPosX + 125, _startPosY + 70, _startPosX+ 50, _startPosY + 70);
}
}
}

25
EntityContainerShip.java Normal file
View File

@ -0,0 +1,25 @@
import java.awt.*;
public class EntityContainerShip {
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean Crane;
public boolean Conteiners;
public int Deck;
public double Step;
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean crane, boolean containers, int deck)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Crane = crane;
Conteiners = containers;
Deck = deck;
Step = (double)Speed * 100 / Weight;
}
}

121
Form1.java Normal file
View File

@ -0,0 +1,121 @@
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class Form1 {
private DrawingContainerShip _drawingShip;
Canvas canv;
public void Draw(){
canv.repaint();
}
public Form1(){
JFrame w=new JFrame ("ContainerShip");
JButton buttonCreate = new JButton("создать");
JButton up = new JButton();
up.setBorderPainted(false);
up.setFocusPainted(false);
up.setContentAreaFilled(false);
up.setName("up");
up.setIcon(new ImageIcon("photo11.png"));
JButton down = new JButton();
down.setBorderPainted(false);
down.setFocusPainted(false);
down.setContentAreaFilled(false);
down.setName("down");
down.setIcon(new ImageIcon("photo33.png"));
JButton left = new JButton();
left.setBorderPainted(false);
left.setFocusPainted(false);
left.setContentAreaFilled(false);
left.setName("left");
left.setIcon(new ImageIcon("photo44.png"));
JButton right = new JButton();
right.setBorderPainted(false);
right.setFocusPainted(false);
right.setContentAreaFilled(false);
right.setName("right");
right.setIcon(new ImageIcon("photo22.png"));
buttonCreate.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingShip = new DrawingContainerShip();
/*_drawingShip.Init(random.nextInt(100, 300),
random.nextInt(1000, 3000),
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(0, 2),random.nextBoolean(0, 2),1000, 560);*/
_drawingShip.Init(random.nextInt(100,300),
random.nextInt(1000,3000),
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(), random.nextInt(1,4), 960, 560);
_drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100));
canv._drawingShip = _drawingShip;
Draw();
}
}
);
ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingShip == null)
{
return;
}
switch(((JButton)(e.getSource())).getName()){
case "up":
_drawingShip.MoveTransport(Direction.Up);
break;
case "down":
_drawingShip.MoveTransport(Direction.Down);
break;
case "left":
_drawingShip.MoveTransport(Direction.Left);
break;
case "right":
_drawingShip.MoveTransport(Direction.Right);
break;
}
Draw();
}
};
up.addActionListener(actioListener);
down.addActionListener(actioListener);
left.addActionListener(actioListener);
right.addActionListener(actioListener);
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 1000, 600);
buttonCreate.setBounds(2, 540, 100, 20);
up.setBounds(900, 480, 45, 45);
down.setBounds(900, 520, 45, 45);
left.setBounds(860, 520, 45, 45);
right.setBounds(940, 520, 45, 45);
w.add(canv);
w.add(buttonCreate);
w.add(up);
w.add(down);
w.add(left);
w.add(right);
w.setVisible (true);
}
}
class Canvas extends JComponent{
public DrawingContainerShip _drawingShip;
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingShip == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingShip.DrawShip(g2d);
super.repaint();
}
}

5
Main.java Normal file
View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args){
Form1 form1 = new Form1();
}
}

3
NumberOfDecks.java Normal file
View File

@ -0,0 +1,3 @@
public enum NumberOfDecks {
Deck_1, Deck_2, Deck_3;
}

BIN
photo11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
photo22.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

BIN
photo33.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
photo44.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB