This commit is contained in:
Arkadiy Radaev 2023-12-01 20:37:58 +04:00
parent 29ea501584
commit cbfbf06e0a
7 changed files with 351 additions and 0 deletions

6
DirectionType.java Normal file
View File

@ -0,0 +1,6 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

125
DrawningCatamaran.java Normal file
View File

@ -0,0 +1,125 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningCatamaran {
JPanel CatamaranPanel;
private EntityCatamaran EntityCatamaran;
private DrawningOars drawningOars;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX = 0;
private int _startPosY = 0;
private int _catamaranWidth = 120;
private int _catamaranHeight = 120;
public EntityCatamaran EntityCatamaran(){
return EntityCatamaran;
}
public boolean Init(int speed, double weight, Color bodyColor, Color oarColor, int width, int height, boolean boduKit, boolean seats, JPanel catamaranPanel){
if(width <= _catamaranWidth || height <= _catamaranHeight)
return false;
Random rand = new Random();
catamaranPanel.setSize(width, height);
CatamaranPanel = catamaranPanel;
catamaranPanel.paint(CatamaranPanel.getGraphics());
_pictureWidth = width;
_pictureHeight = height;
EntityCatamaran = new EntityCatamaran();
EntityCatamaran.Init(speed, weight, bodyColor, oarColor, boduKit, seats);
SetPosition(rand.nextInt(50), rand.nextInt(50));
drawningOars =new DrawningOars();
drawningOars.Init(_startPosX, _startPosY, oarColor, catamaranPanel);
drawningOars.ChangeOarsNumb(rand.nextInt(3));
return true;
}
public void SetPosition(int x, int y){
if(EntityCatamaran == null)
return;
_startPosX = x;
_startPosY = y;
if(x + _catamaranWidth >= _pictureWidth || y + _catamaranHeight >= _pictureHeight){
_startPosX = 0;
_startPosY = 0;
}
}
public void MoveTransport(DirectionType direction){
if (EntityCatamaran == null)
return;
CatamaranPanel.paint(CatamaranPanel.getGraphics());
switch (direction)
{
case Left:
if (_startPosX - EntityCatamaran.Step() >= 0)
_startPosX -= (int)EntityCatamaran.Step();
else
_startPosX = 0;
break;
case Up:
if (_startPosY - EntityCatamaran.Step() >= 0)
_startPosY -= (int)EntityCatamaran.Step();
else
_startPosY = 0;
break;
case Right:
if (_startPosX + _catamaranWidth + EntityCatamaran.Step() < _pictureWidth)
_startPosX += (int)EntityCatamaran.Step();
else
_startPosX = _pictureWidth - _catamaranWidth - 20;
break;
case Down:
if (_startPosY + _catamaranHeight + EntityCatamaran.Step() < _pictureHeight)
_startPosY += (int)EntityCatamaran.Step();
else
_startPosY = _pictureHeight - _catamaranHeight;
break;
}
drawningOars.OarPosX = _startPosX;
drawningOars.OarPosY = _startPosY;
}
public void DrawnCatamaran(){
Graphics2D g2d = (Graphics2D)CatamaranPanel.getGraphics();
if (EntityCatamaran == null) return;
g2d.setColor(EntityCatamaran.BodyColor());
// korpus
g2d.fillRect(_startPosX+10, _startPosY+10, _catamaranWidth - 50, 60);
// nos
int[] x_triag = {_startPosX + _catamaranWidth - 40,_startPosX + _catamaranWidth,_startPosX + _catamaranWidth - 40};
int[] y_triag = { _startPosY+10, _startPosY + 40,_startPosY + 70};
g2d.fillPolygon(x_triag,y_triag,3);
// poplav
g2d.setColor(Color.DARK_GRAY);
int[] poplav1_x = {_startPosX + _catamaranWidth -20,_startPosX + _catamaranWidth,_startPosX+_catamaranWidth - 20};
int[] poplav1_y = {_startPosY,_startPosY + 5,_startPosY + 10};
g2d.fillPolygon(poplav1_x,poplav1_y,3);
int[] poplav2_x = {_startPosX + _catamaranWidth -20,_startPosX + _catamaranWidth,_startPosX+_catamaranWidth - 20};
int[] poplav2_y = {_startPosY+70,_startPosY + 75,_startPosY + 80};
g2d.fillPolygon(poplav2_x,poplav2_y,3);
g2d.fillRect(_startPosX, _startPosY, _catamaranWidth - 20, 10);
g2d.fillRect( _startPosX, _startPosY + 70, _catamaranWidth - 20, 10);
//
g2d.setColor(Color.BLACK);
g2d.drawPolygon(poplav1_x,poplav1_y,3);
g2d.drawPolygon(poplav2_x,poplav2_y,3);
g2d.drawPolygon(x_triag,y_triag,3);
g2d.drawRect(_startPosX+10, _startPosY+10, _catamaranWidth - 50, 60);
g2d.drawRect(_startPosX, _startPosY, _catamaranWidth - 20, 10);
g2d.drawRect( _startPosX, _startPosY + 70, _catamaranWidth - 20, 10);
// sedenie
g2d.setColor(Color.CYAN);
g2d.fillOval(_startPosX + 15, _startPosY + 25, 60, 30);
if (EntityCatamaran.Seats())
{
g2d.setColor(Color.ORANGE);
g2d.fillOval(_startPosX + 20, _startPosY + 35, 10, 10);
g2d.fillOval(_startPosX + 35, _startPosY + 35, 10, 10);
g2d.fillOval(_startPosX + 50, _startPosY + 35, 10, 10);
}
drawningOars.DrawOars();
}
}

