finished d-f

This commit is contained in:
lukas 2020-05-18 10:46:19 +02:00
parent acc9602214
commit 84658670ad
4 changed files with 193 additions and 120 deletions

View File

@ -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() {

View File

@ -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<Foto> fotocollection = new ArrayList<>();
ArrayList<Foto> 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);
}
}

View File

@ -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<String> 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<String> 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<EFBFBD>gen der <EFBFBD>bergebenen Namen in die JList
* @param namesPhoto
*/
public void setPhotoList(String[] namesPhoto) {
// nichts ver<EFBFBD>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<EFBFBD>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<EFBFBD>he des Bildes (int)
*/
public void setData(String photoType, int width, int height) {
// nichts ver<EFBFBD>ndern
lblStatus.setText( String.format("Typ: %s, Bildgr<67><72>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<EFBFBD>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<EFBFBD>gen
//this.graphPanel.add(???);
// zum Neuzeichnen der Anwendung nach dem Einf<EFBFBD>gen - nicht <EFBFBD>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<EFBFBD>gen der <EFBFBD>bergebenen Namen in die JList
*
* @param namesPhoto
*/
public void setPhotoList(String[] namesPhoto) {
// nichts ver<EFBFBD>ndern
if (namesPhoto != null) {
photoList.removeAll();
photoList.setListData(namesPhoto);
}
}
/**
* Frame anzeigen
*/
public void display() {
// nichts ver<EFBFBD>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<EFBFBD>he des Bildes (int)
*/
public void setData(String photoType, int width, int height) {
// nichts ver<EFBFBD>ndern
lblStatus.setText(String.format("Typ: %s, Bildgr<67><72>e: (%d,%d)", photoType, width, height));
}
/**
* Erstellen eines Kuchendiagrammes mit 3 Segmenten. Die 3 Parameter
* geben die H<EFBFBD>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<EFBFBD>gen - nicht <EFBFBD>ndern!
this.getContentPane().validate();
this.getContentPane().repaint();
}
}

26
src/Medien.java Normal file
View File

@ -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;
}
}