PIBD-13_Marinenkova_V.A._LabWork01_Hard #1
16
OOP_Hard/src/CanvasAccordionBus.java
Normal file
16
OOP_Hard/src/CanvasAccordionBus.java
Normal file
@ -0,0 +1,16 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class CanvasAccordionBus extends JComponent {
|
||||
public DrawningAccordionBus _drawningAccordionBus;
|
||||
public CanvasAccordionBus(){}
|
||||
public void paintComponent(Graphics g) {
|
||||
if (_drawningAccordionBus == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_drawningAccordionBus.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
5
OOP_Hard/src/CountEntrances.java
Normal file
5
OOP_Hard/src/CountEntrances.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum CountEntrances {
|
||||
Three,
|
||||
Four,
|
||||
Five
|
||||
}
|
6
OOP_Hard/src/DirectionType.java
Normal file
6
OOP_Hard/src/DirectionType.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
56
OOP_Hard/src/DrawingEntrances.java
Normal file
56
OOP_Hard/src/DrawingEntrances.java
Normal file
@ -0,0 +1,56 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingEntrances {
|
||||
private CountEntrances countEntrances;
|
||||
|
||||
Random rand = new Random();
|
||||
int randomNum = rand.nextInt(5);
|
||||
|
||||
public CountEntrances getCount() {
|
||||
return countEntrances;
|
||||
}
|
||||
public void setCountEntrances() {
|
||||
switch (randomNum) {
|
||||
case 3:
|
||||
countEntrances = CountEntrances.Three;
|
||||
break;
|
||||
case 4:
|
||||
countEntrances = CountEntrances.Four;
|
||||
break;
|
||||
case 5:
|
||||
countEntrances = CountEntrances.Five;
|
||||
break;
|
||||
default:
|
||||
countEntrances = CountEntrances.Five;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawEntrances(Graphics2D g, int _StartPosX, int _StartPosY) {
|
||||
if (countEntrances == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX + 74, _StartPosY + 18, 5, 20);
|
||||
g.drawRect(_StartPosX + 84, _StartPosY + 18, 5, 20);
|
||||
|
||||
if (countEntrances == CountEntrances.Three) {
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX + 94, _StartPosY + 18, 5, 20);
|
||||
}
|
||||
|
||||
if (countEntrances == CountEntrances.Four) {
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX + 94, _StartPosY + 18, 5, 20);
|
||||
g.drawRect(_StartPosX + 104, _StartPosY + 18, 5, 20);
|
||||
}
|
||||
|
||||
if (countEntrances == CountEntrances.Five) {
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX + 94, _StartPosY + 18, 5, 20);
|
||||
g.drawRect(_StartPosX + 104, _StartPosY + 18, 5, 20);
|
||||
g.drawRect(_StartPosX + 112, _StartPosY + 18, 5, 20);
|
||||
}
|
||||
}
|
||||
}
|
138
OOP_Hard/src/DrawningAccordionBus.java
Normal file
138
OOP_Hard/src/DrawningAccordionBus.java
Normal file
@ -0,0 +1,138 @@
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningAccordionBus extends JPanel {
|
||||
public EntityAccordionBus entityAccordionBus;
|
||||
public DrawingEntrances drawingEntrances = null;
|
||||
|
||||
private Integer picture_width;
|
||||
private Integer picture_height;
|
||||
|
||||
private Integer _StartPosX;
|
||||
private Integer _StartPosY;
|
||||
|
||||
private int drawingBusWidth = 60;
|
||||
private int drawingBusHeight = 40;
|
||||
|
||||
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean compartment, boolean entrance, boolean windows) {
|
||||
entityAccordionBus = new EntityAccordionBus();
|
||||
entityAccordionBus.Init(speed, weight, bodycolor, additionalcolor, compartment, entrance, windows);
|
||||
picture_width = null;
|
||||
picture_height = null;
|
||||
_StartPosX = null;
|
||||
_StartPosY = null;
|
||||
drawingEntrances = new DrawingEntrances();
|
||||
Random rand = new Random();
|
||||
drawingEntrances.setCountEntrances();
|
||||
}
|
||||
|
||||
public boolean SetPictureSize(int width, int height) {
|
||||
if (width < drawingBusWidth || height < drawingBusHeight) return false;
|
||||
picture_width = width;
|
||||
picture_height = height;
|
||||
if (_StartPosX != null || _StartPosY != null) {
|
||||
if (_StartPosX + drawingBusWidth > picture_width) {
|
||||
_StartPosX = _StartPosX - (_StartPosX + drawingBusWidth - picture_width);
|
||||
}
|
||||
else if (_StartPosX < 0) _StartPosX = 0;
|
||||
if (_StartPosY + drawingBusHeight > picture_height) {
|
||||
_StartPosY = _StartPosY - (_StartPosY + drawingBusHeight - picture_height);
|
||||
}
|
||||
else if (_StartPosY < 0) _StartPosY = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y) {
|
||||
if (!(picture_width != null && picture_height != null)) return;
|
||||
if (x + drawingBusWidth > picture_width) {
|
||||
_StartPosX = x - (x + drawingBusWidth - picture_width);
|
||||
}
|
||||
else if (x < 0) _StartPosX = 0;
|
||||
else _StartPosX = x;
|
||||
if (y + drawingBusHeight > picture_height) {
|
||||
_StartPosY = y - (y + drawingBusHeight - picture_height);
|
||||
}
|
||||
else if (y < 0) _StartPosY = 0;
|
||||
else _StartPosY = y;
|
||||
}
|
||||
|
||||
public boolean MoveTransport(DirectionType direction) {
|
||||
if (entityAccordionBus == null || _StartPosX == null || _StartPosY == null) return false;
|
||||
switch (direction) {
|
||||
case Left:
|
||||
if (_StartPosX - entityAccordionBus.Step > 0) {
|
||||
_StartPosX -= (int) entityAccordionBus.Step;
|
||||
}
|
||||
return true;
|
||||
case Up:
|
||||
if (_StartPosY - entityAccordionBus.Step > 0) {
|
||||
_StartPosY -= (int) entityAccordionBus.Step;
|
||||
}
|
||||
return true;
|
||||
case Right:
|
||||
if (_StartPosX + drawingBusWidth + (int) entityAccordionBus.Step < picture_width - entityAccordionBus.Step) {
|
||||
_StartPosX += (int) entityAccordionBus.Step;
|
||||
}
|
||||
return true;
|
||||
case Down:
|
||||
if (_StartPosY + drawingBusHeight + (int) entityAccordionBus.Step < picture_height - entityAccordionBus.Step) {
|
||||
_StartPosY += (int) entityAccordionBus.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
if (entityAccordionBus == null || _StartPosX == null || _StartPosY == null) return;
|
||||
|
||||
//вагончик
|
||||
g.setColor(entityAccordionBus.getBodyColor());
|
||||
g.fillRect(_StartPosX + 10, _StartPosY + 10, 50, 30);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX + 10, _StartPosY + 10, 50, 30);
|
||||
|
||||
//двери
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX + 26, _StartPosY + 18,10, 20);
|
||||
|
||||
//окна
|
||||
g.setColor(Color.BLUE);
|
||||
g.fillOval(_StartPosX + 15, _StartPosY + 15, 5, 10);
|
||||
g.fillOval(_StartPosX + 40, _StartPosY + 15, 5, 10);
|
||||
g.fillOval(_StartPosX + 50, _StartPosY + 15, 5, 10);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawOval(_StartPosX + 15, _StartPosY + 15, 5, 10);
|
||||
g.drawOval(_StartPosX + 40, _StartPosY + 15, 5, 10);
|
||||
g.drawOval(_StartPosX + 50, _StartPosY + 15, 5, 10);
|
||||
|
||||
//колеса
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval(_StartPosX + 15, _StartPosY + 35, 10, 10);
|
||||
g.fillOval(_StartPosX + 45, _StartPosY + 35, 10, 10);
|
||||
g.drawOval(_StartPosX + 15, _StartPosY + 35, 10, 10);
|
||||
g.drawOval(_StartPosX + 45, _StartPosY + 35, 10, 10);
|
||||
|
||||
if(entityAccordionBus.withCompartment()) {
|
||||
drawingBusWidth = 130;
|
||||
g.setColor(entityAccordionBus.getAdditionalColor());
|
||||
g.fillRect(_StartPosX + 70, _StartPosY + 10, 50, 30);
|
||||
g.fillRect(_StartPosX + 60, _StartPosY + 15, 10, 20);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX + 70, _StartPosY + 10, 50, 30);
|
||||
g.drawRect(_StartPosX + 60, _StartPosY + 15, 10, 20);
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval(_StartPosX + 75, _StartPosY + 35, 10, 10);
|
||||
g.fillOval(_StartPosX + 105, _StartPosY + 35, 10, 10);
|
||||
g.drawOval(_StartPosX + 75, _StartPosY + 35, 10, 10);
|
||||
g.drawOval(_StartPosX + 105, _StartPosY + 35, 10, 10);
|
||||
|
||||
drawingEntrances.DrawEntrances(g, _StartPosX, _StartPosY);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
47
OOP_Hard/src/EntityAccordionBus.java
Normal file
47
OOP_Hard/src/EntityAccordionBus.java
Normal file
@ -0,0 +1,47 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityAccordionBus {
|
||||
|
||||
private int Speed;
|
||||
|
||||
private double Weight;
|
||||
|
||||
private Color BodyColor;
|
||||
public Color getBodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
|
||||
private Color AdditionalColor;
|
||||
public Color getAdditionalColor() {
|
||||
return AdditionalColor;
|
||||
}
|
||||
|
||||
public double Step;
|
||||
|
||||
private boolean Compartment;
|
||||
public boolean withCompartment() {
|
||||
return Compartment;
|
||||
}
|
||||
|
||||
private boolean Entrance;
|
||||
public boolean withEntrance() {
|
||||
return Entrance;
|
||||
}
|
||||
|
||||
private boolean Windows;
|
||||
public boolean withWindows() {
|
||||
return Windows;
|
||||
}
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
boolean compartment, boolean entrance, boolean windows) {
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Compartment = compartment; // отсек
|
||||
Entrance = entrance; // крылья
|
||||
Windows = windows;
|
||||
Step = Speed * 100 / weight;
|
||||
}
|
||||
}
|
124
OOP_Hard/src/FormAccordionBus.java
Normal file
124
OOP_Hard/src/FormAccordionBus.java
Normal file
@ -0,0 +1,124 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormAccordionBus extends JFrame {
|
||||
private String title;
|
||||
private Dimension dimension;
|
||||
private int Width, Height;
|
||||
private CanvasAccordionBus canvasAccordionBus = new CanvasAccordionBus();
|
||||
private JButton CreateButton = new JButton("Create");;
|
||||
private JButton UpButton = new JButton();
|
||||
private JButton DownButton = new JButton();;
|
||||
private JButton LeftButton = new JButton();;
|
||||
private JButton RightButton = new JButton();
|
||||
public FormAccordionBus(String title, Dimension dimension) {
|
||||
this.title = title;
|
||||
this.dimension = dimension;
|
||||
}
|
||||
public void Init() {
|
||||
setTitle(title);
|
||||
setMinimumSize(dimension);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
|
||||
CreateButton.setName("CREATE");
|
||||
Icon iconUp = new ImageIcon("src\\images\\up.jpg");
|
||||
UpButton.setIcon(iconUp);
|
||||
UpButton.setName("UP");
|
||||
DownButton.setName("DOWN");
|
||||
Icon iconDown = new ImageIcon("src\\images\\down.jpg");
|
||||
DownButton.setIcon(iconDown);
|
||||
LeftButton.setName("LEFT");
|
||||
Icon iconLeft = new ImageIcon("src\\images\\left.jpg");
|
||||
LeftButton.setIcon(iconLeft);
|
||||
RightButton.setName("RIGHT");
|
||||
Icon iconRight = new ImageIcon("src\\images\\right.jpg");
|
||||
RightButton.setIcon(iconRight);
|
||||
|
||||
CreateButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int StartPositionX = (int)(Math.random() * 90 + 10);
|
||||
int StartPositionY = (int)(Math.random() * 90 + 10);
|
||||
int speed = (int)(Math.random() * 300 + 100);
|
||||
double weight = (double)(Math.random() * 3000 + 1000);
|
||||
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||||
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
|
||||
boolean compartment = new Random().nextBoolean();
|
||||
boolean entrance = new Random().nextBoolean();
|
||||
boolean windows = new Random().nextBoolean();
|
||||
canvasAccordionBus._drawningAccordionBus = new DrawningAccordionBus();
|
||||
canvasAccordionBus._drawningAccordionBus.Init(speed, weight, bodyColor, additionalColor, compartment, entrance, windows);
|
||||
canvasAccordionBus._drawningAccordionBus.SetPictureSize(Width, Height);
|
||||
canvasAccordionBus._drawningAccordionBus.SetPosition( StartPositionX, StartPositionY);
|
||||
canvasAccordionBus.repaint();
|
||||
}
|
||||
});
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if (canvasAccordionBus._drawningAccordionBus == null) return;
|
||||
boolean result = false;
|
||||
switch ((((JButton)(event.getSource())).getName())) {
|
||||
case "UP":
|
||||
result = canvasAccordionBus._drawningAccordionBus.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "DOWN":
|
||||
result = canvasAccordionBus._drawningAccordionBus.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "LEFT":
|
||||
result = canvasAccordionBus._drawningAccordionBus.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "RIGHT":
|
||||
result = canvasAccordionBus._drawningAccordionBus.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result) {
|
||||
canvasAccordionBus.repaint();
|
||||
}
|
||||
}
|
||||
};
|
||||
UpButton.addActionListener(actionListener);
|
||||
DownButton.addActionListener(actionListener);
|
||||
LeftButton.addActionListener(actionListener);
|
||||
RightButton.addActionListener(actionListener);
|
||||
|
||||
setSize(dimension.width,dimension.height);
|
||||
setLayout(null);
|
||||
canvasAccordionBus.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
|
||||
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
|
||||
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
|
||||
add(CreateButton);
|
||||
add(UpButton);
|
||||
add(DownButton);
|
||||
add(RightButton);
|
||||
add(LeftButton);
|
||||
add(canvasAccordionBus);
|
||||
setVisible(true);
|
||||
//обработка события изменения размеров окна
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
public void componentResized(ComponentEvent e) {
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
if (canvasAccordionBus._drawningAccordionBus != null)
|
||||
canvasAccordionBus._drawningAccordionBus.SetPictureSize(Width, Height);
|
||||
canvasAccordionBus.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
|
||||
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
|
||||
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,15 +1,8 @@
|
||||
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
|
||||
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
||||
import java.awt.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
|
||||
// to see how IntelliJ IDEA suggests fixing it.
|
||||
System.out.printf("Hello and welcome!");
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
|
||||
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
FormAccordionBus form = new FormAccordionBus("Автобус", new Dimension(500,300));
|
||||
form.Init();
|
||||
}
|
||||
}
|
BIN
OOP_Hard/src/images/down.jpg
Normal file
BIN
OOP_Hard/src/images/down.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 141 KiB |
BIN
OOP_Hard/src/images/left.jpg
Normal file
BIN
OOP_Hard/src/images/left.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 139 KiB |
BIN
OOP_Hard/src/images/right.jpg
Normal file
BIN
OOP_Hard/src/images/right.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.8 KiB |
BIN
OOP_Hard/src/images/up.jpg
Normal file
BIN
OOP_Hard/src/images/up.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 141 KiB |
Loading…
Reference in New Issue
Block a user