Compare commits

...

2 Commits
main ... Lab1

Author SHA1 Message Date
gg12 darfren
523fbaee82 Исправил ошибку 2023-10-12 20:51:36 +04:00
gg12 darfren
8b768049a0 Done 2023-10-04 11:37:04 +04:00
8 changed files with 500 additions and 22 deletions

47
.gitignore vendored
View File

@ -1,26 +1,29 @@
# ---> Java
# Compiled class file
*.class
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
# Log file
*.log
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
# BlueJ files
*.ctxt
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,9 @@
package MonorailHard;
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,235 @@
package MonorailHard;
import javax.swing.*;
import javax.swing.text.html.parser.Entity;
import java.awt.*;
import java.util.Random;
public class DrawningMonorail {
JPanel MonorailPanel;
private EntityMonorail EntityMonorail;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX = 0;
private int _startPosY = 0;
private int _monorailWidth = 133;
private int _monorailHeight = 50;
private DrawningWheels DrawningWheels;
protected int wheelSz;
public EntityMonorail EntityMonorail(){
return EntityMonorail;
}
public boolean Init(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb,
int width, int height, boolean secondCabine, boolean magniteRail, JPanel monorailPanel){
if(width <= _monorailWidth || height <= _monorailHeight)
return false;
_startPosY=0;
_startPosX = 0;
monorailPanel.setSize(width, height);
MonorailPanel = monorailPanel;
monorailPanel.paint(MonorailPanel.getGraphics());
wheelSz = _monorailHeight - _monorailHeight * 7 / 10;
_pictureWidth = width;
_pictureHeight = height;
EntityMonorail = new EntityMonorail();
EntityMonorail.Init(speed, weight, bodyColor, wheelColor, tireColor, wheelNumb, secondCabine, magniteRail);
int dif = _monorailWidth / 10;
DrawningWheels = new DrawningWheels();
DrawningWheels.Init(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel);
Random rand = new Random();
DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6));
return true;
}
public void SetPosition(int x, int y){
if(EntityMonorail == null)
return;
_startPosX = x;
_startPosY = y;
if(x + _monorailWidth <= _pictureWidth|| y + _monorailHeight <= _pictureHeight){
_startPosX = 0;
_startPosY = 0;
}
}
public void MoveTransport(DirectionType direction){
if (EntityMonorail == null)
return;
MonorailPanel.paint(MonorailPanel.getGraphics());
switch (direction)
{
case Left:
if (_startPosX - EntityMonorail.Step() >= 0)
_startPosX -= (int)EntityMonorail.Step();
else
_startPosX = 0;
break;
case Up:
if (_startPosY - EntityMonorail.Step() >= 0)
_startPosY -= (int)EntityMonorail.Step();
else
_startPosY = 0;
break;
case Right:
if (_startPosX + EntityMonorail.Step() + _monorailWidth < _pictureWidth)
_startPosX += (int)EntityMonorail.Step();
else
_startPosX = _pictureWidth - _monorailWidth;
break;
case Down:
if (_startPosY + EntityMonorail.Step() + _monorailHeight < _pictureHeight)
_startPosY += (int)EntityMonorail.Step();
else
_startPosY = _pictureHeight - _monorailHeight;
break;
}
DrawningWheels.CurX = _startPosX;
DrawningWheels.CurY = _startPosY;
}
public void DrawMonorail(){
Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics();
if (EntityMonorail == null)
return;
int dif = _monorailWidth / 10;
_monorailWidth -= dif;
if (_monorailWidth - _monorailWidth / 20 * 17 < wheelSz)
wheelSz = _monorailWidth - _monorailWidth / 20 * 17;
g2d.setColor(Color.BLACK);
//нижняя часть локомотива
int[] xPointsArrLow = { _startPosX + _monorailWidth / 10 * 4, _startPosX + _monorailWidth / 10,
_startPosX + _monorailWidth / 10, _startPosX + _monorailWidth / 20 * 19,
_startPosX + _monorailWidth / 20 * 19, _startPosX + _monorailWidth / 10 * 5,
_startPosX + _monorailWidth / 10 * 4 };
int[] yPointsArrLow = {_startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 5 * 2,
_startPosY + _monorailHeight / 10 * 7, _startPosY + _monorailHeight / 10 * 7,
_startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 5 * 2,
_startPosY + _monorailHeight / 5 * 2};
g2d.setColor(EntityMonorail.BodyColor());
g2d.fillPolygon(xPointsArrLow, yPointsArrLow, xPointsArrLow.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrLow, yPointsArrLow, xPointsArrLow.length);
//крыша локомотива
int[] xPointsArrRoof = {_startPosX + _monorailWidth / 10, _startPosX + _monorailWidth / 10 * 2,
_startPosX + _monorailWidth /20 * 19, _startPosX + _monorailWidth /20 * 19,
_startPosX + _monorailWidth / 10};
int[] yPointsArrRoof = { _startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 10,
_startPosY + _monorailHeight / 10, _startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 5 * 2};
g2d.setColor(EntityMonorail.BodyColor());
g2d.fillPolygon(xPointsArrRoof, yPointsArrRoof, xPointsArrRoof.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrRoof, yPointsArrRoof, xPointsArrRoof.length);
//дверь локомотива
int[] xPointsArrDoor = { _startPosX + _monorailWidth / 10 * 4, _startPosX + _monorailWidth / 10 * 4,
_startPosX + _monorailWidth / 10 * 5, _startPosX + _monorailWidth / 10 * 5,
_startPosX + _monorailWidth / 10 * 4 };
int[] yPointsArrDoor = { _startPosY + _monorailHeight / 5 * 2, _startPosY + _monorailHeight / 5,
_startPosY + _monorailHeight / 5, _startPosY + _monorailHeight / 5 * 3,
_startPosY + _monorailHeight / 5 * 3 };
g2d.setColor(EntityMonorail.TireColor());
g2d.fillPolygon(xPointsArrDoor, yPointsArrDoor, xPointsArrDoor.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPointsArrDoor, yPointsArrDoor, xPointsArrDoor.length);
//передняя часть тележки
int[] xPointsArrFrontCart = { _startPosX + _monorailWidth / 10 * 4, _startPosX + _monorailWidth / 10 * 2,
_startPosX, _startPosX + _monorailWidth / 10 * 4,
_startPosX + _monorailWidth / 10 * 4};
int[] yPointsArrFrontCart = { _startPosY + _monorailHeight / 10 * 7, _startPosY + _monorailHeight / 10 * 7,
_startPosY + _monorailHeight / 10 * 9, _startPosY + _monorailHeight / 10 * 9,
_startPosY + _monorailHeight / 10 * 7};
g2d.setColor(Color.BLACK);
g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length);
//задняя часть тележки
int[] xPointsArrBackCart = {_startPosX + _monorailWidth / 10 * 6, _startPosX + _monorailWidth / 10 * 9,
_startPosX + _monorailWidth, _startPosX + _monorailWidth / 10 * 6};
int[] yPointsArrBackCart = { _startPosY + _monorailHeight / 10 * 7, _startPosY + _monorailHeight / 10 * 7,
_startPosY + _monorailHeight / 10 * 9, _startPosY + _monorailHeight / 10 * 9};
g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length);
//левое окно
Rectangle leftRect = new Rectangle();
leftRect.x = _startPosX + _monorailWidth / 10 * 2;
leftRect.y = _startPosY + _monorailHeight / 25 * 4;
leftRect.width = _monorailWidth / 120 * 8;
leftRect.height = _monorailHeight/ 50 * 10;
g2d.setColor(Color.WHITE);
g2d.fillRect(leftRect.x, leftRect.y, leftRect.width, leftRect.height);
g2d.setColor(Color.BLUE);
g2d.drawRect(leftRect.x, leftRect.y, leftRect.width, leftRect.height);
//среднее окно
Rectangle midRect = new Rectangle();
midRect.x= _startPosX + _monorailWidth / 10 * 3;
midRect.y = _startPosY + _monorailHeight / 25 * 4;
midRect.width = _monorailWidth / 120 * 8;
midRect.height = _monorailHeight / 50 * 10;
g2d.setColor(Color.WHITE);
g2d.fillRect(midRect.x, midRect.y, midRect.width, midRect.height);
g2d.setColor(Color.BLUE);
g2d.drawRect(midRect.x, midRect.y, midRect.width, midRect.height);
//правое окно
Rectangle rightRect = new Rectangle();
rightRect.x = _startPosX + _monorailWidth / 20 * 17;
rightRect.y= _startPosY + _monorailHeight / 25 * 4;
rightRect.width = _monorailWidth / 120 * 8;
rightRect.height = _monorailHeight / 50 * 10;
g2d.setColor(Color.WHITE);
g2d.fillRect(rightRect.x, rightRect.y, rightRect.width, rightRect.height);
g2d.setColor(Color.BLUE);
g2d.drawRect(rightRect.x, rightRect.y, rightRect.width, rightRect.height);
//вторая кабина
if (EntityMonorail.SecondCabine())
{
int[] pointsSecondCabineX = { _startPosX + _monorailWidth / 20 * 19,
_startPosX + _monorailWidth + dif,
_startPosX + _monorailWidth + dif,
_startPosX + _monorailWidth / 20 * 19};
int[] pointsSecondCabineY = { _startPosY + _monorailHeight / 10,
_startPosY + _monorailHeight / 5 * 2,
_startPosY + _monorailHeight / 10 * 7,
_startPosY + _monorailHeight / 10 * 7};
g2d.setColor(EntityMonorail.BodyColor());
g2d.fillPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length);
Rectangle Rect = new Rectangle();
Rect.x = _startPosX + _monorailWidth / 20 * 19;
Rect.y = _startPosY + _monorailHeight / 25 * 4 + _monorailHeight / 50 * 3;
Rect.width = _monorailWidth / 120 * 6;
Rect.height = _monorailHeight / 50 * 7;
g2d.setColor(Color.WHITE);
g2d.fillRect(Rect.x, Rect.y, Rect.width, Rect.height);
g2d.setColor(Color.BLUE);
g2d.drawRect(Rect.x, Rect.y, Rect.width, Rect.height);
}
DrawningWheels.DrawWheels();
_monorailWidth+=dif;
//магнитная линия
if (EntityMonorail.MagniteRail())
{
g2d.setColor(Color.BLACK);
g2d.drawLine(_startPosX, _startPosY + _monorailHeight, _startPosX + _monorailWidth, _startPosY + _monorailHeight);
}
}
}