86
DrawningOars.java Normal file
View File

@ -0,0 +1,86 @@
import javax.swing.*;
import java.awt.*;
public class DrawningOars {
private NumberType OarsNumb;
public int OarPosX, OarPosY;
JPanel CatamaranPanel;
private Color OarsColor;
// vesla Oars
boolean Init(int oarPosX,int oarPosY, Color oarsColor, JPanel catamaranPanel){
OarPosX = oarPosX;
OarPosY = oarPosY;
OarsColor = oarsColor;
CatamaranPanel = catamaranPanel;
return true;
}
public void ChangeOarsNumb(int x){
if(x<=1)
OarsNumb = NumberType.One;
if(x == 2)
OarsNumb = NumberType.Two;
if(x >= 3)
OarsNumb = NumberType.Three;
}
public NumberType OarsNumb(){
return OarsNumb;
}
public void DrawOars(){
Graphics2D g2d = (Graphics2D)CatamaranPanel.getGraphics();
g2d.setColor(OarsColor);
int ind = 0;
// 1 veslo
int[] x_lop1 = {OarPosX+ind,OarPosX+15+ind,OarPosX+25+ind,OarPosX+10+ind};
int[] y_lop1 = {OarPosY+5,OarPosY,OarPosY+10,OarPosY+15};
int[] x_oar1 = {OarPosX+10+ind,OarPosX+20+ind,OarPosX+36+ind,OarPosX+26+ind};
int[] y_oar1 = {OarPosY+10,OarPosY+10,OarPosY+26,OarPosY+26};
g2d.fillPolygon(x_oar1,y_oar1,4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar1,y_oar1,4);
g2d.setColor(OarsColor);
g2d.fillPolygon(x_lop1,y_lop1,4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_lop1,y_lop1,4);
//2 vesla
if (OarsNumb == NumberType.Two || OarsNumb == NumberType.Three)
{
ind = 30;
g2d.setColor(OarsColor);
int[] x_lop2 = {OarPosX+ind,OarPosX+15+ind,OarPosX+25+ind,OarPosX+10+ind};
int[] y_lop2 = {OarPosY+5,OarPosY,OarPosY+10,OarPosY+15};
int[] x_oar2 = {OarPosX+10+ind,OarPosX+20+ind,OarPosX+36+ind,OarPosX+26+ind};
int[] y_oar2 = {OarPosY+10,OarPosY+10,OarPosY+26,OarPosY+26};
g2d.fillPolygon(x_oar2,y_oar2,4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar2,y_oar2,4);
g2d.setColor(OarsColor);
g2d.fillPolygon(x_lop2,y_lop2,4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_lop2,y_lop2,4);
}
//3 vesla
if (OarsNumb == NumberType.Three)
{
ind = 0;
g2d.setColor(OarsColor);
int[] x_oar3 = {OarPosX+36+ind,OarPosX+26+ind,OarPosX+10+ind,OarPosX+20+ind};
int[] y_oar3 = {OarPosY+55,OarPosY+55,OarPosY+71,OarPosY+71};
int[] y_lop3 = {OarPosY+71,OarPosY+80,OarPosY+76,OarPosY+67};
int[] x_lop3 = {OarPosX+24+ind,OarPosX+16+ind,OarPosX+3+ind,OarPosX+10+ind};
g2d.fillPolygon(x_oar3,y_oar3,4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar3,y_oar3,4);
g2d.setColor(OarsColor);
g2d.fillPolygon(x_lop3,y_lop3,4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_lop3,y_lop3,4);
}
}
}

