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);
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);
}
/**
* event-dispatching thread.
*/
public void showGUI() {
-
+ System.out.println("Created GUI on EDT? " +
+ SwingUtilities.isEventDispatchThread());
//Display the window.
//this.pack();
this.setVisible(true);
--- /dev/null
+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;
+ }
+}
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;
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);
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() {
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.
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());
}
}
}