Лабораторная работа №1
This commit is contained in:
parent
090e7f0918
commit
bc9d72d0a2
16
src/src/CanvasDumpTrack.java
Normal file
16
src/src/CanvasDumpTrack.java
Normal file
@ -0,0 +1,16 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class CanvasDumpTrack extends JComponent {
|
||||
public DrawingDumpTrack _drawingDumpTrack;
|
||||
public CanvasDumpTrack(){}
|
||||
public void paintComponent(Graphics g) {
|
||||
if (_drawingDumpTrack == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_drawingDumpTrack.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
6
src/src/DirectionType.java
Normal file
6
src/src/DirectionType.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
110
src/src/DrawingDumpTrack.java
Normal file
110
src/src/DrawingDumpTrack.java
Normal file
@ -0,0 +1,110 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingDumpTrack extends JPanel {
|
||||
public EntityDumpTrack EntityDumpTrack;
|
||||
public DrawingWheels _wheels = null;
|
||||
private Integer picture_width;
|
||||
private Integer picture_height;
|
||||
private Integer _StartPosX;
|
||||
private Integer _StartPosY;
|
||||
private int drawingDumpTrackWidth = 155;
|
||||
private int drawingDumpTrackHeight = 85;
|
||||
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean _bodyWork, boolean _awning) {
|
||||
EntityDumpTrack = new EntityDumpTrack();
|
||||
EntityDumpTrack.Init(speed, weight, bodycolor, additionalcolor, _bodyWork, _awning);
|
||||
picture_width = null;
|
||||
picture_height = null;
|
||||
_StartPosX = null;
|
||||
_StartPosY = null;
|
||||
_wheels = new DrawingWheels();
|
||||
_wheels.setWheelNumber((int)(Math.random() * 8 + 0));
|
||||
}
|
||||
public boolean SetPictureSize(int width, int height) {
|
||||
if (width < drawingDumpTrackWidth || height < drawingDumpTrackHeight) return false;
|
||||
picture_width = width;
|
||||
picture_height = height;
|
||||
if (_StartPosX != null || _StartPosY != null) {
|
||||
if (_StartPosX + drawingDumpTrackWidth > picture_width)
|
||||
{
|
||||
_StartPosX = _StartPosX - (_StartPosX + drawingDumpTrackWidth - picture_width);
|
||||
}
|
||||
else if (_StartPosX < 0) _StartPosX = 0;
|
||||
if (_StartPosY + drawingDumpTrackHeight > picture_height)
|
||||
{
|
||||
_StartPosY = _StartPosY - (_StartPosY + drawingDumpTrackHeight - picture_height);
|
||||
}
|
||||
else if (_StartPosY < 0) _StartPosY = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x < 0 || x + drawingDumpTrackWidth > picture_width) { x = 0; }
|
||||
if (y < 0 || y + drawingDumpTrackHeight > picture_height) { y = 0; }
|
||||
_StartPosX = x;
|
||||
_StartPosY = y;
|
||||
}
|
||||
public boolean MoveTransport(DirectionType direction) {
|
||||
if (EntityDumpTrack == null || _StartPosX == null || _StartPosY == null) return false;
|
||||
switch (direction) {
|
||||
case Left:
|
||||
if (_StartPosX - EntityDumpTrack.Step > 0) {
|
||||
_StartPosX -= (int)EntityDumpTrack.Step;
|
||||
}
|
||||
return true;
|
||||
case Up:
|
||||
if (_StartPosY - EntityDumpTrack.Step > 0)
|
||||
{
|
||||
_StartPosY -= (int)EntityDumpTrack.Step;
|
||||
}
|
||||
return true;
|
||||
case Right:
|
||||
if (_StartPosX + drawingDumpTrackWidth + (int)EntityDumpTrack.Step < picture_width - EntityDumpTrack.Step)
|
||||
{
|
||||
_StartPosX += (int)EntityDumpTrack.Step;
|
||||
}
|
||||
return true;
|
||||
case Down:
|
||||
if (_StartPosY + drawingDumpTrackHeight + (int)EntityDumpTrack.Step < picture_height - EntityDumpTrack.Step)
|
||||
{
|
||||
_StartPosY += (int)EntityDumpTrack.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics2D g2D)
|
||||
{
|
||||
if (EntityDumpTrack == null) {
|
||||
return;
|
||||
}
|
||||
g2D.setStroke(new BasicStroke(3));
|
||||
g2D.setPaint(EntityDumpTrack.getBodyColor());
|
||||
g2D.fillRect(_StartPosX, _StartPosY + 40, 160, 10);
|
||||
g2D.fillRect(_StartPosX + 120, _StartPosY, 40, 40);
|
||||
_wheels.drawWheels(g2D, Color.BLACK, _StartPosX, _StartPosY);
|
||||
if (EntityDumpTrack.getBodyWork())
|
||||
{
|
||||
g2D.setPaint(EntityDumpTrack.getAdditionalColor());
|
||||
Polygon dumpBoxPolygon = new Polygon();
|
||||
dumpBoxPolygon.addPoint(_StartPosX + 20, _StartPosY);
|
||||
dumpBoxPolygon.addPoint(_StartPosX + 120, _StartPosY);
|
||||
dumpBoxPolygon.addPoint(_StartPosX + 100, _StartPosY + 39);
|
||||
dumpBoxPolygon.addPoint(_StartPosX, _StartPosY + 39);
|
||||
g2D.fillPolygon(dumpBoxPolygon);
|
||||
}
|
||||
if (EntityDumpTrack.getAwning())
|
||||
{
|
||||
g2D.setPaint(EntityDumpTrack.getAdditionalColor());
|
||||
Polygon tentPolygon = new Polygon();
|
||||
tentPolygon.addPoint(_StartPosX + 15, _StartPosY);
|
||||
tentPolygon.addPoint(_StartPosX + 120, _StartPosY);
|
||||
tentPolygon.addPoint(_StartPosX + 115, _StartPosY + 10);
|
||||
tentPolygon.addPoint(_StartPosX + 10, _StartPosY + 10);
|
||||
g2D.fillPolygon(tentPolygon);
|
||||
}
|
||||
}
|
||||
}
|
54
src/src/DrawingWheels.java
Normal file
54
src/src/DrawingWheels.java
Normal file
@ -0,0 +1,54 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingWheels {
|
||||
|
||||
private WheelNumber wheelNumber;
|
||||
|
||||
public void setWheelNumber(int number) {
|
||||
switch (number) {
|
||||
case 2:
|
||||
wheelNumber = wheelNumber.Two;
|
||||
break;
|
||||
case 3:
|
||||
wheelNumber = wheelNumber.Three;
|
||||
break;
|
||||
case 4:
|
||||
wheelNumber = wheelNumber.Four;
|
||||
break;
|
||||
default: wheelNumber = wheelNumber.Two;
|
||||
}
|
||||
}
|
||||
|
||||
public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
|
||||
switch (wheelNumber) {
|
||||
case Two:
|
||||
drawTwoWheels(g2D, color, _startPosX, _startPosY);
|
||||
break;
|
||||
case Three:
|
||||
drawThreeWheels(g2D, color, _startPosX, _startPosY);
|
||||
break;
|
||||
case Four:
|
||||
drawFourWheels(g2D, color, _startPosX, _startPosY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void drawTwoWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
|
||||
g2D.setColor(color);
|
||||
g2D.fillOval(_startPosX, _startPosY + 50, 40, 40);
|
||||
g2D.fillOval(_startPosX + 120, _startPosY + 50, 40, 40);
|
||||
}
|
||||
|
||||
private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
|
||||
g2D.setColor(color);
|
||||
drawTwoWheels(g2D, color, _startPosX, _startPosY);
|
||||
g2D.fillOval(_startPosX + 40, _startPosY + 50, 40, 40);
|
||||
}
|
||||
|
||||
private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
|
||||
g2D.setColor(color);
|
||||
drawThreeWheels(g2D, color, _startPosX, _startPosY);
|
||||
g2D.fillOval(_startPosX + 80, _startPosY + 50, 40, 40);
|
||||
}
|
||||
}
|
||||
|
25
src/src/EntityDumpTrack.java
Normal file
25
src/src/EntityDumpTrack.java
Normal file
@ -0,0 +1,25 @@
|
||||
import java.awt.*;
|
||||
public class EntityDumpTrack {
|
||||
private int Speed;
|
||||
private double Weight;
|
||||
private Color BodyColor;
|
||||
public Color getBodyColor() {return BodyColor;}
|
||||
private Color AdditionalColor;
|
||||
public Color getAdditionalColor() {return AdditionalColor;}
|
||||
private boolean BodyWork;
|
||||
public boolean getBodyWork() {return BodyWork;}
|
||||
private boolean Awning;
|
||||
public boolean getAwning() {return Awning;}
|
||||
public double Step;
|
||||
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean _bodyWork, boolean _awning)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodycolor;
|
||||
AdditionalColor = additionalcolor;
|
||||
BodyWork = _bodyWork;
|
||||
Awning = _awning;
|
||||
Step = Speed * 100 / Weight;
|
||||
}
|
||||
}
|
||||
|
124
src/src/FormDumpTrack.java
Normal file
124
src/src/FormDumpTrack.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 FormDumpTrack extends JFrame {
|
||||
private String title;
|
||||
private Dimension dimension;
|
||||
private int Width, Height;
|
||||
private CanvasDumpTrack canvasAirbus = new CanvasDumpTrack();
|
||||
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 FormDumpTrack(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 sheepPipes = new Random().nextBoolean();
|
||||
boolean fuelTank = new Random().nextBoolean();;
|
||||
canvasAirbus._drawingDumpTrack = new DrawingDumpTrack();
|
||||
canvasAirbus._drawingDumpTrack.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
|
||||
canvasAirbus._drawingDumpTrack.SetPictureSize(Width, Height);
|
||||
canvasAirbus._drawingDumpTrack.SetPosition(StartPositionX, StartPositionY);
|
||||
canvasAirbus.repaint();
|
||||
}
|
||||
});
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if (canvasAirbus._drawingDumpTrack == null) return;
|
||||
boolean result = false;
|
||||
switch ((((JButton)(event.getSource())).getName())) {
|
||||
case "UP":
|
||||
result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "DOWN":
|
||||
result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "LEFT":
|
||||
result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "RIGHT":
|
||||
result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result) {
|
||||
canvasAirbus.repaint();
|
||||
}
|
||||
}
|
||||
};
|
||||
UpButton.addActionListener(actionListener);
|
||||
DownButton.addActionListener(actionListener);
|
||||
LeftButton.addActionListener(actionListener);
|
||||
RightButton.addActionListener(actionListener);
|
||||
|
||||
setSize(dimension.width,dimension.height);
|
||||
setLayout(null);
|
||||
canvasAirbus.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(canvasAirbus);
|
||||
setVisible(true);
|
||||
//обработка события изменения размеров окна
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
public void componentResized(ComponentEvent e) {
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
if (canvasAirbus._drawingDumpTrack != null)
|
||||
canvasAirbus._drawingDumpTrack.SetPictureSize(Width, Height);
|
||||
canvasAirbus.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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
7
src/src/WheelNumber.java
Normal file
7
src/src/WheelNumber.java
Normal file
@ -0,0 +1,7 @@
|
||||
public enum WheelNumber {
|
||||
Two,
|
||||
|
||||
Three,
|
||||
|
||||
Four
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user