Laba1 KozyrevSS GasolineTanker JAVA HARD #1

Closed
Serxionaft wants to merge 1 commits from Laba1 into main
11 changed files with 359 additions and 0 deletions
Showing only changes of commit c6fc3c0b37 - Show all commits

BIN
Arrows/Down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 810 B

BIN
Arrows/Left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 802 B

BIN
Arrows/Right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 817 B

BIN
Arrows/Up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 821 B

79
BaseCar.java Normal file
View File

@ -0,0 +1,79 @@
import java.awt.*;
public class BaseCar {
private int Speed;
public int getSpeed() {
return Speed;
}
private void setSpeed(int speed)
{
Speed = speed;
}
private double Weight;
public double getWeight()
{
return Weight;
}
private void setWeight(double weight)
{
Weight = weight;
}
private Color BodyColor;
public Color getBodyColor()
{
return BodyColor;
}
private void setBodyColor(Color bodyColor)
{
BodyColor = bodyColor;
}
private Color AdditionalColor;
public Color getAdditionalColor()
{
return AdditionalColor;
}
private void setAdditionalColor(Color additionalColor)
{
AdditionalColor = additionalColor;
}
private boolean BodyKit;
public boolean getBodyKit()
{
return BodyKit;
}
private void setBodyKit(boolean bodyKit)
{
BodyKit = bodyKit;
}
private boolean Wing;
public boolean getWing()
{
return Wing;
}
private void setWing(boolean wing)
{
Wing = wing;
}
private boolean SportLine;
public boolean getSportLine()
{
return SportLine;
}
private void setSportLine(boolean sportLine)
{
SportLine = sportLine;
}
public double Step;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
BodyKit = bodyKit;
Wing = wing;
SportLine = sportLine;
Step = (double)Speed * 100 / Weight;
}
}

16
Direction.java Normal file
View File

@ -0,0 +1,16 @@
public enum Direction {
Up ("U"),
Down ("D"),
Left ("L"),
Right ("R");
private String direct;
Direction(String d)
{
direct = d;
}
public String getDirect()
{
return direct;
}
}

125
DrawFrame.java Normal file
View File

@ -0,0 +1,125 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
class DrawGasoline extends JComponent {
private DrawingTanker _drawingTanker;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (_drawingTanker == null)
return;
_drawingTanker.DrawTransport(g);
super.repaint();
}
protected void CreateCarButton_Click()
{
Random rnd = new Random();
_drawingTanker = new DrawingTanker();
Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
_drawingTanker.Init(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000),
bodyColor, addColor, IntToBool(rnd.nextInt(0, 2)), IntToBool(rnd.nextInt(0, 2)),
IntToBool(rnd.nextInt(0, 2)), Frame.Width, Frame.Height, rnd.nextInt(0, 30));
_drawingTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
super.repaint();
}
protected void ButtonMove_Click(String tag)
{
if (_drawingTanker == null)
return;
switch (tag) {
case "U" -> _drawingTanker.MoveTransport(Direction.Up);
case "D" -> _drawingTanker.MoveTransport(Direction.Down);
case "L" -> _drawingTanker.MoveTransport(Direction.Left);
case "R" -> _drawingTanker.MoveTransport(Direction.Right);
}
super.repaint();
}
private boolean IntToBool(int n)
{
return n > 0;
}
}
class Frame extends JFrame {
public Frame()
{
initUI();
}
protected static final int Width = 1000;
protected static final int Height = 600;
DrawGasoline Gasoline;
private void initUI()
{
setSize(Width, Height);
setTitle("TankGasoline");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
Gasoline = new DrawGasoline();
Gasoline.setBounds(0, 0, Width, Height);
add(Gasoline);
MoveAL moving = new MoveAL();
JButton buttonUp = new JButton();
buttonUp.setActionCommand("U");
buttonUp.setBounds(Width - 90, Height - 120,30,30);
buttonUp.setLayout(null);
add(buttonUp);
buttonUp.addActionListener(moving);
JButton buttonRight = new JButton();
buttonRight.setActionCommand("R");
buttonRight.setBounds(Width - 60, Height - 90,30,30);
buttonRight.setLayout(null);
add(buttonRight);
buttonRight.addActionListener(moving);
JButton buttonLeft = new JButton();
buttonLeft.setActionCommand("L");
buttonLeft.setBounds(Width - 120, Height - 90,30,30);
buttonLeft.setLayout(null);
add(buttonLeft);
buttonLeft.addActionListener(moving);
JButton buttonDown = new JButton();
buttonDown.setActionCommand("D");
buttonDown.setBounds(Width - 90, Height - 90,30,30);
buttonDown.setLayout(null);
add(buttonDown);
buttonDown.addActionListener(moving);
buttonUp.setIcon(new ImageIcon("Arrows/Up.png"));
buttonDown.setIcon(new ImageIcon("Arrows/Down.png"));
buttonLeft.setIcon(new ImageIcon("Arrows/Left.png"));
buttonRight.setIcon(new ImageIcon("Arrows/Right.png"));
JButton buttonCreate = new JButton("Create");
buttonCreate.setBounds(60, Height - 120, 120, 50);
buttonCreate.setLayout(null);
add(buttonCreate);
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Gasoline.CreateCarButton_Click();
}
});
}
public class MoveAL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Gasoline.ButtonMove_Click(e.getActionCommand());
}
}
}

