Browse Source

added new grafics menu

master
Hannes Dürr 4 years ago
parent
commit
6b02d2b0da

+ 0
- 0
TEST View File


+ 8
- 5
src/mvcgrafik/Start.java View File

@@ -10,6 +10,7 @@ import javax.swing.JFrame;
import javax.swing.WindowConstants;
import mvcgrafik.controller.GrafikController;
import mvcgrafik.model.GrafikModel;
import mvcgrafik.view.GrafikMenuView;
import mvcgrafik.view.GrafikView;
//import mvcgrafik.ohmLogger;

@@ -23,14 +24,16 @@ public class Start
{
JFrame frm = new JFrame();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
GrafikView view = new GrafikView();

GrafikMenuView view = new GrafikMenuView();
GrafikModel model = new GrafikModel();
view.setModel(model);
view.getGrafikView1().setModel(model);
// view.setModel(model);
GrafikController controller = new GrafikController(view, model);
controller.registerEvents();
frm.setContentPane(view);
frm.setSize(800, 600);
frm.setVisible(true);
view.setSize(800, 600);
view.setVisible(true);

}


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

@@ -7,30 +7,40 @@
package mvcgrafik.controller;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import mvcgrafik.model.Figure;
import mvcgrafik.model.GrafikModel;
import mvcgrafik.view.GrafikView;
import mvcgrafik.view.GrafikMenuView;
import mvcgrafik.logger.OhmLogger;


