LabWork01
This commit is contained in:
parent
564c6c8ab4
commit
523e7d8412
6
ProjectMotorBoat/.idea/vcs.xml
Normal file
6
ProjectMotorBoat/.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
17
ProjectMotorBoat/src/CanvasMotorBoat.java
Normal file
17
ProjectMotorBoat/src/CanvasMotorBoat.java
Normal file
@ -0,0 +1,17 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class CanvasMotorBoat extends JComponent {
|
||||
public DrawingMotorBoat _drawingMotorBoat;
|
||||
public CanvasMotorBoat(){}
|
||||
public void paintComponent(Graphics g) {
|
||||
if (_drawingMotorBoat == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_drawingMotorBoat.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
|
6
ProjectMotorBoat/src/DirectionType.java
Normal file
6
ProjectMotorBoat/src/DirectionType.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
145
ProjectMotorBoat/src/DrawingMotorBoat.java
Normal file
145
ProjectMotorBoat/src/DrawingMotorBoat.java
Normal file
@ -0,0 +1,145 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingMotorBoat extends JPanel {
|
||||
public EntityMotorBoat EntityMotorBoat;
|
||||
public DrawingPaddles drawingPaddles = null;
|
||||
private Integer picture_width;
|
||||
private Integer picture_height;
|
||||
private Integer _StartPosX;
|
||||
private Integer _StartPosY;
|
||||
private int drawingBoatWidth = 140;
|
||||
private int drawingBoatHeight = 110;
|
||||
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean glass, boolean engine) {
|
||||
EntityMotorBoat = new EntityMotorBoat();
|
||||
EntityMotorBoat.Init(speed, weight, bodycolor, additionalcolor, glass, engine);
|
||||
picture_width = null;
|
||||
picture_height = null;
|
||||
_StartPosX = null;
|
||||
_StartPosY = null;
|
||||
drawingPaddles = new DrawingPaddles();
|
||||
drawingPaddles.setNumberOfPaddles((int)(Math.random() * 4 + 0));
|
||||
}
|
||||
public boolean SetPictureSize(int width, int height) {
|
||||
if (width < drawingBoatWidth || height < drawingBoatHeight) return false;
|
||||
picture_width = width;
|
||||
picture_height = height;
|
||||
if (_StartPosX != null || _StartPosY != null) {
|
||||
if (_StartPosX + drawingBoatWidth > picture_width)
|
||||
{
|
||||
_StartPosX = _StartPosX - (_StartPosX + drawingBoatWidth - picture_width);
|
||||
}
|
||||
else if (_StartPosX < 10) _StartPosX = 10;
|
||||
if (_StartPosY + drawingBoatHeight + 15 > picture_height)
|
||||
{
|
||||
_StartPosY = 15+_StartPosY - (_StartPosY + drawingBoatHeight - picture_height);
|
||||
}
|
||||
else if (_StartPosY < 10) _StartPosY = 10;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public void SetPosition(int x, int y) {
|
||||
if (!(picture_width != null && picture_height != null)) return;
|
||||
if (x + drawingBoatWidth > picture_width)
|
||||
{
|
||||
_StartPosX = x - (x + drawingBoatWidth - picture_width);
|
||||
}
|
||||
else if (x < 10) _StartPosX = 10;
|
||||
else _StartPosX = x;
|
||||
if (y + drawingBoatHeight + 15 > picture_height)
|
||||
{
|
||||
_StartPosY = 15 + y - (y + drawingBoatHeight - picture_height);
|
||||
}
|
||||
else if (y < 10) _StartPosY = 15;
|
||||
else _StartPosY = y;
|
||||
}
|
||||
public boolean MoveTransport(DirectionType direction) {
|
||||
if (EntityMotorBoat == null || _StartPosX == null || _StartPosY == null) return false;
|
||||
switch (direction) {
|
||||
case Left:
|
||||
if (_StartPosX - EntityMotorBoat.Step > 0) {
|
||||
_StartPosX -= (int)EntityMotorBoat.Step;
|
||||
}
|
||||
return true;
|
||||
case Up:
|
||||
if (_StartPosY - (EntityMotorBoat.Step+15) > 0)
|
||||
{
|
||||
_StartPosY -= (int)EntityMotorBoat.Step;
|
||||
}
|
||||
return true;
|
||||
case Right:
|
||||
if (_StartPosX + drawingBoatWidth + (int)EntityMotorBoat.Step < picture_width - EntityMotorBoat.Step)
|
||||
{
|
||||
_StartPosX += (int)EntityMotorBoat.Step;
|
||||
}
|
||||
return true;
|
||||
case Down:
|
||||
if (_StartPosY + drawingBoatHeight + (int)EntityMotorBoat.Step < picture_height - EntityMotorBoat.Step)
|
||||
{
|
||||
_StartPosY += (int)EntityMotorBoat.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
if (EntityMotorBoat == null || _StartPosX == null || _StartPosY == null) return;
|
||||
int y = _StartPosY;
|
||||
|
||||
g.setColor(EntityMotorBoat.getBodyColor());
|
||||
if (drawingPaddles.getNumberOfPaddles() != null) {
|
||||
switch (drawingPaddles.getNumberOfPaddles()) {
|
||||
case OnePaddle:
|
||||
drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityMotorBoat.getBodyColor());
|
||||
|
||||
break;
|
||||
case TwoPaddles:
|
||||
drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityMotorBoat.getBodyColor());
|
||||
drawingPaddles.DrawPaddles(g, _StartPosX + 45, y , 100, 15, EntityMotorBoat.getBodyColor());
|
||||
|
||||
break;
|
||||
case ThreePaddles:
|
||||
drawingPaddles.DrawPaddles(g, _StartPosX + 15, y, 100, 15, EntityMotorBoat.getBodyColor());
|
||||
drawingPaddles.DrawPaddles(g, _StartPosX + 30, y , 100, 15, EntityMotorBoat.getBodyColor());
|
||||
drawingPaddles.DrawPaddles(g, _StartPosX + 45, y , 100, 15, EntityMotorBoat.getBodyColor());
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Point[] hull = new Point[] {
|
||||
new Point(_StartPosX + 5, _StartPosY),
|
||||
new Point(_StartPosX + 100, _StartPosY),
|
||||
new Point(_StartPosX + 140, _StartPosY + 35),
|
||||
new Point(_StartPosX + 100, _StartPosY + 70),
|
||||
new Point(_StartPosX + 5, _StartPosY + 70),
|
||||
};
|
||||
|
||||
Polygon hullPolygon = new Polygon();
|
||||
for (Point point : hull) {
|
||||
hullPolygon.addPoint(point.x, point.y);
|
||||
}
|
||||
g.setColor(EntityMotorBoat.getBodyColor());
|
||||
g.fillPolygon(hullPolygon);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawPolygon(hullPolygon);
|
||||
|
||||
|
||||
if (EntityMotorBoat.getGlass()) {
|
||||
//стекло
|
||||
g.setColor(Color.blue);
|
||||
g.fillRect(_StartPosX + 20, _StartPosY + 15, 80, 40);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX + 20, _StartPosY + 15, 80, 40);
|
||||
}
|
||||
|
||||
// Двигатель
|
||||
if (EntityMotorBoat.getEngine()) {
|
||||
g.setColor(EntityMotorBoat.getAdditionalColor());
|
||||
g.fillRect(_StartPosX, _StartPosY + 10, 5, 50);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX, _StartPosY + 10, 5, 50);
|
||||
}
|
||||
}
|
||||
}
|
39
ProjectMotorBoat/src/DrawingPaddles.java
Normal file
39
ProjectMotorBoat/src/DrawingPaddles.java
Normal file
@ -0,0 +1,39 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingPaddles {
|
||||
private NumberOfPaddles numberOfPaddles;
|
||||
public void setNumberOfPaddles(int numberofpaddle) {
|
||||
for (NumberOfPaddles numofenum : NumberOfPaddles.values()) {
|
||||
if (numofenum.getNumpaddles() == numberofpaddle) {
|
||||
numberOfPaddles = numofenum;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
public NumberOfPaddles getNumberOfPaddles() {
|
||||
return numberOfPaddles;
|
||||
}
|
||||
public void DrawPaddles(Graphics g, int x, int y, int width, int height, Color bodyColor) {
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
// Весло слева
|
||||
int paddleLeftX1 = x + 35;
|
||||
int paddleLeftY1 = y + 10;
|
||||
int paddleLeftX2 = x + 35;
|
||||
int paddleLeftY2 = y - 10;
|
||||
g.drawLine(paddleLeftX1, paddleLeftY1, paddleLeftX2, paddleLeftY2);
|
||||
|
||||
// Весло справа
|
||||
int paddleRightX1 = x + 35;
|
||||
int paddleRightY1 = y + 60;
|
||||
int paddleRightX2 = x + 35;
|
||||
int paddleRightY2 = y + 80;
|
||||
g.drawLine(paddleRightX1, paddleRightY1, paddleRightX2, paddleRightY2);
|
||||
|
||||
// Лопасти весел
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval(paddleLeftX2 - 5, paddleLeftY2 - 5, 10, 10);
|
||||
g.fillOval(paddleRightX2 - 5, paddleRightY2 - 5, 10, 10);
|
||||
}
|
||||
}
|
||||
|
25
ProjectMotorBoat/src/EntityMotorBoat.java
Normal file
25
ProjectMotorBoat/src/EntityMotorBoat.java
Normal file
@ -0,0 +1,25 @@
|
||||
import java.awt.*;
|
||||
public class EntityMotorBoat {
|
||||
private int Speed;
|
||||
private double Weight;
|
||||
private Color BodyColor;
|
||||
public Color getBodyColor() {return BodyColor;}
|
||||
private Color AdditionalColor;
|
||||
public Color getAdditionalColor() {return AdditionalColor;}
|
||||
private boolean Glass;
|
||||
public boolean getGlass() {return Glass;}
|
||||
private boolean Engine;
|
||||
public boolean getEngine() {return Engine;}
|
||||
public double Step;
|
||||
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean glass, boolean engine)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodycolor;
|
||||
AdditionalColor = additionalcolor;
|
||||
Glass = glass;
|
||||
Engine = engine;
|
||||
Step = Speed * 100 / Weight;
|
||||
}
|
||||
}
|
||||
|
123
ProjectMotorBoat/src/FormMotorBoat.java
Normal file
123
ProjectMotorBoat/src/FormMotorBoat.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 FormMotorBoat extends JFrame {
|
||||
private String title;
|
||||
private Dimension dimension;
|
||||
private int Width, Height;
|
||||
private CanvasMotorBoat canvasMotorBoat = new CanvasMotorBoat();
|
||||
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 FormMotorBoat(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.png");
|
||||
UpButton.setIcon(iconUp);
|
||||
UpButton.setName("UP");
|
||||
DownButton.setName("DOWN");
|
||||
Icon iconDown = new ImageIcon("src\\images\\down.png");
|
||||
DownButton.setIcon(iconDown);
|
||||
LeftButton.setName("LEFT");
|
||||
Icon iconLeft = new ImageIcon("src\\images\\left.png");
|
||||
LeftButton.setIcon(iconLeft);
|
||||
RightButton.setName("RIGHT");
|
||||
Icon iconRight = new ImageIcon("src\\images\\right.png");
|
||||
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 engine = new Random().nextBoolean();
|
||||
boolean glass = new Random().nextBoolean();;
|
||||
canvasMotorBoat._drawingMotorBoat = new DrawingMotorBoat();
|
||||
canvasMotorBoat._drawingMotorBoat.Init(speed, weight, bodyColor, additionalColor, glass, engine);
|
||||
canvasMotorBoat._drawingMotorBoat.SetPictureSize(Width, Height);
|
||||
canvasMotorBoat._drawingMotorBoat.SetPosition( StartPositionX, StartPositionY);
|
||||
canvasMotorBoat.repaint();
|
||||
}
|
||||
});
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if (canvasMotorBoat._drawingMotorBoat == null) return;
|
||||
boolean result = false;
|
||||
switch ((((JButton)(event.getSource())).getName())) {
|
||||
case "UP":
|
||||
result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "DOWN":
|
||||
result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "LEFT":
|
||||
result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "RIGHT":
|
||||
result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result) {
|
||||
canvasMotorBoat.repaint();
|
||||
}
|
||||
}
|
||||
};
|
||||
UpButton.addActionListener(actionListener);
|
||||
DownButton.addActionListener(actionListener);
|
||||
LeftButton.addActionListener(actionListener);
|
||||
RightButton.addActionListener(actionListener);
|
||||
|
||||
setSize(dimension.width,dimension.height);
|
||||
setLayout(null);
|
||||
canvasMotorBoat.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 35, 34);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 35, 34);
|
||||
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 35, 34);
|
||||
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 35, 34);
|
||||
add(CreateButton);
|
||||
add(UpButton);
|
||||
add(DownButton);
|
||||
add(RightButton);
|
||||
add(LeftButton);
|
||||
add(canvasMotorBoat);
|
||||
setVisible(true);
|
||||
//обработка события изменения размеров окна
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
public void componentResized(ComponentEvent e) {
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
if (canvasMotorBoat._drawingMotorBoat != null)
|
||||
canvasMotorBoat._drawingMotorBoat.SetPictureSize(Width, Height);
|
||||
canvasMotorBoat.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 35, 34);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 35, 34);
|
||||
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 35, 34);
|
||||
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 35, 34);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
FormMotorBoat form = new FormMotorBoat("Моторная лодка", new Dimension(500,500));
|
||||
form.Init();
|
||||
}
|
||||
}
|
11
ProjectMotorBoat/src/NumberOfPaddles.java
Normal file
11
ProjectMotorBoat/src/NumberOfPaddles.java
Normal file
@ -0,0 +1,11 @@
|
||||
public enum NumberOfPaddles {
|
||||
OnePaddle(1),
|
||||
TwoPaddles(2),
|
||||
ThreePaddles(3);
|
||||
private int numberofpaddles;
|
||||
NumberOfPaddles(int numberofpaddles) {
|
||||
this.numberofpaddles = numberofpaddles;
|
||||
}
|
||||
public int getNumpaddles() {return numberofpaddles;}
|
||||
}
|
||||
|
BIN
ProjectMotorBoat/src/images/down.png
Normal file
BIN
ProjectMotorBoat/src/images/down.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 407 B |
BIN
ProjectMotorBoat/src/images/left.png
Normal file
BIN
ProjectMotorBoat/src/images/left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 463 B |
BIN
ProjectMotorBoat/src/images/right.png
Normal file
BIN
ProjectMotorBoat/src/images/right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 446 B |
BIN
ProjectMotorBoat/src/images/up.png
Normal file
BIN
ProjectMotorBoat/src/images/up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 413 B |
Loading…
Reference in New Issue
Block a user