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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 java.util.List;
  18. import java.util.logging.Logger;
  19. import javax.print.attribute.HashPrintRequestAttributeSet;
  20. import javax.print.attribute.standard.DialogTypeSelection;
  21. import javax.swing.JComponent;
  22. import javax.swing.JOptionPane;
  23. import mvcgrafik.model.GrafikModel;
  24. import mvcgrafik.util.OhmLogger;
  25. /**
  26. *
  27. * @author le
  28. */
  29. public class GrafikView extends JComponent implements Printable
  30. {
  31. private static Logger lg = OhmLogger.getLogger();
  32. private static Dimension EINS = new Dimension(1, 1); // Dimension ist eine Klasse die width udn height hält
  33. private Rectangle2D.Float pixel;
  34. private Line2D.Float line;
  35. private GrafikModel model;
  36. public GrafikView()
  37. {
  38. pixel = new Rectangle2D.Float();
  39. line = new Line2D.Float();
  40. }
  41. public void setModel(GrafikModel model)
  42. {
  43. this.model = model;
  44. }
  45. /**
  46. * Zeichnet den aktuellen Pfad (solange die maus gedrückt gehalten wird)
  47. * @param p -> Der aktuelle punkt als x-y-Koordinate
  48. */
  49. public void drawPoint(Point p)
  50. {
  51. Graphics2D g2 = (Graphics2D)this.getGraphics(); // gefährlich!
  52. drawPath(model.getPunkte(),g2);
  53. g2.dispose(); //SEEEEHHHHRRRR WICHTIG!!!!!!!
  54. }
  55. /**
  56. * Hier werden alle Pfade aus dem model neu gezeichnet
  57. * Jedes mal wenn die Maus los gelassen wird, wird dder aktuelle Pfad gespeichert
  58. * @param g
  59. */
  60. public void paintComponent(Graphics g)
  61. {
  62. if (model == null) return;
  63. super.paintComponent(g);
  64. Graphics2D g2 = (Graphics2D)g;
  65. model.getFiguren().forEach(figuren->
  66. {
  67. drawPath(figuren,g2);
  68. });
  69. }
  70. public void drawPath(List<Point> path, Graphics2D g2){
  71. for(int i=0; i < path.size()-1; i++)
  72. {
  73. Point from = path.get(i);
  74. Point to = path.get(i+1);
  75. line.setLine(from,to);
  76. g2.draw(line);
  77. }
  78. }
  79. public void doPrint()
  80. {
  81. HashPrintRequestAttributeSet printSet =
  82. new HashPrintRequestAttributeSet();
  83. printSet.add(DialogTypeSelection.NATIVE);
  84. PrinterJob pj = PrinterJob.getPrinterJob();
  85. pj.setPrintable(this);
  86. //Dialog
  87. if (pj.printDialog(printSet))
  88. {
  89. try
  90. {
  91. pj.print(printSet);
  92. }
  93. catch (Exception ex)
  94. {
  95. JOptionPane.showMessageDialog(this, ex.toString());
  96. }
  97. }
  98. }
  99. @Override
  100. public int print(Graphics gp, PageFormat pf, int pageIndex) throws PrinterException
  101. {
  102. Graphics2D g2p = (Graphics2D)gp;
  103. if (pageIndex == 0)
  104. {
  105. g2p.translate(pf.getImageableX(), pf.getImageableY());
  106. g2p.scale(pf.getImageableWidth() / this.getWidth(),
  107. pf.getImageableHeight() / this.getHeight());
  108. super.print(g2p);
  109. return Printable.PAGE_EXISTS;
  110. }
  111. else
  112. {
  113. return Printable.NO_SUCH_PAGE; // wichtig sonst Papiervernichtung
  114. }
  115. }
  116. }