/**
*
* @author le
*/
public class GrafikController implements MouseMotionListener, MouseListener, KeyListener
public class GrafikController implements MouseMotionListener, MouseListener, ActionListener
{
private GrafikView view;
private GrafikMenuView view;
//private GrafikMenuView menuview;
private GrafikModel model;
private Figure figure;
private Point p_old;
private static Logger lg = OhmLogger.getLogger();
public GrafikController(GrafikView view, GrafikModel model)
public GrafikController(GrafikMenuView view, GrafikModel model)
{
this.view = view;
this.model = model;
@@ -40,14 +50,22 @@ public class GrafikController implements MouseMotionListener, MouseListener, Key
{
view.addMouseMotionListener(this);
view.addMouseListener(this);
view.getBtnOpen().addActionListener(this);
view.getBtnSafe().addActionListener(this);
}
@Override
public void mouseDragged(MouseEvent evt)
{
if(figure == null)
{
lg.info("new Figure");
figure = model.addFigure();
}
Point p = evt.getPoint();
if(p_old != null){
view.drawLine(p, p_old);
// view.drawLine(p, p_old);
view.getGrafikView1().drawLine(p, p_old);
}
p_old = p;
figure.addPoint(p);
@@ -61,7 +79,7 @@ public class GrafikController implements MouseMotionListener, MouseListener, Key
@Override
public void mouseClicked(MouseEvent e)
{
lg.info("clicked");
figure = model.addFigure();
}
@@ -74,10 +92,14 @@ public class GrafikController implements MouseMotionListener, MouseListener, Key
@Override
public void mouseReleased(MouseEvent evt)
{
p_old = null;
figure = null;
lg.info("Figure finished");
if (evt.getButton() == MouseEvent.BUTTON3)
{
view.doPrint();
// view.doPrint();
view.getGrafikView1().doPrint();
}
}

@@ -90,45 +112,64 @@ public class GrafikController implements MouseMotionListener, MouseListener, Key
public void mouseExited(MouseEvent e)
{
}

@Override
public void keyTyped(KeyEvent e)
{
}

@Override
public void keyPressed(KeyEvent e)
public void actionPerformed(ActionEvent ae)
{
if(e.getKeyCode() == KeyEvent.VK_S)
if(ae.getSource() == view.getBtnSafe())
{
try
{
model.speichereDatei("TEST");
}
catch (IOException ex)
Preferences pref = Preferences.userNodeForPackage(this.getClass());
String path = pref.get("DEFAULT_PATH", "");
view.getjFileChooser1().setCurrentDirectory(new File(path));


int choice = view.getjFileChooser1().showSaveDialog(view);
if (choice == JFileChooser.APPROVE_OPTION)
{
Logger.getLogger(GrafikController.class.getName()).log(Level.SEVERE, null, ex);
File selectedFile = view.getjFileChooser1().getSelectedFile();
pref.put("DEFAULT_PATH", selectedFile.getAbsolutePath());
try
{
//model.datenSpeichern(selectedFile);
model.speichereDatei("TEST");
}
catch (UnsupportedEncodingException ex)
{
Logger.getLogger(GrafikController.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(GrafikController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
if(e.getKeyCode() == KeyEvent.VK_O)
if(ae.getSource() == view.getBtnOpen())
{
try
{
model.ladeDatei("TEST");
}
catch (IOException ex)
{
Logger.getLogger(GrafikController.class.getName()).log(Level.SEVERE, null, ex);
}
catch (ClassNotFoundException ex)
Preferences pref = Preferences.userNodeForPackage(this.getClass());
String path = pref.get("DEFAULT_PATH", "");
view.getjFileChooser1().setCurrentDirectory(new File(path));

int choice = view.getjFileChooser1().showOpenDialog(view);
if (choice == JFileChooser.APPROVE_OPTION)
{
Logger.getLogger(GrafikController.class.getName()).log(Level.SEVERE, null, ex);
File selectedFile = view.getjFileChooser1().getSelectedFile();

pref.put("DEFAULT_PATH", selectedFile.getAbsolutePath());
try
{
//model.datenLesen(selectedFile);
model.ladeDatei("TEST");
}
catch (UnsupportedEncodingException ex)
{
Logger.getLogger(GrafikController.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException | ClassNotFoundException ex)
{
Logger.getLogger(GrafikController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}

@Override
public void keyReleased(KeyEvent e)
{
}
}


+ 26
- 0
src/mvcgrafik/logger/MyFormatter.java View File

@@ -0,0 +1,26 @@
/*
* 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.logger;

import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

/**
*
* @author chris
*/
public class MyFormatter extends Formatter {
@Override
public String format(LogRecord lr) {
String date = String.format("%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp", new Date(lr.getMillis()));
String s = ("| ")+lr.getMillis()+(" | ")+date+(" | ")+lr.getLevel().toString()+(" | ")+lr.getSourceClassName()+(" | ")+lr.getMessage()+(" | ")+"\n";
return s;
}
}

+ 54
- 0
src/mvcgrafik/logger/OhmLogger.java View File

@@ -0,0 +1,54 @@
/*
* 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.logger;


import mvcgrafik.logger.MyFormatter;
import java.io.File;
import java.io.IOException;
import java.util.logging.*;



/**
*
* @author chris, hd
*/
public class OhmLogger
{
public OhmLogger()
{
}
private static Logger lg = null;
public static Logger getLogger()
{
if (lg == null)
{
lg = Logger.getLogger("OhmLogger");
initLogger();
}
return lg;
}
private static void initLogger()
{
try{
String datei = System.getProperty("java.io.tmpdir") + File.separator + "log.txt";
FileHandler fh = new FileHandler(datei);
ConsoleHandler ch = new ConsoleHandler();
lg.addHandler(fh);
ch.setFormatter(new MyFormatter());
lg.setUseParentHandlers(false);
lg.addHandler(ch);
lg.setLevel(Level.ALL);
}
catch(IOException ioex)
{
System.err.println(ioex);
}
}
}

+ 3
- 3
src/mvcgrafik/model/GrafikModel.java View File

@@ -51,8 +51,9 @@ public class GrafikModel
oos.writeObject(figures);
oos.flush();
oos.close();

}
public void ladeDatei(String dateiname) throws FileNotFoundException, IOException, ClassNotFoundException
{
FileInputStream fis = new FileInputStream(dateiname);
@@ -70,5 +71,4 @@ public class GrafikModel
figures = (ArrayList<Figure>) ois.readObject();
ois.close();
}
}
}

+ 91
- 0
src/mvcgrafik/view/GrafikMenuView.form View File

@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<NonVisualComponents>
<Component class="javax.swing.JFileChooser" name="jFileChooser1">
</Component>
<Menu class="javax.swing.JMenuBar" name="jMenuBar1">
<SubComponents>
<Menu class="javax.swing.JMenu" name="jMenu1">
<Properties>
<Property name="text" type="java.lang.String" value="File"/>
</Properties>
<SubComponents>
<MenuItem class="javax.swing.JMenuItem" name="btnOpen">
<Properties>
<Property name="text" type="java.lang.String" value="Open"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnOpenActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="btnSafe">
<Properties>
<Property name="text" type="java.lang.String" value="Safe"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnSafeActionPerformed"/>
</Events>
</MenuItem>
</SubComponents>
</Menu>
<Menu class="javax.swing.JMenu" name="jMenu2">
<Properties>
<Property name="text" type="java.lang.String" value="Edit"/>
</Properties>
</Menu>
</SubComponents>
</Menu>
</NonVisualComponents>
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="menuBar" type="java.lang.String" value="jMenuBar1"/>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</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="javax.swing.JPanel" name="jPanel1">
<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>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="grafikView1" alignment="0" pref="400" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="grafikView1" alignment="1" pref="279" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="mvcgrafik.view.GrafikView" name="grafikView1">

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout">
<Property name="useNullLayout" type="boolean" value="true"/>
</Layout>
</Container>
</SubComponents>
</Container>
</SubComponents>
</Form>

+ 192
- 0
src/mvcgrafik/view/GrafikMenuView.java View File

@@ -0,0 +1,192 @@
/*
* 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;

/**
*
* @author hd
*/
public class GrafikMenuView extends javax.swing.JFrame
{

/**
* Creates new form GrafikMenuView
*/
public GrafikMenuView()
{
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()
{

jFileChooser1 = new javax.swing.JFileChooser();
jPanel1 = new javax.swing.JPanel();
grafikView1 = new mvcgrafik.view.GrafikView();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
btnOpen = new javax.swing.JMenuItem();
btnSafe = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(grafikView1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(grafikView1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);

getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

jMenu1.setText("File");

btnOpen.setText("Open");
btnOpen.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
btnOpenActionPerformed(evt);
}
});
jMenu1.add(btnOpen);

btnSafe.setText("Safe");
btnSafe.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
btnSafeActionPerformed(evt);
}
});
jMenu1.add(btnSafe);

jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

pack();
}// </editor-fold>//GEN-END:initComponents

private void btnOpenActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnOpenActionPerformed
{//GEN-HEADEREND:event_btnOpenActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_btnOpenActionPerformed

private void btnSafeActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSafeActionPerformed
{//GEN-HEADEREND:event_btnSafeActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_btnSafeActionPerformed

/**
* @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(GrafikMenuView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(GrafikMenuView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(GrafikMenuView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(GrafikMenuView.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 GrafikMenuView().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem btnOpen;
private javax.swing.JMenuItem btnSafe;
private mvcgrafik.view.GrafikView grafikView1;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
// End of variables declaration//GEN-END:variables

/**
* @return the grafikView1
*/
public mvcgrafik.view.GrafikView getGrafikView1()
{
return grafikView1;
}

/**
* @return the jFileChooser1
*/
public javax.swing.JFileChooser getjFileChooser1()
{
return jFileChooser1;
}

/**
* @return the btnOpen
*/
public javax.swing.JMenuItem getBtnOpen()
{
return btnOpen;
}

/**
* @return the btnSafe
*/
public javax.swing.JMenuItem getBtnSafe()
{
return btnSafe;
}
}

+ 6
- 3
src/mvcgrafik/view/GrafikView.java View File

@@ -22,10 +22,11 @@ import javax.print.attribute.standard.DialogTypeSelection;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import mvcgrafik.model.GrafikModel;
import mvcgrafik.logger.OhmLogger;

/**
*
* @author le
* @author hd, chris
*/
public class GrafikView extends JComponent implements Printable
{
@@ -58,6 +59,7 @@ public class GrafikView extends JComponent implements Printable
@Override
public void paintComponent(Graphics g)
{
lg.info("repaint");
if (model == null)
{
lg.severe("keine Referenz auf Model vorhanden");
@@ -67,8 +69,9 @@ public class GrafikView extends JComponent implements Printable
Graphics2D g2 = (Graphics2D) g;

model.getFigures().forEach(figure -> {
old_punkt = null;
figure.getPunkte().forEach(punkt -> {
pixel.setFrame(punkt, EINS);
// pixel.setFrame(punkt, EINS);
// g2.draw(pixel);
if(old_punkt != null)
{
@@ -82,7 +85,7 @@ public class GrafikView extends JComponent implements Printable
public void doPrint()
{
HashPrintRequestAttributeSet printSet =
new HashPrintRequestAttributeSet();
new HashPrintRequestAttributeSet();
printSet.add(DialogTypeSelection.NATIVE);
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(this);

Loading…
Cancel
Save