From 84658670ad12f4b69e235f4ba216d2110c95a87d Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 18 May 2020 10:46:19 +0200 Subject: [PATCH] finished d-f --- src/Foto.java | 24 +---- src/MediaController.java | 56 ++++++++--- src/MediaView.java | 207 ++++++++++++++++++++++----------------- src/Medien.java | 26 +++++ 4 files changed, 193 insertions(+), 120 deletions(-) create mode 100644 src/Medien.java diff --git a/src/Foto.java b/src/Foto.java index 3d4db03..f670bb0 100644 --- a/src/Foto.java +++ b/src/Foto.java @@ -1,32 +1,16 @@ -public class Foto { - private String name; - private int ref1; - private int ref2; +public class Foto extends Medien { private int height; private int width; private String fileformat; - public Foto(String name, int ref1, int ref2, int height, int width, String fileformat) { - this.name = name; - this.ref1 = ref1; - this.ref2 = ref2; + public Foto(String name, int rating, int nrused, String fileformat, int height, int width) { + super(name, rating, nrused); this.height = height; this.width = width; this.fileformat = fileformat; } - public Foto() { } - - public String getName() { - return name; - } - - public int getRef1() { - return ref1; - } - - public int getRef2() { - return ref2; + public Foto() { } public int getHeight() { diff --git a/src/MediaController.java b/src/MediaController.java index 76bb540..addf19a 100644 --- a/src/MediaController.java +++ b/src/MediaController.java @@ -2,16 +2,16 @@ import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.filechooser.FileNameExtensionFilter; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.ArrayList; -import java.util.Collection; public class MediaController implements ActionListener, ListSelectionListener { MediaView mv = new MediaView(this, this); //TODO: Collection für Fotos einfügen - Collection fotocollection = new ArrayList<>(); + ArrayList fotocollection = new ArrayList<>(); /** @@ -31,10 +31,8 @@ public class MediaController implements ActionListener, ListSelectionListener { String[] getNamesPhoto() { String[] strar = new String[fotocollection.size()]; - int i = 0; - for (Foto ft: fotocollection) { - strar[i] = ft.getName(); - i++; + for (int i = 0; i < fotocollection.size(); i++) { + strar[i] = fotocollection.get(i).getName(); } return strar; @@ -68,12 +66,15 @@ public class MediaController implements ActionListener, ListSelectionListener { Foto ft = new Foto(spl[0], Integer.parseInt(spl[1]), Integer.parseInt(spl[2]), + spl[3], Integer.parseInt(spl[4]), - Integer.parseInt(spl[5]), - spl[3]); + Integer.parseInt(spl[5]) + ); fotocollection.add(ft); + mv.setPhotoList(getNamesPhoto()); } catch (NumberFormatException ee) { + showPopup(ee.getMessage()); throw new WrongFileFormatException("parse error- no integer in file column"); } } @@ -83,7 +84,8 @@ public class MediaController implements ActionListener, ListSelectionListener { } catch (IOException ioException) { ioException.printStackTrace(); } catch (WrongFileFormatException en) { - System.err.println("an File Format exception occured: "+en.getMessage()); + System.err.println("an File Format exception occured: " + en.getMessage()); + showPopup(en.getMessage()); } } @@ -91,8 +93,24 @@ public class MediaController implements ActionListener, ListSelectionListener { } else if (e.getActionCommand() == "Grafik") { // TODO: die Häufigkeiten der Fotoformate muss berechnet werden // und an die Methode displayGraph übergeben werden + int raw = 0; + int jpeg = 0; + int gif = 0; - // mv.displayGraph(???); + for (Foto ft : fotocollection) { + switch (ft.getFileformat()) { + case "JPEG": + jpeg++; + break; + case "RAW": + raw++; + break; + case "GIF": + gif++; + break; + } + } + mv.displayGraph(raw, jpeg, gif); } } @@ -102,9 +120,23 @@ public class MediaController implements ActionListener, ListSelectionListener { * ListSelectionListener - wenn sich die */ public void valueChanged(ListSelectionEvent e) { - // TODO: die Daten zum ausgewähltem Foto müssen mit setData angezeigt werden + Foto ft = fotocollection.get(mv.photoList.getSelectedIndex()); - // mv.setData(???); + mv.setData(ft.getFileformat(), ft.getWidth(), ft.getHeight()); } + public void showPopup(String message) { + JDialog dialog = new JDialog(mv, "Title"); + dialog.setSize(200, 200); + dialog.setLayout(new BorderLayout()); + JButton btn = new JButton("ok"); + btn.addActionListener(e1 -> { + dialog.setVisible(false); + dialog.dispose(); + }); + + dialog.add(btn, BorderLayout.SOUTH); + dialog.add(new JLabel(message), BorderLayout.CENTER); + dialog.setVisible(true); + } } diff --git a/src/MediaView.java b/src/MediaView.java index 11bc762..b54dbea 100644 --- a/src/MediaView.java +++ b/src/MediaView.java @@ -1,97 +1,128 @@ -import java.awt.BorderLayout; -import java.awt.GridLayout; -import java.awt.ScrollPane; +import org.jfree.chart.ChartFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PiePlot; +import org.jfree.data.general.DefaultPieDataset; + +import javax.swing.*; +import javax.swing.event.ListSelectionListener; +import java.awt.*; import java.awt.event.ActionListener; -import javax.swing.DefaultListModel; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.JPanel; -import javax.swing.event.ListSelectionListener; - public class MediaView extends JFrame { - JList photoList = new JList<>(); - JButton btnLoad = new JButton("Laden"); - JButton btnGraph = new JButton("Grafik"); - - JPanel ctrlPanel = new JPanel(); - JPanel photoPanel = new JPanel(new BorderLayout()); - JPanel graphPanel = new JPanel(); - - JLabel lblStatus = new JLabel("Data: "); - - /** - * Konstruktor - * @param al - * @param lsl - */ - public MediaView(ActionListener al, ListSelectionListener lsl) { - this.setTitle("ihren Namen"); + JList photoList = new JList<>(); + JButton btnLoad = new JButton("Laden"); + JButton btnGraph = new JButton("Grafik"); - photoPanel.add(new JLabel("Fotos:"),BorderLayout.NORTH); - photoPanel.add(new ScrollPane().add(photoList)); - photoList.addListSelectionListener(lsl); - - ctrlPanel.add(btnLoad); - ctrlPanel.add(btnGraph); - btnLoad.addActionListener(al); - btnGraph.addActionListener(al); - - this.getContentPane().add(photoPanel, BorderLayout.WEST); - this.getContentPane().add(graphPanel); - this.getContentPane().add(ctrlPanel, BorderLayout.NORTH); - this.getContentPane().add(lblStatus, BorderLayout.SOUTH); - } + JPanel ctrlPanel = new JPanel(); + JPanel photoPanel = new JPanel(new BorderLayout()); + JPanel graphPanel = new JPanel(); - /** - * Einf�gen der �bergebenen Namen in die JList - * @param namesPhoto - */ - public void setPhotoList(String[] namesPhoto) { - // nichts ver�ndern - if (namesPhoto != null){ - DefaultListModel listModel = (DefaultListModel) photoList.getModel(); - listModel.removeAllElements(); - photoList.setListData(namesPhoto); - } - } + JLabel lblStatus = new JLabel("Data: "); - /** - * Frame anzeigen - */ - public void display() { - // nichts ver�ndern - this.setSize(600,530); - this.setVisible(true); - this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - } + /** + * Konstruktor + * + * @param al + * @param lsl + */ + public MediaView(ActionListener al, ListSelectionListener lsl) { + this.setTitle("ihren Namen"); - /** - * Daten eines Fotos in der Statuszeile der View anzeigen - * @param photoType ... Datentyp (als String) - * @param width ... Breite des Bildes (int) - * @param height ... H�he des Bildes (int) - */ - public void setData(String photoType, int width, int height) { - // nichts ver�ndern - lblStatus.setText( String.format("Typ: %s, Bildgr��e: (%d,%d)", photoType, width, height)); - } + photoPanel.add(new JLabel("Fotos:"), BorderLayout.NORTH); + photoPanel.add(new ScrollPane().add(photoList)); + photoList.addListSelectionListener(lsl); - /** - * Erstellen eines Kuchendiagrammes mit 3 Segmenten. Die 3 Parameter - * geben die H�ufigkeiten der Bildtypen an - * @param raw - * @param jpeg - * @param gif - */ - public void displayGraph(float raw, float jpeg, float gif) { - // TODO: Kuchendiagramm erzeugen und in graphPanel einf�gen - //this.graphPanel.add(???); - - // zum Neuzeichnen der Anwendung nach dem Einf�gen - nicht �ndern! - this.getContentPane().validate(); - this.getContentPane().repaint(); - } + ctrlPanel.add(btnLoad); + ctrlPanel.add(btnGraph); + btnLoad.addActionListener(al); + btnGraph.addActionListener(al); + + this.getContentPane().add(photoPanel, BorderLayout.WEST); + this.getContentPane().add(graphPanel); + this.getContentPane().add(ctrlPanel, BorderLayout.NORTH); + this.getContentPane().add(lblStatus, BorderLayout.SOUTH); + } + + /** + * Einf�gen der �bergebenen Namen in die JList + * + * @param namesPhoto + */ + public void setPhotoList(String[] namesPhoto) { + // nichts ver�ndern + if (namesPhoto != null) { + photoList.removeAll(); + photoList.setListData(namesPhoto); + } + } + + /** + * Frame anzeigen + */ + public void display() { + // nichts ver�ndern + this.setSize(600, 530); + this.setVisible(true); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + /** + * Daten eines Fotos in der Statuszeile der View anzeigen + * + * @param photoType ... Datentyp (als String) + * @param width ... Breite des Bildes (int) + * @param height ... H�he des Bildes (int) + */ + public void setData(String photoType, int width, int height) { + // nichts ver�ndern + lblStatus.setText(String.format("Typ: %s, Bildgr��e: (%d,%d)", photoType, width, height)); + } + + /** + * Erstellen eines Kuchendiagrammes mit 3 Segmenten. Die 3 Parameter + * geben die H�ufigkeiten der Bildtypen an + * + * @param raw + * @param jpeg + * @param gif + */ + public void displayGraph(float raw, float jpeg, float gif) { + // create a dataset... + DefaultPieDataset dataset = new DefaultPieDataset(); + dataset.setValue("raw", raw); + dataset.setValue("jpeg", jpeg); + dataset.setValue("gif", gif); + + + // create a chart... + JFreeChart chart = ChartFactory.createPieChart( + "Das supergeile pie chart", + dataset, + true, + true, + false + ); + + PiePlot plot = (PiePlot) chart.getPlot(); + if (raw >= jpeg && raw >= gif) { + plot.setExplodePercent("raw", 0.10); + } + if (jpeg >= raw && jpeg >= gif) { + plot.setExplodePercent("jpeg", 0.10); + } + if (gif >= raw && gif >= jpeg){ + plot.setExplodePercent("gif", 0.10); + } + + + // create and display a frame... + ChartPanel frame = new ChartPanel(chart); + this.graphPanel.add(frame); + + + // zum Neuzeichnen der Anwendung nach dem Einf�gen - nicht �ndern! + this.getContentPane().validate(); + this.getContentPane().repaint(); + } } diff --git a/src/Medien.java b/src/Medien.java new file mode 100644 index 0000000..f4187f8 --- /dev/null +++ b/src/Medien.java @@ -0,0 +1,26 @@ +public class Medien { + private String name; + private int rating; + private int nrused; + + public Medien(String name, int rating, int nrused) { + this.name = name; + this.rating = rating; + this.nrused = nrused; + } + + public Medien() { + } + + public String getName() { + return name; + } + + public int getRating() { + return rating; + } + + public int getNrused() { + return nrused; + } +}