From: Jérôme Benoit Date: Wed, 16 Jan 2019 12:13:42 +0000 (+0100) Subject: Reorganize the PersonView : X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;ds=inline;h=d252f672e7ed73d9f4e588f855d20f042f2e2df8;p=Persons_Comparator.git Reorganize the PersonView : * 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 --- diff --git a/data/personImage.png b/data/personImage.png new file mode 100644 index 0000000..f174952 Binary files /dev/null and b/data/personImage.png differ diff --git a/src/MainWindowsView.java b/src/MainWindowsView.java index 85470ff..df6839f 100644 --- a/src/MainWindowsView.java +++ b/src/MainWindowsView.java @@ -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 index 0000000..7c8e241 --- /dev/null +++ b/src/PersonPanel.java @@ -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; + } +} diff --git a/src/PersonView.java b/src/PersonView.java index 8cee3e2..6823579 100644 --- a/src/PersonView.java +++ b/src/PersonView.java @@ -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 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 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()); } } } diff --git a/src/Utils.java b/src/Utils.java index b551cb1..e7a5d2d 100644 --- a/src/Utils.java +++ b/src/Utils.java @@ -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++; } }