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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package kommunikation.view;
  7. import java.awt.Color;
  8. import java.awt.Dimension;
  9. import java.awt.Graphics;
  10. import java.awt.Graphics2D;
  11. import java.awt.Point;
  12. import java.awt.geom.Line2D;
  13. import java.awt.geom.Rectangle2D;
  14. import java.awt.print.PageFormat;
  15. import java.awt.print.Printable;
  16. import java.awt.print.PrinterException;
  17. import java.awt.print.PrinterJob;
  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 kommunikation.transmitter.Figur;
  24. import kommunikation.transmitter.GrafikModel;
  25. /**
  26. *
  27. * @author le
  28. */
  29. public class GrafikView extends JComponent implements Printable
  30. {
  31. private final static Dimension EINS = new Dimension(1, 1);
  32. private final static Logger lg = Logger.getLogger("mvcGrafik");
  33. private Rectangle2D.Float pixel;
  34. private Line2D.Float linie;
  35. private GrafikModel model;
  36. public GrafikView()
  37. {
  38. pixel = new Rectangle2D.Float();
  39. linie = new Line2D.Float();
  40. this.setBackground(Color.WHITE);
  41. }
  42. public void setModel(GrafikModel model)
  43. {
  44. this.model = model;
  45. }
  46. public void drawPoint(Point p)
  47. {
  48. Graphics2D g2 = (Graphics2D) this.getGraphics();
  49. pixel.setFrame(p, EINS);
  50. g2.draw(pixel);
  51. g2.dispose(); // VERY, VERY WICHTIG
  52. }
  53. @Override
  54. public void paintComponent(Graphics g)
  55. {
  56. if (model == null)
  57. {
  58. lg.severe("keine Referenz auf Model vorhanden");
  59. return;
  60. }
  61. super.paintComponent(g);
  62. Graphics2D g2 = (Graphics2D) g;
  63. Point zwischenSpeicher = new Point();
  64. for (Figur f : model.getFigur())
  65. {
  66. g2.setColor(Color.BLACK);
  67. Boolean first = true;
  68. for (Point p : f.getPunkte())
  69. {
  70. pixel.setFrame(p, EINS);
  71. g2.draw(pixel);
  72. if(!first)
  73. {
  74. linie.setLine(zwischenSpeicher, p);
  75. g2.draw(linie);
  76. }
  77. zwischenSpeicher = p;
  78. first = false;
  79. }
  80. }
  81. for (Figur f : model.getFigurFremd())
  82. {
  83. Boolean first = true;
  84. g2.setColor(Color.RED);
  85. for (Point p : f.getPunkte())
  86. {
  87. pixel.setFrame(p, EINS);
  88. g2.draw(pixel);
  89. if(!first)
  90. {
  91. linie.setLine(zwischenSpeicher, p);
  92. g2.draw(linie);
  93. }
  94. zwischenSpeicher = p;
  95. first = false;
  96. }
  97. }
  98. }
  99. public void line(Point p1, Point p2)
  100. {
  101. Graphics2D g2 = (Graphics2D) this.getGraphics();
  102. Line2D.Float lin = new Line2D.Float(p1, p2);
  103. g2.draw(lin);
  104. g2.dispose();
  105. }
  106. public void doPrint()
  107. {
  108. HashPrintRequestAttributeSet printSet =
  109. new HashPrintRequestAttributeSet();
  110. printSet.add(DialogTypeSelection.NATIVE);
  111. PrinterJob pj = PrinterJob.getPrinterJob();
  112. pj.setPrintable(this);
  113. // Druckdialog
  114. if (pj.printDialog(printSet))
  115. {
  116. try
  117. {
  118. pj.print(printSet);
  119. }
  120. catch (Exception ex)
  121. {
  122. JOptionPane.showMessageDialog(this, ex.toString());
  123. }
  124. }
  125. }
  126. @Override
  127. public int print(Graphics gp, PageFormat pf, int pageIndex) throws PrinterException
  128. {
  129. Graphics2D g2p = (Graphics2D) gp;
  130. Logger lg = Logger.getLogger("mvcGrafik");
  131. lg.info("pgIndex: " + Integer.toString(pageIndex));
  132. if (pageIndex == 0)
  133. {
  134. g2p.translate(pf.getImageableX(), pf.getImageableY());
  135. g2p.scale(pf.getImageableWidth()/this.getWidth(),
  136. pf.getImageableHeight() / this.getHeight());
  137. super.print(g2p);
  138. lg.info("Druck ist möglich");
  139. return Printable.PAGE_EXISTS;
  140. }
  141. else
  142. {
  143. lg.info("Druck ist unmöglich");
  144. return Printable.NO_SUCH_PAGE;
  145. }
  146. }
  147. }