From d31b791bd96909e5bbd73c3da763dacd2b43eac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20D=C3=BCrr?= Date: Tue, 22 Dec 2020 15:18:10 +0100 Subject: [PATCH] letzter Stand --- src/graphicChat/Start.java | 70 ++++++ .../controller/CommandConnect.java | 73 +++++++ src/graphicChat/controller/CommandSend.java | 55 +++++ .../controller/GraphicsController.java} | 24 +- .../controller/ReceiveAdapter.java | 67 ++++++ .../logger/MyFormatter.java | 2 +- .../logger/OhmLogger.java | 4 +- src/graphicChat/model/ChatModel.java | 206 ++++++++++++++++++ .../model/Figure.java | 2 +- src/graphicChat/model/GraphicData.java | 48 ++++ .../view/ChatView.form} | 95 ++++---- .../view/ChatView.java} | 164 ++++++++------ .../view/Surface.java} | 62 +----- src/mvcgrafik/Start.java | 51 ----- src/mvcgrafik/controller/BtnController.java | 109 --------- src/mvcgrafik/model/GrafikModel.java | 74 ------- 16 files changed, 692 insertions(+), 414 deletions(-) create mode 100755 src/graphicChat/Start.java create mode 100644 src/graphicChat/controller/CommandConnect.java create mode 100644 src/graphicChat/controller/CommandSend.java rename src/{mvcgrafik/controller/GrafikController.java => graphicChat/controller/GraphicsController.java} (74%) create mode 100644 src/graphicChat/controller/ReceiveAdapter.java rename src/{mvcgrafik => graphicChat}/logger/MyFormatter.java (96%) rename src/{mvcgrafik => graphicChat}/logger/OhmLogger.java (93%) create mode 100644 src/graphicChat/model/ChatModel.java rename src/{mvcgrafik => graphicChat}/model/Figure.java (95%) create mode 100644 src/graphicChat/model/GraphicData.java rename src/{mvcgrafik/view/GrafikMenuView.form => graphicChat/view/ChatView.form} (54%) rename src/{mvcgrafik/view/GrafikMenuView.java => graphicChat/view/ChatView.java} (50%) rename src/{mvcgrafik/view/GrafikView.java => graphicChat/view/Surface.java} (52%) delete mode 100755 src/mvcgrafik/Start.java delete mode 100644 src/mvcgrafik/controller/BtnController.java delete mode 100755 src/mvcgrafik/model/GrafikModel.java diff --git a/src/graphicChat/Start.java b/src/graphicChat/Start.java new file mode 100755 index 0000000..e008cfc --- /dev/null +++ b/src/graphicChat/Start.java @@ -0,0 +1,70 @@ +/* + * 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 graphicChat; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; +import graphicChat.controller.CommandConnect; +import graphicChat.controller.CommandSend; +import graphicChat.controller.GraphicsController; +import graphicChat.controller.ReceiveAdapter; +import graphicChat.view.ChatView; +import graphicChat.model.ChatModel; +import javax.swing.JOptionPane; +import javax.swing.UIManager; +/** + * Builder Class + * @author le + */ +public class Start +{ + public Start() + { + JFrame frm = new JFrame(); + frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + + ChatView view = new ChatView(); + ChatModel model = new ChatModel(); + view.getGrafikView1().setModel(model); + + GraphicsController controller = new GraphicsController(view.getGrafikView1(), model); + controller.registerEvents(); + + CommandConnect cmdConnect = new CommandConnect(view, model); + cmdConnect.registerEvents(); + + CommandSend cmdSend = new CommandSend(view, model); + cmdSend.registerEvents(); + + ReceiveAdapter recAdapter = new ReceiveAdapter(view, model); + recAdapter.subscribe(); + + view.setVisible(true); + view.setTitle("Chat"); + + view.setSize(800, 600); + view.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(); + new Start(); + } +} diff --git a/src/graphicChat/controller/CommandConnect.java b/src/graphicChat/controller/CommandConnect.java new file mode 100644 index 0000000..ce32825 --- /dev/null +++ b/src/graphicChat/controller/CommandConnect.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 graphicChat.controller; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import java.util.logging.Logger; +import graphicChat.logger.OhmLogger; +import graphicChat.model.ChatModel; +import graphicChat.view.ChatView; + +/** + * + * @author hd, chris + */ +public class CommandConnect implements ActionListener +{ + + private static Logger lg = OhmLogger.getLogger(); + ChatView view; + ChatModel model; + + private static final int PORT = 35000; + private static final String IP_ADRESSE = "127.0.0.1"; + + + public CommandConnect(ChatView view, ChatModel model) + { + this.view = view; + this.model = model; + } + + public void registerEvents(){ + view.getBtnServer().addActionListener(this); + view.getBtnClient().addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + + Object src = e.getSource(); + + // connect to server + if(src == view.getBtnServer()){ + synchronized (this){ + try { + view.getLblType().setText("Server"); + model.setServer(PORT); + } catch (IOException ex) { + lg.severe(ex.toString()); + } + } + } + + // connect to client + if(src == view.getBtnClient()){ + synchronized (this){ + try { + view.getLblType().setText("Client"); + model.setClient(PORT, IP_ADRESSE); + + } catch (IOException ex) { + lg.severe(ex.toString()); + } + } + } + } +} diff --git a/src/graphicChat/controller/CommandSend.java b/src/graphicChat/controller/CommandSend.java new file mode 100644 index 0000000..783ab7b --- /dev/null +++ b/src/graphicChat/controller/CommandSend.java @@ -0,0 +1,55 @@ +/* + * 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 graphicChat.controller; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.logging.Logger; +import graphicChat.logger.OhmLogger; +import graphicChat.model.ChatModel; +import graphicChat.model.Figure; +import graphicChat.view.ChatView; +import java.util.List; + + + +/** + * + * @author hd, christ + */ +public class CommandSend implements ActionListener +{ + private static Logger lg = OhmLogger.getLogger(); + ChatView view; + ChatModel model; + private List
msg; + + public CommandSend(ChatView view, ChatModel model) + { + this.view = view; + this.model = model; + } + + + public void registerEvents(){ + view.getBtnSend().addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) + { + Object src = e.getSource(); + + if(src == view.getBtnSend()){ + /* view get figures*/ + msg = model.getFigures(); + + model.sendMessage(msg); + view.getLblStatus().setText("Nachricht gesendet"); + } + } +} diff --git a/src/mvcgrafik/controller/GrafikController.java b/src/graphicChat/controller/GraphicsController.java similarity index 74% rename from src/mvcgrafik/controller/GrafikController.java rename to src/graphicChat/controller/GraphicsController.java index a7ec53f..de4cb32 100755 --- a/src/mvcgrafik/controller/GrafikController.java +++ b/src/graphicChat/controller/GraphicsController.java @@ -4,32 +4,32 @@ * and open the template in the editor. */ -package mvcgrafik.controller; +package graphicChat.controller; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.logging.Logger; -import mvcgrafik.model.Figure; -import mvcgrafik.model.GrafikModel; -import mvcgrafik.logger.OhmLogger; -import mvcgrafik.view.GrafikView; +import graphicChat.model.Figure; +import graphicChat.model.ChatModel; +import graphicChat.logger.OhmLogger; +import graphicChat.view.Surface; /** * * @author le */ -public class GrafikController implements MouseMotionListener, MouseListener +public class GraphicsController implements MouseMotionListener, MouseListener { - private GrafikView view; - private GrafikModel model; + private Surface view; + private ChatModel model; private Figure figure; private Point p_old; private static Logger lg = OhmLogger.getLogger(); - public GrafikController(GrafikView view, GrafikModel model) + public GraphicsController(Surface view, ChatModel model) { this.view = view; this.model = model; @@ -51,7 +51,6 @@ public class GrafikController implements MouseMotionListener, MouseListener } Point p = evt.getPoint(); if(p_old != null){ -// view.drawLine(p, p_old); view.drawLine(p, p_old); } p_old = p; @@ -83,11 +82,6 @@ public class GrafikController implements MouseMotionListener, MouseListener p_old = null; figure = null; lg.info("Figure finished"); - if (evt.getButton() == MouseEvent.BUTTON3) - { -// view.doPrint(); - view.doPrint(); - } } @Override diff --git a/src/graphicChat/controller/ReceiveAdapter.java b/src/graphicChat/controller/ReceiveAdapter.java new file mode 100644 index 0000000..e4ec9bc --- /dev/null +++ b/src/graphicChat/controller/ReceiveAdapter.java @@ -0,0 +1,67 @@ +/* + * 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 graphicChat.controller; + +import java.util.concurrent.Flow; +import java.util.logging.Logger; +import graphicChat.logger.OhmLogger; +import graphicChat.model.ChatModel; +import graphicChat.model.Figure; +import graphicChat.view.ChatView; +import java.util.List; + +/** + * + * @author hd, chris + */ + +public class ReceiveAdapter implements Flow.Subscriber> +{ + private static Logger lg = OhmLogger.getLogger(); + + private ChatView view; + private ChatModel model; + private Flow.Subscription subscription; + + public ReceiveAdapter(ChatView view, ChatModel model) + { + this.view = view; + this.model = model; + } + + public void subscribe() + { + model.addSubscription(this); + } + + @Override + public void onSubscribe(Flow.Subscription subscription) + { + this.subscription = subscription; + subscription.request(1); + } + + @Override + public void onNext(List
msg) + { + view.getLblStatus().setText("Nachricht empfangen"); + model.addList(msg); + subscription.request(1); + } + + @Override + public void onError(Throwable throwable) + { + } + + @Override + public void onComplete() + { + } + + +} diff --git a/src/mvcgrafik/logger/MyFormatter.java b/src/graphicChat/logger/MyFormatter.java similarity index 96% rename from src/mvcgrafik/logger/MyFormatter.java rename to src/graphicChat/logger/MyFormatter.java index b6f49f7..4335f7c 100644 --- a/src/mvcgrafik/logger/MyFormatter.java +++ b/src/graphicChat/logger/MyFormatter.java @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package mvcgrafik.logger; +package graphicChat.logger; import java.util.Date; import java.util.logging.Formatter; diff --git a/src/mvcgrafik/logger/OhmLogger.java b/src/graphicChat/logger/OhmLogger.java similarity index 93% rename from src/mvcgrafik/logger/OhmLogger.java rename to src/graphicChat/logger/OhmLogger.java index 6b1fcd3..09b37f8 100644 --- a/src/mvcgrafik/logger/OhmLogger.java +++ b/src/graphicChat/logger/OhmLogger.java @@ -3,10 +3,10 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package mvcgrafik.logger; +package graphicChat.logger; -import mvcgrafik.logger.MyFormatter; +import graphicChat.logger.MyFormatter; import java.io.File; import java.io.IOException; import java.util.logging.*; diff --git a/src/graphicChat/model/ChatModel.java b/src/graphicChat/model/ChatModel.java new file mode 100644 index 0000000..4b76804 --- /dev/null +++ b/src/graphicChat/model/ChatModel.java @@ -0,0 +1,206 @@ +/* + * 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 graphicChat.model; + + +import graphicChat.logger.OhmLogger; + +import java.io.BufferedReader; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Flow; +import java.util.concurrent.SubmissionPublisher; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.List; + +/** + * + * @author hd, chris + */ +public class ChatModel implements Runnable +{ + private static final Logger lg = OhmLogger.getLogger(); + + private final ExecutorService eService; + private final SubmissionPublisher> iPublisher; + private volatile boolean laufend; + private final GraphicData gdata; + + List
msg; + + BufferedReader in; + PrintWriter out; + ObjectInputStream objectisr; + + private Socket socket; + + + public ChatModel() + { + laufend = false; + iPublisher = new SubmissionPublisher<>(); + eService = Executors.newSingleThreadExecutor(); + gdata = new GraphicData(); + } + + public void addSubscription(Flow.Subscriber> subscriber) + { + iPublisher.subscribe(subscriber); + } + + public synchronized void start(){ + laufend = true; + eService.submit(this); + this.notifyAll();//muss sync + lg.info("startet"); + } + + @Override + public void run() + { + while(true){ + try { + try + { + msg = (List
)objectisr.readObject(); + } + catch (ClassNotFoundException ex) + { + lg.log(Level.SEVERE, null, ex); + } + lg.info("Nachricht empfangen: "); + addList(msg); + iPublisher.submit(msg);//wenn neue Nachricht + } catch (IOException ex) { + lg.log(Level.SEVERE, ex.toString()); + } + } + } + + public void setSocket(Socket s) throws IOException { + lg.info("start communication\n"); + InputStream iStream = s.getInputStream(); + OutputStream oStream = s.getOutputStream(); + + ObjectInputStream oisr = new ObjectInputStream(iStream); + InputStreamReader isr = new InputStreamReader(iStream, "UTF-8"); + OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8"); + + +// BufferedWriter out = new BufferedWriter(osr); + objectisr = oisr; + out = new PrintWriter(osr); + in = new BufferedReader(isr); + start(); + } + + public void setClient(int PORT, String IP_ADRESSE) throws IOException + { + Thread connectThread; + connectThread = new Thread(new Runnable() { + @Override + public void run() { + try { + lg.info("Client: verbinde ..."); + Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert! + lg.info("Client: Verbindung hergestellt"); + setSocket(s); + } catch (IOException e) { + lg.info("io exception in setClient"); + } + } + }); + connectThread.start(); + } + + public void setServer(int PORT) throws IOException + { + Thread connectThread; + connectThread = new Thread(new Runnable() { + @Override + public void run() { + try { + ServerSocket sSocket = new ServerSocket(PORT); + lg.info("Server: Warte auf Verbindung ..."); + Socket s = sSocket.accept(); // Achtung: blockiert! + lg.info("Server: Verbindung akzeptiert"); + setSocket(s); + } catch (IOException e) { + lg.info("io exception in setServer"); + } + } + }); + connectThread.start(); + } + + public void sendMessage(List
msg) + { + if(laufend){ + lg.log(Level.INFO, "Sende Nachricht: "); + out.println(msg); + out.flush(); + } + else{ + lg.log(Level.INFO, "Keine Verbindung!"); + } + } + + public Figure addFigure() + { + return gdata.addFigure(); + } + + public List
getFigures() + { + return gdata.getFigures(); + } + public void addList(List
list) + { + gdata.addList(list); + } + +// public void speichereDatei(String dateiname) throws FileNotFoundException, IOException +// { +// FileOutputStream fos = new FileOutputStream(dateiname); +// BufferedOutputStream buffout = new BufferedOutputStream(fos); +// ObjectOutputStream oos = new ObjectOutputStream(buffout); +// oos.writeObject(figures); +// oos.flush(); +// oos.close(); +// +// } +// +// public void ladeDatei(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 .... +//// } +// figures = (ArrayList
) ois.readObject(); +// ois.close(); +// } + +} \ No newline at end of file diff --git a/src/mvcgrafik/model/Figure.java b/src/graphicChat/model/Figure.java similarity index 95% rename from src/mvcgrafik/model/Figure.java rename to src/graphicChat/model/Figure.java index 677bc35..2d619dd 100644 --- a/src/mvcgrafik/model/Figure.java +++ b/src/graphicChat/model/Figure.java @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package mvcgrafik.model; +package graphicChat.model; import java.awt.Point; import java.util.ArrayList; diff --git a/src/graphicChat/model/GraphicData.java b/src/graphicChat/model/GraphicData.java new file mode 100644 index 0000000..12ecf5d --- /dev/null +++ b/src/graphicChat/model/GraphicData.java @@ -0,0 +1,48 @@ +/* + * 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 graphicChat.model; + +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 hd, chris + */ +public class GraphicData +{ + private ArrayList
figures; + public GraphicData() + { + figures = new ArrayList<>(); + + } + + public void addList(List
list) + { + figures.addAll(list); + } + public Figure addFigure() + { + figures.add(new Figure()); + return figures.get(figures.size() - 1); + } + + public List
getFigures() + { + return Collections.unmodifiableList(figures); + } +} \ No newline at end of file diff --git a/src/mvcgrafik/view/GrafikMenuView.form b/src/graphicChat/view/ChatView.form similarity index 54% rename from src/mvcgrafik/view/GrafikMenuView.form rename to src/graphicChat/view/ChatView.form index 0999e37..7d5803b 100644 --- a/src/mvcgrafik/view/GrafikMenuView.form +++ b/src/graphicChat/view/ChatView.form @@ -1,48 +1,11 @@
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -56,7 +19,7 @@ - + @@ -71,7 +34,7 @@ - + @@ -81,7 +44,7 @@ - + @@ -89,5 +52,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mvcgrafik/view/GrafikMenuView.java b/src/graphicChat/view/ChatView.java similarity index 50% rename from src/mvcgrafik/view/GrafikMenuView.java rename to src/graphicChat/view/ChatView.java index 783b562..c0832c3 100644 --- a/src/mvcgrafik/view/GrafikMenuView.java +++ b/src/graphicChat/view/ChatView.java @@ -3,15 +3,55 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package mvcgrafik.view; +package graphicChat.view; /** * * @author hd */ -public class GrafikMenuView extends javax.swing.JFrame +public class ChatView extends javax.swing.JFrame { + /** + * @return the btnClient + */ + public javax.swing.JButton getBtnClient() + { + return btnClient; + } + + /** + * @return the btnSend + */ + public javax.swing.JButton getBtnSend() + { + return btnSend; + } + + /** + * @return the btnServer + */ + public javax.swing.JButton getBtnServer() + { + return btnServer; + } + + /** + * @return the lblStatus + */ + public javax.swing.JLabel getLblStatus() + { + return lblStatus; + } + + /** + * @return the lblType + */ + public javax.swing.JLabel getLblType() + { + return lblType; + } + /** * @return the jPanel1 */ @@ -23,7 +63,7 @@ public class GrafikMenuView extends javax.swing.JFrame /** * Creates new form GrafikMenuView */ - public GrafikMenuView() + public ChatView() { initComponents(); } @@ -40,14 +80,14 @@ public class GrafikMenuView extends javax.swing.JFrame 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(); + grafikView1 = new graphicChat.view.Surface(); + jPanel2 = new javax.swing.JPanel(); + lblType = new javax.swing.JLabel(); + btnClient = new javax.swing.JButton(); + btnServer = new javax.swing.JButton(); + btnSend = new javax.swing.JButton(); + lblStatus = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Zeichentool"); @@ -56,7 +96,7 @@ public class GrafikMenuView extends javax.swing.JFrame jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(grafikView1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) + .addComponent(grafikView1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) @@ -65,47 +105,59 @@ public class GrafikMenuView extends javax.swing.JFrame getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); - jMenu1.setText("File"); + jPanel2.setLayout(new java.awt.GridLayout(1, 0)); + jPanel2.add(lblType); - btnOpen.setText("Open"); - btnOpen.addActionListener(new java.awt.event.ActionListener() + btnClient.setText("Client"); + btnClient.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - btnOpenActionPerformed(evt); + btnClientActionPerformed(evt); } }); - jMenu1.add(btnOpen); + jPanel2.add(btnClient); - btnSafe.setText("Safe"); - btnSafe.addActionListener(new java.awt.event.ActionListener() + btnServer.setText("Server"); + btnServer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - btnSafeActionPerformed(evt); + btnServerActionPerformed(evt); } }); - jMenu1.add(btnSafe); + jPanel2.add(btnServer); - jMenuBar1.add(jMenu1); + getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START); - jMenu2.setText("Edit"); - jMenuBar1.add(jMenu2); - - setJMenuBar(jMenuBar1); + btnSend.setText("Send"); + btnSend.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { + btnSendActionPerformed(evt); + } + }); + getContentPane().add(btnSend, java.awt.BorderLayout.LINE_END); + getContentPane().add(lblStatus, java.awt.BorderLayout.PAGE_END); pack(); }// //GEN-END:initComponents - private void btnOpenActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnOpenActionPerformed - {//GEN-HEADEREND:event_btnOpenActionPerformed + private void btnClientActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnClientActionPerformed + {//GEN-HEADEREND:event_btnClientActionPerformed // TODO add your handling code here: - }//GEN-LAST:event_btnOpenActionPerformed + }//GEN-LAST:event_btnClientActionPerformed - private void btnSafeActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSafeActionPerformed - {//GEN-HEADEREND:event_btnSafeActionPerformed + private void btnServerActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnServerActionPerformed + {//GEN-HEADEREND:event_btnServerActionPerformed // TODO add your handling code here: - }//GEN-LAST:event_btnSafeActionPerformed + }//GEN-LAST:event_btnServerActionPerformed + + private void btnSendActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSendActionPerformed + {//GEN-HEADEREND:event_btnSendActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_btnSendActionPerformed /** * @param args the command line arguments @@ -130,72 +182,50 @@ public class GrafikMenuView extends javax.swing.JFrame } catch (ClassNotFoundException ex) { - java.util.logging.Logger.getLogger(GrafikMenuView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + java.util.logging.Logger.getLogger(ChatView.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); + java.util.logging.Logger.getLogger(ChatView.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); + java.util.logging.Logger.getLogger(ChatView.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); + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // + // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { - new GrafikMenuView().setVisible(true); + new ChatView().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.JButton btnClient; + private javax.swing.JButton btnSend; + private javax.swing.JButton btnServer; + private graphicChat.view.Surface grafikView1; private javax.swing.JPanel jPanel1; + private javax.swing.JPanel jPanel2; + private javax.swing.JLabel lblStatus; + private javax.swing.JLabel lblType; // End of variables declaration//GEN-END:variables /** * @return the grafikView1 */ - public mvcgrafik.view.GrafikView getGrafikView1() + public graphicChat.view.Surface 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; - } } diff --git a/src/mvcgrafik/view/GrafikView.java b/src/graphicChat/view/Surface.java similarity index 52% rename from src/mvcgrafik/view/GrafikView.java rename to src/graphicChat/view/Surface.java index bd68911..8802021 100755 --- a/src/mvcgrafik/view/GrafikView.java +++ b/src/graphicChat/view/Surface.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package mvcgrafik.view; +package graphicChat.view; import java.awt.Color; import java.awt.Dimension; @@ -12,37 +12,30 @@ import java.awt.Graphics2D; import java.awt.Point; import java.awt.geom.Line2D; 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; -import mvcgrafik.logger.OhmLogger; +import graphicChat.logger.OhmLogger; +import graphicChat.model.ChatModel; /** * * @author hd, chris */ -public class GrafikView extends JComponent implements Printable +public class Surface extends JComponent { private final static Dimension EINS = new Dimension(1, 1); private static Logger lg = OhmLogger.getLogger(); private Rectangle2D.Float pixel; - private GrafikModel model; + private ChatModel model; private Point old_punkt = null; - public GrafikView() + public Surface() { pixel = new Rectangle2D.Float(); this.setBackground(Color.WHITE); } - public void setModel(GrafikModel model) + public void setModel(ChatModel model) { this.model = model; } @@ -52,7 +45,7 @@ public class GrafikView extends JComponent implements Printable Graphics2D g2 = (Graphics2D) this.getGraphics(); Line2D.Double line = new Line2D.Double(p.getX(),p.getY(),p_old.getX(),p_old.getY()); g2.draw(line); - g2.dispose(); // VERY, VERY WICHTIG + g2.dispose(); // VERY, VERY IMPORTANT } public void drawLineG2(Point p, Point p_old, Graphics2D g2) { @@ -84,44 +77,5 @@ public class GrafikView extends JComponent implements Printable }); }); } - - 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()/this.getWidth(), - pf.getImageableHeight() / this.getHeight()); - super.print(g2p); - return Printable.PAGE_EXISTS; - } - else - { - return Printable.NO_SUCH_PAGE; - } - } } diff --git a/src/mvcgrafik/Start.java b/src/mvcgrafik/Start.java deleted file mode 100755 index 777ff6a..0000000 --- a/src/mvcgrafik/Start.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.JPanel; -import javax.swing.WindowConstants; -import mvcgrafik.controller.BtnController; -import mvcgrafik.controller.GrafikController; -import mvcgrafik.model.GrafikModel; -import mvcgrafik.view.GrafikMenuView; -import mvcgrafik.view.GrafikView; -//import mvcgrafik.ohmLogger; - -/** - * Builder Class - * @author le - */ -public class Start -{ - public Start() - { - JFrame frm = new JFrame(); - frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - - GrafikMenuView view = new GrafikMenuView(); - GrafikModel model = new GrafikModel(); - view.getGrafikView1().setModel(model); - - GrafikController controller = new GrafikController(view.getGrafikView1(), model); - BtnController btncontroller = new BtnController(view, model); - controller.registerEvents(); - btncontroller.registerEvents(); - - view.setSize(800, 600); - view.setVisible(true); - - } - - /** - * @param args the command line arguments - */ - public static void main(String[] args) - { - new Start(); - } -} diff --git a/src/mvcgrafik/controller/BtnController.java b/src/mvcgrafik/controller/BtnController.java deleted file mode 100644 index ebfc133..0000000 --- a/src/mvcgrafik/controller/BtnController.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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.ActionEvent; -import java.awt.event.ActionListener; -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.GrafikMenuView; -import mvcgrafik.logger.OhmLogger; - - -/** - * - * @author le - */ -public class BtnController implements ActionListener -{ - private GrafikMenuView view; - //private GrafikMenuView menuview; - private GrafikModel model; - private Figure figure; - private Point p_old; - private static Logger lg = OhmLogger.getLogger(); - - public BtnController(GrafikMenuView view, GrafikModel model) - { - this.view = view; - this.model = model; - } - - public void registerEvents() - { - view.getBtnOpen().addActionListener(this); - view.getBtnSafe().addActionListener(this); - } - - @Override - public void actionPerformed(ActionEvent ae) - { - if(ae.getSource() == view.getBtnSafe()) - { - 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) - { - 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(ae.getSource() == view.getBtnOpen()) - { - 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) - { - 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); - } - } - } - } -} - diff --git a/src/mvcgrafik/model/GrafikModel.java b/src/mvcgrafik/model/GrafikModel.java deleted file mode 100755 index b69d6c3..0000000 --- a/src/mvcgrafik/model/GrafikModel.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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
figures; - - public GrafikModel() - { - figures = new ArrayList<>(); - } - - public Figure addFigure() - { - figures.add(new Figure()); - return figures.get(figures.size() - 1); - } - - public List
getFigures() - { - return Collections.unmodifiableList(figures); - } - - public void speichereDatei(String dateiname) throws FileNotFoundException, IOException - { - FileOutputStream fos = new FileOutputStream(dateiname); - BufferedOutputStream buffout = new BufferedOutputStream(fos); - ObjectOutputStream oos = new ObjectOutputStream(buffout); - oos.writeObject(figures); - oos.flush(); - oos.close(); - - } - - public void ladeDatei(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 .... -// } - figures = (ArrayList
) ois.readObject(); - ois.close(); - } -} \ No newline at end of file