From 4e821a6c8fa40ba44af68b869d5eefadab82700e Mon Sep 17 00:00:00 2001 From: Armin Wolf Date: Mon, 29 May 2017 15:42:25 +0200 Subject: [PATCH] =?UTF-8?q?plot=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Presenter/Presenter.java | 37 +++++++----- src/main/java/View/MainFrame.java | 74 ++++++++++++++++------- src/main/java/View/PlotDialog.java | 84 +++++++++++++++++++++++++- 3 files changed, 156 insertions(+), 39 deletions(-) diff --git a/src/main/java/Presenter/Presenter.java b/src/main/java/Presenter/Presenter.java index e422b2b..047a23f 100644 --- a/src/main/java/Presenter/Presenter.java +++ b/src/main/java/Presenter/Presenter.java @@ -30,6 +30,18 @@ public class Presenter { this.model = model; this.view = view; this.lines = new LinkedList<>(); + + Double[] x = {1d,2d,3d,4d,10d,12d,18d}; + Double[] y = {9d,15d,19d,20d,45d,55d,78d}; +// Float[] x = {18f,24f,30f,34f,38f}; +// Float[] y = {18f,26f,30f,40f,70f}; +// Double[] x = {1d,3d,4d,5d,8d}; +// Double[] y = {4d,2d,1d,0d,0d}; + for (int j=0;j<7;j++){ + Pair p = new Pair(x[j], y[j]); + lines.add(p); + } + extractBounds(); } public void getDataFromModel() { @@ -44,23 +56,14 @@ public class Presenter { } - public void setActionByView() { - Double[] x = {1d,2d,3d,4d,10d,12d,18d}; - Double[] y = {9d,15d,19d,20d,45d,55d,78d}; -// Float[] x = {18f,24f,30f,34f,38f}; -// Float[] y = {18f,26f,30f,40f,70f}; -// Double[] x = {1d,3d,4d,5d,8d}; -// Double[] y = {4d,2d,1d,0d,0d}; - - - for (int j=0;j<7;j++){ - Pair p = new Pair(x[j], y[j]); - lines.add(p); - } - - extractBounds(); + public void startArrangementVisualization() { view.createArrangement(); } + + public void startScatterPlotVisualization(){ + view.createPlot(); + } + private void extractBounds(){ Pair pmax = Collections.max(lines); Pair pmin = Collections.min(lines); @@ -69,6 +72,10 @@ public class Presenter { min = pmin.getX() <= pmin.getY() ? pmin.getX() : pmin.getY(); } + /*************************************************************************************************************************** + * Getter und Setter Methoden + ***************************************************************************************************************************/ + public Arrangement getModel() { return model; diff --git a/src/main/java/View/MainFrame.java b/src/main/java/View/MainFrame.java index 87c3fd8..7879178 100644 --- a/src/main/java/View/MainFrame.java +++ b/src/main/java/View/MainFrame.java @@ -1,10 +1,10 @@ package View; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.LinkedList; /** * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden. @@ -16,11 +16,14 @@ import java.util.LinkedList; public class MainFrame extends View { //TODO refactoring - private JButton button1; - private JButton button2; + private JButton arrangementButton; + private JButton plotButton; private JButton button3; - private JDialog dialog; + private JPanel pane; + private JDialog arrangementDialog; + private JDialog plotDialog; private ArrangementDialog arrangement; + private PlotDialog plot; public MainFrame(){ initGUI(); @@ -30,24 +33,39 @@ public class MainFrame extends View { protected void initGUI(){ this.setTitle("MainFrame"); this.setSize(800,800); - this.setLayout(new FlowLayout()); - dialog = new JDialog(); - dialog.setSize(new Dimension(700,470)); - dialog.setTitle("Arrangement Dialog"); - dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); - button1 = new JButton("Button 1"); - button2 = new JButton("Button 2"); - button3 = new JButton("Button 3"); + this.setLayout(new BorderLayout()); + pane = new JPanel(); + pane.setLayout(new FlowLayout()); + arrangementDialog = new JDialog(); + plotDialog = new JDialog(); + arrangementDialog.setSize(new Dimension(700,470)); + plotDialog.setSize(new Dimension(700,470)); + arrangementDialog.setTitle("Arrangement Dialog"); + plotDialog.setTitle("Scatter Plot Dialog"); + arrangementDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); + plotDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); + arrangementButton = new JButton("Arrangement"); + plotButton = new JButton("Plot"); + button3 = new JButton("Import"); - button1.addActionListener(new ActionListener() { + arrangementButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - getPresenter().setActionByView(); + getPresenter().startArrangementVisualization(); } }); - this.add(button1); - this.add(button2); - this.add(button3); + + plotButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + getPresenter().startScatterPlotVisualization(); + } + }); + + pane.add(arrangementButton); + pane.add(plotButton); + pane.add(button3); + this.add(pane, BorderLayout.WEST); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @@ -61,15 +79,13 @@ public class MainFrame extends View { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - dialog.add(arrangement); - dialog.setVisible(true); + arrangementDialog.add(arrangement); + arrangementDialog.setVisible(true); } }); } else { - dialog.setVisible(true); + arrangementDialog.setVisible(true); } - - } @Override @@ -79,6 +95,18 @@ public class MainFrame extends View { @Override public void createPlot() { - //TODO + if (plot == null){ + plot = new PlotDialog(); + plot.createPlot(getPresenter().getLines()); + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + plotDialog.add(plot); + plotDialog.setVisible(true); + } + }); + } else { + plotDialog.setVisible(true); + } } } diff --git a/src/main/java/View/PlotDialog.java b/src/main/java/View/PlotDialog.java index f3013be..b624723 100644 --- a/src/main/java/View/PlotDialog.java +++ b/src/main/java/View/PlotDialog.java @@ -1,5 +1,20 @@ package View; +import Model.Pair; +import org.jfree.chart.ChartFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.chart.plot.XYPlot; +import org.jfree.chart.renderer.xy.XYItemRenderer; +import org.jfree.data.xy.XYSeries; +import org.jfree.data.xy.XYSeriesCollection; +import org.jfree.util.ShapeUtilities; + +import javax.swing.*; +import java.awt.*; +import java.util.LinkedList; + /** * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden. * @@ -7,5 +22,72 @@ package View; * @Email: a_wolf28@uni-muenster.de * @Date: 29.05.2017. */ -public class PlotDialog { +public class PlotDialog extends JPanel { + + private JFreeChart chart; + private ChartPanel panel; + private XYSeriesCollection datapoints; + private XYSeries series; + + public PlotDialog() { + super(); + this.setPreferredSize(new Dimension(800, 500)); + } + + public void createPlot(LinkedList points) { + + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + System.out.println("start"); + + convertData(points); + } + }); + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + chart = ChartFactory.createScatterPlot("", + "X", "Y", datapoints, PlotOrientation.VERTICAL, false, true, false); + Shape diamond = ShapeUtilities.createDiamond(2f); + chart.setBorderVisible(false); + chart.setAntiAlias(true); + chart.getPlot().setBackgroundPaint(Color.WHITE); + + XYPlot xyPlot = (XYPlot) chart.getPlot(); + xyPlot.setDomainCrosshairVisible(true); + xyPlot.setRangeCrosshairVisible(true); + + XYItemRenderer renderer = xyPlot.getRenderer(); + renderer.setSeriesPaint(0, Color.blue); + renderer.setSeriesShape(0, diamond); + + xyPlot.setDomainCrosshairVisible(true); + xyPlot.setRangeCrosshairVisible(true); + + panel = new ChartPanel(chart); + this.add(panel); + } + + public void addLineToPlot() { + //TODO Line Estimator + } + + private void convertData(LinkedList points) { + + datapoints = new XYSeriesCollection(); + series = new XYSeries(""); + for (Pair p : points) { + series.add(p.getX(), p.getY()); + } + + datapoints.addSeries(series); + } + + } +