Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laba6 Sokolova #6

Open
wants to merge 2 commits into
base: laba5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions src/laba/Dock.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ public class Dock<T extends IShip> {
// ������������ ���������� ����
private int _maxCount;

// ������� � ������� ��� PictureWudth � PictureHeight.
public int getPictureWidth() {
return PictureWidth;
}

public void setPictureWidth(int pictureWidth) {
PictureWidth = pictureWidth;
}

public int getPictureHeight() {
return PictureHeight;
}

public void setPictureHeight(int pictureHeight) {
PictureHeight = pictureHeight;
}

// ����������� ����.
public Dock(int size, int pictureWidth, int pictureHeight) {
_maxCount = size;
Expand Down Expand Up @@ -69,25 +52,25 @@ public int Add(T ship) {

// �������� "-": ������� �������
public T Del(int index) {
if (!CheckFreePlace(index))
{
T ship = _places.get(index);
_places.remove(index);
return ship;
}
return null; }
if (!CheckFreePlace(index)) {
T ship = _places.get(index);
_places.remove(index);
return ship;
}
return null;
}

// ��������� �� ������ �����, �������� �� ���
private boolean CheckFreePlace(int index) {
return !_places.containsKey(index);
return !_places.containsKey(index);
}

// ��������� ����
public void Draw(Graphics g) {
DrawMarking(g);
for (T ship: _places.values()) {
ship.DrawShip(g);
}
for (T ship : _places.values()) {
ship.DrawShip(g);
}
}

// ��������� �����
Expand All @@ -104,4 +87,20 @@ private void DrawMarking(Graphics g) {
}
}
}

public T getAt(int index) {

if (_places.containsKey(index)) {
return _places.get(index);
}
return null;
}

public void setAt(int index, T ship) {
if (CheckFreePlace(index)) {
ship.SetPosition(5 + index / 5 * _placeSizeWidth + 5, index % 5 * _placeSizeHeight + 80,
PictureWidth, PictureHeight);
_places.put(index, ship);
}
}
}
83 changes: 82 additions & 1 deletion src/laba/FormDock.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import java.awt.SystemColor;
import javax.swing.JTextArea;
Expand All @@ -21,6 +23,13 @@
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JScrollPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileFilter;

