You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GrafikModel.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3. * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
  4. */
  5. package mvcgrafik.model;
  6. import java.awt.Point;
  7. import java.io.BufferedInputStream;
  8. import java.io.BufferedOutputStream;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.io.ObjectInputStream;
  14. import java.io.ObjectOutputStream;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.List;
  18. /**
  19. *
  20. * @author le
  21. */
  22. public class GrafikModel
  23. {
  24. private ArrayList<Point> punkte;
  25. private ArrayList<ArrayList> figuren;
  26. public GrafikModel()
  27. {
  28. punkte = new ArrayList<>();
  29. figuren = new ArrayList<>();
  30. }
  31. public void addPoint(Point p)
  32. {
  33. punkte.add(p);
  34. }
  35. public List<Point> getPunkte()
  36. {
  37. return Collections.unmodifiableList(punkte);
  38. }
  39. public List<ArrayList> getFiguren(){
  40. return Collections.unmodifiableList(figuren);
  41. }
  42. /**
  43. * Persistenz via Serialisierung
  44. * @param dateiname
  45. * @throws FileNotFoundException
  46. * @throws IOException
  47. */
  48. public void speicherePunkte(String dateiname) throws FileNotFoundException, IOException
  49. {
  50. // an Preferences denken!!
  51. FileOutputStream fos = new FileOutputStream(dateiname);
  52. //wichtig Puffer -> Performance
  53. BufferedOutputStream bos = new BufferedOutputStream(fos);
  54. //Serialisierung
  55. ObjectOutputStream oos = new ObjectOutputStream(bos);
  56. oos.writeObject(punkte);
  57. oos.flush(); // Puffer
  58. oos.close();
  59. }
  60. public void lesePunkte(String dateiname) throws FileNotFoundException, IOException, ClassNotFoundException
  61. {
  62. // an Preferences denken!!
  63. FileInputStream fis = new FileInputStream(dateiname);
  64. //wichtig Puffer -> Performance
  65. BufferedInputStream bis = new BufferedInputStream(fis);
  66. //Serialisierung
  67. ObjectInputStream ois = new ObjectInputStream(bis);
  68. Object daten = ois.readObject(); // Achtung
  69. // if (daten instanceof ArrayList)
  70. // {
  71. // punkte = (ArrayList<Point>)daten;
  72. // }
  73. //eleganter
  74. if (daten instanceof ArrayList liste)
  75. {
  76. punkte = liste;
  77. }
  78. }
  79. public void endShape() {
  80. figuren.add(new ArrayList<Point>(punkte));
  81. punkte.clear();
  82. }
  83. }