123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /*
- * 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
- */
-
- package ChatProgramm.model;
-
- import java.awt.Point;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.logging.Logger;
- import java.util.prefs.Preferences;
- import ChatProgramm.util.OhmLogger;
- /**
- *
- * @author le
- */
- public class GrafikModel
- {
- private Figur aktuelleFigur;
- private ArrayList<Figur> figuren;
- private Preferences pref;
- private static Logger lg = OhmLogger.getLogger();
-
- public GrafikModel()
- {
- aktuelleFigur = new Figur();
- figuren = new ArrayList<>();
- }
-
- public void addPoint(Point p)
- {
- aktuelleFigur.addPoint(p);
- }
-
- public List<Point> getPunkte()
- {
- return aktuelleFigur.getPunkte();
- }
-
- public List<Figur> getFiguren(){
- return Collections.unmodifiableList(figuren);
- }
-
-
- /**
- * Persistenz via Serialisierung
- * @param dateiname
- * @throws FileNotFoundException
- * @throws IOException
- */
- public void speicherePunkte(String dateiname) throws FileNotFoundException, IOException
- {
- // an Preferences denken!!
- FileOutputStream fos = new FileOutputStream(dateiname);
- //wichtig Puffer -> Performance
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- //Serialisierung
- ObjectOutputStream oos = new ObjectOutputStream(bos);
- oos.writeObject(figuren);
- oos.flush(); // Puffer
- oos.close();
- }
-
- public void lesePunkte(String dateiname) throws FileNotFoundException, IOException, ClassNotFoundException
- {
- // an Preferences denken!!
- FileInputStream fis = new FileInputStream(dateiname);
- //wichtig Puffer -> Performance
- BufferedInputStream bis = new BufferedInputStream(fis);
- //Serialisierung
- ObjectInputStream ois = new ObjectInputStream(bis);
- Object daten = ois.readObject(); // Achtung
- // if (daten instanceof ArrayList)
- // {
- // punkte = (ArrayList<Point>)daten;
- // }
- //eleganter
- if (daten instanceof ArrayList liste)
- {
- figuren = liste;
- }
-
- }
-
- public void endShape() {
- figuren.add(aktuelleFigur);
- aktuelleFigur = new Figur();
- }
-
- /**
- * Bestimmt die Adresse des zuletzt besuchten Ordners
- * @return letzter Ordner
- */
- public String getPref()
- {
- pref = Preferences.userNodeForPackage(getClass());
- return pref.get("lastDirectory", null);
- }
-
- /**
- * Setzt die Preferenz zu dem zuletzt besuchten Ordner
- * @param lastDirectory Bezeichner "lastDirectory"
- * @param lastAdress Adresse des zuletzt besuchten Ordners
- */
- public void putPref(String lastDirectory, String lastAdress)
- {
- pref.put(lastDirectory, lastAdress);
- }
- }
-
|