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.

GrafikView.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.view;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9. import java.awt.Point;
  10. import java.awt.geom.Rectangle2D;
  11. import java.awt.geom.Line2D;
  12. import java.awt.print.PageFormat;
  13. import java.awt.print.Printable;
  14. import java.awt.print.PrinterException;
  15. import java.awt.print.PrinterJob;
  16. import java.util.ArrayList;
  17. import javax.print.attribute.HashPrintRequestAttributeSet;
  18. import javax.print.attribute.standard.DialogTypeSelection;
  19. import javax.swing.JComponent;
  20. import javax.swing.JOptionPane;
  21. import mvcgrafik.model.GrafikModel;
  22. /**
  23. *
  24. * @author le
  25. */
  26. public class GrafikView extends JComponent implements Printable
  27. {
  28. private static Dimension EINS = new Dimension(1, 1); // Dimension ist eine Klasse die width udn height hält
  29. private Rectangle2D.Float pixel;
  30. private Line2D.Float line;
  31. private GrafikModel model;
  32. public GrafikView()
  33. {
  34. pixel = new Rectangle2D.Float();
  35. }
  36. public void setModel(GrafikModel model)
  37. {
  38. this.model = model;
  39. }
  40. public void drawPoint(Point p)
  41. {
  42. Graphics2D g2 = (Graphics2D)this.getGraphics(); // gefährlich!
  43. paintComponent(g2);
  44. // pixel.setFrame(p, EINS);
  45. // g2.draw(pixel);
  46. //Um die aktuelle Figur zu zeichnen
  47. int pathSize = model.getPunkte().size();
  48. for(int i=0; i < pathSize-1; i++)
  49. {
  50. Point from = model.getPunkte().get(i);
  51. Point to = model.getPunkte().get(i+1);
  52. line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
  53. g2.draw(line);
  54. }
  55. //Um die fertigen Figuren zu zeichnen
  56. model.getFiguren().forEach(figure->
  57. {
  58. ArrayList<Point> currentPath = new ArrayList<>(figure);
  59. int figurePath = currentPath.size();
  60. for(int i=0; i < figurePath-1; i++)
  61. {
  62. Point from = currentPath.get(i);
  63. Point to = model.getPunkte().get(i+1);
  64. line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
  65. g2.draw(line);
  66. }
  67. });
  68. g2.dispose(); //SEEEEHHHHRRRR WICHTIG!!!!!!!
  69. }
  70. public void paintComponent(Graphics g)
  71. {
  72. if (model == null) return;
  73. super.paintComponent(g);
  74. Graphics2D g2 = (Graphics2D)g;
  75. // int pathSize = model.getPunkte().size();
  76. //
  77. // for(int i=0; i < pathSize-1; i++)
  78. // {
  79. // Point from = model.getPunkte().get(i);
  80. // Point to = model.getPunkte().get(i+1);
  81. //
  82. // line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
  83. // g2.draw(line);
  84. // }
  85. model.getFiguren().forEach(figure->
  86. {
  87. ArrayList<Point> currentPath = new ArrayList<>(figure);
  88. int figurePath = currentPath.size();
  89. for(int i=0; i < figurePath-1; i++)
  90. {
  91. Point from = currentPath.get(i);
  92. Point to = model.getPunkte().get(i+1);
  93. line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
  94. g2.draw(line);
  95. }
  96. });
  97. // model.getPunkte().forEach(p ->
  98. // {
  99. // pixel.setFrame(p, EINS);
  100. // g2.draw(pixel);
  101. // });
  102. }
  103. // public void drawPath(ArrayList<Point> path){
  104. // Graphics2D g2 = (Graphics2D)this.getGraphics();
  105. //
  106. // int pathSize = path.size();
  107. //
  108. // for(int i=0; i < pathSize-1; i++)
  109. // {
  110. // Point from = model.getPunkte().get(i);
  111. // Point to = model.getPunkte().get(i+1);
  112. //
  113. // line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
  114. // g2.draw(line);
  115. // }
  116. //
  117. // g2.dispose();
  118. //
  119. // }
  120. public void doPrint()
  121. {
  122. HashPrintRequestAttributeSet printSet =
  123. new HashPrintRequestAttributeSet();
  124. printSet.add(DialogTypeSelection.NATIVE);
  125. PrinterJob pj = PrinterJob.getPrinterJob();
  126. pj.setPrintable(this);
  127. //Dialog
  128. if (pj.printDialog(printSet))
  129. {
  130. try
  131. {
  132. pj.print(printSet);
  133. }
  134. catch (Exception ex)
  135. {
  136. JOptionPane.showMessageDialog(this, ex.toString());
  137. }
  138. }
  139. }
  140. @Override
  141. public int print(Graphics gp, PageFormat pf, int pageIndex) throws PrinterException
  142. {
  143. Graphics2D g2p = (Graphics2D)gp;
  144. if (pageIndex == 0)
  145. {
  146. g2p.translate(pf.getImageableX(), pf.getImageableY());
  147. g2p.scale(pf.getImageableWidth() / this.getWidth(),
  148. pf.getImageableHeight() / this.getHeight());
  149. super.print(g2p);
  150. return Printable.PAGE_EXISTS;
  151. }
  152. else
  153. {
  154. return Printable.NO_SUCH_PAGE; // wichtig sonst Papiervernichtung
  155. }
  156. }
  157. }