This commit is contained in:
Alenka 2023-12-29 16:43:56 +04:00
parent beff4a4b13
commit e3c4b3b319
7 changed files with 85 additions and 152 deletions

View File

@ -42,8 +42,8 @@ public class CruiserCollectionFrame extends JFrame {
}
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
GameFrame form = new GameFrame();
if (form.getSelectedCar() != null) {
if (_cruisers.plus(_cruisers, form.SelectedCar)) {
if (form.getSelectedCruiser() != null) {
if (_cruisers.plus(_cruisers, form.SelectedCruiser)) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
@ -53,9 +53,9 @@ public class CruiserCollectionFrame extends JFrame {
}
});
this.add(ButtonAddCruiser);
JButton ButtonRemoveCar = new JButton("Удалить объект");
ButtonRemoveCar.setBounds(600, 250, 150, 30);
ButtonRemoveCar.addActionListener(new ActionListener() {
JButton ButtomRemoveCruiser = new JButton("Удалить объект");
ButtomRemoveCruiser.setBounds(600, 250, 150, 30);
ButtomRemoveCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
@ -80,7 +80,7 @@ public class CruiserCollectionFrame extends JFrame {
repaint();
}
});
this.add(ButtonRemoveCar);
this.add(ButtomRemoveCruiser);
JButton ButtonRefresh = new JButton("Обновить");
ButtonRefresh.setBounds(600, 300, 150, 30);
ButtonRefresh.addActionListener(new ActionListener() {
@ -174,8 +174,8 @@ public class CruiserCollectionFrame extends JFrame {
}
public void draw(Graphics g) {
if (listBoxStoragesJList.getSelectedIndex() != -1) {
_storage.getByIndex(listBoxStorages.get(listBoxStoragesJList.getSelectedIndex())).ShowCars(g);
_cruisers.ShowCars(g);
_storage.getByIndex(listBoxStorages.get(listBoxStoragesJList.getSelectedIndex())).Showcruisers(g);
_cruisers.Showcruisers(g);
}
}
}

View File

@ -42,7 +42,7 @@ public class CruiserGenericCollection<T extends DrawingCruiser, U extends IMovea
return null;
return _collection.Get(position);
}
public void ShowCars(Graphics gr)
public void Showcruisers(Graphics gr)
{
DrawBackground(gr);
DrawObjects(gr);
@ -54,7 +54,7 @@ public class CruiserGenericCollection<T extends DrawingCruiser, U extends IMovea
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j)
{//линия рамзетки места
{
g.drawLine( i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
@ -65,22 +65,17 @@ public class CruiserGenericCollection<T extends DrawingCruiser, U extends IMovea
}
private void DrawObjects(Graphics g)
{
DrawingCruiser car;
DrawingCruiser cruiser;
int numPlacesInRow = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count(); i++)
{
// TODO получение объекта
// TODO установка позиции
// TODO прорисовка объекта
car = _collection.Get(i);
if (car != null)
cruiser = _collection.Get(i);
if (cruiser != null)
{
car.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10);
//car.SetPosition(_placeSizeWidth * (i/ numPlacesInColumn) + _placeSizeWidth / 20, (i % numPlacesInColumn ) *_placeSizeHeight + _placeSizeHeight / 10);
car.DrawTransport(g);
cruiser.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10);
cruiser.DrawTransport(g);
}
}
}
}
}

View File

