123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- /*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
- */
-
- package mvcgrafik.view;
-
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.geom.Rectangle2D;
- 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.ArrayList;
- 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 mvcgrafik.model.GrafikModel;
- import mvcgrafik.util.OhmLogger;
-
-
- /**
- *
- * @author le
- */
- public class GrafikView extends JComponent implements Printable
- {
- private static Logger lg = OhmLogger.getLogger();
- private static Dimension EINS = new Dimension(1, 1); // Dimension ist eine Klasse die width udn height hält
- private Rectangle2D.Float pixel;
- private Line2D.Float line;
- private GrafikModel model;
-
- public GrafikView()
- {
- pixel = new Rectangle2D.Float();
- line = new Line2D.Float();
- }
-
- public void setModel(GrafikModel model)
- {
- this.model = model;
- }
-
-
-
- /**
- * Zeichnet den aktuellen Pfad (solange die maus gedrückt gehalten wird)
- * @param p -> Der aktuelle punkt als x-y-Koordinate
- */
- public void drawPoint(Point p)
- {
- Graphics2D g2 = (Graphics2D)this.getGraphics(); // gefährlich!
-
- drawPath(model.getPunkte(),g2);
-
- g2.dispose(); //SEEEEHHHHRRRR WICHTIG!!!!!!!
- }
-
-
- /**
- * Hier werden alle Pfade aus dem model neu gezeichnet
- * Jedes mal wenn die Maus los gelassen wird, wird dder aktuelle Pfad gespeichert
- * @param g
- */
- public void paintComponent(Graphics g)
- {
- if (model == null) return;
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D)g;
-
-
- model.getFiguren().forEach(figuren->
- {
- drawPath(figuren,g2);
- });
-
- }
-
- public void drawPath(List<Point> path, Graphics2D g2){
-
- for(int i=0; i < path.size()-1; i++)
- {
- Point from = path.get(i);
- Point to = path.get(i+1);
-
- line.setLine(from,to);
- g2.draw(line);
- }
- }
-
- public void doPrint()
- {
- HashPrintRequestAttributeSet printSet =
- new HashPrintRequestAttributeSet();
- printSet.add(DialogTypeSelection.NATIVE);
- PrinterJob pj = PrinterJob.getPrinterJob();
- pj.setPrintable(this);
- //Dialog
- 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;
- if (pageIndex == 0)
- {
- g2p.translate(pf.getImageableX(), pf.getImageableY());
- g2p.scale(pf.getImageableWidth() / this.getWidth(),
- pf.getImageableHeight() / this.getHeight());
- super.print(g2p);
- return Printable.PAGE_EXISTS;
- }
- else
- {
- return Printable.NO_SUCH_PAGE; // wichtig sonst Papiervernichtung
- }
- }
- }
|