123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package kommunikation.view;
-
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.geom.Line2D;
- import java.awt.geom.Rectangle2D;
- import java.awt.print.PageFormat;
- import java.awt.print.Printable;
- import java.awt.print.PrinterException;
- import java.awt.print.PrinterJob;
- 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 kommunikation.transmitter.Figur;
- import kommunikation.transmitter.GrafikModel;
-
-
- /**
- *
- * @author le
- */
- public class GrafikView extends JComponent implements Printable
- {
- private final static Dimension EINS = new Dimension(1, 1);
- private final static Logger lg = Logger.getLogger("mvcGrafik");
- private Rectangle2D.Float pixel;
- private Line2D.Float linie;
- private GrafikModel model;
-
- public GrafikView()
- {
- pixel = new Rectangle2D.Float();
- linie = new Line2D.Float();
- this.setBackground(Color.WHITE);
- }
-
- public void setModel(GrafikModel model)
- {
- this.model = model;
- }
-
- public void drawPoint(Point p)
- {
- Graphics2D g2 = (Graphics2D) this.getGraphics();
- pixel.setFrame(p, EINS);
- g2.draw(pixel);
- g2.dispose(); // VERY, VERY WICHTIG
- }
-
- @Override
- public void paintComponent(Graphics g)
- {
- if (model == null)
- {
- lg.severe("keine Referenz auf Model vorhanden");
- return;
- }
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g;
-
- Point zwischenSpeicher = new Point();
- for (Figur f : model.getFigur())
- {
- g2.setColor(Color.BLACK);
- Boolean first = true;
- for (Point p : f.getPunkte())
- {
- pixel.setFrame(p, EINS);
- g2.draw(pixel);
- if(!first)
- {
- linie.setLine(zwischenSpeicher, p);
- g2.draw(linie);
- }
- zwischenSpeicher = p;
- first = false;
- }
- }
- for (Figur f : model.getFigurFremd())
- {
- Boolean first = true;
- g2.setColor(Color.RED);
- for (Point p : f.getPunkte())
- {
- pixel.setFrame(p, EINS);
- g2.draw(pixel);
- if(!first)
- {
- linie.setLine(zwischenSpeicher, p);
- g2.draw(linie);
- }
- zwischenSpeicher = p;
- first = false;
- }
- }
- }
-
- public void line(Point p1, Point p2)
- {
- Graphics2D g2 = (Graphics2D) this.getGraphics();
- Line2D.Float lin = new Line2D.Float(p1, p2);
- g2.draw(lin);
- g2.dispose();
- }
-
- 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());
- }
- }
- }
-
- @Override
- public int print(Graphics gp, PageFormat pf, int pageIndex) throws PrinterException
- {
- Graphics2D g2p = (Graphics2D) gp;
- Logger lg = Logger.getLogger("mvcGrafik");
- lg.info("pgIndex: " + Integer.toString(pageIndex));
- if (pageIndex == 0)
- {
- g2p.translate(pf.getImageableX(), pf.getImageableY());
- g2p.scale(pf.getImageableWidth()/this.getWidth(),
- pf.getImageableHeight() / this.getHeight());
- super.print(g2p);
-
- lg.info("Druck ist möglich");
- return Printable.PAGE_EXISTS;
- }
- else
- {
- lg.info("Druck ist unmöglich");
- return Printable.NO_SUCH_PAGE;
- }
- }
-
- }
|