работа 3 база на яве
This commit is contained in:
parent
f9a8216e8c
commit
1924451a63
@ -2,6 +2,7 @@ package DrawningObjects;
|
||||
|
||||
import Entities.EntityLocomotive;
|
||||
import MovementStrategy.DirectionType;
|
||||
import MovementStrategy.IMoveableObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
@ -96,8 +97,6 @@ public class DrawningLocomotive {
|
||||
if (entityLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
g.clearRect(0, 0, _pictureWidth, _pictureHeight);
|
||||
// корпус
|
||||
g.setColor(entityLocomotive.BodyColor);
|
||||
|
||||
@ -136,6 +135,9 @@ public class DrawningLocomotive {
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
public IMoveableObject GetMoveableObject(){
|
||||
return (IMoveableObject) this;
|
||||
}
|
||||
|
||||
public boolean CanMove(DirectionType direction) {
|
||||
if (entityLocomotive == null) {
|
||||
|
@ -15,6 +15,7 @@ public class FormElectricLocomotive extends JFrame {
|
||||
private Canvas canvas;
|
||||
private JButton buttonCreateElectricLocomotive;
|
||||
private JButton buttonCreateLocomotive;
|
||||
public JButton buttonSelectLocomotive;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonDown;
|
||||
@ -28,7 +29,12 @@ public class FormElectricLocomotive extends JFrame {
|
||||
{
|
||||
return;
|
||||
}
|
||||
_drawningLocomotive.DrawTransport(canvas.getGraphics());
|
||||
Graphics g = canvas.getGraphics();
|
||||
g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
_drawningLocomotive.DrawTransport(g);
|
||||
}
|
||||
public DrawningLocomotive GetSelectedLocomotive(){
|
||||
return _drawningLocomotive;
|
||||
}
|
||||
private void InitializeComponent(){
|
||||
buttonCreateElectricLocomotive = new JButton("Создать Электропоезд");
|
||||
@ -37,6 +43,9 @@ public class FormElectricLocomotive extends JFrame {
|
||||
buttonCreateLocomotive = new JButton("Создать локомотив");
|
||||
buttonCreateLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonSelectLocomotive = new JButton("Выбор");
|
||||
buttonSelectLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonStep = new JButton("шаг");
|
||||
buttonStep.setName("шаг");
|
||||
|
||||
@ -85,12 +94,14 @@ public class FormElectricLocomotive extends JFrame {
|
||||
|
||||
setSize(1000, 600);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setTitle("Рисование локомотивов");
|
||||
setLayout(null);
|
||||
|
||||
canvas = new Canvas();
|
||||
canvas.setBounds(0, 0, 980, 560);
|
||||
buttonCreateElectricLocomotive.setBounds(10, 470, 200, 40);
|
||||
buttonCreateLocomotive.setBounds(10, 520, 200, 40);
|
||||
buttonSelectLocomotive.setBounds(770, 520, 200, 40);
|
||||
buttonUp.setBounds(50, 380, 40, 40);
|
||||
buttonDown.setBounds(50, 420, 40, 40);
|
||||
buttonRight.setBounds(90, 420, 40, 40);
|
||||
@ -100,9 +111,9 @@ public class FormElectricLocomotive extends JFrame {
|
||||
comboBoxStrategy.setBounds(770, 10, 200, 30);
|
||||
comboBoxWheels.setBounds(220, 520, 250, 40);
|
||||
|
||||
|
||||
add(buttonCreateElectricLocomotive);
|
||||
add(buttonCreateLocomotive);
|
||||
add(buttonSelectLocomotive);
|
||||
add(buttonUp);
|
||||
add(buttonDown);
|
||||
add(buttonRight);
|
||||
@ -127,14 +138,22 @@ public class FormElectricLocomotive extends JFrame {
|
||||
_drawWheels.setWheelsCount(countWheels);
|
||||
System.out.println(e.getActionCommand());
|
||||
Random random = new Random();
|
||||
Color baseColor = JColorChooser.showDialog(null,"Основной цвет", Color.RED);
|
||||
if (baseColor == null){
|
||||
baseColor = new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256));
|
||||
}
|
||||
Color addColor = JColorChooser.showDialog(null,"Дополнительный цвет", Color.RED);
|
||||
if (addColor == null){
|
||||
addColor = new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256));
|
||||
}
|
||||
_drawningLocomotive = new DrawningElectricLocomotive(
|
||||
random.nextInt(100, 300), random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
baseColor,
|
||||
addColor,
|
||||
comboBoxWheels.getSelectedIndex(), countWheels,
|
||||
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
|
||||
canvas.getWidth(), canvas.getHeight()
|
||||
@ -156,11 +175,15 @@ public class FormElectricLocomotive extends JFrame {
|
||||
}
|
||||
System.out.println(e.getActionCommand());
|
||||
Random random = new Random();
|
||||
Color baseColor = JColorChooser.showDialog(null,"Основной цвет", Color.RED);
|
||||
if (baseColor == null){
|
||||
baseColor = new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256));
|
||||
}
|
||||
_drawningLocomotive = new DrawningLocomotive(
|
||||
random.nextInt(100, 300), random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
baseColor,
|
||||
comboBoxWheels.getSelectedIndex(), countWheels,
|
||||
canvas.getWidth(), canvas.getHeight()
|
||||
);
|
||||
|
@ -0,0 +1,132 @@
|
||||
package Forms;
|
||||
|
||||
import DrawningObjects.DrawningLocomotive;
|
||||
import Generics.LocomotivesGenericCollection;
|
||||
import MovementStrategy.DrawningObjectLocomotive;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class FormLocomotiveCollection extends JFrame {
|
||||
private final LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive> _locomotives;
|
||||
private Canvas canvas;
|
||||
private JButton buttonAddLocomotive;
|
||||
private JButton buttonRemoveLocomotive;
|
||||
private JButton buttonRefreshCollection;
|
||||
private JTextField textBoxNumber;
|
||||
private JPanel buttonBox;
|
||||
|
||||
public void repaint(){
|
||||
|
||||
if (_locomotives == null)
|
||||
return;
|
||||
Graphics g = canvas.getGraphics();
|
||||
g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
canvas.setBackground(getBackground());
|
||||
g.drawImage(_locomotives.ShowLocomotives(), 0, 0, canvas);
|
||||
}
|
||||
private void InitializeComponent(){
|
||||
canvas = new Canvas();
|
||||
|
||||
|
||||
buttonAddLocomotive = new JButton("Добавить локомотив");
|
||||
buttonAddLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
textBoxNumber = new JTextField();
|
||||
|
||||
buttonRemoveLocomotive = new JButton("Удалить локомотив");
|
||||
buttonRemoveLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonRefreshCollection = new JButton("Обновить колекцию");
|
||||
buttonRefreshCollection.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonBox = new JPanel(new GridLayout(0, 1, 0, 10));
|
||||
JLabel labelBB = new JLabel("Инструменты");
|
||||
|
||||
buttonBox.add(labelBB);
|
||||
buttonBox.add(buttonAddLocomotive);
|
||||
buttonBox.add(textBoxNumber);
|
||||
buttonBox.add(buttonRemoveLocomotive);
|
||||
buttonBox.add(buttonRefreshCollection);
|
||||
|
||||
buttonBox.setBounds(760, 3, 200, 220);
|
||||
canvas.setBounds(10, 3, 740, 700);
|
||||
|
||||
setSize(1000, 700);
|
||||
setTitle("Колекция локомотивов");
|
||||
setLayout(null);
|
||||
|
||||
add(buttonBox);
|
||||
add(canvas);
|
||||
}
|
||||
void InitializeLogic(){
|
||||
buttonAddLocomotive.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
FormElectricLocomotive form = new FormElectricLocomotive();
|
||||
form.setVisible(true);
|
||||
form.buttonSelectLocomotive.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
if (_locomotives.Add(form.GetSelectedLocomotive()) != -1){
|
||||
JOptionPane.showMessageDialog(null,
|
||||
"Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
System.out.println("Объект добавлен");
|
||||
repaint();
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null,
|
||||
"Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
System.out.println("Не удалось добавить объект");
|
||||
}
|
||||
form.dispose();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
buttonRemoveLocomotive.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (JOptionPane.showConfirmDialog(null,
|
||||
"Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
|
||||
{
|
||||
int pos = 0;
|
||||
try {
|
||||
pos = Integer.parseInt(textBoxNumber.getText());
|
||||
}
|
||||
catch (Exception ex){
|
||||
System.out.println(ex.toString());
|
||||
pos = 0;
|
||||
}
|
||||
if (_locomotives.Sub(pos) != null)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null,
|
||||
"Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null,
|
||||
"Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonRefreshCollection.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
public FormLocomotiveCollection(){
|
||||
InitializeComponent();
|
||||
_locomotives = new LocomotivesGenericCollection<>(canvas.getWidth(), canvas.getHeight());
|
||||
InitializeLogic();
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package Generics;
|
||||
|
||||
import DrawningObjects.DrawningLocomotive;
|
||||
import MovementStrategy.IMoveableObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class LocomotivesGenericCollection<T extends DrawningLocomotive, U extends IMoveableObject> {
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private final int _placeSizeWidth = 210;
|
||||
private final int _placeSizeHeight = 90;
|
||||
private SetGeneric<T> _collection;
|
||||
public LocomotivesGenericCollection(int pictureWidth, int pictureHeight){
|
||||
int width = pictureWidth / _placeSizeWidth;
|
||||
int height = pictureHeight / _placeSizeHeight;
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
_collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
public int Add(T obj)
|
||||
{
|
||||
if (obj == null) {
|
||||
return -1;
|
||||
}
|
||||
obj.SetPictureSize(_pictureWidth, _pictureHeight);
|
||||
if (_collection.Insert(obj)) {
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public T Sub(int pos){
|
||||
T obj = _collection.Get(pos);
|
||||
_collection.Remove(pos);
|
||||
return obj;
|
||||
}
|
||||
public U GetU(int pos){
|
||||
return (U) (_collection.Get(pos)).GetMoveableObject();
|
||||
}
|
||||
public Image ShowLocomotives()
|
||||
{
|
||||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics g = bmp.getGraphics();
|
||||
DrawBackground(g);
|
||||
DrawObjects(g);
|
||||
return bmp;
|
||||
}
|
||||
private void DrawBackground(Graphics g){
|
||||
g.setColor(Color.BLACK);
|
||||
g.setFont(new Font("myFont", Font.PLAIN, 3));
|
||||
for (int i = 0; i < _pictureHeight / _placeSizeWidth; ++i){
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j){
|
||||
g.drawLine(i * _placeSizeWidth,
|
||||
j * _placeSizeHeight,
|
||||
i * _placeSizeWidth + _placeSizeWidth / 2,
|
||||
j * _placeSizeHeight);
|
||||
}
|
||||
g.drawLine(i * _placeSizeWidth,
|
||||
0,
|
||||
i * _placeSizeWidth,
|
||||
_pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
private void DrawObjects(Graphics g) {
|
||||
int ColumnCount = _pictureWidth / _placeSizeWidth;
|
||||
int RowsCount = _pictureHeight / _placeSizeHeight;
|
||||
for (int i = 0; i < _collection.Count(); ++i) {
|
||||
DrawningLocomotive drawningLocomotive = _collection.Get(i);
|
||||
if (drawningLocomotive == null) {
|
||||
continue;
|
||||
}
|
||||
drawningLocomotive.SetPosition(
|
||||
(i % ColumnCount) * _placeSizeWidth,
|
||||
((RowsCount - (i / ColumnCount) - 1) * _placeSizeHeight));
|
||||
drawningLocomotive.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
50
ProjectElectricLocomotive/src/Generics/SetGeneric.java
Normal file
50
ProjectElectricLocomotive/src/Generics/SetGeneric.java
Normal file
@ -0,0 +1,50 @@
|
||||
package Generics;
|
||||
|
||||
import DrawningObjects.DrawningLocomotive;
|
||||
|
||||
public class SetGeneric<T extends DrawningLocomotive> {
|
||||
private T[] _places;
|
||||
public int Count(){
|
||||
return _places.length;
|
||||
}
|
||||
public SetGeneric(int count){
|
||||
_places = (T[])new DrawningLocomotive[count];
|
||||
}
|
||||
boolean Insert(T obj, int position){
|
||||
if (position >= _places.length || position < 0){
|
||||
return false;
|
||||
}
|
||||
int fillLineEnd = _places.length;
|
||||
for (int i = position; i < _places.length; ++i){
|
||||
if (_places[i] == null){
|
||||
fillLineEnd = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (fillLineEnd == _places.length){
|
||||
return false;
|
||||
}
|
||||
for (int i = fillLineEnd; i > position; --i){
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[fillLineEnd] = obj;
|
||||
return true;
|
||||
}
|
||||
boolean Insert(T obj){
|
||||
return Insert(obj, 0);
|
||||
}
|
||||
public boolean Remove(int position){
|
||||
if (position >= _places.length || position < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
public T Get(int position){
|
||||
if (position >= _places.length || position < 0){
|
||||
return null;
|
||||
}
|
||||
return _places[position];
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ import Forms.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FormElectricLocomotive form = new FormElectricLocomotive();
|
||||
|
||||
FormLocomotiveCollection form = new FormLocomotiveCollection();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user