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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package grafikchat.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Point;
  7. import java.awt.RenderingHints;
  8. import java.awt.geom.Line2D;
  9. import java.awt.print.PageFormat;
  10. import java.awt.print.Printable;
  11. import java.awt.print.PrinterException;
  12. import java.awt.print.PrinterJob;
  13. import java.util.List;
  14. import java.util.logging.Logger;
  15. import javax.print.attribute.HashPrintRequestAttributeSet;
  16. import javax.print.attribute.standard.DialogTypeSelection;
  17. import javax.swing.JComponent;
  18. import javax.swing.JOptionPane;
  19. import grafikchat.model.Figure;
  20. import grafikchat.model.ChatModel;
  21. import grafikchat.model.Figures;
  22. import grafikchat.ohmlogger.OhmLogger;
  23. /**
  24. * View of zeichenprogramm
  25. *
  26. * @author marian
  27. */
  28. public class GrafikView extends JComponent implements Printable
  29. {
  30. private final static Logger lg = OhmLogger.getLogger();
  31. private Line2D.Double line;
  32. private ChatModel model;
  33. private final static Color colorInternal = Color.BLACK;
  34. private final static Color colorExternal = Color.RED;
  35. /**
  36. * Constructor, initialize UI
  37. */
  38. public GrafikView()
  39. {
  40. this.model = null;
  41. this.setBackground(Color.WHITE);
  42. line = new Line2D.Double();
  43. lg.info("Call constructor of grafikview");
  44. }
  45. /**
  46. * Set model
  47. * @param model model of MVC
  48. */
  49. public void setModel(ChatModel model)
  50. {
  51. this.model = model;
  52. lg.info("Set model of grafik view");
  53. }
  54. /**
  55. * Draw point (line with the point before)
  56. * @param mode True to draw internal point, false to draw external point
  57. */
  58. public void drawPoint(boolean mode)
  59. {
  60. if (this.model == null) {
  61. lg.warning("Model not initialized");
  62. return;
  63. }
  64. Graphics2D g2 = (Graphics2D) this.getGraphics();
  65. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  66. if (mode) {
  67. drawLine(model.getIntern(), colorInternal, g2);
  68. } else {
  69. drawLine(model.getExtern(), colorExternal, g2);
  70. }
  71. g2.dispose();
  72. }
  73. /**
  74. * Draw new line between two points
  75. * @param f Figures with points
  76. * @param c Color of line
  77. * @param g2 Graphics context
  78. */
  79. private void drawLine(Figures f, Color c, Graphics2D g2)
  80. {
  81. List<Point> points;
  82. try {
  83. points = f.getLastTwoPoints();
  84. } catch (Exception e) {
  85. return;
  86. }
  87. line.setLine(points.get(0), points.get(1));
  88. g2.setPaint(c);
  89. g2.setStroke(new BasicStroke(3));
  90. g2.draw(line);
  91. }
  92. /**
  93. * Repaint view
  94. * @param g Something special
  95. */
  96. @Override
  97. public void paintComponent(Graphics g)
  98. {
  99. super.paintComponent(g);
  100. if (model == null) {
  101. lg.warning("Model not initialized");
  102. return;
  103. }
  104. Graphics2D g2 = (Graphics2D) g;
  105. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  106. redrawFigures(model.getIntern(), colorInternal, g2);
  107. redrawFigures(model.getExtern(), colorExternal, g2);
  108. }
  109. /**
  110. * Redraw figure
  111. * @param figures Figure to redraw
  112. * @param g2 Graphics context
  113. */
  114. private void redrawFigures(Figures figures, Color c, Graphics2D g2)
  115. {
  116. for (Figure f : figures.getFigures()) {
  117. List<Point> points = f.getPoints();
  118. for (int i=1; i<points.size(); i++) {
  119. line.setLine(points.get(i-1), points.get(i));
  120. g2.setPaint(c);
  121. g2.setStroke(new BasicStroke(3));
  122. g2.draw(line);
  123. }
  124. }
  125. }
  126. /**
  127. * Try to print current graphics
  128. */
  129. public void doPrint()
  130. {
  131. HashPrintRequestAttributeSet printSet = new HashPrintRequestAttributeSet();
  132. printSet.add(DialogTypeSelection.NATIVE);
  133. PrinterJob pj = PrinterJob.getPrinterJob();
  134. pj.setPrintable(this);
  135. // Druckdialog
  136. if (pj.printDialog(printSet)) {
  137. try {
  138. pj.print(printSet);
  139. } catch (Exception ex) {
  140. JOptionPane.showMessageDialog(this, ex.toString());
  141. }
  142. }
  143. }
  144. /**
  145. * Perform print of graphics
  146. * @param gp Current graphics
  147. * @param pf format of page
  148. * @param pageIndex index of page
  149. * @return result if successful
  150. * @throws PrinterException if something went wrong
  151. */
  152. @Override
  153. public int print(Graphics gp, PageFormat pf, int pageIndex) throws PrinterException
  154. {
  155. Graphics2D g2p = (Graphics2D) gp;
  156. if (pageIndex == 0) {
  157. g2p.translate(pf.getImageableX(), pf.getImageableY());
  158. g2p.scale(pf.getImageableWidth()/pf.getWidth(), pf.getImageableHeight() / pf.getHeight());
  159. super.print(g2p);
  160. return Printable.PAGE_EXISTS;
  161. } else {
  162. return Printable.NO_SUCH_PAGE;
  163. }
  164. }
  165. }