123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- 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<Point> 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<Point> points = f.getPoints();
- for (int i=1; i<points.size(); i++) {
- line.setLine(points.get(i-1), points.get(i));
- g2.setPaint(c);
- g2.setStroke(new BasicStroke(3));
- g2.draw(line);
- }
- }
- }
-
- /**
- * Try to print current graphics
- */
- public void doPrint()
- {
- HashPrintRequestAttributeSet printSet = new HashPrintRequestAttributeSet();
- printSet.add(DialogTypeSelection.NATIVE);
- PrinterJob pj = PrinterJob.getPrinterJob();
- pj.setPrintable(this);
- // Druckdialog
- if (pj.printDialog(printSet)) {
- try {
- pj.print(printSet);
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(this, ex.toString());
- }
- }
- }
-
- /**
- * Perform print of graphics
- * @param gp Current graphics
- * @param pf format of page
- * @param pageIndex index of page
- * @return result if successful
- * @throws PrinterException if something went wrong
- */
- @Override
- public int print(Graphics gp, PageFormat pf, int pageIndex) throws PrinterException
- {
- Graphics2D g2p = (Graphics2D) gp;
- if (pageIndex == 0) {
- g2p.translate(pf.getImageableX(), pf.getImageableY());
- g2p.scale(pf.getImageableWidth()/pf.getWidth(), pf.getImageableHeight() / pf.getHeight());
- super.print(g2p);
- return Printable.PAGE_EXISTS;
- } else {
- return Printable.NO_SUCH_PAGE;
- }
- }
- }
|