package grafikchat.view; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.awt.geom.Line2D; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import java.util.List; import java.util.logging.Logger; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.standard.DialogTypeSelection; import javax.swing.JComponent; import javax.swing.JOptionPane; import grafikchat.model.Figure; import grafikchat.model.ChatModel; import grafikchat.model.Figures; import grafikchat.ohmlogger.OhmLogger; /** * View of zeichenprogramm * * @author marian */ public class GrafikView extends JComponent implements Printable { private final static Logger lg = OhmLogger.getLogger(); private Line2D.Double line; private ChatModel model; private final static Color colorInternal = Color.BLACK; private final static Color colorExternal = Color.RED; /** * Constructor, initialize UI */ public GrafikView() { this.model = null; this.setBackground(Color.WHITE); line = new Line2D.Double(); lg.info("Call constructor of grafikview"); } /** * Set model * @param model model of MVC */ public void setModel(ChatModel model) { this.model = model; lg.info("Set model of grafik view"); } /** * Draw point (line with the point before) * @param mode True to draw internal point, false to draw external point */ public void drawPoint(boolean mode) { if (this.model == null) { lg.warning("Model not initialized"); return; } Graphics2D g2 = (Graphics2D) this.getGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (mode) { drawLine(model.getIntern(), colorInternal, g2); } else { drawLine(model.getExtern(), colorExternal, g2); } g2.dispose(); } /** * Draw new line between two points * @param f Figures with points * @param c Color of line * @param g2 Graphics context */ private void drawLine(Figures f, Color c, Graphics2D g2) { List points; try { points = f.getLastTwoPoints(); } catch (Exception e) { return; } line.setLine(points.get(0), points.get(1)); g2.setPaint(c); g2.setStroke(new BasicStroke(3)); g2.draw(line); } /** * Repaint view * @param g Something special */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (model == null) { lg.warning("Model not initialized"); return; } Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); redrawFigures(model.getIntern(), colorInternal, g2); redrawFigures(model.getExtern(), colorExternal, g2); } /** * Redraw figure * @param figures Figure to redraw * @param g2 Graphics context */ private void redrawFigures(Figures figures, Color c, Graphics2D g2) { for (Figure f : figures.getFigures()) { List points = f.getPoints(); for (int i=1; i