PIbd-23. Panina A. D. Lab _1_Hard #1

Closed
alyona.panina wants to merge 2 commits from laba1 into main
7 changed files with 316 additions and 0 deletions

6
Direction.java Normal file
View File

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

108
DrawningCruiser.java Normal file
View File

@ -0,0 +1,108 @@
import java.awt.*;
public class DrawningCruiser {
public EntityCruiser EntityCruiser;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
final private int _cruiserWidth = 110;
final private int _cruiserHeight = 60;
private NumberOfWheels wheels = new NumberOfWheels();
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean helicopterPad, boolean coating, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
EntityCruiser = new EntityCruiser();
EntityCruiser.Init(speed, weight, bodyColor, additionalColor, helicopterPad, coating);
return true;
}
public void SetPosition(int x, int y)
{
if (EntityCruiser == null) {
return;
}
_startPosX = x;
_startPosY = y;
if (x + _cruiserWidth <= _pictureWidth || y + _cruiserHeight <= _pictureHeight) {
_startPosX = 0;
_startPosY = 0;
}
}
public void MoveTransport(Direction direction)
{
if (EntityCruiser == null)
{
return;
}
switch (direction)
{
case Left:
if (_startPosX - EntityCruiser.Step() > 0)
{
_startPosX -= (int)EntityCruiser.Step();
}
break;
case Up:
if (_startPosY - EntityCruiser.Step() > 0)
{
_startPosY -= (int)EntityCruiser.Step();
}
break;
case Right:
if (_startPosX + EntityCruiser.Step() + _cruiserWidth < _pictureWidth)
{
_startPosX += (int)EntityCruiser.Step();
}
break;
case Down:
if (_startPosY + EntityCruiser.Step() + _cruiserHeight < _pictureHeight)
{
_startPosY += (int)EntityCruiser.Step();
}
break;
}
}
public void setWheels(int num){
wheels.setNumOfWheels(Integer.toString(num));
}
public void DrawTransport(Graphics g)
{
if (EntityCruiser == null)
{
return;
}
g.setColor(EntityCruiser.BodyColor());
g.drawRect( _startPosX + 9, _startPosY + 15, 10, 30);
g.drawRect( _startPosX + 90, _startPosY + 15, 10,
30);
g.drawRect( _startPosX + 20, _startPosY + 4, 70, 52);
g.fillRect( _startPosX + 10, _startPosY + 15, 10, 30);
g.fillRect( _startPosX + 90, _startPosY + 15, 10, 30);
g.fillRect( _startPosX + 20, _startPosY + 5, 70, 50);
Point[] points1 = new Point[3];
points1[0] = new Point(_startPosX + 100, _startPosY + 5);
points1[1] = new Point(_startPosX + 100, _startPosY + 55);
points1[2] = new Point(_startPosX + 150, _startPosY + 30);
g.fillPolygon(new int[] {points1[0].x, points1[1].x, points1[2].x},
new int[] {points1[0].y, points1[1].y, points1[2].y}, 3);
g.fillRect( _startPosX + 5, _startPosY + 15, 10, 10);
g.fillRect( _startPosX + 5, _startPosY + 35, 10, 10);
if (EntityCruiser.HelicopterPad())
{
g.setColor(EntityCruiser.AdditionalColor());
g.drawRect(_startPosX + 35,
_startPosY + 23, 15, 15);
g.drawRect(_startPosX + 50,
_startPosY + 19, 30, 25);
}
if (EntityCruiser.Coating())
{
g.setColor(EntityCruiser.AdditionalColor());
g.fillRect(_startPosX + 35,
_startPosY + 23, 15, 15);
g.fillRect(_startPosX + 50,
_startPosY + 19, 30, 25);
}
wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor());
}
}

47
EntityCruiser.java Normal file
View File

