Browse Source

init

master
Hannes Dürr 4 years ago
parent
commit
7dd6e6905e

+ 9
- 0
.gitignore View File

/build/
/nbproject/private/
*.xml
*.properties
*.mf
/dist/
/wurfel/nbproject/private/
/wurfel/dist/
/wurfel/build/

+ 43
- 0
src/mvcgrafik/Start.java View File

/*
* 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();
}
}

+ 78
- 0
src/mvcgrafik/controller/GrafikController.java View File

/*
* 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)
{
}
}

+ 73
- 0
src/mvcgrafik/model/GrafikModel.java View File

/*
* 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<Point> punkte;
public GrafikModel()
{
punkte = new ArrayList<>();
}
public void addPoint(Point p)
{
punkte.add(p);
}

public List<Point> 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<Point>) obj;
// }
// else
// {
// Fehler ....
// }
punkte = (ArrayList<Point>) ois.readObject();
ois.close();
}
}

+ 112
- 0
src/mvcgrafik/view/GrafikView.java View File

/*
* 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;
}
}

}

Loading…
Cancel
Save