@ -1,51 +1,47 @@
import java.util.*;
import java.util.stream.Collectors;
public class CruiserGenericStorage {
Map<String, CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>> _carStorages;
Map<String, CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>> _cruiserStorages;
public List<String> Keys;
public List<String> Keys(){
if(_carStorages == null)
if(_cruiserStorages == null)
return null;
return _carStorages.keySet().stream().collect(Collectors.toList());
return _cruiserStorages.keySet().stream().collect(Collectors.toList());
}
private int _pictureWidth;
private int _pictureHeight;
public CruiserGenericStorage(int pictureWidth, int pictureHeight)
{
_carStorages = new HashMap<String,
_cruiserStorages = new HashMap<String,
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(String name)
{
_carStorages.put(name, new CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>(_pictureWidth, _pictureHeight));
_cruiserStorages.put(name, new CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name)
{
if (!_carStorages.containsKey(name))
if (!_cruiserStorages.containsKey(name))
{
return;
}
_carStorages.remove(name);
_cruiserStorages.remove(name);
}
public CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> getByIndex(String indexOfDict){
if (_carStorages.containsKey(indexOfDict)){
return _carStorages.get(indexOfDict);
if (_cruiserStorages.containsKey(indexOfDict)){
return _cruiserStorages.get(indexOfDict);
}
return null;
}
public CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> Get(String name){
if(!_carStorages.containsKey(name))
if(!_cruiserStorages.containsKey(name))
return null;
return _carStorages.get(name);
return _cruiserStorages.get(name);
}
public DrawingCruiser Get(String collectionName, int position){
return _carStorages.get(collectionName).Get(position);
return _cruiserStorages.get(collectionName).Get(position);
}
}

View File

@ -4,154 +4,129 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class GameFrame extends JDialog {
protected DrawingCruiser SelectedCar;
protected DrawingCruiser SelectedCruiser;
GamePanel panel;
public DrawingCruiser _drawningCar;
public DrawingCruiser _drawningCruiser;
GameFrame() {
this.setSize(710, 535);
this.setTitle("DumpTruck");
this.setTitle("Cruiser");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
this.setModalityType(ModalityType.APPLICATION_MODAL);
this.setResizable(false);
this.setLocationRelativeTo(null);
panel = new GamePanel();
this.add(panel);
this.setVisible(true);
}
GameFrame(DrawingCruiser newCar){
_drawningCar = newCar;
GameFrame(DrawingCruiser newCruiser){
_drawningCruiser = newCruiser;
this.setSize(710, 535);
this.setTitle("DumpTruck");
this.setTitle("Cruiser");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
this.setModalityType(ModalityType.APPLICATION_MODAL);
this.setResizable(false);
this.setLocationRelativeTo(null);
panel = new GamePanel();
this.add(panel);
this.setVisible(true);
}
public DrawingCruiser getSelectedCar(){
return SelectedCar;
public DrawingCruiser getSelectedCruiser(){
return SelectedCruiser;
}
public void ChangeCar(DrawingCruiser newCar){panel.ChangeCarOnPanel(newCar);}
public void ChangeCruiser(DrawingCruiser newCruiser){panel.ChangeCruiserOnPanel(newCruiser);}
public class GamePanel extends JPanel {
static final int SCREEN_W = 700;
static final int SCREEN_H = 500;
int xx = 0;
int yy = 0;
private AbstractStrategy _abstractStrategy;
JColorChooser dialogColor = new JColorChooser();
GamePanel() {
if (_drawningCar != null){repaint();}
if (_drawningCruiser != null){repaint();}
this.setLayout(null);
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
GridBagConstraints layers = new GridBagConstraints();
JButton buttonCar = new JButton("Создать грузовик");
buttonCar.addActionListener(new ActionListener() {
JButton buttonCruiser = new JButton("Создать объект");
buttonCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color color = Color.BLACK;
Color choosenColor = JColorChooser.showDialog(getParent(), "Выберите цвет", Color.WHITE);
if(choosenColor != null){
color = choosenColor;
}
_drawningCar = new DrawingCruiser(random.nextInt(100, 300),
_drawningCruiser = new DrawingCruiser(random.nextInt(100, 300),
random.nextInt(1000, 3000),
color,
GamePanel.SCREEN_W, GamePanel.SCREEN_H);
_drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10,
_drawningCruiser.SetPosition(random.nextInt(10, 100), random.nextInt(10,
100));
int ornament = random.nextInt(1, 4);
switch (ornament){
case 1:
_drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5));
_drawningCruiser.wheels = new NumberOfWheels(random.nextInt(2, 5));
break;
case 2:
_drawningCar.wheels = new DopClassRect(random.nextInt(2, 5));
_drawningCruiser.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCar.wheels = new DopClassMixed(random.nextInt(2, 5));
_drawningCruiser.wheels = new DopClassMixed(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonCar.setBounds(20 + 150, 390, 120, 30);
this.add(buttonCar);
JButton buttonDumpCar = new JButton("Создать самосвал");
buttonDumpCar.addActionListener(new ActionListener() {
buttonCruiser.setBounds(20 + 150, 390, 120, 30);
this.add(buttonCruiser);
JButton buttonAdvancedCruiser = new JButton("Создать продвинутый объект");
buttonAdvancedCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color color = Color.BLACK; Color colorAdd = Color.WHITE;
Color choosenColor = JColorChooser.showDialog(getParent(), "Выберите цвет", Color.WHITE);
Color choosenAddColor = JColorChooser.showDialog(getParent(), "Выберите цвет", Color.WHITE);
if(choosenColor != null){
color = choosenColor;
}
if(choosenAddColor != null){
colorAdd = choosenAddColor;
}
_drawningCar = new DrawingAdvancedCruiser(random.nextInt(100, 300),
_drawningCruiser = new DrawingAdvancedCruiser(random.nextInt(100, 300),
random.nextInt(1000, 3000),
color, colorAdd, random.nextBoolean(),random.nextBoolean(),random.nextBoolean(),
GamePanel.SCREEN_W, GamePanel.SCREEN_H); //TODO
_drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10,
GamePanel.SCREEN_W, GamePanel.SCREEN_H);
_drawningCruiser.SetPosition(random.nextInt(10, 100), random.nextInt(10,
100));
int ornament = random.nextInt(1, 4);
switch (ornament){
case 1:
_drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5));
_drawningCruiser.wheels = new NumberOfWheels(random.nextInt(2, 5));
break;
case 2:
_drawningCar.wheels = new DopClassRect(random.nextInt(2, 5));
_drawningCruiser.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCar.wheels = new DopClassMixed(random.nextInt(2, 5));
_drawningCruiser.wheels = new DopClassMixed(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonDumpCar.setBounds( 20, 390, 120, 30);
this.add(buttonDumpCar);
buttonAdvancedCruiser.setBounds( 20, 390, 120, 30);
this.add(buttonAdvancedCruiser);
JTextField textStrategy = new JTextField();
textStrategy.setBounds(550, 20, 120, 30);
this.add(textStrategy);
JButton buttonStep = new JButton("Шаг");
buttonStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawningCar == null)
if (_drawningCruiser == null)
{
return;
}
@ -167,18 +142,14 @@ public class GameFrame extends JDialog {
_abstractStrategy = new MoveToBorder();
break;
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData((IMoveableObject) _drawningCar, SCREEN_W, SCREEN_H);
//textStrategy.setText();
_abstractStrategy.SetData((IMoveableObject) _drawningCruiser, SCREEN_W, SCREEN_H);
}
if (_abstractStrategy == null)
{
return;
}
System.out.println("step");
@ -186,7 +157,6 @@ public class GameFrame extends JDialog {
repaint();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
//comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
@ -198,7 +168,7 @@ public class GameFrame extends JDialog {
up.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCar.MoveTransport(Direction.Up);
_drawningCruiser.MoveTransport(Direction.Up);
repaint();
}
});
@ -206,7 +176,7 @@ public class GameFrame extends JDialog {
left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCar.MoveTransport(Direction.Left);
_drawningCruiser.MoveTransport(Direction.Left);
repaint();
}
});
@ -214,7 +184,7 @@ public class GameFrame extends JDialog {
down.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCar.MoveTransport(Direction.Down);
_drawningCruiser.MoveTransport(Direction.Down);
repaint();
}
});
@ -222,65 +192,52 @@ public class GameFrame extends JDialog {
right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCar.MoveTransport(Direction.Right);
_drawningCruiser.MoveTransport(Direction.Right);
repaint();
}
});
up.setBounds(570, 350, 30, 30);
this.add(up);
down.setBounds(570, 350 + 40, 30, 30);
this.add(down);
left.setBounds(570 - 40, 350 + 40, 30, 30);
this.add(left);
right.setBounds(570 + 40, 350 + 40, 30, 30);
this.add(right);
JButton buttonSelectedCar = new JButton("Выбрать машину");
buttonSelectedCar.addActionListener(new ActionListener() {
JButton buttonSelectedCruiser = new JButton("Выбрать объект");
buttonSelectedCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SelectedCar = _drawningCar;
SelectedCruiser = _drawningCruiser;
dispose();
}
});
buttonSelectedCar.setBounds(510, 100, 150, 30);
this.add(buttonSelectedCar);
buttonSelectedCruiser.setBounds(510, 100, 150, 30);
this.add(buttonSelectedCruiser);
}
private void Draw()
{
if (_drawningCar == null)
if (_drawningCruiser == null)
{
return;
}
Graphics gr =new DebugGraphics();
_drawningCar.DrawTransport(gr);
_drawningCruiser.DrawTransport(gr);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (_drawningCar != null) {
_drawningCar.DrawTransport(g);
if (_drawningCruiser != null) {
_drawningCruiser.DrawTransport(g);
}
}
public void ChangeCarOnPanel(DrawingCruiser newCar){
newCar.SetPosition(0,0);
_drawningCar = newCar;
public void ChangeCruiserOnPanel(DrawingCruiser newCruiser){
newCruiser.SetPosition(0,0);
_drawningCruiser = newCruiser;
repaint();
}
}
public Color ChooseColor(JFrame MonorailFrame){
@ -288,8 +245,4 @@ public class GameFrame extends JDialog {
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.WHITE);
return res;
}
}
}

View File

@ -31,12 +31,12 @@ public class RandomFrame extends JFrame {
@Override
public void actionPerformed(ActionEvent e) {
GameFrame dialogWindow = new GameFrame();
if (dialogWindow.getSelectedCar() != null){
if (dialogWindow.getSelectedCar() instanceof DrawingAdvancedCruiser) {
_genericCreate.Add(((DrawingAdvancedCruiser) dialogWindow.getSelectedCar()).getEntityCruiser());
if (dialogWindow.getSelectedCruiser() != null){
if (dialogWindow.getSelectedCruiser() instanceof DrawingAdvancedCruiser) {
_genericCreate.Add(((DrawingAdvancedCruiser) dialogWindow.getSelectedCruiser()).getEntityCruiser());
}
else {
_genericCreate.Add(dialogWindow.getSelectedCar().EntityCruiser);
_genericCreate.Add(dialogWindow.getSelectedCruiser().EntityCruiser);
}
repaint();}

View File

@ -1,28 +1,23 @@
import java.util.ArrayList;
import java.util.List;
public class SetGeneric<T extends Object> {
private final List<T> _places;
private final int _maxCount;
public int Count;
public int Count() {
return Count;
}
public SetGeneric(int count)
{
_places = new ArrayList<>();
_maxCount = count;
}
public boolean Insert(T car){
public boolean Insert(T cruiser){
if(_places.size() == _maxCount)
return false;
Insert(car, 0);
Insert(cruiser, 0);
return true;
}
public boolean Insert(T monorail, int position){
if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount))
return false;
@ -30,7 +25,6 @@ public class SetGeneric<T extends Object> {
Count++;
return true;
}
public boolean Remove(int position)
{
if(!(position >= 0 && position < _places.size()))
@ -41,10 +35,9 @@ public class SetGeneric<T extends Object> {
Count--;
return true;
}
public T Get(int position)
{
if (position < Count() && position >= 0) { return (T)_places.get(position); }
return null;
}
}
}

View File

@ -1,15 +1,11 @@
import java.util.LinkedList;
public class TrashCollection <T extends DrawingCruiser>{
LinkedList<T> linkedList;
public TrashCollection(){
linkedList = new LinkedList<>();
}
public void Add(T car){
linkedList.add(car);
public void Add(T cruiser){
linkedList.add(cruiser);
}
public void PopHead(){
if(linkedList.size() ==0)