Небольшие исправления

This commit is contained in:
Victor 2013-12-02 16:55:48 +02:00
parent d9e1fb9d16
commit 2d216c3109
5 changed files with 44 additions and 14 deletions

View File

@ -1,5 +1,7 @@
package tse; package tse;
import tse.lr5.TrianglesWindow;
/** /**
* @author aNNiMON * @author aNNiMON
@ -7,7 +9,7 @@ package tse;
public class LR_5 implements ILabRab { public class LR_5 implements ILabRab {
private static final String[] TITLES = { private static final String[] TITLES = {
"Чтение объектов из CSV и рисование их на экране", "Чтение и рисование объектов из CSV",
"Движущиеся объекты и их управление" "Движущиеся объекты и их управление"
}; };
@ -15,7 +17,7 @@ public class LR_5 implements ILabRab {
public void execute(int index) { public void execute(int index) {
switch(index) { switch(index) {
case 0: case 0:
// new LoginWindow().setVisible(true); new TrianglesWindow().execute();
break; break;
case 1: case 1:
// new DailyPad().setVisible(true); // new DailyPad().setVisible(true);

View File

@ -45,7 +45,8 @@ public class Main extends JFrame {
private final ILabRab[] labs = new ILabRab[] { private final ILabRab[] labs = new ILabRab[] {
new LR_1(), new LR_2(), new LR_3(), new LR_1(), new LR_2(), new LR_3(),
new LR_4(), new LR_5(), new Tools() new LR_4(), new LR_5(),
// new Tools()
}; };
private JButton executeButton; private JButton executeButton;

View File

@ -36,15 +36,15 @@ public class CsvReader<T> {
this.handler = handler; this.handler = handler;
} }
public List<T> readCsv() throws IOException { public List<T> readCsvToList() throws IOException {
return readCsv(true); return readCsvToList(true);
} }
public List<T> readCsv(boolean skipHeaderLine) throws IOException { public List<T> readCsvToList(boolean skipHeaderLine) throws IOException {
return readCsv(skipHeaderLine, ","); return readCsvToList(skipHeaderLine, ",");
} }
public List<T> readCsv(boolean skipHeaderLine, String separator) throws IOException { public List<T> readCsvToList(boolean skipHeaderLine, String separator) throws IOException {
if (handler == null) throw new RuntimeException("Не установлен ReaderHandler"); if (handler == null) throw new RuntimeException("Не установлен ReaderHandler");
handler.onStartRead(file); handler.onStartRead(file);
@ -72,4 +72,30 @@ public class CsvReader<T> {
handler.onFinishRead(file); handler.onFinishRead(file);
return list; return list;
} }
public void readCsv(boolean skipHeaderLine, String separator) throws IOException {
if (handler == null) throw new RuntimeException("Не установлен ReaderHandler");
handler.onStartRead(file);
BufferedReader reader = new BufferedReader(
new InputStreamReader( new FileInputStream(file), "UTF-8" )
);
if (skipHeaderLine) {
reader.readLine();
}
String line;
while ( (line = reader.readLine()) != null ) {
T obj = null;
try {
if (!line.isEmpty()) {
String[] params = line.split(separator);
obj = (T) handler.createObject(params);
}
} catch (RuntimeException ex) {
Util.handleException(ex);
}
}
reader.close();
handler.onFinishRead(file);
}
} }

View File

@ -10,8 +10,8 @@ import java.awt.Polygon;
*/ */
public class TrianglePaintable implements PaintableObject { public class TrianglePaintable implements PaintableObject {
private Polygon triangle; private final Polygon triangle;
private Color fillColor, strokeColor; private final Color fillColor, strokeColor;
public TrianglePaintable(Color fillColor, Color strokeColor, Point p1, Point p2, Point p3) { public TrianglePaintable(Color fillColor, Color strokeColor, Point p1, Point p2, Point p3) {
this.fillColor = fillColor; this.fillColor = fillColor;

View File

@ -17,17 +17,18 @@ public class TrianglesWindow extends JFrame {
public TrianglesWindow() { public TrianglesWindow() {
super("Треугольники"); super("Треугольники");
setAlwaysOnTop(true);
setLocationByPlatform(true); setLocationByPlatform(true);
setDefaultCloseOperation(HIDE_ON_CLOSE); setDefaultCloseOperation(HIDE_ON_CLOSE);
panel = new PaintPanel(600, 400); panel = new PaintPanel(600, 450);
add(panel); add(panel);
pack(); pack();
} }
public void execute() { public void execute() {
File[] csvFiles = Util.readFiles("lr5", ".csv"); File[] csvFiles = Util.readFiles("lr5", ".csv");
if (csvFiles == null) { if (csvFiles == null || csvFiles.length == 0) {
JOptionPane.showMessageDialog(this, "CSV-файлов не обнаружено! Сворачиваемся, ребята", JOptionPane.showMessageDialog(this, "CSV-файлов не обнаружено! Сворачиваемся, ребята",
"Ошибка", JOptionPane.ERROR_MESSAGE); "Ошибка", JOptionPane.ERROR_MESSAGE);
return; return;
@ -64,7 +65,7 @@ public class TrianglesWindow extends JFrame {
CsvReader<TrianglePaintable> csvReader = new CsvReader<>(file); CsvReader<TrianglePaintable> csvReader = new CsvReader<>(file);
csvReader.setReaderHandler(trianglesHandler); csvReader.setReaderHandler(trianglesHandler);
try { try {
csvReader.readCsv(); csvReader.readCsv(false, ",");
} catch (IOException ex) { } catch (IOException ex) {
Util.handleException(ex); Util.handleException(ex);
} }
@ -90,7 +91,7 @@ public class TrianglesWindow extends JFrame {
TrianglePaintable obj = new TrianglePaintable(fill, stroke, p1, p2, p3); TrianglePaintable obj = new TrianglePaintable(fill, stroke, p1, p2, p3);
panel.addPaintable(obj); panel.addPaintable(obj);
try { try {
Thread.sleep(500); Thread.sleep(10);
} catch (InterruptedException ex) { } } catch (InterruptedException ex) { }
return obj; return obj;
} }