Сделанная 1 лабораторная
This commit is contained in:
parent
544f81d9ae
commit
4144ff067a
BIN
Resources/down.png
Normal file
BIN
Resources/down.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 588 B |
BIN
Resources/left.png
Normal file
BIN
Resources/left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 645 B |
BIN
Resources/right.png
Normal file
BIN
Resources/right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 571 B |
BIN
Resources/up.png
Normal file
BIN
Resources/up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 601 B |
13
src/CountPaddles.java
Normal file
13
src/CountPaddles.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
public enum CountPaddles {
|
||||||
|
One(1),
|
||||||
|
Two(2),
|
||||||
|
Three(3);
|
||||||
|
|
||||||
|
private final int Value;
|
||||||
|
CountPaddles(int Count) {
|
||||||
|
Value = Count;
|
||||||
|
}
|
||||||
|
public int getCountPaddles(){
|
||||||
|
return Value;
|
||||||
|
}
|
||||||
|
}
|
6
src/Direction.java
Normal file
6
src/Direction.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
public enum Direction {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right
|
||||||
|
}
|
95
src/DrawingBoat.java
Normal file
95
src/DrawingBoat.java
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
public class DrawingBoat {
|
||||||
|
public EntityBoat Boat;
|
||||||
|
|
||||||
|
public DrawingPaddles Paddles;
|
||||||
|
private int _startPosX;
|
||||||
|
private int _startPosY;
|
||||||
|
private Integer _pictureWidth = null;
|
||||||
|
private Integer _pictureHeight = null;
|
||||||
|
private final int _BoatWidth = 180;
|
||||||
|
private final int _BoatHeight = 160;
|
||||||
|
|
||||||
|
public void Init(int speed, float weight, Color bodyColor){
|
||||||
|
Boat = new EntityBoat();
|
||||||
|
Boat.Init(speed, weight, bodyColor);
|
||||||
|
Paddles = new DrawingPaddles();
|
||||||
|
Paddles.SetCountPaddles((int)(1+Math.random()+Math.random()*2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetPosition(int x, int y, int width, int height){
|
||||||
|
if(x >= 0 && x+_BoatWidth <= width && y+_BoatHeight <= height){
|
||||||
|
_startPosX = x;
|
||||||
|
_startPosY = y;
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MoveTransport(Direction direction){
|
||||||
|
if(_pictureWidth == null || _pictureHeight == null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(direction){
|
||||||
|
case Right:
|
||||||
|
if(_startPosX + _BoatWidth + Boat.Step < _pictureWidth){
|
||||||
|
_startPosX += Boat.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Left:
|
||||||
|
if(_startPosX - Boat.Step >= 0){
|
||||||
|
_startPosX -= Boat.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Up:
|
||||||
|
if (_startPosY - Boat.Step >= 0)
|
||||||
|
{
|
||||||
|
_startPosY -= Boat.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Down:
|
||||||
|
if (_startPosY + _BoatHeight + Boat.Step < _pictureHeight)
|
||||||
|
{
|
||||||
|
_startPosY += Boat.Step;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawTransport(Graphics g){
|
||||||
|
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight== null || _pictureWidth== null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Graphics2D g2 = (Graphics2D)g;
|
||||||
|
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
|
||||||
|
g2.drawLine(_startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60);
|
||||||
|
g2.drawLine( _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90);
|
||||||
|
g2.drawLine( _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120);
|
||||||
|
g2.drawLine( _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120);
|
||||||
|
g2.drawLine(_startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60);
|
||||||
|
g2.drawOval( _startPosX + 15, _startPosY + 65, 95, 50);
|
||||||
|
|
||||||
|
Paddles.DrawPaddles(g2, _startPosX, _startPosY);
|
||||||
|
|
||||||
|
g2.setColor(Color.GRAY);
|
||||||
|
|
||||||
|
g2.fillRect( _startPosX + 2, _startPosY + 65, 10, 50);
|
||||||
|
g2.fillRect( _startPosX + 15, _startPosY + 53, 90, 10);
|
||||||
|
g2.fillRect( _startPosX + 15, _startPosY + 118, 90, 10);
|
||||||
|
|
||||||
|
g2.setColor(Color.BLACK);
|
||||||
|
|
||||||
|
g2.drawRect( _startPosX + 60, _startPosY , 4, 90);
|
||||||
|
g2.drawRect( _startPosX + 60, _startPosY , 4, 90);
|
||||||
|
g2.drawLine( _startPosX + 25, _startPosY + 20, _startPosX + 100, _startPosY);
|
||||||
|
g2.drawLine( _startPosX + 100, _startPosY, _startPosX + 100, _startPosY + 50);
|
||||||
|
g2.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 100, _startPosY + 50);
|
||||||
|
g2.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 25, _startPosY + 20);
|
||||||
|
}
|
||||||
|
}
|
52
src/DrawingField.java
Normal file
52
src/DrawingField.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
public class DrawingField extends JPanel{
|
||||||
|
private final FormBoat field;
|
||||||
|
DrawingBoat _Boat;
|
||||||
|
public DrawingField(FormBoat field){
|
||||||
|
this.field = field;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g){
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2 = (Graphics2D)g;
|
||||||
|
if(_Boat != null){
|
||||||
|
_Boat.DrawTransport(g2);
|
||||||
|
}
|
||||||
|
else return;
|
||||||
|
}
|
||||||
|
public void UpButtonAction(){
|
||||||
|
if(_Boat != null){
|
||||||
|
_Boat.MoveTransport(Direction.Up);
|
||||||
|
} else return;
|
||||||
|
}
|
||||||
|
public void DownButtonAction(){
|
||||||
|
if (_Boat!=null){
|
||||||
|
_Boat.MoveTransport(Direction.Down);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
public void RightButtonAction(){
|
||||||
|
if (_Boat!=null){
|
||||||
|
_Boat.MoveTransport(Direction.Right);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
public void LeftButtonAction(){
|
||||||
|
if (_Boat!=null){
|
||||||
|
_Boat.MoveTransport(Direction.Left);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreateButtonAction(){
|
||||||
|
Random r = new Random();
|
||||||
|
_Boat = new DrawingBoat();
|
||||||
|
_Boat.Init(r.nextInt(50)+10,r.nextInt(100)+500,new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
|
||||||
|
_Boat.SetPosition(r.nextInt(100)+10,r.nextInt(100)+10,getWidth(),getHeight());
|
||||||
|
}
|
||||||
|
}
|
43
src/DrawingPaddles.java
Normal file
43
src/DrawingPaddles.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
public class DrawingPaddles {
|
||||||
|
private CountPaddles _paddles;
|
||||||
|
|
||||||
|
private int step = 0;
|
||||||
|
|
||||||
|
public void SetCountPaddles(int Count){
|
||||||
|
for(CountPaddles temp : CountPaddles.values())
|
||||||
|
if(temp.getCountPaddles() == Count){
|
||||||
|
_paddles = temp;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawPaddles(Graphics2D g, int _startPosX, int _startPosY){
|
||||||
|
step = 0;
|
||||||
|
switch(_paddles.getCountPaddles()){
|
||||||
|
case 1:
|
||||||
|
drawPaddle(g,_startPosX,_startPosY);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
for(int i = 0;i<2;i++){
|
||||||
|
drawPaddle(g,_startPosX,_startPosY);
|
||||||
|
step+=25;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
for(int i = 0;i<3;i++){
|
||||||
|
drawPaddle(g,_startPosX,_startPosY);
|
||||||
|
step+=25;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawPaddle(Graphics2D g, int _startPosX, int _startPosY){
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.drawLine(_startPosX + 20+step, _startPosY + 60, _startPosX + 35+step, _startPosY + 27);
|
||||||
|
g.drawOval(_startPosX+32+step, _startPosY+13, 10, 15);
|
||||||
|
g.drawLine(_startPosX + 20+step, _startPosY + 120, _startPosX + 35+step, _startPosY + 150);
|
||||||
|
g.drawOval(_startPosX+32+step, _startPosY+150, 10, 15);
|
||||||
|
}
|
||||||
|
}
|
17
src/EntityBoat.java
Normal file
17
src/EntityBoat.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
public class EntityBoat {
|
||||||
|
private int Speed;
|
||||||
|
|
||||||
|
private float Weight;
|
||||||
|
private Color BodyColor;
|
||||||
|
public float Step;
|
||||||
|
|
||||||
|
public void Init(int speed, float weight, Color bodyColor){
|
||||||
|
Random r = new Random();
|
||||||
|
Speed = speed <= 0 ? r.nextInt(50)+10 : speed;
|
||||||
|
Weight = weight <= 0 ? r.nextInt(100)+500:weight;
|
||||||
|
BodyColor = bodyColor;
|
||||||
|
Step = Speed * 600 / (int)Weight;
|
||||||
|
}
|
||||||
|
}
|
123
src/FormBoat.java
Normal file
123
src/FormBoat.java
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class FormBoat extends JFrame{
|
||||||
|
private final int Width;
|
||||||
|
private final int Height;
|
||||||
|
|
||||||
|
JPanel BottomPanel = new JPanel();
|
||||||
|
JPanel CreatePanel = new JPanel();
|
||||||
|
JPanel BottomAndCreatePanel = new JPanel();
|
||||||
|
JPanel DimentionPanel = new JPanel();
|
||||||
|
JPanel UpPanel = new JPanel();
|
||||||
|
JPanel DownPanel = new JPanel();
|
||||||
|
JPanel LRPanel = new JPanel();
|
||||||
|
|
||||||
|
DrawingField field = new DrawingField(this);
|
||||||
|
|
||||||
|
JButton ButtonCreate = new JButton("Создать");
|
||||||
|
|
||||||
|
Icon arrowUp = new ImageIcon("Resources/up.png");
|
||||||
|
JButton ButtonUp = new JButton(arrowUp);
|
||||||
|
|
||||||
|
Icon arrowLeft = new ImageIcon("Resources/left.png");
|
||||||
|
JButton ButtonLeft = new JButton(arrowLeft);
|
||||||
|
|
||||||
|
Icon arrowDown = new ImageIcon("Resources/down.png");
|
||||||
|
JButton ButtonDown = new JButton(arrowDown);
|
||||||
|
|
||||||
|
Icon arrowRight = new ImageIcon("Resources/right.png");
|
||||||
|
JButton ButtonRight = new JButton(arrowRight);
|
||||||
|
|
||||||
|
public FormBoat(){
|
||||||
|
super("Boat");
|
||||||
|
setSize(800, 600);
|
||||||
|
Width = getWidth();
|
||||||
|
Height = getHeight();
|
||||||
|
ShowWindow();
|
||||||
|
RefreshWindow();
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ShowWindow() {
|
||||||
|
Dimension dimen = new Dimension(35,35);
|
||||||
|
|
||||||
|
// Обработка нажатия кнопки
|
||||||
|
ButtonUp.setPreferredSize(dimen);
|
||||||
|
ButtonUp.addActionListener(e->{
|
||||||
|
field.UpButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Обработка нажатия кнопки
|
||||||
|
ButtonDown.setPreferredSize(dimen);
|
||||||
|
ButtonDown.addActionListener(e->{
|
||||||
|
field.DownButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Обработка нажатия кнопки
|
||||||
|
ButtonRight.setPreferredSize(dimen);
|
||||||
|
ButtonRight.addActionListener(e->{
|
||||||
|
field.RightButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Обработка нажатия кнопки
|
||||||
|
ButtonLeft.setPreferredSize(dimen);
|
||||||
|
ButtonLeft.addActionListener(e->{
|
||||||
|
field.LeftButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Добавление кнопок на панель (Левая и правая стрелки)
|
||||||
|
LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0));
|
||||||
|
LRPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
LRPanel.add(ButtonLeft);
|
||||||
|
LRPanel.add(ButtonRight);
|
||||||
|
|
||||||
|
// Добавление кнопки (Стрелка вверх)
|
||||||
|
UpPanel.setLayout(new FlowLayout());
|
||||||
|
UpPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
UpPanel.add(ButtonUp);
|
||||||
|
|
||||||
|
// Добавление кнопки (Стрелка вниз)
|
||||||
|
DownPanel.setLayout(new FlowLayout());
|
||||||
|
DownPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
DownPanel.add(ButtonDown);
|
||||||
|
|
||||||
|
DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS));
|
||||||
|
DimentionPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
DimentionPanel.add(UpPanel);
|
||||||
|
DimentionPanel.add(LRPanel);
|
||||||
|
DimentionPanel.add(DownPanel);
|
||||||
|
add(DimentionPanel);
|
||||||
|
|
||||||
|
//нажатие кнопки
|
||||||
|
CreatePanel.setLayout(new FlowLayout());
|
||||||
|
CreatePanel.setBackground(new Color(0,0,0,0));
|
||||||
|
CreatePanel.add(ButtonCreate);
|
||||||
|
ButtonCreate.addActionListener(e->{
|
||||||
|
field.CreateButtonAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
BottomPanel.setLayout(new FlowLayout());
|
||||||
|
BottomPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
|
||||||
|
BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS));
|
||||||
|
BottomAndCreatePanel.setBackground(new Color(0,0,0,0));
|
||||||
|
BottomAndCreatePanel.add(CreatePanel);
|
||||||
|
BottomAndCreatePanel.add(BottomPanel);
|
||||||
|
|
||||||
|
add(BottomAndCreatePanel);
|
||||||
|
add(field);
|
||||||
|
|
||||||
|
}
|
||||||
|
public void RefreshWindow(){
|
||||||
|
field.setBounds(0,0,Width,Height);
|
||||||
|
BottomAndCreatePanel.setBounds(-320,Height-110,Width,80);
|
||||||
|
DimentionPanel.setBounds(Width-170,Height-170,190,140);
|
||||||
|
}
|
||||||
|
}
|
5
src/Main.java
Normal file
5
src/Main.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new FormBoat();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user