Merge pull request 'Kashin M.I. Lab work 1' (#1) from LabWork01 into master

Reviewed-on: http://student.git.athene.tech/Sosees04ka/PIbd-22_Kashin_M.I_GasolineTanker._Hard/pulls/1
This commit is contained in:
Евгений Эгов 2022-09-30 11:16:19 +04:00
commit 0f25143372
13 changed files with 439 additions and 1 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/1 lab hard.iml" filepath="$PROJECT_DIR$/1 lab hard.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

12
src/CountWheels.java Normal file
View File

@ -0,0 +1,12 @@
public enum CountWheels {
Two(2),
Three(3),
Four(4);
private final int Value;
CountWheels(int Count){
Value=Count;
}
public int getCountWheels(){
return Value;
}
}

6
src/Direction.java Normal file
View File

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

57
src/DrawingField.java Normal file
View File

@ -0,0 +1,57 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingField extends JPanel {
private final FormGasolineTanker field;
DrawingGasolineTanker _gasolineTanker;
public DrawingField(FormGasolineTanker field) {
this.field = field;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
if (_gasolineTanker!=null)
_gasolineTanker.DrawTransport(g2);
else return;
}
public void UpButtonAction(){
if (_gasolineTanker!=null)
_gasolineTanker.MoveTransport(Direction.Up);
else
return;
}
public void DownButtonAction(){
if (_gasolineTanker!=null)
_gasolineTanker.MoveTransport(Direction.Down);
else
return;
}
public void RightButtonAction(){
if (_gasolineTanker!=null)
_gasolineTanker.MoveTransport(Direction.Right);
else
return;
}
public void LeftButtonAction(){
if (_gasolineTanker!=null)
_gasolineTanker.MoveTransport(Direction.Left);
else
return;
}
public void CreateButtonAction(){
Random rnd=new Random();
_gasolineTanker=new DrawingGasolineTanker();
_gasolineTanker.Init(rnd.nextInt(50)+10,rnd.nextInt(100)+500,new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)));
_gasolineTanker.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10,getWidth(),getHeight());
field.SpeedLabel.setText("Speed: "+_gasolineTanker.getGasolineTanker().getSpeed());
field.WeightLabel.setText("Weight: "+_gasolineTanker.getGasolineTanker().getWeight());
field.BodyColorLabel.setText("Color: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2));
}
public void ResizeField(){
if (_gasolineTanker!=null)
_gasolineTanker.ChangeBorders(getWidth(),getHeight());
else return;
}
}

View File

@ -0,0 +1,125 @@
import java.awt.*;
public class DrawingGasolineTanker {
public EntityGasolineTanker GasolineTanker;
public EntityGasolineTanker getGasolineTanker() {
return GasolineTanker;
}
public DrawingWheels Wheels;
private int _startPosX;
private int _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private final int _gasolineTankerWidth = 160;
private final int _gasolineTankerHeight = 55;
public void Init(int speed, float weight, Color bodyColor)
{
GasolineTanker = new EntityGasolineTanker();
GasolineTanker.Init(speed, weight, bodyColor);
Wheels = new DrawingWheels();
Wheels.SetCountWheels((int)(2 + Math.random() + Math.random()*2));
}
public void SetPosition(int x, int y, int width, int height)
{
if (x >= 0 && x+_gasolineTankerWidth <= width && y >= 0 && y+_gasolineTankerHeight <= height)
{
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
else return;
}
public void MoveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null)
{
return;
}
switch (direction)
{
case Right:
if (_startPosX + _gasolineTankerWidth + GasolineTanker.Step < _pictureWidth)
{
_startPosX+=GasolineTanker.Step;
}
break;
case Left:
if (_startPosX- GasolineTanker.Step >= 0)
{
_startPosX -= GasolineTanker.Step;
}
break;
case Up:
if (_startPosY - GasolineTanker.Step >= 0)
{
_startPosY -= GasolineTanker.Step;
}
break;
case Down:
if (_startPosY + _gasolineTankerHeight + GasolineTanker.Step < _pictureHeight)
{
_startPosY += GasolineTanker.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.drawOval(_startPosX + 130, _startPosY + 35, 20, 20);
g2.fillOval(_startPosX + 130, _startPosY + 35, 20, 20);
Wheels.DrawWheels(g2, _startPosX, _startPosY);
g2.setColor(Color.yellow);
g2.drawOval(_startPosX + 150, _startPosY + 30, 10, 10);
g2.fillOval(_startPosX + 150, _startPosY + 30, 10, 10);
g2.setColor(Color.RED);
g2.drawOval(_startPosX+5, _startPosY + 35, 10, 10);
g2.fillOval(_startPosX+5, _startPosY + 35, 10, 10);
g2.setColor(GasolineTanker.getBodyColor());
g2.drawRect(_startPosX + 115, _startPosY+5, 40, 40);
g2.fillRect(_startPosX + 115, _startPosY+5, 40, 40);
g2.drawRect(_startPosX + 10, _startPosY + 35, 140, 10);
g2.fillRect(_startPosX + 10, _startPosY + 35, 140, 10);
Color lightBlue = new Color(82, 186, 255);
g2.setColor(lightBlue);
g2.drawRect(_startPosX + 120, _startPosY + 10, 25, 25);
g2.fillRect(_startPosX + 120, _startPosY + 10, 25, 25);
}
public void ChangeBorders(int width,int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth<=_gasolineTankerWidth||_pictureHeight<=_gasolineTankerHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _gasolineTankerWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _gasolineTankerWidth;
}
if (_startPosY + _gasolineTankerHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _gasolineTankerHeight;
}
}
}

