This commit is contained in:
2023-12-04 11:33:35 +01:00
commit 72fb7d9782
12 changed files with 2498 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package mvcgrafik;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import mvcgrafik.controller.GrafikController;
import mvcgrafik.model.GrafikModel;
import mvcgrafik.view.GrafikFrame;
import mvcgrafik.view.GrafikView;
/**
* Builder Class
* @author le
*/
public class Start
{
public Start()
{
GrafikModel model = new GrafikModel();
GrafikFrame frm = new GrafikFrame();
GrafikView view = frm.getgZeichenflaeche();
view.setModel(model);
GrafikController controller = new GrafikController(view, model);
controller.registerEvents();
frm.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.toString());
}
new Start();
}
}
@@ -0,0 +1,58 @@
/*
* 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.controller;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import mvcgrafik.model.GrafikModel;
import mvcgrafik.view.GrafikView;
/**
*
* @author le
*/
public class GrafikController extends MouseAdapter implements MouseMotionListener
{
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();
model.addPoint(p);
view.drawPoint(p);
}
@Override
public void mouseMoved(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent evt)
{
model.endShape();
if (evt.getButton() == MouseEvent.BUTTON3)
{
view.doPrint();
}
}
}
+98
View File
@@ -0,0 +1,98 @@
/*
* 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.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;
private ArrayList<ArrayList> figuren;
public GrafikModel()
{
punkte = new ArrayList<>();
figuren = new ArrayList<>();
}
public void addPoint(Point p)
{
punkte.add(p);
}
public List<Point> getPunkte()
{
return Collections.unmodifiableList(punkte);
}
public List<ArrayList> getFiguren(){
return Collections.unmodifiableList(figuren);
}
/**
* Persistenz via Serialisierung
* @param dateiname
* @throws FileNotFoundException
* @throws IOException
*/
public void speicherePunkte(String dateiname) throws FileNotFoundException, IOException
{
// an Preferences denken!!
FileOutputStream fos = new FileOutputStream(dateiname);
//wichtig Puffer -> Performance
BufferedOutputStream bos = new BufferedOutputStream(fos);
//Serialisierung
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(punkte);
oos.flush(); // Puffer
oos.close();
}
public void lesePunkte(String dateiname) throws FileNotFoundException, IOException, ClassNotFoundException
{
// an Preferences denken!!
FileInputStream fis = new FileInputStream(dateiname);
//wichtig Puffer -> Performance
BufferedInputStream bis = new BufferedInputStream(fis);
//Serialisierung
ObjectInputStream ois = new ObjectInputStream(bis);
Object daten = ois.readObject(); // Achtung
// if (daten instanceof ArrayList)
// {
// punkte = (ArrayList<Point>)daten;
// }
//eleganter
if (daten instanceof ArrayList liste)
{
punkte = liste;
}
}
public void endShape() {
figuren.add(punkte);
punkte.clear();
}
}
+39
View File
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,-100,0,0,2,28"/>
<SyntheticProperty name="formSizePolicy" type="int" value="0"/>
<SyntheticProperty name="generateSize" type="boolean" value="true"/>
<SyntheticProperty name="generateCenter" type="boolean" value="true"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/>
<SubComponents>
<Container class="mvcgrafik.view.GrafikView" name="gZeichenflaeche">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription">
<BorderConstraints direction="Center"/>
</Constraint>
</Constraints>
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout">
<Property name="useNullLayout" type="boolean" value="true"/>
</Layout>
</Container>
</SubComponents>
</Form>
+100
View File
@@ -0,0 +1,100 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package mvcgrafik.view;
/**
*
* @author le
*/
public class GrafikFrame extends javax.swing.JFrame
{
/**
* @return the gZeichenflaeche
*/
public mvcgrafik.view.GrafikView getgZeichenflaeche()
{
return gZeichenflaeche;
}
/**
* Creates new form GrafikFrame
*/
public GrafikFrame()
{
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents()
{
gZeichenflaeche = new mvcgrafik.view.GrafikView();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(gZeichenflaeche, java.awt.BorderLayout.CENTER);
setSize(new java.awt.Dimension(540, 412));
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(GrafikFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(GrafikFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(GrafikFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(GrafikFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new GrafikFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private mvcgrafik.view.GrafikView gZeichenflaeche;
// End of variables declaration//GEN-END:variables
}
+191
View File
@@ -0,0 +1,191 @@
/*
* 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
}
}
}