public class FormDock {

Expand All @@ -36,6 +45,10 @@ public class FormDock {
private final int countLevel = 5;
private FormShipConfig select;
private static JList list;
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem_Save;
private JMenuItem menuItem_Load;

/**
* Launch the application.
Expand Down Expand Up @@ -65,7 +78,7 @@ public void getShip() {
if (select.res()) {
IShip ship = select.ship;
int temp = list.getSelectedIndex();
if(temp == -1) {
if (temp == -1) {
temp = 0;
}
int place = dock.getAt(temp).Add(ship);
Expand Down Expand Up @@ -158,5 +171,73 @@ public void actionPerformed(ActionEvent e) {
button_Take.setBounds(865, 311, 89, 23);
frame.getContentPane().add(button_Take);

menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);

menu = new JMenu("\u0424\u0430\u0439\u043B");
menuBar.add(menu);

menuItem_Save = new JMenuItem("\u0421\u043E\u0445\u0440\u0430\u043D\u0438\u0442\u044C");
menuItem_Save.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new MyFilter());
int returnVal = fc.showOpenDialog(frame); // Where frame is the parent component

File file = null;
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
if (dock.SaveData(file.getPath())) {
JOptionPane.showMessageDialog(null, "���������� ������ �������", "���������",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "�� �����������", "���������",
JOptionPane.INFORMATION_MESSAGE);
}

}
}
});
menu.add(menuItem_Save);

menuItem_Load = new JMenuItem("\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044C");
menuItem_Load.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new MyFilter());
int returnVal = fc.showOpenDialog(frame); // Where frame is the parent component

File file = null;
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
if (dock.LoadData(file.getPath())) {
JOptionPane.showMessageDialog(null, "���������", "���������", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "�� ���������", "���������",
JOptionPane.INFORMATION_MESSAGE);
}
}
panel_Dock.repaint();
}
});
menu.add(menuItem_Load);

}
}

class MyFilter extends javax.swing.filechooser.FileFilter {

@Override
public boolean accept(File arg0) {
if (arg0.getName().lastIndexOf('.') == -1)
return true;
return arg0.getName().substring(arg0.getName().lastIndexOf('.')).equals(".txt");
}
@Override
public String getDescription() {
return null;
}

}
1 change: 1 addition & 0 deletions src/laba/IShip.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface IShip {

//������������ ��������� �����
void SetMainColor(Color color);

}
165 changes: 150 additions & 15 deletions src/laba/MultiLevelDock.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,160 @@
package laba;

import java.beans.Encoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import javax.swing.Spring;

import org.omg.CORBA.Environment;

public class MultiLevelDock {
ArrayList<Dock<IShip>> dockStages;
ArrayList<Dock<IShip>> dockStages;

private final int countPlaces = 20;
private int pictureWidth;
private int pictureHeight;

public MultiLevelDock(int countStages, int pictureWidth, int pictureHeight) {
dockStages = new ArrayList<Dock<IShip>>();
this.pictureWidth = pictureWidth;
this.pictureHeight = pictureHeight;
for (int i = 0; i < countStages; ++i) {
dockStages.add(new Dock<IShip>(countPlaces, pictureWidth, pictureHeight));
}
}

public Dock<IShip> getAt(int index) {
if (index > -1 && index < dockStages.size()) {
return dockStages.get(index);
}

return null;
}

private void WriteToFile(String text, FileOutputStream stream) {
try {
byte[] info = text.getBytes();
stream.write(info, 0, info.length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public boolean SaveData(String filename) {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
FileOutputStream fs;
try {
fs = new FileOutputStream(filename);
// ���������� ���������� �������
WriteToFile("CountLeveles:" + dockStages.size() + "\r\n", fs);
for (Dock<IShip> level : dockStages) {
// �������� �������
WriteToFile("Level" + "\r\n", fs);
for (int i = 0; i < countPlaces; i++) {
IShip ship = level.getAt(i);
if (ship != null) {
// ���� ����� �� ������
// ���������� ��� ������
if (ship instanceof Ship_Liner) {
WriteToFile(i + ":Ship_Liner:", fs);
}else if (ship instanceof Ship) {
WriteToFile(i + ":Ship:", fs);
}

// ������������ ���������
System.out.println(ship.toString() + "\r\n");
WriteToFile(ship.toString() + "\r\n", fs);

}
}
}

return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return false;
}

public Boolean LoadData(String filename) {
try {
File file = new File(filename);
if (!file.exists()) {
return false;
}
String bufferTextFromFile = "";
FileInputStream fn;

fn = new FileInputStream(filename);

int i = -1;
int size = 0;
while ((i = fn.read()) != -1) {
size++;
}
char[] b = new char[size];
fn = new FileInputStream(filename);
int j = 0;
while ((i = fn.read()) != -1) {
b[j] = (char) i;
j++;
}

private final int countPlaces = 20;
bufferTextFromFile = new String(b);

public MultiLevelDock(int countStages, int pictureWidth, int pictureHeight) {
dockStages = new ArrayList<Dock<IShip>>();
for (int i = 0; i < countStages; ++i)
{
dockStages.add(new Dock<IShip>(countPlaces, pictureWidth, pictureHeight));
}
}
bufferTextFromFile = bufferTextFromFile.replace("\r", "");
String[] strs = bufferTextFromFile.split("\n", 0);
if (strs[0].contains("CountLeveles")) {
// ��������� ���������� �������
int count = Integer.parseInt(strs[0].split(":", 0)[1]);
if (dockStages != null) {
dockStages.clear();
}
dockStages = new ArrayList<Dock<IShip>>(count);
} else {
// ���� ��� ����� ������, �� ��� �� �� ������
return false;
}
int counter = -1;
IShip ship = null;
for (int k = 1; k < strs.length; ++k) {
// ���� �� ��������� �������
if (strs[k].equals("Level")) {
// �������� ����� �������
counter++;
dockStages.add(new Dock<IShip>(countPlaces, pictureWidth, pictureHeight));
continue;
}
if (strs[k] == null || strs[k].isEmpty()) {
continue;
}
if (strs[k].contains("Ship_Liner")) {
ship = new Ship_Liner(strs[k].split(":", 0)[2]);
} else if (strs[k].contains("Ship")) {
ship = new Ship(strs[k].split(":", 0)[2]);
}
dockStages.get(counter).setAt(Integer.parseInt(strs[k].split(":", 0)[0]), ship);
}

public Dock<IShip> getAt(int index) {
if (index > -1 && index < dockStages.size()) {
return dockStages.get(index);
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

return null;
}
}
Loading