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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 mvcgrafik.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 mvcgrafik.model.GrafikModel;
  24. /**
  25. *
  26. * @author le
  27. */
  28. public class GrafikView extends JComponent implements Printable
  29. {
  30. private final static Dimension EINS = new Dimension(1, 1);
  31. private final static Logger lg = Logger.getLogger("mvcGrafik");
  32. private Rectangle2D.Float pixel;
  33. private GrafikModel model;
  34. private Point old_punkt = null;
  35. public GrafikView()
  36. {
  37. pixel = new Rectangle2D.Float();
  38. this.setBackground(Color.WHITE);
  39. }
  40. public void setModel(GrafikModel model)
  41. {
  42. this.model = model;
  43. }
  44. public void drawLine(Point p, Point p_old)
  45. {
  46. Graphics2D g2 = (Graphics2D) this.getGraphics();
  47. Line2D.Double line = new Line2D.Double(p.getX(),p.getY(),p_old.getX(),p_old.getY());
  48. // pixel.setFrame(p, EINS);
  49. g2.draw(line);
  50. g2.dispose(); // VERY, VERY WICHTIG
  51. }
  52. @Override
  53. public void paintComponent(Graphics g)
  54. {
  55. if (model == null)
  56. {
  57. lg.severe("keine Referenz auf Model vorhanden");
  58. return;
  59. }
  60. super.paintComponent(g);
  61. Graphics2D g2 = (Graphics2D) g;
  62. model.getFigures().forEach(figure -> {
  63. figure.getPunkte().forEach(punkt -> {
  64. pixel.setFrame(punkt, EINS);
  65. // g2.draw(pixel);
  66. if(old_punkt != null)
  67. {
  68. drawLine(punkt, old_punkt);
  69. }
  70. old_punkt = punkt;
  71. });
  72. });
  73. }
  74. public void doPrint()
  75. {
  76. HashPrintRequestAttributeSet printSet =
  77. new HashPrintRequestAttributeSet();
  78. printSet.add(DialogTypeSelection.NATIVE);
  79. PrinterJob pj = PrinterJob.getPrinterJob();
  80. pj.setPrintable(this);
  81. // Druckdialog
  82. if (pj.printDialog(printSet))
  83. {
  84. try
  85. {
  86. pj.print(printSet);
  87. }
  88. catch (Exception ex)
  89. {
  90. JOptionPane.showMessageDialog(this, ex.toString());
  91. }
  92. }
  93. }
  94. @Override
  95. public int print(Graphics gp, PageFormat pf, int pageIndex) throws PrinterException
  96. {
  97. Graphics2D g2p = (Graphics2D) gp;
  98. if (pageIndex == 1)
  99. {
  100. g2p.translate(pf.getImageableX(), pf.getImageableY());
  101. g2p.scale(pf.getImageableWidth()/pf.getWidth(),
  102. pf.getImageableHeight() / pf.getHeight());
  103. super.print(g2p);
  104. return Printable.PAGE_EXISTS;
  105. }
  106. else
  107. {
  108. return Printable.NO_SUCH_PAGE;
  109. }
  110. }
  111. }