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

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

View File

@ -45,7 +45,8 @@ public class Main extends JFrame {
private final ILabRab[] labs = new ILabRab[] {
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;

View File

@ -36,15 +36,15 @@ public class CsvReader<T> {
this.handler = handler;
}
public List<T> readCsv() throws IOException {
return readCsv(true);
public List<T> readCsvToList() throws IOException {
return readCsvToList(true);
}
public List<T> readCsv(boolean skipHeaderLine) throws IOException {
return readCsv(skipHeaderLine, ",");
public List<T> readCsvToList(boolean skipHeaderLine) throws IOException {
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");
handler.onStartRead(file);
@ -72,4 +72,30 @@ public class CsvReader<T> {
handler.onFinishRead(file);
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 {
private Polygon triangle;
private Color fillColor, strokeColor;
private final Polygon triangle;
private final Color fillColor, strokeColor;
public TrianglePaintable(Color fillColor, Color strokeColor, Point p1, Point p2, Point p3) {
this.fillColor = fillColor;

View File

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