View File

@ -0,0 +1,73 @@
package MonorailHard;
import javax.swing.*;
import java.awt.*;
public class DrawningWheels {
private int WheelSz;
JPanel MonorailPanel;
private NumberType WheelsNumb;
private Color WheelColor, TireColor;
private int Width, Height;
public int CurX, CurY;
public int WheelSz(){
return WheelSz;
}
boolean Init(int width, int height, int curX, int curY, Color wheelColor, Color tireColor, JPanel monorailPanel){
Width = width;
Height = height;
CurX = curX;
CurY = curY;
WheelColor = wheelColor;
TireColor = tireColor;
WheelSz = Height - Height * 7 / 10;
MonorailPanel = monorailPanel;
return true;
}
public void ChangeWheelsNumb(int x){
if(x <= 2)
WheelsNumb = NumberType.Two;
if(x == 3)
WheelsNumb = NumberType.Three;
if(x >= 4)
WheelsNumb = NumberType.Four;
}
public NumberType WheelsNumb(){
return WheelsNumb;
}
public void DrawWheels(){
Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics();
g2d.setColor(WheelColor);
g2d.fillOval( CurX + Width / 10, CurY + Height / 10 * 7, WheelSz, WheelSz);
g2d.setColor(TireColor);
g2d.drawOval(CurX + Width / 10, CurY + Height / 10 * 7, WheelSz, WheelSz);
g2d.setColor(WheelColor);
g2d.fillOval(CurX + Width / 10 * 8, CurY + Height / 10 * 7, WheelSz, WheelSz);
g2d.setColor(TireColor);
g2d.drawOval(CurX + Width / 10 * 8, CurY + Height / 10 * 7, WheelSz, WheelSz);
//3 колеса
if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four)
{
g2d.setColor(WheelColor);
g2d.fillOval(CurX + Width / 10 * 6, CurY + Height / 10 * 7, WheelSz, WheelSz);
g2d.setColor(TireColor);
g2d.drawOval(CurX + Width / 10 * 6, CurY + Height / 10 * 7, WheelSz, WheelSz);
}
//4 колеса
if (WheelsNumb == NumberType.Four)
{
g2d.setColor(WheelColor);
g2d.fillOval(CurX + Width / 10 * 3, CurY + Height / 10 * 7, WheelSz, WheelSz);
g2d.setColor(TireColor);
g2d.drawOval(CurX + Width / 10 * 3, CurY + Height / 10 * 7, WheelSz, WheelSz);
}
}
}