@ -0,0 +1,47 @@
import java.awt.*;
public class EntityCruiser {
private int Speed;
private double Weight;
private Color BodyColor;
private Color AdditionalColor;
private boolean HelicopterPad;
private boolean Coating;
private double Step;
public int Speed() {
return Speed;
}
public double Weight() {
return Weight;
}
public Color BodyColor() {
return BodyColor;
}
public Color AdditionalColor() {
return AdditionalColor;
}
public boolean HelicopterPad() {
return HelicopterPad;
}
public boolean Coating() {
return Coating;
}
public double Step() {
return Step;
}
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean helicopterPad, boolean coating)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
HelicopterPad = helicopterPad;
Coating = coating;
Step = Speed * 100 / Weight;
}
}

106
GameFrame.java Normal file
View File

@ -0,0 +1,106 @@
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class GameFrame extends JFrame {
GameFrame() {
this.setSize(710, 535);
this.setTitle("Cruiser");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
GamePanel panel = new GamePanel();
this.add(panel);
this.setVisible(true);
}
private boolean intToBoolean(int input) {
return input != 0;
}
public class GamePanel extends JPanel implements ActionListener {
static final int SCREEN_W = 700;
static final int SCREEN_H = 500;
int xx = 0;
int yy = 0;
private DrawningCruiser _drawningCruiser;
GamePanel() {
this.setLayout(null);
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
GridBagConstraints layers = new GridBagConstraints();
JButton button = new JButton("Создать") ;
button.setBounds(20, 390, 90, 30);
this.add(button);
button.addActionListener(this);
JPanel panel = new JPanel(new GridBagLayout());
JButton up = new BasicArrowButton(BasicArrowButton.NORTH);
up.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Up);
repaint();
}
});
JButton left = new BasicArrowButton(BasicArrowButton.WEST);
left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Left);
repaint();
}
});
JButton down = new BasicArrowButton(BasicArrowButton.SOUTH);
down.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Down);
repaint();
}
});
JButton right = new BasicArrowButton(BasicArrowButton.EAST);
right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Right);
repaint();
}
});
up.setBounds(570, 350, 30, 30);
this.add(up);
down.setBounds(570, 350 + 40, 30, 30);
this.add(down);
left.setBounds(570 - 40, 350 + 40, 30, 30);
this.add(left);
right.setBounds(570 + 40, 350 + 40, 30, 30);
this.add(right);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g){
if (_drawningCruiser != null){
_drawningCruiser.DrawTransport(g);
}
}
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_drawningCruiser = new DrawningCruiser();
_drawningCruiser.Init(random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
intToBoolean(random.nextInt(0, 2)),
intToBoolean(random.nextInt(0, 2)),
GamePanel.SCREEN_W, GamePanel.SCREEN_H);
_drawningCruiser.SetPosition(random.nextInt(10, 100),
random.nextInt(10, 100));
_drawningCruiser.setWheels(random.nextInt(1, 5));
repaint();
};
}
}

5
Main.java Normal file
View File

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

35
NumberOfWheels.java Normal file
View File

@ -0,0 +1,35 @@
import java.awt.*;
public class NumberOfWheels {
public int numOfWheels;
public void setNumOfWheels(String num){
switch (Integer.valueOf(num)){
case 2:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
break;
case 3:
numOfWheels = NumberOfWheelsEnum.wheel_3.value;
break;
case 4:
numOfWheels = NumberOfWheelsEnum.wheel_4.value;
break;
default:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
}
}
public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) {
g.setColor(Color.black);
if (numOfWheels == 2) {
g.fillOval(_startPosX+20, _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 45, _startPosY + 20, 15, 15);
} else if (numOfWheels == 3) {
g.fillOval(_startPosX, _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 25, _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 55, _startPosY + 20, 15, 15);
} else {
g.fillOval(_startPosX, _startPosY + 20, 15, 15);
Review

Имеется дублирующийся код

Имеется дублирующийся код
g.fillOval(_startPosX + 15, _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 65 , _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 85, _startPosY + 20, 15, 15);
}
}
}

9
NumberOfWheelsEnum.java Normal file
View File

@ -0,0 +1,9 @@
public enum NumberOfWheelsEnum {
wheel_2(2),
wheel_3(3),
wheel_4(4);
public int value;
NumberOfWheelsEnum(int value) {
this.value = value;
}
}