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

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