View File

@ -0,0 +1,47 @@
package MonorailHard;
import java.awt.*;
public class EntityMonorail {
private int Speed, WheelNumb;
private double Weight, Step;
private Color BodyColor, WheelColor, TireColor;
private boolean MagniteRail;
private boolean SecondCabine;
public int Speed(){
return Speed;
}
public int WheelNumb(){
return WheelNumb;
}
public double Weight(){
return Weight;
}
public double Step(){
return Step;
}
public Color BodyColor(){
return BodyColor;
}
public Color WheelColor(){
return WheelColor;
}
public Color TireColor(){
return TireColor;
}
public boolean SecondCabine(){return SecondCabine;}
public boolean MagniteRail(){return MagniteRail;}
public void Init(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb,
boolean secondCabine, boolean magniteRail){
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
WheelColor = wheelColor;
TireColor = tireColor;
WheelNumb = wheelNumb;
SecondCabine = secondCabine;
MagniteRail = magniteRail;
}
}

View File

@ -0,0 +1,96 @@
package MonorailHard;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
public static void main(String[] args) throws IOException {
JFrame MonorailFrame = new JFrame();
JPanel MonorailPanel = new JPanel();
MonorailFrame.setLayout(new BorderLayout());
MonorailFrame.setSize(900, 500);
MonorailFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MonorailFrame.setLayout(new BorderLayout(1,1));
DrawningMonorail DrawningMonorail = new DrawningMonorail();
MonorailPanel.setLayout(null);
BufferedImage RightIcon = ImageIO.read(new File("RightButton.png"));
BufferedImage LeftIcon = ImageIO.read(new File("LeftButton.png"));
BufferedImage UpIcon = ImageIO.read(new File("UpButton.png"));
BufferedImage DownIcon = ImageIO.read(new File("DownButton.png"));
JButton RightButton = new JButton(new ImageIcon(RightIcon));
JButton LeftButton = new JButton(new ImageIcon(LeftIcon));
JButton UpButton = new JButton(new ImageIcon(UpIcon));
JButton DownButton = new JButton(new ImageIcon(DownIcon));
JButton CreateButton = new JButton();
CreateButton.setText("Создать");
CreateButton.setBounds(12, 401, 90, 40);
RightButton.setBounds(840,411,30,30);
LeftButton.setBounds(768,411,30,30);
UpButton.setBounds(804,375,30,30);
DownButton.setBounds(804,411,30,30);
MonorailPanel.add(CreateButton);
MonorailPanel.add(RightButton);
MonorailPanel.add(LeftButton);
MonorailPanel.add(UpButton);
MonorailPanel.add(DownButton);
MonorailFrame.add(MonorailPanel, BorderLayout.CENTER);
Random random = new Random();
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningMonorail.Init(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
random.nextInt(2, 5),
MonorailPanel.getWidth(), MonorailPanel.getHeight(), random.nextBoolean(), random.nextBoolean(), MonorailPanel);
DrawningMonorail.DrawMonorail();
}
});
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningMonorail.EntityMonorail() == null)
return;
DrawningMonorail.MoveTransport(DirectionType.Right);
DrawningMonorail.DrawMonorail();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningMonorail.EntityMonorail() == null)
return;
DrawningMonorail.MoveTransport(DirectionType.Left);
DrawningMonorail.DrawMonorail();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningMonorail.EntityMonorail() == null)
return;
DrawningMonorail.MoveTransport(DirectionType.Up);
DrawningMonorail.DrawMonorail();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningMonorail.EntityMonorail() == null)
return;
DrawningMonorail.MoveTransport(DirectionType.Down);
DrawningMonorail.DrawMonorail();
}
});
MonorailFrame.setVisible(true);
}
}

View File

@ -0,0 +1,7 @@
package MonorailHard;
public enum NumberType {
Two,
Three,
Four
}