PIbd-14 Antonova_A.A. LabWork03 Hard #3
17
CanvasDumpTruck.java
Normal file
17
CanvasDumpTruck.java
Normal file
@ -0,0 +1,17 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class CanvasDumpTruck extends JComponent {
|
||||
public DrawningDumpTruck _drawningDumpTruck;
|
||||
public CanvasDumpTruck() {}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
if (_drawningDumpTruck == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_drawningDumpTruck.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
6
DirectionType.java
Normal file
6
DirectionType.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
13
DopEnum.java
Normal file
13
DopEnum.java
Normal file
@ -0,0 +1,13 @@
|
||||
public enum DopEnum {
|
||||
Two (2),
|
||||
Three (3),
|
||||
Four (4);
|
||||
|
||||
private int number;
|
||||
|
||||
DopEnum(int n){
|
||||
this.number = n;
|
||||
}
|
||||
|
||||
public int get_number() { return number; }
|
||||
}
|
207
DrawningDumpTruck.java
Normal file
207
DrawningDumpTruck.java
Normal file
@ -0,0 +1,207 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
//import javax.swing.*;
|
||||
|
||||
public class DrawningDumpTruck {
|
||||
public EntityDumpTruck EntityDumpTruck;
|
||||
|
||||
private EntityDumpTruck get_EntityDumpTruck() { return EntityDumpTruck; }
|
||||
public DrawningWeels _drawingWeels = null;
|
||||
|
||||
// Ширина окна
|
||||
private Integer _pictureWidth;
|
||||
|
||||
// Высота окна
|
||||
private Integer _pictureHeight;
|
||||
|
||||
// Левая координата прорисовки автомобиля
|
||||
private Integer _startPosX;
|
||||
|
||||
// Верхняя кооридната прорисовки автомобиля
|
||||
private Integer _startPosY;
|
||||
|
||||
// Ширина прорисовки самосвала
|
||||
private final int _drawningDumpTruckWidth = 116;
|
||||
|
||||
// Высота прорисовки самосвала
|
||||
private final int _drawingDumpTruckHeight = 100;
|
||||
Random random = new Random();
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent)
|
||||
{
|
||||
EntityDumpTruck = new EntityDumpTruck();
|
||||
EntityDumpTruck.Init(speed, weight, bodyColor, additionalColor, body, tent);
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
_drawingWeels = new DrawningWeels(random.nextInt(2, 5));
|
||||
|
||||
}
|
||||
|
||||
public Boolean SetPictureSize(int width, int height)
|
||||
{
|
||||
if (_drawningDumpTruckWidth > width || _drawingDumpTruckHeight > height)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_pictureWidth != null && width != _pictureWidth || _pictureHeight != null && height != _pictureHeight)
|
||||
{
|
||||
if (_startPosX + _drawningDumpTruckWidth > width)
|
||||
{
|
||||
_startPosX -= _drawningDumpTruckWidth;
|
||||
}
|
||||
|
||||
if (_startPosY + _drawingDumpTruckHeight > height)
|
||||
{
|
||||
_startPosY -= _drawingDumpTruckHeight;
|
||||
}
|
||||
}
|
||||
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (_pictureHeight == null|| _pictureWidth == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (x < 0)
|
||||
{
|
||||
x = 0;
|
||||
}
|
||||
if (x + _drawningDumpTruckWidth > _pictureWidth)
|
||||
{
|
||||
x = (int)_pictureWidth - _drawningDumpTruckWidth;
|
||||
}
|
||||
if (y < 0)
|
||||
{
|
||||
y = 0;
|
||||
}
|
||||
if (y + _drawingDumpTruckHeight > _pictureHeight)
|
||||
{
|
||||
y = (int)_pictureHeight - _drawingDumpTruckHeight;
|
||||
|
||||
}
|
||||
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
|
||||
}
|
||||
|
||||
public Boolean MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityDumpTruck == null || _startPosX == null || _startPosY == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - _drawningDumpTruckWidth > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityDumpTruck.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - _drawingDumpTruckHeight > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityDumpTruck.Step;
|
||||
}
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + _drawningDumpTruckWidth + (int)EntityDumpTruck.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityDumpTruck.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _drawingDumpTruckHeight + (int)EntityDumpTruck.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityDumpTruck.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (EntityDumpTruck == null || _startPosX == null || _startPosY == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
g.setPaint(Color.BLACK);
|
||||
|
||||
if (EntityDumpTruck.Body)
|
||||
{
|
||||
int[] x_b = {_startPosX, _startPosX + 77, _startPosX + 77, _startPosX + 20};
|
||||
int[] y_b = {_startPosY + 15, _startPosY + 15, _startPosY + 50, _startPosY + 50};
|
||||
|
||||
g.setPaint(Color.BLACK);
|
||||
g.drawPolygon(x_b, y_b, 4);
|
||||
g.setPaint(EntityDumpTruck.AdditionalColor);
|
||||
g.fillPolygon(x_b, y_b, 4);
|
||||
}
|
||||
|
||||
if (EntityDumpTruck.Tent && EntityDumpTruck.Body)
|
||||
{
|
||||
int[] x_t = {_startPosX, _startPosX + 39, _startPosX + 77};
|
||||
int[] y_t = {_startPosY + 13, _startPosY, _startPosY + 13};
|
||||
|
||||
g.setPaint(Color.BLACK);
|
||||
g.drawPolygon(x_t, y_t, 3);
|
||||
g.setPaint(EntityDumpTruck.AdditionalColor);
|
||||
g.fillPolygon(x_t, y_t, 3);
|
||||
}
|
||||
|
||||
// Колёса
|
||||
g.setPaint(Color.BLACK);
|
||||
if (_drawingWeels != null){
|
||||
switch (_drawingWeels.get_countWheels()){
|
||||
case 2:
|
||||
_drawingWeels.DrawningWeel(g, _startPosX + 10, _startPosY + 67);
|
||||
_drawingWeels.DrawningWeel(g, _startPosX + 85, _startPosY + 67);
|
||||
break;
|
||||
case 3:
|
||||
_drawingWeels.DrawningWeel(g, _startPosX + 10, _startPosY + 67);
|
||||
_drawingWeels.DrawningWeel(g, _startPosX + 38, _startPosY + 67);
|
||||
_drawingWeels.DrawningWeel(g, _startPosX + 85, _startPosY + 67);
|
||||
break;
|
||||
case 4:
|
||||
_drawingWeels.DrawningWeel(g, _startPosX + 5, _startPosY + 67);
|
||||
_drawingWeels.DrawningWeel(g, _startPosX + 32, _startPosY + 67);
|
||||
_drawingWeels.DrawningWeel(g, _startPosX + 61, _startPosY + 67);
|
||||
_drawingWeels.DrawningWeel(g, _startPosX + 88, _startPosY + 67);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Границы самосвала
|
||||
g.drawRect( _startPosX + 10, _startPosY + 53, 100, 13);
|
||||
g.drawRect( _startPosX + 80, _startPosY + 15, 30, 36);
|
||||
g.drawRect(_startPosX + 85, _startPosY + 20, 20, 20);
|
||||
|
||||
// Кузов
|
||||
g.setPaint(EntityDumpTruck.BodyColor);
|
||||
g.fillRect(_startPosX + 10, _startPosY + 53, 100, 13);
|
||||
g.fillRect(_startPosX + 80, _startPosY + 15, 30, 36);
|
||||
|
||||
//Окно
|
||||
g.setPaint(Color.CYAN);
|
||||
g.fillRect(_startPosX + 85, _startPosY + 20, 20, 20);
|
||||
|
||||
}
|
||||
}
|
17
DrawningWeels.java
Normal file
17
DrawningWeels.java
Normal file
@ -0,0 +1,17 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWeels {
|
||||
|
||||
private int count = 0;
|
||||
DrawningWeels (int n){
|
||||
count = n;
|
||||
}
|
||||
public int get_countWheels(){
|
||||
return count;
|
||||
}
|
||||
|
||||
public void DrawningWeel (Graphics g, int x, int y){
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval(x, y, 25, 25);
|
||||
}
|
||||
}
|
35
EntityDumpTruck.java
Normal file
35
EntityDumpTruck.java
Normal file
@ -0,0 +1,35 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityDumpTruck {
|
||||
public int Speed;
|
||||
private int get_Speed(){ return Speed; }
|
||||
|
||||
public double Weight;
|
||||
private double get_Weight(){return Weight; }
|
||||
|
||||
public Color BodyColor;
|
||||
private Color get_BodyColor(){ return BodyColor; }
|
||||
|
||||
public Color AdditionalColor;
|
||||
private Color get_AdditionalColor(){ return AdditionalColor; }
|
||||
|
||||
public Boolean Body;
|
||||
private Boolean get_Body() { return Body; }
|
||||
|
||||
public Boolean Tent;
|
||||
private Boolean get_Tent() { return Tent; }
|
||||
|
||||
public double Step;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Body = body;
|
||||
Tent = tent;
|
||||
Step = speed * 100 / weight;
|
||||
}
|
||||
|
||||
}
|
129
FormDumpTruck.java
Normal file
129
FormDumpTruck.java
Normal file
@ -0,0 +1,129 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import javax.swing.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormDumpTruck extends JFrame{
|
||||
|
||||
private CanvasDumpTruck _canvasDumpTruck = new CanvasDumpTruck();
|
||||
|
||||
// Размер формы
|
||||
private Dimension dimension;
|
||||
// Название формы
|
||||
private String title;
|
||||
|
||||
private JButton CreateButton = new JButton("Создать самосвал");
|
||||
private JButton UpButton = new JButton();
|
||||
private JButton DownButton = new JButton();;
|
||||
private JButton LeftButton = new JButton();;
|
||||
private JButton RightButton = new JButton();
|
||||
|
||||
public FormDumpTruck(String title, Dimension dimension) {
|
||||
this.title = title;
|
||||
this.dimension = dimension;
|
||||
}
|
||||
|
||||
public void Form() {
|
||||
CreateButton.setName("CREATE");
|
||||
setMinimumSize(dimension);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
UpButton.setName("UP");
|
||||
Icon iconUp = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowUp.png");
|
||||
UpButton.setIcon(iconUp);
|
||||
|
||||
DownButton.setName("DOWN");
|
||||
Icon iconDown = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowDown.png");
|
||||
DownButton.setIcon(iconDown);
|
||||
|
||||
LeftButton.setName("LEFT");
|
||||
Icon iconLeft = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowLeft.png");
|
||||
LeftButton.setIcon(iconLeft);
|
||||
|
||||
RightButton.setName("RIGHT");
|
||||
Icon iconRight = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowRight.png");
|
||||
RightButton.setIcon(iconRight);
|
||||
|
||||
CreateButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Random random = new Random();
|
||||
_canvasDumpTruck._drawningDumpTruck = new DrawningDumpTruck();
|
||||
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
_canvasDumpTruck._drawningDumpTruck.Init(random.nextInt(1000, 3000), random.nextInt(1000, 3000),
|
||||
bodyColor, additionalColor,
|
||||
random.nextBoolean(), random.nextBoolean());
|
||||
|
||||
_canvasDumpTruck._drawningDumpTruck.SetPictureSize(getWidth(), getHeight());
|
||||
_canvasDumpTruck._drawningDumpTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
_canvasDumpTruck.repaint();
|
||||
}
|
||||
});
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if (_canvasDumpTruck._drawningDumpTruck == null) return;
|
||||
boolean result = false;
|
||||
switch ((((JButton) (event.getSource())).getName())) {
|
||||
case "UP":
|
||||
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "DOWN":
|
||||
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "LEFT":
|
||||
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "RIGHT":
|
||||
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result) {
|
||||
_canvasDumpTruck.repaint();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
UpButton.addActionListener(actionListener);
|
||||
DownButton.addActionListener(actionListener);
|
||||
LeftButton.addActionListener(actionListener);
|
||||
RightButton.addActionListener(actionListener);
|
||||
|
||||
setSize(dimension.width, dimension.height);
|
||||
setLayout(null);
|
||||
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 160, 40);
|
||||
UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 60, 60);
|
||||
RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60);
|
||||
LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60);
|
||||
add(CreateButton);
|
||||
add(UpButton);
|
||||
add(DownButton);
|
||||
add(RightButton);
|
||||
add(LeftButton);
|
||||
add(_canvasDumpTruck);
|
||||
pack();
|
||||
setVisible(true);
|
||||
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
public void componentResized(ComponentEvent e) {
|
||||
int Width = getWidth();
|
||||
int Height = getHeight();
|
||||
if (_canvasDumpTruck._drawningDumpTruck != null)
|
||||
_canvasDumpTruck._drawningDumpTruck.SetPictureSize(Width, Height);
|
||||
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 160, 40);
|
||||
UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60);
|
||||
DownButton.setBounds(getWidth() - 150, getHeight() - 100, 60, 60);
|
||||
RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60);
|
||||
LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user