Reorganize the PersonView :
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 16 Jan 2019 12:13:42 +0000 (13:13 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 16 Jan 2019 12:13:42 +0000 (13:13 +0100)
* Use BorderLayout for the PersonView;
* Add four panels to the PersonView : north, south, east, west;
* Add PersonPanel class for the center in the layout and use it in the PersonView.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
data/personImage.png [new file with mode: 0644]
src/MainWindowsView.java
src/PersonPanel.java [new file with mode: 0644]
src/PersonView.java
src/Utils.java

diff --git a/data/personImage.png b/data/personImage.png
new file mode 100644 (file)
index 0000000..f174952
Binary files /dev/null and b/data/personImage.png differ
index 85470ff4636bc67730f5ca54eff5d8e34cc38af9..df6839f9813916af37709317683a550835ca2046 100644 (file)
@@ -8,10 +8,11 @@ import java.util.ArrayList;
 
 public class MainWindowsView extends JFrame {
 
-    MainWindowsView(String title, JPanel panel) {
+    MainWindowsView(String title, PersonView view) {
+        Container panel = getContentPane();
         //Create and set up the window.
         setTitle(title);
-        setSize(panel.getPreferredSize());
+        setSize(view.getDimension());
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
@@ -28,7 +29,11 @@ public class MainWindowsView extends JFrame {
             panel.add(component);
         }
 
-        setContentPane(panel);
+        panel.add(view.getNorthPanel(), BorderLayout.NORTH);
+        panel.add(view.getSouthPanel(), BorderLayout.SOUTH);
+        panel.add(view.getEastPanel(), BorderLayout.EAST);
+        panel.add(view.getWestPanel(), BorderLayout.WEST);
+        panel.add(view.getPersonPanel(), BorderLayout.CENTER);
     }
 
     /**
@@ -77,7 +82,8 @@ public class MainWindowsView extends JFrame {
      * event-dispatching thread.
      */
     public void showGUI() {
-
+        System.out.println("Created GUI on EDT? " +
+                SwingUtilities.isEventDispatchThread());
         //Display the window.
         //this.pack();
         this.setVisible(true);
diff --git a/src/PersonPanel.java b/src/PersonPanel.java
new file mode 100644 (file)
index 0000000..7c8e241
--- /dev/null
@@ -0,0 +1,59 @@
+import javax.imageio.ImageIO;
+import javax.swing.*;
+import java.awt.*;
+import java.awt.geom.Ellipse2D;
+import java.awt.geom.Point2D;
+import java.io.File;
+import java.io.IOException;
+
+public class PersonPanel extends JPanel {
+    private boolean debug = true;
+    private String titleText = new String();
+    private JLabel personLabel = new JLabel();
+    private String contentText = new String();
+    private Image personImage;
+
+    public PersonPanel(String title) {
+        setTitleText(title);
+        personLabel.setText(this.getTitleText());
+        add(personLabel);
+        try {
+            this.personImage = ImageIO.read(new File("data/personImage.png"));
+        } catch (IOException e) {
+            System.out.println(e.getStackTrace());
+        }
+    }
+
+    public void setTitleText(String titleText) {
+        this.titleText = titleText;
+    }
+
+    public String getTitleText() {
+        return titleText;
+    }
+
+    public void setContentText(String contentText) {
+        this.contentText = contentText;
+    }
+
+    public String getContentText() {
+        return contentText;
+    }
+
+    public void paintComponent(Graphics g) {
+        super.paintComponent(g);
+
+        // Draw
+        g.setColor(Color.black);
+        if (debug)
+            // Below the JLabel
+            g.drawString(this.getContentText(), 5, 35);
+        Graphics2D g2d = (Graphics2D) g;
+        g2d.drawImage(this.personImage.getScaledInstance(getWidth() / 2, getHeight() / 2, Image.SCALE_SMOOTH), getWidth() / 4, getHeight() / 4, this);
+    }
+
+    private static Ellipse2D getCircleByCenter(Point2D center, double radius) {
+        Ellipse2D.Double myCircle = new Ellipse2D.Double(center.getX() - radius, center.getY() - radius, 2 * radius, 2 * radius);
+        return myCircle;
+    }
+}
index 8cee3e23e100c5ed70d01dd669f78c294693b0da..6823579d2bc162a43ea1fc6d1b377a1e43c73fe0 100644 (file)
@@ -4,9 +4,9 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.ArrayList;
 
-public class PersonView extends JPanel implements ActionListener {
-    private int width = 400;
-    private int height = 600;
+public class PersonView extends JComponent implements ActionListener {
+    private int width = 800;
+    private int height = 500;
     private Person personObj;
     private ArrayList<Person> personArrayList;
     private FirstnameView firstnameView;
@@ -14,10 +14,12 @@ public class PersonView extends JPanel implements ActionListener {
     private SizeView sizeView;
     private WeightView weightView;
     private EyeView eyeView;
-    private JPanel personPanel = new JPanel();
+    private JPanel northPanel = new JPanel();
+    private JPanel southPanel = new JPanel();
+    private JPanel eastPanel = new JPanel();
+    private JPanel westPanel = new JPanel();
+    private PersonPanel personPanel = new PersonPanel("Person comparison");
     private JButton compareButton = new JButton("Compare");
-    /*private ImageIcon originImage;
-    private ImageIcon personImage;*/
 
     PersonView(Person personObj, ArrayList<Person> personArrayList) {
         setPersonObj(personObj);
@@ -29,17 +31,18 @@ public class PersonView extends JPanel implements ActionListener {
         setPersonArrayList(personArrayList);
 
         this.compareButton.addActionListener(this);
-        JLabel personLabel = new JLabel("Closest person found");
-        personPanel.add(personLabel);
-        setPreferredSize(new Dimension(this.width, this.height));
-        setLayout(new GridLayout(7, 1, 0, 4));
-        add(firstnameView);
-        add(originView);
-        add(sizeView);
-        add(weightView);
-        add(eyeView);
-        add(compareButton);
-        add(personPanel);
+        northPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
+        northPanel.add(firstnameView);
+        southPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
+        southPanel.add(compareButton);
+        eastPanel.setLayout(new BoxLayout(this.eastPanel, BoxLayout.Y_AXIS));
+        eastPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
+        eastPanel.add(sizeView);
+        eastPanel.add(weightView);
+        eastPanel.add(eyeView);
+        westPanel.setLayout(new BoxLayout(this.westPanel, BoxLayout.Y_AXIS));
+        westPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
+        westPanel.add(originView);
     }
 
     public Person getPersonObj() {
@@ -128,6 +131,30 @@ public class PersonView extends JPanel implements ActionListener {
         this.personArrayList = personArrayList;
     }
 
+    public Dimension getDimension() {
+        return new Dimension(this.width, this.height);
+    }
+
+    public JPanel getNorthPanel() {
+        return northPanel;
+    }
+
+    public JPanel getSouthPanel() {
+        return southPanel;
+    }
+
+    public JPanel getEastPanel() {
+        return eastPanel;
+    }
+
+    public JPanel getWestPanel() {
+        return westPanel;
+    }
+
+    public PersonPanel getPersonPanel() {
+        return personPanel;
+    }
+
     @Override
     public void actionPerformed(ActionEvent actionEvent) {
         //TODO: one can implement a smarter way of getting all inputs values inside the main panel.
@@ -155,9 +182,7 @@ public class PersonView extends JPanel implements ActionListener {
             personArrayList.sort(getPersonObj());
             //Utils.displayArrayList(personArrayList);
             Person closestPerson = personArrayList.get(personArrayList.indexOf(this.getPersonObj()) + 1);
-            JLabel closestPersonLabel = new JLabel();
-            closestPersonLabel.setText(closestPerson.toString());
-            this.personPanel.add(closestPersonLabel);
+            personPanel.setContentText(closestPerson.toString());
         }
     }
 }
index b551cb1bb2f4541f98abb5e28d478e3ce617185d..e7a5d2d3268e9fb4c336c3e71da316efd9ebb5d5 100644 (file)
@@ -4,9 +4,9 @@ import java.util.ListIterator;
 public class Utils {
     public static void displayArrayList(ArrayList arrayList) {
         ListIterator iter = arrayList.listIterator();
-        Integer i = 0;
+        int i = 0;
         while (iter.hasNext()) {
-            System.out.println(i.toString() + " " + iter.next());
+            System.out.println(i + " " + iter.next());
             i++;
         }
     }