PIbd-22. Stroev V.M. Lab Work 01 #2
30
CountWheels.java
Normal file
30
CountWheels.java
Normal file
@ -0,0 +1,30 @@
|
||||
public enum CountWheels{
|
||||
Min(2),
|
||||
Mid(3),
|
||||
Max(4);
|
||||
private final int val;
|
||||
private CountWheels(int val){
|
||||
this.val = val;
|
||||
}
|
||||
private int countWheels;
|
||||
public int getCountWheels(){
|
||||
return val;
|
||||
}
|
||||
public void setCountWheels(int countWheels){
|
||||
this.countWheels = countWheels;
|
||||
}
|
||||
public void setEnumValue(){
|
||||
CountWheels enumvalue = CountWheels.values()[countWheels];
|
||||
}
|
||||
public static CountWheels fromNumberToEnum(int number) {
|
||||
try{
|
||||
for (CountWheels countWheels : CountWheels.values()) {
|
||||
if (countWheels.getCountWheels() == number) {
|
||||
return countWheels;
|
||||
}
|
||||
}
|
||||
}catch(NumberFormatException e){
|
||||
}
|
||||
return Min;
|
||||
}
|
||||
}
|
11
Direction.java
Normal file
11
Direction.java
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
enum Direction{
|
||||
UP(1),
|
||||
DOWN(2),
|
||||
LEFT(3),
|
||||
RIGHT(4);
|
||||
private final int val;
|
||||
private Direction(int val){
|
||||
this.val = val;
|
||||
}
|
||||
}
|
51
DrawingWheels.java
Normal file
51
DrawingWheels.java
Normal file
@ -0,0 +1,51 @@
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
public class DrawingWheels{
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
Graphics g;
|
||||
Color color;
|
||||
private CountWheels _countWheels;
|
||||
|
||||
public DrawingWheels(Graphics g, int startPosX, int startPosY, Color color, int countWheels){
|
||||
this.g = g;
|
||||
_startPosX = startPosX;
|
||||
_startPosY = startPosY;
|
||||
this.color = color;
|
||||
_countWheels.setCountWheels(countWheels);
|
||||
}
|
||||
public void Draw(){
|
||||
|
||||
switch(_countWheels){
|
||||
case Min:
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawOval(_startPosX + 20, _startPosY + 35, 15, 15);
|
||||
g.drawOval(_startPosX + 110, _startPosY + 35, 15, 15);
|
||||
g.setColor(color);
|
||||
g.fillOval(_startPosX + 20, _startPosY + 35, 15, 15);
|
||||
g.fillOval(_startPosX + 110, _startPosY + 35, 15, 15);
|
||||
break;
|
||||
case Mid:
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawOval(_startPosX + 20, _startPosY + 35, 15, 15);
|
||||
g.drawOval(_startPosX + 65, _startPosY + 35, 15, 15);
|
||||
g.drawOval(_startPosX + 110, _startPosY + 35, 15, 15);
|
||||
g.setColor(color);
|
||||
g.fillOval(_startPosX + 20, _startPosY + 35, 15, 15);
|
||||
eegov
commented
Имеется дублирующийся код Имеется дублирующийся код
|
||||
g.fillOval(_startPosX + 65, _startPosY + 35, 15, 15);
|
||||
g.fillOval(_startPosX + 110, _startPosY + 35, 15, 15);
|
||||
break;
|
||||
case Max:
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawOval(_startPosX + 20, _startPosY + 35, 15, 15);
|
||||
g.drawOval(_startPosX + 50, _startPosY + 35, 15, 15);
|
||||
g.drawOval( _startPosX + 80, _startPosY + 35, 15, 15);
|
||||
g.drawOval(_startPosX + 110, _startPosY + 35, 15, 15);
|
||||
g.setColor(color);
|
||||
eegov
commented
Имеется дублирующийся код Имеется дублирующийся код
|
||||
g.fillOval(_startPosX + 20, _startPosY + 35, 15, 15);
|
||||
g.fillOval(_startPosX + 50, _startPosY + 35, 15, 15);
|
||||
g.fillOval(_startPosX + 80, _startPosY + 35, 15, 15);
|
||||
g.fillOval(_startPosX + 110, _startPosY + 35, 15, 15);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
188
DrawningMonorail.java
Normal file
188
DrawningMonorail.java
Normal file
@ -0,0 +1,188 @@
|
||||
import java.awt.*;
|
||||
import javax.swing.JPanel;
|
||||
import java.util.Random;
|
||||
import java.lang.*;
|
||||
|
||||
public class DrawningMonorail extends JPanel {
|
||||
private Entity entity;
|
||||
private int pictureWidth;
|
||||
private int pictureHeight;
|
||||
private int _countWheels;
|
||||
private int _startPosX;
|
||||
private int _startPosY ;
|
||||
private int relWidth = 150;
|
||||
private final int relHeight = 46;
|
||||
Random rd = new Random();
|
||||
public DrawningMonorail(){}
|
||||
|
||||
public boolean init(int wheelCount ,int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
boolean monorails, boolean secondCabin, int width, int height) {
|
||||
if (0 + relWidth >= width || 0 + relHeight >= height) {
|
||||
return false;
|
||||
}
|
||||
_countWheels = wheelCount;
|
||||
pictureWidth = width;
|
||||
pictureHeight = height;
|
||||
entity = new Entity();
|
||||
entity.init(wheelCount, speed, weight, bodyColor, additionalColor, monorails, secondCabin);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setPosition(int x, int y) {
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void moveTransport(Direction dr) {
|
||||
if (entity == null) {
|
||||
return;
|
||||
}
|
||||
switch (dr) {
|
||||
case LEFT:
|
||||
if (_startPosX - entity.getStep() > 0) {
|
||||
_startPosX -= entity.getStep();
|
||||
}
|
||||
break;
|
||||
case RIGHT:
|
||||
if (_startPosX + entity.getStep() < pictureWidth - relWidth) {
|
||||
_startPosX += entity.getStep();
|
||||
}
|
||||
break;
|
||||
case UP:
|
||||
if (_startPosY - entity.getStep() > 0) {
|
||||
_startPosY -= entity.getStep();
|
||||
}
|
||||
break;
|
||||
case DOWN:
|
||||
if (_startPosY + entity.getStep() < pictureHeight - relHeight) {
|
||||
_startPosY += entity.getStep();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void drawMonorail(Graphics g) {
|
||||
if (entity == null) {
|
||||
return;
|
||||
}
|
||||
relWidth = 150;
|
||||
//Колёса
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_startPosX + 22, _startPosY + 36, 40, 8);
|
||||
g.drawRect(_startPosX+82, _startPosY + 36, 40, 8);
|
||||
g.drawOval(_startPosX + 3, _startPosY + 37, 30, 8);
|
||||
g.drawOval(_startPosX + 110, _startPosY + 37, 29, 8);
|
||||
|
||||
g.fillRect(_startPosX + 22, _startPosY + 36, 40, 8);
|
||||
g.fillRect(_startPosX + 82, _startPosY + 36, 40, 8);
|
||||
g.fillOval( _startPosX + 3, _startPosY + 37, 30, 8);
|
||||
g.fillOval( _startPosX + 110, _startPosY + 37, 29, 8);
|
||||
DrawingWheels _drawingWheels = new DrawingWheels(g, _startPosX, _startPosY,
|
||||
entity.getBodyColor(), _countWheels);
|
||||
_drawingWheels.Draw();
|
||||
|
||||
//Кабина
|
||||
g.setColor(entity.getBodyColor());
|
||||
g.fillRect(_startPosX + 10, _startPosY + 20, 120, 16);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_startPosX + 10, _startPosY + 20, 120, 16);
|
||||
|
||||
int[] xp1 = new int[]{_startPosX+10,_startPosX+130,_startPosX+130,_startPosX+13};
|
||||
int[] yp1 = new int[]{_startPosY + 20,_startPosY + 20,_startPosY + 5,_startPosY + 5};
|
||||
Polygon pol1 = new Polygon(xp1, yp1, yp1.length);
|
||||
|
||||
g.setColor(entity.getBodyColor());
|
||||
g.fillPolygon(pol1);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawPolygon(pol1);
|
||||
|
||||
//Дверь
|
||||
g.setColor(Color.WHITE);
|
||||
g.fillRect(_startPosX + 49, _startPosY + 9, 12, 22);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_startPosX + 49, _startPosY + 9, 12, 22);
|
||||
|
||||
//Окна и прочее
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawRect(_startPosX + 20, _startPosY + 8, 10, 10);
|
||||
g.drawRect(_startPosX + 35, _startPosY + 8, 10, 10);
|
||||
g.drawRect(_startPosX + 117, _startPosY + 8, 10, 10);
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_startPosX + 130, _startPosY + 10, 2, 22);
|
||||
g.fillRect(_startPosX + 130, _startPosY + 10, 2, 22);
|
||||
|
||||
//Магнитная рельса
|
||||
|
||||
if(entity.getMonorails())
|
||||
{
|
||||
g.drawRect(_startPosX, _startPosY + 50, 140, 10);
|
||||
g.fillRect(_startPosX, _startPosY + 50, 140, 10);
|
||||
|
||||
g.setColor(Color.WHITE);
|
||||
g.fillRect(_startPosX + 5, _startPosY + 53, 130, 5);
|
||||
}
|
||||
|
||||
if (entity.getSecondCabin())
|
||||
{
|
||||
relWidth = 290;
|
||||
//низ
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_startPosX + 162, _startPosY + 36, 40, 8);
|
||||
g.drawRect(_startPosX + 222, _startPosY + 36, 40, 8);
|
||||
g.drawOval(_startPosX + 143, _startPosY + 37, 30, 8);
|
||||
g.drawOval(_startPosX + 250, _startPosY + 37, 29, 8);
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillRect(_startPosX + 162, _startPosY + 36, 40, 8);
|
||||
g.fillRect(_startPosX + 222, _startPosY + 36, 40, 8);
|
||||
g.fillOval(_startPosX + 143, _startPosY + 37, 30, 8);
|
||||
g.fillOval(_startPosX + 250, _startPosY + 37, 29, 8);
|
||||
|
||||
_drawingWheels = new DrawingWheels(g, _startPosX + 140, _startPosY,
|
||||
entity.getAdditionalColor(), _countWheels);
|
||||
_drawingWheels.Draw();
|
||||
if (entity.getMonorails())
|
||||
{
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_startPosX + 140, _startPosY + 50, 145, 10);
|
||||
g.fillRect(_startPosX + 140, _startPosY + 50, 145, 10);
|
||||
|
||||
g.setColor(Color.WHITE);
|
||||
g.fillRect(_startPosX + 135, _startPosY + 53, 145, 5);
|
||||
}
|
||||
|
||||
//Кабина
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_startPosX + 150, _startPosY + 20, 120, 16);
|
||||
|
||||
g.fillRect(_startPosX + 150, _startPosY + 20, 120, 16);
|
||||
|
||||
int[] xp2 = new int[]{_startPosX + 150,_startPosX + 270,_startPosX + 267,_startPosX + 150};
|
||||
int[] yp2 = new int[] {_startPosY + 20, _startPosY + 20, _startPosY + 5, _startPosY + 6};
|
||||
Polygon pol2 = new Polygon(xp2, yp2, yp2.length);
|
||||
|
||||
g.setColor(entity.getAdditionalColor());
|
||||
g.fillPolygon(pol2);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawPolygon(pol2);
|
||||
|
||||
|
||||
//дверь
|
||||
g.setColor(Color.WHITE);
|
||||
g.fillRect(_startPosX + 189, _startPosY + 9, 12, 22);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_startPosX + 189, _startPosY + 9, 12, 22);
|
||||
|
||||
//Окна и прочее
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawRect(_startPosX + 160, _startPosY + 8, 10, 10);
|
||||
g.drawRect(_startPosX + 175, _startPosY + 8, 10, 10);
|
||||
g.drawRect(_startPosX + 257, _startPosY + 8, 10, 10);
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_startPosX + 270, _startPosY + 10, 2, 22);
|
||||
g.fillRect(_startPosX + 270, _startPosY + 10, 2, 22);
|
||||
}
|
||||
}
|
||||
}
|
79
Entity.java
Normal file
79
Entity.java
Normal file
@ -0,0 +1,79 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Entity {
|
||||
private int speed;
|
||||
private int wheelCount;
|
||||
private double weight;
|
||||
private Color bodyColor;
|
||||
private Color additionalColor;
|
||||
private boolean monorails;
|
||||
private boolean secondCabin;
|
||||
|
||||
public double getStep() {
|
||||
return (double) speed * 130 / weight;
|
||||
}
|
||||
|
||||
public void init(int wheelCount, int speed, double weight, Color bodyColor, Color additionalColor, boolean monorails, boolean secondCabin) {
|
||||
setSpeed(speed);
|
||||
setWeight(weight);
|
||||
setBodyColor(bodyColor);
|
||||
setAdditionalColor(additionalColor);
|
||||
setMonorails(monorails);
|
||||
setSecondCabin(secondCabin);
|
||||
setWheelCount(wheelCount);
|
||||
}
|
||||
|
||||
public int getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
private void setSpeed(int speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
private void setWeight(double weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public Color getBodyColor() {
|
||||
return bodyColor;
|
||||
}
|
||||
|
||||
private void setBodyColor(Color bodyColor) {
|
||||
this.bodyColor = bodyColor;
|
||||
}
|
||||
|
||||
public Color getAdditionalColor() {
|
||||
return additionalColor;
|
||||
}
|
||||
|
||||
private void setAdditionalColor(Color additionalColor) {
|
||||
this.additionalColor = additionalColor;
|
||||
}
|
||||
|
||||
public boolean getMonorails() {
|
||||
return monorails;
|
||||
}
|
||||
|
||||
private void setMonorails(boolean monorails) {
|
||||
this.monorails = monorails;
|
||||
}
|
||||
|
||||
public boolean getSecondCabin() {
|
||||
return secondCabin;
|
||||
}
|
||||
|
||||
private void setSecondCabin(boolean secondCabin) {
|
||||
this.secondCabin = secondCabin;
|
||||
}
|
||||
public int getWheelCount(){
|
||||
return wheelCount;
|
||||
}
|
||||
private void setWheelCount(int wheelCount){
|
||||
this.wheelCount = wheelCount;
|
||||
}
|
||||
}
|
168
MonoFrame.java
Normal file
168
MonoFrame.java
Normal file
@ -0,0 +1,168 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import javax.swing.*;
|
||||
import java.util.Random;
|
||||
|
||||
class Form extends JFrame{
|
||||
private JLabel pictureBoxMonorail;
|
||||
private JPanel mainPanel;
|
||||
private JButton btUp;
|
||||
private JButton btDown;
|
||||
private JButton btRight;
|
||||
private JButton btLeft;
|
||||
private JButton btCreate;
|
||||
private JLabel wheelsLabel;
|
||||
private JTextField wheelsTextField;
|
||||
|
||||
Random rd = new Random();
|
||||
private DrawningMonorail _drawningMonorail;
|
||||
|
||||
public Form(){
|
||||
initComponents();
|
||||
}
|
||||
private void Draw(){
|
||||
if(_drawningMonorail == null){
|
||||
return;
|
||||
}
|
||||
BufferedImage bmp = new BufferedImage(pictureBoxMonorail.getWidth(),
|
||||
pictureBoxMonorail.getHeight(),BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D gr = bmp.createGraphics();
|
||||
_drawningMonorail.drawMonorail(gr);
|
||||
ImageIcon imageIcon = new ImageIcon(bmp);
|
||||
pictureBoxMonorail.setIcon(imageIcon);
|
||||
}
|
||||
private void initComponents(){
|
||||
mainPanel = new JPanel();
|
||||
btCreate = new JButton("Создать");
|
||||
|
||||
btUp = new JButton(new ImageIcon("resources/upper-arrow.png"));
|
||||
btDown = new JButton(new ImageIcon("resources/down-arrow.png"));
|
||||
btLeft = new JButton(new ImageIcon("resources/left-arrow.png"));
|
||||
btRight = new JButton(new ImageIcon("resources/right-arrow.png"));
|
||||
|
||||
pictureBoxMonorail = new JLabel();
|
||||
wheelsLabel = new JLabel("Кол-во колёс: ");
|
||||
wheelsTextField = new JTextField();
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
add(mainPanel, BorderLayout.CENTER);
|
||||
mainPanel.setLayout(null);
|
||||
|
||||
pictureBoxMonorail.setBounds(0, 0, 882, 453);
|
||||
mainPanel.add(pictureBoxMonorail);
|
||||
|
||||
wheelsLabel.setBounds(22, 410, 150, 29);
|
||||
mainPanel.add(wheelsLabel);
|
||||
|
||||
wheelsTextField.setBounds(150, 410, 80, 29);
|
||||
mainPanel.add(wheelsTextField);
|
||||
|
||||
btCreate.setBounds(250, 410, 100, 29);
|
||||
mainPanel.add(btCreate);
|
||||
btCreate.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
buttonCreateActionPerformed(e);
|
||||
}
|
||||
});
|
||||
|
||||
btUp.setBounds(790, 355, 40, 40);
|
||||
mainPanel.add(btUp);
|
||||
ImageIcon iconUp = new ImageIcon("resources/upper-arrow.png");
|
||||
btUp.setIcon(iconUp);
|
||||
btUp.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
buttonMoveActionPerformed(btUp, e);
|
||||
}
|
||||
});
|
||||
|
||||
btDown.setBounds(790, 401, 40, 40);
|
||||
mainPanel.add(btDown);
|
||||
ImageIcon iconDown = new ImageIcon("resources/down-arrow.png");
|
||||
btDown.setIcon(iconDown);
|
||||
btDown.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
buttonMoveActionPerformed(btDown, e);
|
||||
}
|
||||
});
|
||||
|
||||
btLeft.setBounds(744, 401, 40, 40);
|
||||
mainPanel.add(btLeft);
|
||||
ImageIcon iconLeft = new ImageIcon("resources/left-arrow.png");
|
||||
btLeft.setIcon(iconLeft);
|
||||
btLeft.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
buttonMoveActionPerformed(btLeft, e);
|
||||
}
|
||||
});
|
||||
|
||||
btRight.setBounds(836, 401, 40, 40);
|
||||
mainPanel.add(btRight);
|
||||
ImageIcon iconRight = new ImageIcon("resources/right-arrow.png");
|
||||
btRight.setIcon(iconRight);
|
||||
btRight.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
buttonMoveActionPerformed(btRight, e);
|
||||
}
|
||||
});
|
||||
btUp.setName("buttonUp");
|
||||
btDown.setName("buttonDown");
|
||||
btLeft.setName("buttonLeft");
|
||||
btRight.setName("buttonRight");
|
||||
|
||||
setPreferredSize(new Dimension(900, 500));
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setVisible(true);
|
||||
pack();
|
||||
}
|
||||
|
||||
protected void buttonCreateActionPerformed(ActionEvent e) {
|
||||
int wheelCount;
|
||||
try {
|
||||
wheelCount = Integer.parseInt(wheelsTextField.getText());
|
||||
} catch (NumberFormatException ex) {
|
||||
wheelCount = 0;
|
||||
}
|
||||
|
||||
if (wheelCount > 0) {
|
||||
Random random = new Random();
|
||||
_drawningMonorail = new DrawningMonorail();
|
||||
_drawningMonorail.init(wheelCount, random.nextInt(500,800),
|
||||
random.nextInt(1000,3000),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||
random.nextBoolean(),
|
||||
random.nextBoolean(),
|
||||
pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight());
|
||||
_drawningMonorail.setPosition(random.nextInt(90) + 10,
|
||||
random.nextInt(90) + 10); Draw();
|
||||
}
|
||||
}
|
||||
protected void buttonMoveActionPerformed(Object sender, ActionEvent e) {
|
||||
if (_drawningMonorail == null) {
|
||||
return;
|
||||
}
|
||||
String name = (sender instanceof JButton) ? ((JButton) sender).getName() : "";
|
||||
switch (name) {
|
||||
case "buttonUp":
|
||||
_drawningMonorail.moveTransport(Direction.UP);
|
||||
break;
|
||||
case "buttonDown":
|
||||
_drawningMonorail.moveTransport(Direction.DOWN);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
_drawningMonorail.moveTransport(Direction.LEFT);
|
||||
break;
|
||||
case "buttonRight":
|
||||
_drawningMonorail.moveTransport(Direction.RIGHT);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
class MonoFrame{
|
||||
public static void main(String[] args) {
|
||||
Form fr = new Form();
|
||||
}
|
||||
}
|
BIN
resources/down-arrow.png
Normal file
BIN
resources/down-arrow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 281 B |
BIN
resources/left-arrow.png
Normal file
BIN
resources/left-arrow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 265 B |
BIN
resources/right-arrow.png
Normal file
BIN
resources/right-arrow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 224 B |
BIN
resources/upper-arrow.png
Normal file
BIN
resources/upper-arrow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 264 B |
Loading…
Reference in New Issue
Block a user
Про конструктор упоминаний в задании не было