lab4 without additional task
This commit is contained in:
parent
0190f57a9c
commit
150d41e1a5
@ -1 +0,0 @@
|
|||||||
Main.java
|
|
@ -1,9 +1,7 @@
|
|||||||
import frames.FrameBattleship;
|
import frames.*;
|
||||||
import frames.FrameShipsCollection;
|
|
||||||
import frames.HardFrame;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException { new HardFrame(); }
|
public static void main(String[] args){
|
||||||
|
new FrameShipsCollection();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,29 @@
|
|||||||
package frames;
|
package frames;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.StrokeBorder;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import drawing_objects.DrawingShip;
|
import drawing_objects.DrawingShip;
|
||||||
import generics.ShipsGenericCollection;
|
import generics.ShipsGenericStorage;
|
||||||
import movement_strategy.DrawingObjectShip;
|
|
||||||
|
|
||||||
public class FrameShipsCollection extends JFrame {
|
public class FrameShipsCollection extends JFrame {
|
||||||
private ShipsGenericCollection<DrawingShip, DrawingObjectShip> ships;
|
private final ShipsGenericStorage storage;
|
||||||
JComponent pictureBoxCollection;
|
private JComponent pictureBoxCollection;
|
||||||
TextField textFieldNumber;
|
private TextField textFieldNumber;
|
||||||
|
private TextField textFieldStorageName;
|
||||||
|
private JList<String> listStorages;
|
||||||
|
private DefaultListModel<String> listModel;
|
||||||
public FrameShipsCollection(){
|
public FrameShipsCollection(){
|
||||||
super("Набор кораблей");
|
super("Набор кораблей");
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
createGui();
|
createGui();
|
||||||
ships = new ShipsGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
pack();
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
|
storage = new ShipsGenericStorage(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||||
}
|
}
|
||||||
private void createGui(){
|
private void createGui(){
|
||||||
//components initialisation
|
//components initialisation
|
||||||
@ -25,7 +31,12 @@ public class FrameShipsCollection extends JFrame {
|
|||||||
public void paintComponent(Graphics graphics){
|
public void paintComponent(Graphics graphics){
|
||||||
super.paintComponent(graphics);
|
super.paintComponent(graphics);
|
||||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||||
if (ships != null) ships.showShips(graphics2D);
|
if (listStorages == null || storage == null)
|
||||||
|
return;
|
||||||
|
var collection = storage.getSet(listStorages.getSelectedValue());
|
||||||
|
if (collection == null)
|
||||||
|
return;
|
||||||
|
collection.showShips(graphics2D);
|
||||||
super.repaint();
|
super.repaint();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -34,34 +45,101 @@ public class FrameShipsCollection extends JFrame {
|
|||||||
textFieldNumber = new TextField();
|
textFieldNumber = new TextField();
|
||||||
JButton buttonRemoveShip = new JButton("Удалить корабль");
|
JButton buttonRemoveShip = new JButton("Удалить корабль");
|
||||||
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
|
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
|
||||||
|
JButton buttonAddSet = new JButton("Добавить набор");
|
||||||
|
JButton buttonDeleteSet = new JButton("Удалить набор");
|
||||||
|
textFieldStorageName = new TextField();
|
||||||
|
listModel = new DefaultListModel<>();
|
||||||
|
JScrollPane scrollPane = new JScrollPane();
|
||||||
|
listStorages= new JList<>(listModel);
|
||||||
|
scrollPane.setViewportView(listStorages);
|
||||||
//ActionListeners and ActionCommand addition
|
//ActionListeners and ActionCommand addition
|
||||||
|
buttonAddSet.addActionListener(e -> buttonAddSet_Click());
|
||||||
|
buttonDeleteSet.addActionListener(e -> buttonDeleteSet_Click());
|
||||||
buttonAddShip.addActionListener(e -> buttonAddShipClick());
|
buttonAddShip.addActionListener(e -> buttonAddShipClick());
|
||||||
buttonRemoveShip.addActionListener(e -> buttonRemoveShipClick());
|
buttonRemoveShip.addActionListener(e -> buttonRemoveShipClick());
|
||||||
buttonRefreshCollection.addActionListener(e -> buttonRefreshCollectionClick());
|
buttonRefreshCollection.addActionListener(e -> buttonRefreshCollectionClick());
|
||||||
//addition to frame
|
//panels and constraints initialisation
|
||||||
JPanel panelCollection = new JPanel(new GridBagLayout());
|
JPanel panelTools = new JPanel(new GridBagLayout());
|
||||||
|
panelTools.setBorder(new StrokeBorder(new BasicStroke(3)));
|
||||||
|
panelTools.setToolTipText("Инструменты");
|
||||||
|
JPanel panelSets = new JPanel(new GridBagLayout());
|
||||||
|
panelSets.setBorder(new StrokeBorder(new BasicStroke(3)));
|
||||||
|
panelSets.setToolTipText("Наборы");
|
||||||
GridBagConstraints constraints = new GridBagConstraints();
|
GridBagConstraints constraints = new GridBagConstraints();
|
||||||
constraints.insets.left = constraints.insets.right = 2;
|
constraints.insets.left = constraints.insets.right = 5;
|
||||||
constraints.insets.top = constraints.insets.bottom = 25;
|
constraints.insets.top = constraints.insets.bottom = 5;
|
||||||
constraints.fill = GridBagConstraints.BOTH;
|
constraints.fill = GridBagConstraints.BOTH;
|
||||||
|
//addition to panelSets
|
||||||
constraints.gridx = 0;
|
constraints.gridx = 0;
|
||||||
constraints.gridy = 0;
|
constraints.gridy = 0;
|
||||||
panelCollection.add(buttonAddShip, constraints);
|
panelSets.add(textFieldStorageName, constraints);
|
||||||
constraints.gridx = 0;
|
constraints.gridx = 0;
|
||||||
constraints.gridy = 1;
|
constraints.gridy = 1;
|
||||||
panelCollection.add(textFieldNumber, constraints);
|
panelSets.add(buttonAddSet, constraints);
|
||||||
constraints.gridx = 0;
|
constraints.gridx = 0;
|
||||||
constraints.gridy = 2;
|
constraints.gridy = 2;
|
||||||
panelCollection.add(buttonRemoveShip, constraints);
|
panelSets.add(scrollPane, constraints);
|
||||||
constraints.gridx = 0;
|
constraints.gridx = 0;
|
||||||
constraints.gridy = 3;
|
constraints.gridy = 3;
|
||||||
panelCollection.add(buttonRefreshCollection, constraints);
|
panelSets.add(buttonDeleteSet, constraints);
|
||||||
|
//addition to panelTools
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 0;
|
||||||
|
panelTools.add(panelSets, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 1;
|
||||||
|
panelTools.add(buttonAddShip, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 2;
|
||||||
|
panelTools.add(textFieldNumber, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 3;
|
||||||
|
panelTools.add(buttonRemoveShip, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 4;
|
||||||
|
panelTools.add(buttonRefreshCollection, constraints);
|
||||||
|
//addition to frame
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
add(panelCollection, BorderLayout.EAST);
|
add(panelTools, BorderLayout.EAST);
|
||||||
add(pictureBoxCollection, BorderLayout.CENTER);
|
add(pictureBoxCollection, BorderLayout.CENTER);
|
||||||
pack();
|
}
|
||||||
|
private void reloadObjects(){
|
||||||
|
int index = listStorages.getSelectedIndex();
|
||||||
|
listModel.clear();
|
||||||
|
ArrayList<String> keys = storage.getKeys();
|
||||||
|
for (String key : keys) {
|
||||||
|
listModel.addElement(key);
|
||||||
|
}
|
||||||
|
if(listModel.size() > 0 && (index == -1 || index >= listModel.size()))
|
||||||
|
listStorages.setSelectedIndex(0);
|
||||||
|
else if(listModel.size() > 0)
|
||||||
|
listStorages.setSelectedIndex(index);
|
||||||
|
}
|
||||||
|
public void buttonAddSet_Click() {
|
||||||
|
if(textFieldStorageName.getText() == null ) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String name = textFieldStorageName.getText();
|
||||||
|
if (Objects.equals(name, "")) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
storage.addSet(name);
|
||||||
|
reloadObjects();
|
||||||
|
}
|
||||||
|
public void buttonDeleteSet_Click() {
|
||||||
|
if (listStorages.getSelectedIndex() == -1)
|
||||||
|
return;
|
||||||
|
storage.delSet(listStorages.getSelectedValue());
|
||||||
|
reloadObjects();
|
||||||
}
|
}
|
||||||
private void buttonAddShipClick() {
|
private void buttonAddShipClick() {
|
||||||
|
if (listStorages.getSelectedIndex() == -1)
|
||||||
|
return;
|
||||||
|
var obj = storage.getSet(listStorages.getSelectedValue());
|
||||||
|
if (obj == null)
|
||||||
|
return;
|
||||||
FrameBattleship form;
|
FrameBattleship form;
|
||||||
try {
|
try {
|
||||||
form = new FrameBattleship();
|
form = new FrameBattleship();
|
||||||
@ -72,7 +150,8 @@ public class FrameShipsCollection extends JFrame {
|
|||||||
form.select();
|
form.select();
|
||||||
DrawingShip ship = form.getSelectedShip();
|
DrawingShip ship = form.getSelectedShip();
|
||||||
form.dispose();
|
form.dispose();
|
||||||
if (ships.insert(ship)) {
|
if (ship != null && obj.insert(ship)) {
|
||||||
|
System.out.println(ship.getWidth() + " " + ship.getHeight() + " " + ship.getPosX() + " " + ship.getPosY());
|
||||||
JOptionPane.showMessageDialog(this, "Объект добавлен");
|
JOptionPane.showMessageDialog(this, "Объект добавлен");
|
||||||
pictureBoxCollection.repaint();
|
pictureBoxCollection.repaint();
|
||||||
}
|
}
|
||||||
@ -82,8 +161,13 @@ public class FrameShipsCollection extends JFrame {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
private void buttonRemoveShipClick(){
|
private void buttonRemoveShipClick(){
|
||||||
|
if (listStorages.getSelectedIndex() == -1)
|
||||||
|
return;
|
||||||
|
var obj = storage.getSet(listStorages.getSelectedValue());
|
||||||
|
if (obj == null)
|
||||||
|
return;
|
||||||
int pos = Integer.parseInt(textFieldNumber.getText());
|
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||||
if (ships.remove(pos)){
|
if (storage.getSet(listStorages.getSelectedValue()).remove(pos)){
|
||||||
JOptionPane.showMessageDialog(this, "Объект удален");
|
JOptionPane.showMessageDialog(this, "Объект удален");
|
||||||
pictureBoxCollection.repaint();
|
pictureBoxCollection.repaint();
|
||||||
}
|
}
|
||||||
@ -91,5 +175,7 @@ public class FrameShipsCollection extends JFrame {
|
|||||||
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
|
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void buttonRefreshCollectionClick(){pictureBoxCollection.repaint();}
|
private void buttonRefreshCollectionClick(){
|
||||||
|
pictureBoxCollection.repaint();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,41 +1,35 @@
|
|||||||
package generics;
|
package generics;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class SetGeneric<T extends Object>{
|
public class SetGeneric<T extends Object>{
|
||||||
private final T[] places;
|
private final ArrayList<T> places;
|
||||||
public int getCount() {return places.length;}
|
public int getCount() {return places.size();}
|
||||||
public SetGeneric(int count, Class<T> type){
|
private final int maxCount;
|
||||||
places = (T[]) Array.newInstance(type, count);
|
public SetGeneric(int count){
|
||||||
|
maxCount = count;
|
||||||
|
places = new ArrayList<>();
|
||||||
}
|
}
|
||||||
public boolean insert(T ship){
|
public boolean insert(T ship){
|
||||||
|
if(places.size() == maxCount)
|
||||||
|
return false;
|
||||||
return insert(ship, 0);
|
return insert(ship, 0);
|
||||||
}
|
}
|
||||||
public boolean insert(T ship, int position){
|
public boolean insert(T ship, int position){
|
||||||
if (!(position >= 0 && position < places.length))
|
if (!(position >= 0 && position <= places.size() && places.size() < maxCount))
|
||||||
return false;
|
return false;
|
||||||
if (places[position] != null)
|
places.add(position, ship);
|
||||||
{
|
|
||||||
int ind = position;
|
|
||||||
while (ind < places.length && places[ind] != null)
|
|
||||||
ind++;
|
|
||||||
if (ind == places.length)
|
|
||||||
return false;
|
|
||||||
for (int i = ind - 1; i >= position; i--)
|
|
||||||
places[i + 1] = places[i];
|
|
||||||
}
|
|
||||||
places[position] = ship;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public boolean remove(int position){
|
public boolean remove(int position){
|
||||||
if(!(position >= 0 && position < getCount()))
|
if(!(position >= 0 && position < places.size()))
|
||||||
return false;
|
return false;
|
||||||
places[position] = null;
|
places.remove(position);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public T get(int position){
|
public T get(int position){
|
||||||
if(!(position >= 0 && position < getCount()))
|
if(!(position >= 0 && position < places.size()))
|
||||||
return null;
|
return null;
|
||||||
return places[position];
|
return places.get(position);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,17 +6,20 @@ import movement_strategy.*;
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
public class ShipsGenericCollection<T extends DrawingShip, U extends IMoveableObject>{
|
public class ShipsGenericCollection<T extends DrawingShip, U extends IMoveableObject>{
|
||||||
private int pictureWidth;
|
private final int pictureWidth;
|
||||||
private int pictureHeight;
|
private final int pictureHeight;
|
||||||
private final int placeSizeWidth = 220;
|
private final int placeSizeWidth = 220;
|
||||||
private final int placeSizeHeight = 60;
|
private final int placeSizeHeight = 60;
|
||||||
private SetGeneric<T> collection;
|
private final SetGeneric<T> collection;
|
||||||
|
public int getCount(){
|
||||||
|
return collection.getCount();
|
||||||
|
}
|
||||||
public ShipsGenericCollection(int picWidth, int picHeight){
|
public ShipsGenericCollection(int picWidth, int picHeight){
|
||||||
int width = picWidth / placeSizeWidth;
|
int width = picWidth / placeSizeWidth;
|
||||||
int height = picHeight / placeSizeHeight;
|
int height = picHeight / placeSizeHeight;
|
||||||
pictureWidth = picWidth;
|
pictureWidth = picWidth;
|
||||||
pictureHeight = picHeight;
|
pictureHeight = picHeight;
|
||||||
collection = new SetGeneric<T>(width * height, (Class<T>) DrawingShip.class);
|
collection = new SetGeneric<>(width * height);
|
||||||
}
|
}
|
||||||
public boolean insert ( T obj) {
|
public boolean insert ( T obj) {
|
||||||
if (obj != null)
|
if (obj != null)
|
||||||
@ -24,16 +27,18 @@ public class ShipsGenericCollection<T extends DrawingShip, U extends IMoveableOb
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
public boolean remove (int pos){
|
public boolean remove (int pos){
|
||||||
T obj = collection.get(pos);
|
return collection.remove(pos);
|
||||||
if (obj != null)
|
|
||||||
return collection.remove(pos);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
public U getU(int pos){
|
public U getU(int pos){
|
||||||
if(collection.get(pos) == null)
|
if(collection.get(pos) == null)
|
||||||
return null;
|
return null;
|
||||||
return (U)collection.get(pos).getMoveableObject();
|
return (U)collection.get(pos).getMoveableObject();
|
||||||
}
|
}
|
||||||
|
public T get(int position){
|
||||||
|
if(position < 0 || position >= collection.getCount())
|
||||||
|
return null;
|
||||||
|
return collection.get(position);
|
||||||
|
}
|
||||||
public void showShips(Graphics2D graphics2D){
|
public void showShips(Graphics2D graphics2D){
|
||||||
DrawBackground(graphics2D);
|
DrawBackground(graphics2D);
|
||||||
DrawObjects(graphics2D);
|
DrawObjects(graphics2D);
|
||||||
|
39
src/generics/ShipsGenericStorage.java
Normal file
39
src/generics/ShipsGenericStorage.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package generics;
|
||||||
|
|
||||||
|
import drawing_objects.DrawingShip;
|
||||||
|
import movement_strategy.DrawingObjectShip;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class ShipsGenericStorage {
|
||||||
|
private final int pictureWidth;
|
||||||
|
private final int pictureHeight;
|
||||||
|
final HashMap<String, ShipsGenericCollection<DrawingShip, DrawingObjectShip>> shipsStorages;
|
||||||
|
public ShipsGenericStorage(int pictureWidth, int pictureHeight){
|
||||||
|
shipsStorages = new HashMap<>();
|
||||||
|
this.pictureWidth = pictureWidth;
|
||||||
|
this.pictureHeight = pictureHeight;
|
||||||
|
}
|
||||||
|
public ArrayList<String> getKeys(){
|
||||||
|
return new ArrayList<>(shipsStorages.keySet());
|
||||||
|
}
|
||||||
|
public void addSet(String name){
|
||||||
|
if(shipsStorages.containsKey(name))
|
||||||
|
return;
|
||||||
|
shipsStorages.put(name, new ShipsGenericCollection<>(pictureWidth, pictureHeight));
|
||||||
|
}
|
||||||
|
public void delSet(String name){
|
||||||
|
if(!shipsStorages.containsKey(name))
|
||||||
|
return;
|
||||||
|
shipsStorages.remove(name);
|
||||||
|
}
|
||||||
|
public ShipsGenericCollection<DrawingShip, DrawingObjectShip> getSet(String name){
|
||||||
|
if(!shipsStorages.containsKey(name))
|
||||||
|
return null;
|
||||||
|
return shipsStorages.get(name);
|
||||||
|
}
|
||||||
|
public DrawingShip getShip(String collectionName, int position){
|
||||||
|
return shipsStorages.get(collectionName).get(position);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user