Лабараторная работа №1
This commit is contained in:
parent
d2fc5a5f21
commit
fe5cdc68d5
16
WarmlyShip/src/CanvasWarmlyShip.java
Normal file
16
WarmlyShip/src/CanvasWarmlyShip.java
Normal file
@ -0,0 +1,16 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class CanvasWarmlyShip extends JComponent {
|
||||
public DrawingWarmlyShip _drawingWarmlyShip;
|
||||
public CanvasWarmlyShip(){}
|
||||
public void paintComponent(Graphics g) {
|
||||
if (_drawingWarmlyShip == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_drawingWarmlyShip.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
6
WarmlyShip/src/DirectionType.java
Normal file
6
WarmlyShip/src/DirectionType.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
23
WarmlyShip/src/DrawingDecks.java
Normal file
23
WarmlyShip/src/DrawingDecks.java
Normal file
@ -0,0 +1,23 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingDecks {
|
||||
private NumberOfDecks numberOfDecks;
|
||||
public void setNumberOfDecks(int numberofdeck) {
|
||||
for (NumberOfDecks numofenum : NumberOfDecks.values()) {
|
||||
if (numofenum.getNumdecks() == numberofdeck) {
|
||||
numberOfDecks = numofenum;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
public NumberOfDecks getNumberOfDecks() {
|
||||
return numberOfDecks;
|
||||
}
|
||||
public void DrawDecks(Graphics g, int x, int y, int width, int height, Color bodyColor) {
|
||||
g.setColor(bodyColor);
|
||||
g.fillRect(x, y, width, height);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(x, y, width, height);
|
||||
g.setColor(bodyColor);
|
||||
}
|
||||
}
|
127
WarmlyShip/src/DrawingWarmlyShip.java
Normal file
127
WarmlyShip/src/DrawingWarmlyShip.java
Normal file
@ -0,0 +1,127 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingWarmlyShip extends JPanel {
|
||||
public EntityWarmlyShip EntityWarmlyShip;
|
||||
public DrawingDecks drawingDecks = null;
|
||||
private Integer picture_width;
|
||||
private Integer picture_height;
|
||||
private Integer _StartPosX;
|
||||
private Integer _StartPosY;
|
||||
private int drawingShipWidth = 150;
|
||||
private int drawingShipHeight = 140;
|
||||
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean sheeppipes, boolean fueltank) {
|
||||
EntityWarmlyShip = new EntityWarmlyShip();
|
||||
EntityWarmlyShip.Init(speed, weight, bodycolor, additionalcolor, sheeppipes, fueltank);
|
||||
picture_width = null;
|
||||
picture_height = null;
|
||||
_StartPosX = null;
|
||||
_StartPosY = null;
|
||||
drawingDecks = new DrawingDecks();
|
||||
drawingDecks.setNumberOfDecks((int)(Math.random() * 4 + 0));
|
||||
}
|
||||
public boolean SetPictureSize(int width, int height) {
|
||||
if (width < drawingShipWidth || height < drawingShipHeight) return false;
|
||||
picture_width = width;
|
||||
picture_height = height;
|
||||
if (_StartPosX != null || _StartPosY != null) {
|
||||
if (_StartPosX + drawingShipWidth > picture_width)
|
||||
{
|
||||
_StartPosX = _StartPosX - (_StartPosX + drawingShipWidth - picture_width);
|
||||
}
|
||||
else if (_StartPosX < 0) _StartPosX = 0;
|
||||
if (_StartPosY + drawingShipHeight > picture_height)
|
||||
{
|
||||
_StartPosY = _StartPosY - (_StartPosY + drawingShipHeight - picture_height);
|
||||
}
|
||||
else if (_StartPosY < 0) _StartPosY = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public void SetPosition(int x, int y) {
|
||||
if (!(picture_width != null && picture_height != null)) return;
|
||||
if (x + drawingShipWidth > picture_width)
|
||||
{
|
||||
_StartPosX = x - (x + drawingShipWidth - picture_width);
|
||||
}
|
||||
else if (x < 0) _StartPosX = 0;
|
||||
else _StartPosX = x;
|
||||
if (y + drawingShipHeight > picture_height)
|
||||
{
|
||||
_StartPosY = y - (y + drawingShipHeight - picture_height);
|
||||
}
|
||||
else if (y < 0) _StartPosY = 0;
|
||||
else _StartPosY = y;
|
||||
}
|
||||
public boolean MoveTransport(DirectionType direction) {
|
||||
if (EntityWarmlyShip == null || _StartPosX == null || _StartPosY == null) return false;
|
||||
switch (direction) {
|
||||
case Left:
|
||||
if (_StartPosX - EntityWarmlyShip.Step > 0) {
|
||||
_StartPosX -= (int)EntityWarmlyShip.Step;
|
||||
}
|
||||
return true;
|
||||
case Up:
|
||||
if (_StartPosY - EntityWarmlyShip.Step > 0)
|
||||
{
|
||||
_StartPosY -= (int)EntityWarmlyShip.Step;
|
||||
}
|
||||
return true;
|
||||
case Right:
|
||||
if (_StartPosX + drawingShipWidth + (int)EntityWarmlyShip.Step < picture_width - EntityWarmlyShip.Step)
|
||||
{
|
||||
_StartPosX += (int)EntityWarmlyShip.Step;
|
||||
}
|
||||
return true;
|
||||
case Down:
|
||||
if (_StartPosY + drawingShipHeight + (int)EntityWarmlyShip.Step < picture_height - EntityWarmlyShip.Step)
|
||||
{
|
||||
_StartPosY += (int)EntityWarmlyShip.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
if (EntityWarmlyShip == null || _StartPosX == null || _StartPosY == null) return;
|
||||
int y = _StartPosY;
|
||||
if (EntityWarmlyShip.getSheepPipes()) {
|
||||
//трубы
|
||||
g.setColor(EntityWarmlyShip.getAdditionalColor());
|
||||
g.fillRect(_StartPosX + 70, _StartPosY, 12, 30);
|
||||
g.fillRect(_StartPosX + 90, _StartPosY, 12, 30);
|
||||
y += 30;
|
||||
}
|
||||
g.setColor(EntityWarmlyShip.getBodyColor());
|
||||
if (drawingDecks.getNumberOfDecks() != null) {
|
||||
switch (drawingDecks.getNumberOfDecks()) {
|
||||
case OneDeck:
|
||||
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityWarmlyShip.getBodyColor());
|
||||
y += 15;
|
||||
break;
|
||||
case TwoDecks:
|
||||
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityWarmlyShip.getBodyColor());
|
||||
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityWarmlyShip.getBodyColor());
|
||||
y += 30;
|
||||
break;
|
||||
case ThreeDecks:
|
||||
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityWarmlyShip.getBodyColor());
|
||||
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityWarmlyShip.getBodyColor());
|
||||
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 30, 100, 15, EntityWarmlyShip.getBodyColor());
|
||||
y += 45;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int[] arrayX = {_StartPosX, _StartPosX+150, _StartPosX+150, _StartPosX+120, _StartPosX+120, _StartPosX+30, _StartPosX+30, _StartPosX};
|
||||
int[] arrayY = {y, y, y, y + 50, y + 50, y + 50, y + 50, y};
|
||||
Polygon poly = new Polygon(arrayX, arrayY, 8);
|
||||
g.fillPolygon(poly);
|
||||
g.setColor(EntityWarmlyShip.getAdditionalColor());
|
||||
if (EntityWarmlyShip.getFuelTank()) {
|
||||
g.setColor(EntityWarmlyShip.getAdditionalColor());
|
||||
g.fillRect(_StartPosX + 40, y + 30, 70, 10);
|
||||
}
|
||||
drawingShipHeight = y + 50 - _StartPosY;
|
||||
}
|
||||
}
|
24
WarmlyShip/src/EntityWarmlyShip.java
Normal file
24
WarmlyShip/src/EntityWarmlyShip.java
Normal file
@ -0,0 +1,24 @@
|
||||
import java.awt.*;
|
||||
public class EntityWarmlyShip {
|
||||
private int Speed;
|
||||
private double Weight;
|
||||
private Color BodyColor;
|
||||
public Color getBodyColor() {return BodyColor;}
|
||||
private Color AdditionalColor;
|
||||
public Color getAdditionalColor() {return AdditionalColor;}
|
||||
private boolean SheepPipes;
|
||||
public boolean getSheepPipes() {return SheepPipes;}
|
||||
private boolean FuelTank;
|
||||
public boolean getFuelTank() {return FuelTank;}
|
||||
public double Step;
|
||||
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean sheeppipes, boolean fueltank)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodycolor;
|
||||
AdditionalColor = additionalcolor;
|
||||
SheepPipes = sheeppipes;
|
||||
FuelTank = fueltank;
|
||||
Step = Speed * 100 / Weight;
|
||||
}
|
||||
}
|
123
WarmlyShip/src/FormWarmlyShip.java
Normal file
123
WarmlyShip/src/FormWarmlyShip.java
Normal file
@ -0,0 +1,123 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormWarmlyShip extends JFrame {
|
||||
private String title;
|
||||
private Dimension dimension;
|
||||
private int Width, Height;
|
||||
private CanvasWarmlyShip canvasWarmlyShip = new CanvasWarmlyShip();
|
||||
private JButton CreateButton = new JButton("Create");;
|
||||
private JButton UpButton = new JButton();
|
||||
private JButton DownButton = new JButton();;
|
||||
private JButton LeftButton = new JButton();;
|
||||
private JButton RightButton = new JButton();
|
||||
public FormWarmlyShip(String title, Dimension dimension) {
|
||||
this.title = title;
|
||||
this.dimension = dimension;
|
||||
}
|
||||
public void Init() {
|
||||
setTitle(title);
|
||||
setMinimumSize(dimension);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
|
||||
CreateButton.setName("CREATE");
|
||||
Icon iconUp = new ImageIcon("src\\images\\up.jpg");
|
||||
UpButton.setIcon(iconUp);
|
||||
UpButton.setName("UP");
|
||||
DownButton.setName("DOWN");
|
||||
Icon iconDown = new ImageIcon("src\\images\\down.jpg");
|
||||
DownButton.setIcon(iconDown);
|
||||
LeftButton.setName("LEFT");
|
||||
Icon iconLeft = new ImageIcon("src\\images\\left.jpg");
|
||||
LeftButton.setIcon(iconLeft);
|
||||
RightButton.setName("RIGHT");
|
||||
Icon iconRight = new ImageIcon("src\\images\\right.jpg");
|
||||
RightButton.setIcon(iconRight);
|
||||
|
||||
CreateButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int StartPositionX = (int)(Math.random() * 90 + 10);
|
||||
int StartPositionY = (int)(Math.random() * 90 + 10);
|
||||
int speed = (int)(Math.random() * 300 + 100);
|
||||
double weight = (double)(Math.random() * 3000 + 1000);
|
||||
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||||
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
|
||||
boolean sheepPipes = new Random().nextBoolean();
|
||||
boolean fuelTank = new Random().nextBoolean();;
|
||||
canvasWarmlyShip._drawingWarmlyShip = new DrawingWarmlyShip();
|
||||
canvasWarmlyShip._drawingWarmlyShip.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
|
||||
canvasWarmlyShip._drawingWarmlyShip.SetPictureSize(Width, Height);
|
||||
canvasWarmlyShip._drawingWarmlyShip.SetPosition( StartPositionX, StartPositionY);
|
||||
canvasWarmlyShip.repaint();
|
||||
}
|
||||
});
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if (canvasWarmlyShip._drawingWarmlyShip == null) return;
|
||||
boolean result = false;
|
||||
switch ((((JButton)(event.getSource())).getName())) {
|
||||
case "UP":
|
||||
result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "DOWN":
|
||||
result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "LEFT":
|
||||
result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "RIGHT":
|
||||
result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result) {
|
||||
canvasWarmlyShip.repaint();
|
||||
}
|
||||
}
|
||||
};
|
||||
UpButton.addActionListener(actionListener);
|
||||
DownButton.addActionListener(actionListener);
|
||||
LeftButton.addActionListener(actionListener);
|
||||
RightButton.addActionListener(actionListener);
|
||||
|
||||
setSize(dimension.width,dimension.height);
|
||||
setLayout(null);
|
||||
canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
|
||||
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
|
||||
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
|
||||
add(CreateButton);
|
||||
add(UpButton);
|
||||
add(DownButton);
|
||||
add(RightButton);
|
||||
add(LeftButton);
|
||||
add(canvasWarmlyShip);
|
||||
setVisible(true);
|
||||
//обработка события изменения размеров окна
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
public void componentResized(ComponentEvent e) {
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
if (canvasWarmlyShip._drawingWarmlyShip != null)
|
||||
canvasWarmlyShip._drawingWarmlyShip.SetPictureSize(Width, Height);
|
||||
canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
|
||||
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
|
||||
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
FormWarmlyShip form = new FormWarmlyShip("Теплоход", new Dimension(500,500));
|
||||
form.Init();
|
||||
}
|
||||
}
|
10
WarmlyShip/src/NumberOfDecks.java
Normal file
10
WarmlyShip/src/NumberOfDecks.java
Normal file
@ -0,0 +1,10 @@
|
||||
public enum NumberOfDecks {
|
||||
OneDeck(1),
|
||||
TwoDecks(2),
|
||||
ThreeDecks(3);
|
||||
private int numberofdecks;
|
||||
NumberOfDecks(int numberofdecks) {
|
||||
this.numberofdecks = numberofdecks;
|
||||
}
|
||||
public int getNumdecks() {return numberofdecks;}
|
||||
}
|
BIN
WarmlyShip/src/images/down.jpg
Normal file
BIN
WarmlyShip/src/images/down.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 141 KiB |
BIN
WarmlyShip/src/images/left.jpg
Normal file
BIN
WarmlyShip/src/images/left.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 139 KiB |
BIN
WarmlyShip/src/images/right.jpg
Normal file
BIN
WarmlyShip/src/images/right.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.8 KiB |
BIN
WarmlyShip/src/images/up.jpg
Normal file
BIN
WarmlyShip/src/images/up.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 141 KiB |
Loading…
Reference in New Issue
Block a user