Скоро будет готово

This commit is contained in:
gg12 darfren 2023-10-26 14:50:40 +04:00
parent bc5671ff30
commit 1c466317b8
6 changed files with 282 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package MonorailHard.DrawningObjects;
import MonorailHard.DirectionType;
import MonorailHard.Entities.EntityMonorail;
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
import MonorailHard.MovementStrategy.IMoveableObject;
import com.sun.source.tree.ImportTree;
@ -78,6 +79,8 @@ public class DrawningMonorail {
return;
_startPosX = x;
_startPosY = y;
DrawningWheels.ChangeX(_startPosX);
DrawningWheels.ChangeY(_startPosY);
if(x + _monorailWidth >= _pictureWidth|| y + _monorailHeight >= _pictureHeight || x < 0 || y < 0){
_startPosX = 0;
_startPosY = 0;
@ -111,7 +114,9 @@ public class DrawningMonorail {
return can;
}
public IMoveableObject GetMoveableObject(){
return new DrawningObjectMonorail(this);
}
public void MoveTransport(DirectionType direction){
if (!CanMove(direction) || EntityMonorail == null)

View File

@ -3,6 +3,7 @@ package MonorailHard;
import MonorailHard.DrawningObjects.DrawningLocomotive;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.MovementStrategy.*;
import org.w3c.dom.ranges.DocumentRange;
import javax.swing.*;
import javax.swing.text.AbstractDocument;
@ -11,22 +12,41 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormMonorail{
private DrawningMonorail DrawningMonorail;
private AbstractStrategy _abstractStrategy;
public JButton buttonSelect;
public JFrame MonorailFrame;
Canvas canv;
static int pictureBoxWidth = 882;
static int pictureBoxHeight = 453;
public Color ChooseColor(JFrame MonorailFrame){
JColorChooser dialog = new JColorChooser();
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.WHITE);
return res;
}
public DrawningMonorail SelectedMonorail(){
return DrawningMonorail;
}
public void Draw(){
if(DrawningMonorail == null)
return;
canv.repaint();
}
public FormMonorail(){
JFrame MonorailFrame =new JFrame ();
MonorailFrame =new JFrame ();
JButton buttonCreate = new JButton("Создать");
JButton buttonCreateLocomotive = new JButton("Создать локомотив");
JButton buttonStep = new JButton("Шаг");
buttonSelect = new JButton ("Выбрать");
JComboBox comboBoxStrategy = new JComboBox(
new String[]{
"Довести до центра",
@ -89,8 +109,13 @@ public class FormMonorail{
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color choosen = ChooseColor(MonorailFrame);
if(choosen != null){
color = choosen;
}
DrawningMonorail = new DrawningMonorail(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
color,
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
pictureBoxWidth, pictureBoxHeight);
@ -104,19 +129,30 @@ public class FormMonorail{
new ActionListener() {
public void actionPerformed(ActionEvent e){
Random random = new Random();
Color color = 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));
Color choosen = ChooseColor(MonorailFrame);
if(choosen != null){
color = choosen;
}
choosen = ChooseColor(MonorailFrame);
if(choosen != null){
additionalColor = choosen;
}
DrawningMonorail = new DrawningLocomotive(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
color,
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
random.nextInt(1, 6),
pictureBoxWidth, pictureBoxHeight, random.nextBoolean(), random.nextBoolean(),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)));
additionalColor);
canv.DrawningMonorail = DrawningMonorail;
comboBoxStrategy.enable(true);
Draw();
}
}
);
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -160,6 +196,7 @@ public class FormMonorail{
MonorailFrame.setLayout(null);
canv = new Canvas();
canv.setSize(pictureBoxWidth, pictureBoxHeight);
buttonSelect.setBounds(383,401, 180, 40);
buttonCreate.setBounds(198, 401, 180, 40);
buttonCreateLocomotive.setBounds(12, 401, 180, 40);
RightButton.setBounds(840,411,30,30);
@ -177,6 +214,7 @@ public class FormMonorail{
MonorailFrame.add(RightButton);
MonorailFrame.add(comboBoxStrategy);
MonorailFrame.add(buttonStep);
MonorailFrame.add(buttonSelect);
MonorailFrame.setVisible(true);
}
}

View File

@ -0,0 +1,96 @@
package MonorailHard;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.Generics.MonorailGenericCollection;
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.Normalizer;
public class FormMonorailCollection {
private final MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails;
private int pictureBoxWidth = 605;
private int pictureBoxHeight = 426;
CollectionCanvas canv;
void Draw(){
if(canv == null)
return;
canv.repaint();
}
public FormMonorailCollection(){
_monorails = new MonorailGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
canv = new CollectionCanvas();
JPanel toolBox = new JPanel();
JFrame collectionFrame = new JFrame();
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880,497);
toolBox.setBounds(623, 12, 227, 426);
canv.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
JButton addButton = new JButton("Добавить");
JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить");
JTextField monorailNumb = new JTextField();
GridLayout lay = new GridLayout(4,1);
toolBox.setLayout(lay);
toolBox.add(addButton);
toolBox.add(monorailNumb);
toolBox.add(removeButton);
toolBox.add(refreshButton);
collectionFrame.add(toolBox);
collectionFrame.add(canv);
collectionFrame.setVisible(true);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_monorails == null) {
return;
}
FormMonorail form = new FormMonorail();
form.buttonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_monorails.Insert(form.SelectedMonorail()) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект добавлен");
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось добавить объект");
}
canv._monorails = _monorails;
form.MonorailFrame.dispose();
Draw();
}
});
}
});
}
}
class CollectionCanvas extends JComponent {
public MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails;
public CollectionCanvas(){
}
@Override
public void paintComponent (Graphics g){
if (_monorails == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(_monorails.ShowMonorails(), 0, 0, this);
super.repaint();
}
}

View File

@ -0,0 +1,84 @@
package MonorailHard.Generics;
import MonorailHard.DrawningObjects.DrawningMonorail;
import MonorailHard.MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class MonorailGenericCollection<T extends DrawningMonorail, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 133;
private final int _placeSizeHeight = 50;
private final SetGeneric<T> _collection;
public MonorailGenericCollection(int picWidth, int picHeight){
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Insert(T obj){
if (obj == null)
{
return -1;
}
return _collection.Insert(obj);
}
public boolean Remove(int position){
return _collection.Remove(position);
}
public U GetU(int pos){
T ans = _collection.Get(pos);
if(ans == null)
return null;
return (U)ans.GetMoveableObject();
}
private void DrawBackground(Graphics g)
{
g.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _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)
{
for (int i = 0; i < _collection.Count; i++)
{
DrawningMonorail monorail = _collection.Get(i);
if (monorail != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
monorail.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight );
monorail.DrawMonorail((Graphics2D) g);
}
}
}
public BufferedImage ShowMonorails()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
}

View File

@ -0,0 +1,53 @@
package MonorailHard.Generics;
public class SetGeneric <T extends Object>{
private final Object[] _places;
public int Count;
public SetGeneric(int count){
_places = new Object[count];
Count = count;
}
public int Insert(T monorail){
return Insert(monorail, 0);
}
public int Insert(T monorail, int position){
if(!(position >= 0 && position < Count))
return -1;
if(_places[position] == null){
_places[position] = monorail;
}
else{
int place = -1;
for(int i = position; i < Count; i++){
if(_places[i] == null){
place = i;
break;
}
}
if(place == -1)
return -1;
for(int i = place - 1; i >= position; i--)
_places[i+1] = _places[i];
_places[position] = monorail;
}
return position;
}
public boolean Remove(int position){
if(!(position >= 0 && position < Count))
return false;
_places[position] = null;
return true;
}
public T Get(int position){
if(!(position >= 0 && position < Count))
return null;
return (T)_places[position];
}
}

View File

@ -15,6 +15,6 @@ import javax.swing.*;
public class Main {
public static void main(String[] args) throws IOException {
FormMonorail form = new FormMonorail();
FormMonorailCollection form = new FormMonorailCollection();
}
}