diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03ef08b --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/build/ +/nbproject/private/ +*.xml +*.properties +*.mf +/dist/ +/wurfel/nbproject/private/ +/wurfel/dist/ +/wurfel/build/ diff --git a/src/mvcgrafik/Start.java b/src/mvcgrafik/Start.java new file mode 100755 index 0000000..eae9998 --- /dev/null +++ b/src/mvcgrafik/Start.java @@ -0,0 +1,43 @@ +/* + * 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 mvcgrafik; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; +import mvcgrafik.controller.GrafikController; +import mvcgrafik.model.GrafikModel; +import mvcgrafik.view.GrafikView; + +/** + * Builder Class + * @author le + */ +public class Start +{ + public Start() + { + JFrame frm = new JFrame(); + frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + GrafikView view = new GrafikView(); + GrafikModel model = new GrafikModel(); + view.setModel(model); + GrafikController controller = new GrafikController(view, model); + controller.registerEvents(); + frm.setContentPane(view); + frm.setSize(800, 600); + frm.setVisible(true); + + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) + { + new Start(); + } +} diff --git a/src/mvcgrafik/controller/GrafikController.java b/src/mvcgrafik/controller/GrafikController.java new file mode 100755 index 0000000..c413eae --- /dev/null +++ b/src/mvcgrafik/controller/GrafikController.java @@ -0,0 +1,78 @@ +/* + * 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 mvcgrafik.controller; + +import java.awt.Point; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import mvcgrafik.model.GrafikModel; +import mvcgrafik.view.GrafikView; + +/** + * + * @author le + */ +public class GrafikController implements MouseMotionListener, MouseListener +{ + private GrafikView view; + private GrafikModel model; + + public GrafikController(GrafikView view, GrafikModel model) + { + this.view = view; + this.model = model; + } + + public void registerEvents() + { + view.addMouseMotionListener(this); + view.addMouseListener(this); + } + + @Override + public void mouseDragged(MouseEvent evt) + { + Point p = evt.getPoint(); + view.drawPoint(p); + model.addPoint(p); + } + + @Override + public void mouseMoved(MouseEvent e) + { + } + + @Override + public void mouseClicked(MouseEvent e) + { + } + + @Override + public void mousePressed(MouseEvent e) + { + } + + @Override + public void mouseReleased(MouseEvent evt) + { + if (evt.getButton() == MouseEvent.BUTTON3) + { + view.doPrint(); + } + } + + @Override + public void mouseEntered(MouseEvent e) + { + } + + @Override + public void mouseExited(MouseEvent e) + { + } +} diff --git a/src/mvcgrafik/model/GrafikModel.java b/src/mvcgrafik/model/GrafikModel.java new file mode 100755 index 0000000..aac8901 --- /dev/null +++ b/src/mvcgrafik/model/GrafikModel.java @@ -0,0 +1,73 @@ +/* + * 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 mvcgrafik.model; + +import java.awt.Point; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * + * @author le + */ +public class GrafikModel +{ + private ArrayList punkte; + + public GrafikModel() + { + punkte = new ArrayList<>(); + } + + public void addPoint(Point p) + { + punkte.add(p); + } + + public List getPunkte() + { + return Collections.unmodifiableList(punkte); + } + + public void speicherePunkte(String dateiname) throws FileNotFoundException, IOException + { + FileOutputStream fos = new FileOutputStream(dateiname); + BufferedOutputStream buffout = new BufferedOutputStream(fos); + ObjectOutputStream oos = new ObjectOutputStream(buffout); + oos.writeObject(punkte); + oos.flush(); + oos.close(); + } + + public void ladePunkte(String dateiname) throws FileNotFoundException, IOException, ClassNotFoundException + { + FileInputStream fis = new FileInputStream(dateiname); + BufferedInputStream buffin = new BufferedInputStream(fis); + ObjectInputStream ois = new ObjectInputStream(buffin); + Object obj = ois.readObject(); +// if (obj instanceof ArrayList) +// { +// punkte = (ArrayList) obj; +// } +// else +// { +// Fehler .... +// } + punkte = (ArrayList) ois.readObject(); + ois.close(); + } + +} diff --git a/src/mvcgrafik/view/GrafikView.java b/src/mvcgrafik/view/GrafikView.java new file mode 100755 index 0000000..919be49 --- /dev/null +++ b/src/mvcgrafik/view/GrafikView.java @@ -0,0 +1,112 @@ +/* + * 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 mvcgrafik.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.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 mvcgrafik.model.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 GrafikModel model; + + public GrafikView() + { + pixel = new Rectangle2D.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; + + for (Point p : model.getPunkte()) + { + pixel.setFrame(p, EINS); + g2.draw(pixel); + } + } + + 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; + if (pageIndex == 1) + { + 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; + } + } + +}