PIbd-11 Karakozov_AK LabWork03 Hard #3

Closed
insideq wants to merge 25 commits from LabWork03 into LabWork02
Showing only changes of commit 9414dc58d1 - Show all commits

View File

@ -1,123 +0,0 @@
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 FormExcavator extends JFrame {
private String title;
private Dimension dimension;
private int Width, Height;
private CanvasExcavator canvasExcavator = new CanvasExcavator();
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 FormExcavator(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("ProjectExcavator/res/up.png");
UpButton.setIcon(iconUp);
UpButton.setName("UP");
DownButton.setName("DOWN");
Icon iconDown = new ImageIcon("ProjectExcavator/res/down.png");
DownButton.setIcon(iconDown);
LeftButton.setName("LEFT");
Icon iconLeft = new ImageIcon("ProjectExcavator/res/left.png");
LeftButton.setIcon(iconLeft);
RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("ProjectExcavator/res/right.png");
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 = (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();
canvasExcavator._drawingExcavator = new DrawingExcavator();
canvasExcavator._drawingExcavator.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
canvasExcavator._drawingExcavator.SetPictureSize(Width, Height);
canvasExcavator._drawingExcavator.SetPosition( StartPositionX, StartPositionY);
canvasExcavator.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasExcavator._drawingExcavator == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "UP":
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Up);
break;
case "DOWN":
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Down);
break;
case "LEFT":
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Left);
break;
case "RIGHT":
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasExcavator.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 35);
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
add(CreateButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(canvasExcavator);
setVisible(true);
//обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasExcavator._drawingExcavator != null)
canvasExcavator._drawingExcavator.SetPictureSize(Width, Height);
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 35);
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
}
});
}
}