PIbd-23. Zakharov R.A. Lab work 04 #5
@ -1 +0,0 @@
|
||||
Main.java
|
@ -1,9 +1,7 @@
|
||||
import frames.FrameBattleship;
|
||||
import frames.FrameShipsCollection;
|
||||
import frames.HardFrame;
|
||||
|
||||
import java.io.IOException;
|
||||
import frames.*;
|
||||
|
||||
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;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.StrokeBorder;
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
import drawing_objects.DrawingShip;
|
||||
import generics.ShipsGenericCollection;
|
||||
import movement_strategy.DrawingObjectShip;
|
||||
import generics.ShipsGenericStorage;
|
||||
|
||||
public class FrameShipsCollection extends JFrame {
|
||||
private ShipsGenericCollection<DrawingShip, DrawingObjectShip> ships;
|
||||
JComponent pictureBoxCollection;
|
||||
TextField textFieldNumber;
|
||||
private final ShipsGenericStorage storage;
|
||||
private JComponent pictureBoxCollection;
|
||||
private TextField textFieldNumber;
|
||||
private TextField textFieldStorageName;
|
||||
private JList<String> listStorages;
|
||||
private DefaultListModel<String> listModel;
|
||||
public FrameShipsCollection(){
|
||||
super("Набор кораблей");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
createGui();
|
||||
ships = new ShipsGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||
pack();
|
||||
setVisible(true);
|
||||
storage = new ShipsGenericStorage(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||
}
|
||||
private void createGui(){
|
||||
//components initialisation
|
||||
@ -25,7 +31,12 @@ public class FrameShipsCollection extends JFrame {
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(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();
|
||||
}
|
||||
};
|
||||
@ -34,34 +45,101 @@ public class FrameShipsCollection extends JFrame {
|
||||
textFieldNumber = new TextField();
|
||||
JButton buttonRemoveShip = 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
|
||||
buttonAddSet.addActionListener(e -> buttonAddSet_Click());
|
||||
buttonDeleteSet.addActionListener(e -> buttonDeleteSet_Click());
|
||||
buttonAddShip.addActionListener(e -> buttonAddShipClick());
|
||||
buttonRemoveShip.addActionListener(e -> buttonRemoveShipClick());
|
||||
buttonRefreshCollection.addActionListener(e -> buttonRefreshCollectionClick());
|
||||
//addition to frame
|
||||
JPanel panelCollection = new JPanel(new GridBagLayout());
|
||||
//panels and constraints initialisation
|
||||
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();
|
||||
constraints.insets.left = constraints.insets.right = 2;
|
||||
constraints.insets.top = constraints.insets.bottom = 25;
|
||||
constraints.insets.left = constraints.insets.right = 5;
|
||||
constraints.insets.top = constraints.insets.bottom = 5;
|
||||
constraints.fill = GridBagConstraints.BOTH;
|
||||
//addition to panelSets
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 0;
|
||||
panelCollection.add(buttonAddShip, constraints);
|
||||
panelSets.add(textFieldStorageName, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 1;
|
||||
panelCollection.add(textFieldNumber, constraints);
|
||||
panelSets.add(buttonAddSet, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 2;
|
||||
panelCollection.add(buttonRemoveShip, constraints);
|
||||
panelSets.add(scrollPane, constraints);
|
||||
constraints.gridx = 0;
|
||||
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());
|
||||
add(panelCollection, BorderLayout.EAST);
|
||||
add(panelTools, BorderLayout.EAST);
|
||||
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() {
|
||||
if (listStorages.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = storage.getSet(listStorages.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
FrameBattleship form;
|
||||
try {
|
||||
form = new FrameBattleship();
|
||||
@ -72,7 +150,8 @@ public class FrameShipsCollection extends JFrame {
|
||||
form.select();
|
||||
DrawingShip ship = form.getSelectedShip();
|
||||
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, "Объект добавлен");
|
||||
pictureBoxCollection.repaint();
|
||||
}
|
||||
@ -82,8 +161,13 @@ public class FrameShipsCollection extends JFrame {
|
||||
});
|
||||
}
|
||||
private void buttonRemoveShipClick(){
|
||||
if (listStorages.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = storage.getSet(listStorages.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||
if (ships.remove(pos)){
|
||||
if (storage.getSet(listStorages.getSelectedValue()).remove(pos)){
|
||||
JOptionPane.showMessageDialog(this, "Объект удален");
|
||||
pictureBoxCollection.repaint();
|
||||
}
|
||||
@ -91,5 +175,7 @@ public class FrameShipsCollection extends JFrame {
|
||||
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
private void buttonRefreshCollectionClick(){pictureBoxCollection.repaint();}
|
||||
private void buttonRefreshCollectionClick(){
|
||||
pictureBoxCollection.repaint();
|
||||
}
|
||||
}
|
||||
|
@ -1,41 +1,35 @@
|
||||
package generics;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SetGeneric<T extends Object>{
|
||||
private final T[] places;
|
||||
public int getCount() {return places.length;}
|
||||
public SetGeneric(int count, Class<T> type){
|
||||
places = (T[]) Array.newInstance(type, count);
|
||||
private final ArrayList<T> places;
|
||||
public int getCount() {return places.size();}
|
||||
private final int maxCount;
|
||||
public SetGeneric(int count){
|
||||
maxCount = count;
|
||||
places = new ArrayList<>();
|
||||
}
|
||||
public boolean insert(T ship){
|
||||
if(places.size() == maxCount)
|
||||
return false;
|
||||
return insert(ship, 0);
|
||||
}
|
||||
public boolean insert(T ship, int position){
|
||||
if (!(position >= 0 && position < places.length))
|
||||
if (!(position >= 0 && position <= places.size() && places.size() < maxCount))
|
||||
return false;
|
||||
if (places[position] != null)
|
||||
{
|
||||
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;
|
||||
places.add(position, ship);
|
||||
return true;
|
||||
}
|
||||
public boolean remove(int position){
|
||||
if(!(position >= 0 && position < getCount()))
|
||||
if(!(position >= 0 && position < places.size()))
|
||||
return false;
|
||||
places[position] = null;
|
||||
places.remove(position);
|
||||
return true;
|
||||
}
|
||||
public T get(int position){
|
||||
if(!(position >= 0 && position < getCount()))
|
||||
if(!(position >= 0 && position < places.size()))
|
||||
return null;
|
||||
return places[position];
|
||||
return places.get(position);
|
||||
}
|
||||
}
|
@ -6,17 +6,20 @@ import movement_strategy.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class ShipsGenericCollection<T extends DrawingShip, U extends IMoveableObject>{
|
||||
private int pictureWidth;
|
||||
private int pictureHeight;
|
||||
private final int pictureWidth;
|
||||
private final int pictureHeight;
|
||||
private final int placeSizeWidth = 220;
|
||||
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){
|
||||
int width = picWidth / placeSizeWidth;
|
||||
int height = picHeight / placeSizeHeight;
|
||||
pictureWidth = picWidth;
|
||||
pictureHeight = picHeight;
|
||||
collection = new SetGeneric<T>(width * height, (Class<T>) DrawingShip.class);
|
||||
collection = new SetGeneric<>(width * height);
|
||||
}
|
||||
public boolean insert ( T obj) {
|
||||
if (obj != null)
|
||||
@ -24,16 +27,18 @@ public class ShipsGenericCollection<T extends DrawingShip, U extends IMoveableOb
|
||||
return false;
|
||||
}
|
||||
public boolean remove (int pos){
|
||||
T obj = collection.get(pos);
|
||||
if (obj != null)
|
||||
return collection.remove(pos);
|
||||
return false;
|
||||
return collection.remove(pos);
|
||||
}
|
||||
public U getU(int pos){
|
||||
if(collection.get(pos) == null)
|
||||
return null;
|
||||
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){
|
||||
DrawBackground(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