1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /*
- * 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 mvcgrafik.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;
-
- /**
- *
- * @author le
- */
- public class GrafikModel
- {
- private ArrayList<Point> punkte;
- private ArrayList<ArrayList> figuren;
-
- public GrafikModel()
- {
- punkte = new ArrayList<>();
- figuren = new ArrayList<>();
- }
-
- public void addPoint(Point p)
- {
- punkte.add(p);
- }
-
- public List<Point> getPunkte()
- {
- return Collections.unmodifiableList(punkte);
- }
-
- public List<ArrayList> 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(punkte);
- 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)
- {
- punkte = liste;
- }
-
- }
-
- public void endShape() {
- figuren.add(punkte);
- punkte.clear();
- }
-
-
-
- }
|