GrafikFrame und View mal eingefügt aber noch nicht eingebunden
Ich hab überall wo der Frame eingebunden werden muss die sachen auskommentiert und ToDos eingefügt
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* 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 ChatProgramm.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 ChatProgramm.model.GrafikModel;
|
||||
import ChatProgramm.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;
|
||||
|
||||
private Point from = null;
|
||||
private Point to = null;
|
||||
|
||||
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
|
||||
*/
|
||||
@Override
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
if (model == null) return;
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
|
||||
|
||||
model.getFiguren().forEach(figuren->
|
||||
{
|
||||
drawPath(figuren.getPunkte(),g2);
|
||||
});
|
||||
}
|
||||
|
||||
public void drawPath(List<Point> path, Graphics2D g2){
|
||||
from = path.getFirst();
|
||||
to = null;
|
||||
|
||||
path.forEach(point->
|
||||
{
|
||||
to = point;
|
||||
line.setLine(from,to);
|
||||
g2.draw(line);
|
||||
from = to;
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user