123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- /*
- * 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 javax.print.attribute.HashPrintRequestAttributeSet;
- import javax.print.attribute.standard.DialogTypeSelection;
- import javax.swing.JComponent;
- import javax.swing.JOptionPane;
- import mvcgrafik.model.GrafikModel;
-
-
- /**
- *
- * @author le
- */
- public class GrafikView extends JComponent implements Printable
- {
- 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();
- }
-
- public void setModel(GrafikModel model)
- {
- this.model = model;
- }
-
-
-
- public void drawPoint(Point p)
- {
-
- Graphics2D g2 = (Graphics2D)this.getGraphics(); // gefährlich!
- paintComponent(g2);
- // pixel.setFrame(p, EINS);
- // g2.draw(pixel);
-
-
- //Um die aktuelle Figur zu zeichnen
- int pathSize = model.getPunkte().size();
-
- for(int i=0; i < pathSize-1; i++)
- {
- Point from = model.getPunkte().get(i);
- Point to = model.getPunkte().get(i+1);
-
- line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
- g2.draw(line);
- }
-
- //Um die fertigen Figuren zu zeichnen
- model.getFiguren().forEach(figure->
- {
- ArrayList<Point> currentPath = new ArrayList<>(figure);
- int figurePath = currentPath.size();
-
- for(int i=0; i < figurePath-1; i++)
- {
- Point from = currentPath.get(i);
- Point to = model.getPunkte().get(i+1);
-
- line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
- g2.draw(line);
- }
-
- });
-
- g2.dispose(); //SEEEEHHHHRRRR WICHTIG!!!!!!!
- }
-
- public void paintComponent(Graphics g)
- {
- if (model == null) return;
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D)g;
-
-
- // int pathSize = model.getPunkte().size();
- //
- // for(int i=0; i < pathSize-1; i++)
- // {
- // Point from = model.getPunkte().get(i);
- // Point to = model.getPunkte().get(i+1);
- //
- // line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
- // g2.draw(line);
- // }
-
- model.getFiguren().forEach(figure->
- {
- ArrayList<Point> currentPath = new ArrayList<>(figure);
- int figurePath = currentPath.size();
-
- for(int i=0; i < figurePath-1; i++)
- {
- Point from = currentPath.get(i);
- Point to = model.getPunkte().get(i+1);
-
- line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
- g2.draw(line);
- }
-
- });
-
-
-
- // model.getPunkte().forEach(p ->
- // {
- // pixel.setFrame(p, EINS);
- // g2.draw(pixel);
- // });
-
-
- }
-
- // public void drawPath(ArrayList<Point> path){
- // Graphics2D g2 = (Graphics2D)this.getGraphics();
- //
- // int pathSize = path.size();
- //
- // for(int i=0; i < pathSize-1; i++)
- // {
- // Point from = model.getPunkte().get(i);
- // Point to = model.getPunkte().get(i+1);
- //
- // line = new Line2D.Float(from.x,from.y,to.x,to.y) {};
- // g2.draw(line);
- // }
- //
- // g2.dispose();
- //
- // }
-
- 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
- }
- }
- }
|