Öffnen und Speichern check
This commit is contained in:
commit
d164ff8251
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1 @@
|
||||
/nbproject/private/
|
||||
/build/
|
||||
|
8
src/config.properties
Normal file
8
src/config.properties
Normal file
@ -0,0 +1,8 @@
|
||||
log.level=INFO
|
||||
|
||||
log.directory=../Aufgabe7_bandit
|
||||
log.filename=ohmLog
|
||||
|
||||
# Beispiel f\u00fcr verschiedene Level f\u00fcr unterschiedliche Pakete
|
||||
# log.level.bandit.Zahlengenerator=FINE
|
||||
# log.level.bandit.Model.Wuerfel=WARNING
|
@ -56,7 +56,7 @@ public class OpenCommand implements CommandInterface
|
||||
{
|
||||
Logger.getLogger(OpenCommand.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
model.deleteArrays();
|
||||
view.getgZeichenflaeche().repaint();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ public class GrafikModel
|
||||
BufferedOutputStream bos = new BufferedOutputStream(fos);
|
||||
//Serialisierung
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos);
|
||||
oos.writeObject(punkte);
|
||||
oos.writeObject(figuren);
|
||||
oos.flush(); // Puffer
|
||||
oos.close();
|
||||
}
|
||||
@ -85,13 +85,13 @@ public class GrafikModel
|
||||
//eleganter
|
||||
if (daten instanceof ArrayList liste)
|
||||
{
|
||||
punkte = liste;
|
||||
figuren = liste;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void endShape() {
|
||||
figuren.add(punkte);
|
||||
figuren.add(new ArrayList<Point>(punkte));
|
||||
punkte.clear();
|
||||
}
|
||||
|
||||
@ -115,10 +115,5 @@ public class GrafikModel
|
||||
pref.put(lastDirectory, lastAdress);
|
||||
}
|
||||
|
||||
public void deleteArrays()
|
||||
{
|
||||
figuren.clear();
|
||||
punkte.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
111
src/mvcgrafik/util/OhmLogger.java
Normal file
111
src/mvcgrafik/util/OhmLogger.java
Normal file
@ -0,0 +1,111 @@
|
||||
package mvcgrafik.util;
|
||||
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
//import java.util.Properties;
|
||||
import java.util.logging.*;
|
||||
|
||||
/**
|
||||
* Klasse zur eigens konfigurierten Log-Ausgabe in der Console
|
||||
* @author ahrens
|
||||
*/
|
||||
public class OhmLogger
|
||||
{
|
||||
private static final String LOGGER_NAME = "OhmLogger";
|
||||
private static Logger lg = null;
|
||||
|
||||
/**
|
||||
* privater Konstrukter -> Singleton
|
||||
*/
|
||||
private OhmLogger()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Public Method zum Erstellen des Loggers und zum Aufruf der Methode
|
||||
* zum Initialisieren
|
||||
* @return Referenz auf Logger
|
||||
*/
|
||||
public static Logger getLogger()
|
||||
{
|
||||
if (lg == null)
|
||||
{
|
||||
lg = Logger.getLogger(LOGGER_NAME);
|
||||
initLogger();
|
||||
}
|
||||
return lg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Methode zum Initialisieren, Suchen der Konfigurationsdatei, Anlegen eines
|
||||
* neuen Console Handlers, Löschen der bisherigen Standard Handler und
|
||||
* Einfügen des eigenen Handler, Setzen des Levels, je nach Properties
|
||||
*/
|
||||
private static void initLogger()
|
||||
{
|
||||
try (InputStream configFile = OhmLogger.class.getClassLoader().getResourceAsStream("config.properties")) {
|
||||
if (configFile != null) {
|
||||
Properties properties = new Properties();
|
||||
properties.load(configFile);
|
||||
java.util.logging.Level classLogLevel = java.util.logging.Level.parse(properties.getProperty("log.level"));
|
||||
|
||||
|
||||
ConsoleHandler ch = new ConsoleHandler();
|
||||
ch.setFormatter(new OhmFormatter());
|
||||
lg.setUseParentHandlers(false);
|
||||
lg.getHandlers();
|
||||
lg.addHandler(ch);
|
||||
|
||||
|
||||
lg.setLevel(classLogLevel);
|
||||
|
||||
String logDirectory = properties.getProperty("log.directory");
|
||||
String logFileName = properties.getProperty("log.filename");
|
||||
|
||||
FileHandler fileHandler = new FileHandler(logDirectory + "/" + logFileName, true);
|
||||
fileHandler.setFormatter(new OhmFormatter());
|
||||
lg.addHandler(fileHandler);
|
||||
|
||||
|
||||
} else {
|
||||
System.err.println("Unable to find config.properties file. OhmLogger will use default settings.");
|
||||
}
|
||||
} catch (IOException | SecurityException e) {
|
||||
System.err.println("Error configuring OhmLogger: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Klasse zum Setzen des Formates des Auszugebenden Log-Strings
|
||||
* @author ahren
|
||||
*/
|
||||
class OhmFormatter extends Formatter
|
||||
{
|
||||
@Override
|
||||
public String format(LogRecord record)
|
||||
{
|
||||
String logline = "| ";
|
||||
|
||||
LocalDateTime ldt = LocalDateTime.now();
|
||||
logline += ldt.toString();
|
||||
logline += " | " + record.getLevel();
|
||||
logline += " | " + record.getSourceClassName();
|
||||
logline += " | " + record.getMessage();
|
||||
logline += " |\n";
|
||||
|
||||
return logline;
|
||||
}
|
||||
}
|
@ -16,11 +16,14 @@ import java.awt.print.Printable;
|
||||
import java.awt.print.PrinterException;
|
||||
import java.awt.print.PrinterJob;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
import javax.print.attribute.HashPrintRequestAttributeSet;
|
||||
import javax.print.attribute.standard.DialogTypeSelection;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JOptionPane;
|
||||
import mvcgrafik.model.GrafikModel;
|
||||
import mvcgrafik.util.OhmLogger;
|
||||
|
||||
|
||||
/**
|
||||
@ -29,6 +32,7 @@ import mvcgrafik.model.GrafikModel;
|
||||
*/
|
||||
public class GrafikView extends JComponent implements Printable
|
||||
{
|
||||
private static Logger lg = OhmLogger.getLogger();
|
||||
private static Dimension EINS = new Dimension(1, 1); // Dimension ist eine Klasse die width udn height hält
|
||||
private Rectangle2D.Float pixel;
|
||||
private Line2D.Float line;
|
||||
@ -37,6 +41,7 @@ public class GrafikView extends JComponent implements Printable
|
||||
public GrafikView()
|
||||
{
|
||||
pixel = new Rectangle2D.Float();
|
||||
line = new Line2D.Float();
|
||||
}
|
||||
|
||||
public void setModel(GrafikModel model)
|
||||
@ -46,47 +51,25 @@ public class GrafikView extends JComponent implements Printable
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Zeichnet den aktuellen Pfad (solange die maus gedrückt gehalten wird)
|
||||
* @param p -> Der aktuelle punkt als x-y-Koordinate
|
||||
*/
|
||||
public void drawPoint(Point p)
|
||||
{
|
||||
|
||||
Graphics2D g2 = (Graphics2D)this.getGraphics(); // gefährlich!
|
||||
paintComponent(g2);
|
||||
// pixel.setFrame(p, EINS);
|
||||
// g2.draw(pixel);
|
||||
|
||||
drawPath(model.getPunkte(),g2);
|
||||
|
||||
//Um die aktuelle Figur zu zeichnen
|
||||
int pathSize = model.getPunkte().size();
|
||||
|
||||
for(int i=0; i < pathSize-1; i++)
|
||||
{
|
||||
Point from = model.getPunkte().get(i);
|
||||
Point to = model.getPunkte().get(i+1);
|
||||
|
||||
line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
|
||||
g2.draw(line);
|
||||
}
|
||||
|
||||
//Um die fertigen Figuren zu zeichnen
|
||||
model.getFiguren().forEach(figure->
|
||||
{
|
||||
ArrayList<Point> currentPath = new ArrayList<>(figure);
|
||||
int figurePath = currentPath.size();
|
||||
|
||||
for(int i=0; i < figurePath-1; i++)
|
||||
{
|
||||
Point from = currentPath.get(i);
|
||||
Point to = model.getPunkte().get(i+1);
|
||||
|
||||
line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
|
||||
g2.draw(line);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
g2.dispose(); //SEEEEHHHHRRRR WICHTIG!!!!!!!
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Hier werden alle Pfade aus dem model neu gezeichnet
|
||||
* Jedes mal wenn die Maus los gelassen wird, wird dder aktuelle Pfad gespeichert
|
||||
* @param g
|
||||
*/
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
if (model == null) return;
|
||||
@ -94,61 +77,25 @@ public class GrafikView extends JComponent implements Printable
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
|
||||
|
||||
// int pathSize = model.getPunkte().size();
|
||||
//
|
||||
// for(int i=0; i < pathSize-1; i++)
|
||||
// {
|
||||
// Point from = model.getPunkte().get(i);
|
||||
// Point to = model.getPunkte().get(i+1);
|
||||
//
|
||||
// line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
|
||||
// g2.draw(line);
|
||||
// }
|
||||
|
||||
model.getFiguren().forEach(figure->
|
||||
model.getFiguren().forEach(figuren->
|
||||
{
|
||||
ArrayList<Point> currentPath = new ArrayList<>(figure);
|
||||
int figurePath = currentPath.size();
|
||||
|
||||
for(int i=0; i < figurePath-1; i++)
|
||||
{
|
||||
Point from = currentPath.get(i);
|
||||
Point to = model.getPunkte().get(i+1);
|
||||
|
||||
line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
|
||||
g2.draw(line);
|
||||
}
|
||||
|
||||
drawPath(figuren,g2);
|
||||
});
|
||||
|
||||
|
||||
|
||||
// model.getPunkte().forEach(p ->
|
||||
// {
|
||||
// pixel.setFrame(p, EINS);
|
||||
// g2.draw(pixel);
|
||||
// });
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void drawPath(List<Point> path, Graphics2D g2){
|
||||
|
||||
for(int i=0; i < path.size()-1; i++)
|
||||
{
|
||||
Point from = path.get(i);
|
||||
Point to = path.get(i+1);
|
||||
|
||||
line.setLine(from,to);
|
||||
g2.draw(line);
|
||||
}
|
||||
}
|
||||
|
||||
// public void drawPath(ArrayList<Point> path){
|
||||
// Graphics2D g2 = (Graphics2D)this.getGraphics();
|
||||
//
|
||||
// int pathSize = path.size();
|
||||
//
|
||||
// for(int i=0; i < pathSize-1; i++)
|
||||
// {
|
||||
// Point from = model.getPunkte().get(i);
|
||||
// Point to = model.getPunkte().get(i+1);
|
||||
//
|
||||
// line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
|
||||
// g2.draw(line);
|
||||
// }
|
||||
//
|
||||
// g2.dispose();
|
||||
//
|
||||
// }
|
||||
|
||||
public void doPrint()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user