28
EntityCatamaran.java Normal file
View File

@ -0,0 +1,28 @@
import java.awt.Color;
public class EntityCatamaran {
private int Speed;
private double Weight, Step;
private Color BodyColor;
private Color OarColor;
private boolean Seats;
private boolean BodyKit;
public int Speed(){return Speed;}
public double Weight(){return Weight;}
public double Step(){return Step;}
public Color BodyColor(){return BodyColor;}
public Color OarColor(){return OarColor;}
public boolean BodyKit(){return BodyKit;}
public boolean Seats(){return Seats;}
public void Init(int speed, double weight, Color bodyColor,Color oarColor, boolean bodyKit, boolean seats){
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
OarColor = oarColor;
BodyKit = bodyKit;
Seats = seats;
}
}

94
FormCatamaran.java Normal file
View File

@ -0,0 +1,94 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random;
import javax.swing.*;
public class FormCatamaran extends JFrame{
private JPanel CatamaranPanel;
private JButton UpButton;
private JButton LeftButton;
private JButton RightButton;
private JButton DownButton;
private JButton CreateButton;
public FormCatamaran() throws IOException{
this.CatamaranPanel = crPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,500);
setLayout(new BorderLayout(1,1));
add(CatamaranPanel, BorderLayout.CENTER);
setTitle("Form Catamaran");
setVisible(true);
}
public JPanel crPanel()throws IOException{
JPanel panel = new JPanel();
panel.setLayout(null);
UpButton = new JButton("");
LeftButton = new JButton("");
RightButton = new JButton("");
DownButton = new JButton("");
CreateButton = new JButton("Создать");
CreateButton.setBounds(12, 401, 90, 40);
RightButton.setBounds(830,391,50,50);
LeftButton.setBounds(718,391,50,50);
UpButton.setBounds(774,335,50,50);
DownButton.setBounds(774,391,50,50);
Random random = new Random();
DrawningCatamaran catamaran = new DrawningCatamaran();
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
catamaran.Init(random.nextInt(1000),1000,Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),900,500,random.nextBoolean(),random.nextBoolean(),panel);
catamaran.DrawnCatamaran();
}
});
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(catamaran.EntityCatamaran() == null)
return;
catamaran.MoveTransport(DirectionType.Right);
catamaran.DrawnCatamaran();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(catamaran.EntityCatamaran() == null)
return;
catamaran.MoveTransport(DirectionType.Left);
catamaran.DrawnCatamaran();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(catamaran.EntityCatamaran() == null)
return;
catamaran.MoveTransport(DirectionType.Up);
catamaran.DrawnCatamaran();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(catamaran.EntityCatamaran() == null)
return;
catamaran.MoveTransport(DirectionType.Down);
catamaran.DrawnCatamaran();
}
});
panel.add(UpButton);
panel.add(LeftButton);
panel.add(RightButton);
panel.add(DownButton);
panel.add(CreateButton);
return panel;
}
}

7
Main.java Normal file
View File

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

5
NumberType.java Normal file
View File

@ -0,0 +1,5 @@
public enum NumberType {
One,
Two,
Three
}