89
DrawingTanker.java Normal file
View File

@ -0,0 +1,89 @@
import java.awt.*;
public class DrawingTanker {
private BaseCar GasolineTanker;
private WheelsDrawing wheelsDrawing;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int _carWidth = 120;
private final int _carHeight = 120;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height, int wheelCount)
{
_pictureHeight = height;
_pictureWidth = width;
GasolineTanker = new BaseCar();
GasolineTanker.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
wheelsDrawing = new WheelsDrawing();
wheelsDrawing.setWheelCount(wheelCount);
}
public void SetPosition(int x, int y)
{
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction)
{
if (GasolineTanker == null)
return;
switch (direction)
{
case Left -> {
if (_startPosX - GasolineTanker.Step > 0)
{
_startPosX -= (int)GasolineTanker.Step;
}
}
case Up -> {
if (_startPosY - GasolineTanker.Step > 0)
{
_startPosY -= (int)GasolineTanker.Step;
}
}
case Right -> {
if (_startPosX + _carWidth + GasolineTanker.Step < _pictureWidth)
{
_startPosX += (int)GasolineTanker.Step;
}
}
case Down -> {
if (_startPosY + GasolineTanker.Step + _carHeight < _pictureHeight)
{
_startPosY += (int)GasolineTanker.Step;
}
}
}
}
public void DrawTransport(Graphics g)
{
if (GasolineTanker == null)
return;
var g2d = (Graphics2D) g;
if (GasolineTanker.getBodyKit())
{
g2d.setColor(GasolineTanker.getAdditionalColor());
g2d.fillOval(10 + _startPosX, 10 + _startPosY, 70, 30);
}
g2d.setColor(GasolineTanker.getBodyColor());
// Отрисовка корпуса
g2d.fillRect(10 + _startPosX, 40 + _startPosY, 90, 20);
g2d.fillRect(80 + _startPosX, 10 + _startPosY, 20, 40);
// Отрисовка колесиков
wheelsDrawing.DrawWheels(_startPosX, _startPosY, GasolineTanker.getBodyColor(), g2d);
if (GasolineTanker.getSportLine())
{
g2d.setColor(GasolineTanker.getAdditionalColor());
g2d.fillRect( 15 + _startPosX, 45 + _startPosY, 20, 5);
g2d.fillRect(40 + _startPosX, 45 + _startPosY, 20, 5);
g2d.fillRect(65 + _startPosX, 45 + _startPosY, 20, 5);
}
if (GasolineTanker.getWing())
{
g2d.setColor(GasolineTanker.getAdditionalColor());
g2d.fillRect(87 + _startPosX, 5 + _startPosY, 5, 5);
}
}
}

6
Main.java Normal file
View File

@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
Frame a = new Frame();
a.setVisible(true);
}
}

14
WheelCounter.java Normal file
View File

@ -0,0 +1,14 @@
public enum WheelCounter {
TWO (2),
THREE (3),
FOUR (4);
private int count;
WheelCounter(int c)
{
count = c;
}
public int getCount()
{
return count;
}
}

30
WheelsDrawing.java Normal file
View File

@ -0,0 +1,30 @@
import java.awt.*;
public class WheelsDrawing {
private WheelCounter wheelCounter;
public WheelCounter getWheelCounter()
{
return wheelCounter;
}
public void setWheelCount(int count)
{
if (count % 3 == 0)
wheelCounter = WheelCounter.THREE;
else if (count % 3 == 1)
wheelCounter = WheelCounter.FOUR;
else if (count % 3 == 2)
wheelCounter = WheelCounter.TWO;
}
public void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d)
{
g2d.setColor(bodyColor);
g2d.fillOval(10 + _startPosX, 60 + _startPosY, 20, 20);
g2d.fillOval(80 + _startPosX, 60 + _startPosY, 20, 20);
switch (wheelCounter)
{
case THREE -> {g2d.fillOval(45 + _startPosX, 60 + _startPosY ,20, 20);}
case FOUR -> {g2d.fillOval(30 + _startPosX, 60 + _startPosY ,20, 20); g2d.fillOval(60 + _startPosX, 60 + _startPosY ,20, 20);}
}
}
}