Compare commits

...
2 Commits
7 changed files with 265 additions and 100 deletions

No files matched your search

+1
View File
@@ -0,0 +1 @@
/build/
+8
View 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
+1 -1
View File
@@ -89,7 +89,7 @@ public class GrafikModel
} }
public void endShape() { public void endShape() {
figuren.add(punkte); figuren.add(new ArrayList<Point>(punkte));
punkte.clear(); punkte.clear();
} }
+111
View 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;
}
}
+42 -3
View File
@@ -1,10 +1,51 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> <Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<NonVisualComponents>
<Menu class="javax.swing.JMenuBar" name="jMenuBar1">
<SubComponents>
<Menu class="javax.swing.JMenu" name="jMenu1">
<Properties>
<Property name="text" type="java.lang.String" value="File"/>
</Properties>
<SubComponents>
<MenuItem class="javax.swing.JMenuItem" name="miSave">
<Properties>
<Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
<KeyStroke key="Ctrl+S"/>
</Property>
<Property name="text" type="java.lang.String" value="Speichern"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="miSaveActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="miOpen">
<Properties>
<Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
<KeyStroke key="Ctrl+O"/>
</Property>
<Property name="text" type="java.lang.String" value="&#xd6;ffnen"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="miOpenActionPerformed"/>
</Events>
</MenuItem>
</SubComponents>
</Menu>
<Menu class="javax.swing.JMenu" name="jMenu2">
<Properties>
<Property name="text" type="java.lang.String" value="Edit"/>
</Properties>
</Menu>
</SubComponents>
</Menu>
</NonVisualComponents>
<Properties> <Properties>
<Property name="defaultCloseOperation" type="int" value="3"/> <Property name="defaultCloseOperation" type="int" value="3"/>
</Properties> </Properties>
<SyntheticProperties> <SyntheticProperties>
<SyntheticProperty name="menuBar" type="java.lang.String" value="jMenuBar1"/>
<SyntheticProperty name="formSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,-100,0,0,2,28"/> <SyntheticProperty name="formSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,-100,0,0,2,28"/>
<SyntheticProperty name="formSizePolicy" type="int" value="0"/> <SyntheticProperty name="formSizePolicy" type="int" value="0"/>
<SyntheticProperty name="generateSize" type="boolean" value="true"/> <SyntheticProperty name="generateSize" type="boolean" value="true"/>
@@ -31,9 +72,7 @@
</Constraint> </Constraint>
</Constraints> </Constraints>
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout"> <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/>
<Property name="useNullLayout" type="boolean" value="true"/>
</Layout>
</Container> </Container>
</SubComponents> </SubComponents>
</Form> </Form>
+72 -12
View File
@@ -32,18 +32,59 @@ public class GrafikFrame extends javax.swing.JFrame
* regenerated by the Form Editor. * regenerated by the Form Editor.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() private void initComponents() {
{
gZeichenflaeche = new mvcgrafik.view.GrafikView(); gZeichenflaeche = new mvcgrafik.view.GrafikView();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
miSave = new javax.swing.JMenuItem();
miOpen = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(gZeichenflaeche, java.awt.BorderLayout.CENTER);
setSize(new java.awt.Dimension(540, 412)); gZeichenflaeche.setLayout(new java.awt.BorderLayout());
setLocationRelativeTo(null); getContentPane().add(gZeichenflaeche, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
jMenu1.setText("File");
miSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_DOWN_MASK));
miSave.setText("Speichern");
miSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
miSaveActionPerformed(evt);
}
});
jMenu1.add(miSave);
miOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_DOWN_MASK));
miOpen.setText("Öffnen");
miOpen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
miOpenActionPerformed(evt);
}
});
jMenu1.add(miOpen);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
setSize(new java.awt.Dimension(540, 412));
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
private void miSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miSaveActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_miSaveActionPerformed
private void miOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miOpenActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_miOpenActionPerformed
/** /**
* @param args the command line arguments * @param args the command line arguments
@@ -94,7 +135,26 @@ public class GrafikFrame extends javax.swing.JFrame
}); });
} }
// Variables declaration - do not modify//GEN-BEGIN:variables // Variables declaration - do not modify//GEN-BEGIN:variables
private mvcgrafik.view.GrafikView gZeichenflaeche; private mvcgrafik.view.GrafikView gZeichenflaeche;
// End of variables declaration//GEN-END:variables private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem miOpen;
private javax.swing.JMenuItem miSave;
// End of variables declaration//GEN-END:variables
/**
* @return the miOpen
*/
public javax.swing.JMenuItem getMiOpen() {
return miOpen;
}
/**
* @return the miSave
*/
public javax.swing.JMenuItem getMiSave() {
return miSave;
}
} }
+30 -84
View File
@@ -16,11 +16,14 @@ import java.awt.print.Printable;
import java.awt.print.PrinterException; import java.awt.print.PrinterException;
import java.awt.print.PrinterJob; import java.awt.print.PrinterJob;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.DialogTypeSelection; import javax.print.attribute.standard.DialogTypeSelection;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import mvcgrafik.model.GrafikModel; import mvcgrafik.model.GrafikModel;
import mvcgrafik.util.OhmLogger;
/** /**
@@ -29,6 +32,7 @@ import mvcgrafik.model.GrafikModel;
*/ */
public class GrafikView extends JComponent implements Printable 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 static Dimension EINS = new Dimension(1, 1); // Dimension ist eine Klasse die width udn height hält
private Rectangle2D.Float pixel; private Rectangle2D.Float pixel;
private Line2D.Float line; private Line2D.Float line;
@@ -37,6 +41,7 @@ public class GrafikView extends JComponent implements Printable
public GrafikView() public GrafikView()
{ {
pixel = new Rectangle2D.Float(); pixel = new Rectangle2D.Float();
line = new Line2D.Float();
} }
public void setModel(GrafikModel model) 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) public void drawPoint(Point p)
{ {
Graphics2D g2 = (Graphics2D)this.getGraphics(); // gefährlich! 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!!!!!!! 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) public void paintComponent(Graphics g)
{ {
if (model == null) return; if (model == null) return;
@@ -94,61 +77,24 @@ public class GrafikView extends JComponent implements Printable
Graphics2D g2 = (Graphics2D)g; Graphics2D g2 = (Graphics2D)g;
// int pathSize = model.getPunkte().size(); model.getFiguren().forEach(figuren->
//
// 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->
{ {
ArrayList<Point> currentPath = new ArrayList<>(figure); drawPath(figuren,g2);
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);
}
}); });
// model.getPunkte().forEach(p ->
// {
// pixel.setFrame(p, EINS);
// g2.draw(pixel);
// });
} }
// public void drawPath(ArrayList<Point> path){ public void drawPath(List<Point> path, Graphics2D g2){
// Graphics2D g2 = (Graphics2D)this.getGraphics();
// for(int i=0; i < path.size()-1; i++)
// int pathSize = path.size(); {
// Point from = path.get(i);
// for(int i=0; i < pathSize-1; i++) Point to = path.get(i+1);
// {
// Point from = model.getPunkte().get(i); line.setLine(from,to);
// Point to = model.getPunkte().get(i+1); g2.draw(line);
// }
// line = new Line2D.Float(from.x,from.y,to.x,to.y) {}; }
// g2.draw(line);
// }
//
// g2.dispose();
//
// }
public void doPrint() public void doPrint()
{ {