41
src/DrawingWheels.java Normal file
View File

@ -0,0 +1,41 @@
import java.awt.*;
public class DrawingWheels {
private CountWheels _wheels;
public void SetCountWheels(int Count){
for (CountWheels temp: CountWheels.values())
if (temp.getCountWheels() == Count){
_wheels=temp;
return;
}
}
public void DrawWheels(Graphics2D g,int _startPosX, int _startPosY) {
switch (_wheels.getCountWheels())
{
case 2:
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 10, _startPosY + 35, 20, 20);
g.fillOval(_startPosX + 10, _startPosY + 35, 20, 20);
break;
case 3:
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 10, _startPosY + 35, 20, 20);
g.fillOval(_startPosX + 10, _startPosY + 35, 20, 20);
g.drawOval(_startPosX + 30, _startPosY + 35, 20, 20);
g.fillOval(_startPosX + 30, _startPosY + 35, 20, 20);
break;
case 4:
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 10, _startPosY + 35, 20, 20);
g.fillOval(_startPosX + 10, _startPosY + 35, 20, 20);
g.drawOval(_startPosX + 30, _startPosY + 35, 20, 20);
g.fillOval(_startPosX + 30, _startPosY + 35, 20, 20);
g.drawOval(_startPosX + 50, _startPosY + 35, 20, 20);
g.fillOval(_startPosX + 50, _startPosY + 35, 20, 20);
break;
}
}
}

View File

@ -0,0 +1,28 @@
import java.awt.*;
import java.util.Random;
public class EntityGasolineTanker {
private int Speed;
public int getSpeed() {
return Speed;
}
private float Weight;
public float getWeight() {
return Weight;
}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
public float Step;
public void Init(int speed, float weight, Color bodyColor){
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50)+10 : speed;
Weight = weight <= 0 ? rnd.nextInt(100)+500 : weight;
BodyColor = bodyColor;
Step = Speed * 100/ (int)Weight;
}
}

135
src/FormGasolineTanker.java Normal file
View File

@ -0,0 +1,135 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
public class FormGasolineTanker extends JFrame{
private int Width;
private int Height;
JPanel BottomPanel = new JPanel();
JPanel CreatePanel = new JPanel();
JPanel BottomAndCreatePanel = new JPanel();
JPanel DimentionPanel = new JPanel();
JPanel UPanel = new JPanel();
JPanel DPanel = new JPanel();
JPanel LRPanel = new JPanel();
JLabel SpeedLabel = new JLabel("Speed: ");
JLabel WeightLabel = new JLabel("Weight: ");
JLabel BodyColorLabel = new JLabel("Color: ");
DrawingField field = new DrawingField(this);
JButton ButtonCreate=new JButton("Create");
Icon iconUp = new ImageIcon("C:\\Users\\kashi\\OneDrive\\Материал\\KeyUp.png");
JButton ButtonUp=new JButton(iconUp);
Icon iconDown = new ImageIcon("C:\\Users\\kashi\\OneDrive\\Материал\\KeyDown.png");
JButton ButtonDown=new JButton(iconDown);
Icon iconRight = new ImageIcon("C:\\Users\\kashi\\OneDrive\\Материал\\KeyRight.png");
JButton ButtonRight=new JButton(iconRight);
Icon iconLeft = new ImageIcon("C:\\Users\\kashi\\OneDrive\\Материал\\KeyLeft.png");
JButton ButtonLeft=new JButton(iconLeft);
public FormGasolineTanker(){
super("Gasoline Tanker");
setSize(800,600);
Width=getWidth();
Height=getHeight();
ShowWindow();
RefreshWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void ShowWindow(){
Dimension dimen=new Dimension(30,30);
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);
UPanel.setLayout(new FlowLayout());
UPanel.setBackground(new Color(0,0,0,0));
UPanel.add(ButtonUp);
DPanel.setLayout(new FlowLayout());
DPanel.setBackground(new Color(0,0,0,0));
DPanel.add(ButtonDown);
DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS));
DimentionPanel.setBackground(new Color(0,0,0,0));
DimentionPanel.add(UPanel);
DimentionPanel.add(LRPanel);
DimentionPanel.add(DPanel);
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));
BottomPanel.add(SpeedLabel);
BottomPanel.add(WeightLabel);
BottomPanel.add(BodyColorLabel);
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);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
Width=getWidth();
Height=getHeight();
field.ResizeField();
repaint();
RefreshWindow();
}
});
}
public void RefreshWindow(){
field.setBounds(0,0,Width,Height);
BottomAndCreatePanel.setBounds(-220,Height-110,Width,80);
DimentionPanel.setBounds(Width-170,Height-170,190,140);
}
}

View File

@ -1,5 +1,5 @@
public class Main {
public static void main(String[] args){
new FormGasolineTanker();
}
}