活跃农民
- 积分
- 448
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-7-31
- 最后登录
- 1970-1-1
|
FacePamphlet.java:- /*
- * File: FacePamphlet.java
- * -----------------------
- * When it is finished, this program will implement a basic social network
- * management system.
- */
- import acm.program.*;
- import acm.graphics.*;
- import acm.util.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class FacePamphlet extends Program implements FacePamphletConstants {
- /**
- * This method has the responsibility for initializing the
- * interactors in the application, and taking care of any other
- * initialization that needs to be performed.
- */
-
- public void init() {
-
- // NORTH
- name = new JLabel ("Name");
- add (name, NORTH);
- textFieldNorth = new JTextField(TEXT_FIELD_SIZE);
- add (textFieldNorth, NORTH);
- add = new JButton ("Add");
- add (add, NORTH);
- delete = new JButton ("Delete");
- add (delete, NORTH);
- lookup = new JButton ("Lookup");
- add (lookup, NORTH);
-
- // WEST
- // Change Status
- textFieldWest1 = new JTextField (TEXT_FIELD_SIZE);
- add (textFieldWest1, WEST);
- status = new JButton ("Change Status");
- add (status, WEST);
- add( new JLabel(EMPTY_LABEL_TEXT), WEST);
- // Change Picture
- textFieldWest2 = new JTextField (TEXT_FIELD_SIZE);
- add (textFieldWest2, WEST);
- picture = new JButton ("Change Picture");
- add (picture, WEST);
- add( new JLabel(EMPTY_LABEL_TEXT), WEST);
- // Add Friend
- textFieldWest3 = new JTextField (TEXT_FIELD_SIZE);
- add (textFieldWest3, WEST);
- friend = new JButton ("Add Friend");
- add (friend, WEST);
- add( new JLabel(EMPTY_LABEL_TEXT), WEST);
-
- // West region need "Enter" function
- textFieldWest1.addActionListener(this);
- textFieldWest2.addActionListener(this);
- textFieldWest3.addActionListener(this);
-
- // ActionListerners
- addActionListeners();
-
- // canvas
- add(canvas);
- }
-
- /**
- * This class is responsible for detecting when the buttons are
- * clicked or interactors are used, so you will have to add code
- * to respond to these actions.
- */
-
- public void actionPerformed(ActionEvent e) {
-
- String cmd = e.getActionCommand();
-
- // North region
- // Add button
- if (cmd.equals("Add")) {
- if (data.containsProfile(textFieldNorth.getText())){
- currentProfile = data.getProfile(textFieldNorth.getText()); // if exist, add = look up
- canvas.displayProfile(currentProfile);
- canvas.showMessage("A profile with the name<" + currentProfile.getName() + ">already exists");
- }
- else {
- currentProfile = new FacePamphletProfile(textFieldNorth.getText());// if do not exist, new a FacePamphletProfile
- data.addProfile(currentProfile);
- canvas.displayProfile(currentProfile);
- canvas.showMessage("New profile created");
- }
- }
-
- // Delete button
- if (cmd.equals("Delete")) {
- if (data.containsProfile(textFieldNorth.getText())){
- data.deleteProfile(textFieldNorth.getText());
- canvas.removeAll(); // clear any existing profile from the display
- canvas.showMessage("Profile of <" +textFieldNorth.getText()+ ">deleted");
- currentProfile = null;
- } else {
- canvas.removeAll();
- canvas.showMessage("A profile with the name<" + textFieldNorth.getText() + ">does not exist");
- currentProfile = null;
- }
- }
-
- // Lookup button
- if (cmd.equals("Lookup")) {
- if (data.containsProfile(textFieldNorth.getText())) {
- currentProfile = data.getProfile(textFieldNorth.getText());
- canvas.displayProfile(currentProfile);
- canvas.showMessage("Displaying<" + currentProfile.getName() + ">");
- } else {
- canvas.removeAll();
- canvas.showMessage("A profile with the name<" + textFieldNorth.getText() + ">does not exist");
- currentProfile = null;
- }
- }
-
- // West region
- // Change Status button
- if ((cmd.equals("Change Status"))||(e.getSource() == textFieldWest1)) { // Change status button
- if (currentProfile != null){
- currentProfile.setStatus(textFieldWest1.getText());
- canvas.displayProfile(currentProfile);
- canvas.showMessage("Status updated to<" + currentProfile.getStatus() + ">");
- }
- else {
- canvas.showMessage("Please select a profile to change status");
- }
- }
-
- // Change Picture button
- if ((cmd.equals("Change Picture"))||(e.getSource() == textFieldWest2)) { // Change picture button
- if (currentProfile != null){
- GImage image = null;
- try{
- image = new GImage (textFieldWest2.getText());
- currentProfile.setImage(image);
- canvas.displayProfile(currentProfile);
- canvas.showMessage("Picture updated");
- } catch (ErrorException ex) {
- canvas.showMessage("Unable to open image file:<" + textFieldWest2.getText() + ">");
- }
- } else {
- canvas.removeAll();
- canvas.showMessage("Please select a profile to change picture");
- }
- }
-
- // Add Friend button
- if ((cmd.equals("Add Friend"))||(e.getSource() == textFieldWest3)) {
- if (currentProfile != null) {
- if (data.containsProfile(textFieldWest3.getText())){ // judge the status of added profile
- boolean b = currentProfile.addFriend(textFieldWest3.getText());
- if (b == false) {
- canvas.showMessage("<" + currentProfile.getName() + ">already has " + "<" + textFieldWest3.getText() + ">as a friend");
- }
- if (b == true) {
- (data.getProfile(textFieldWest3.getText())).addFriend(currentProfile.getName()); // revise adding friend! IMPORTANT
- canvas.displayProfile(currentProfile);
- canvas.showMessage ("<" + textFieldWest3.getText() + ">added as a friend");
- }
- } else {
- canvas.showMessage("<" + textFieldWest3.getText() + ">does not exist");
- }
- } else {
- canvas.removeAll();
- canvas.showMessage("Please select a profile to add friend");
- }
- }
- }
-
- /** instance variable*/
- // North
- private JLabel name;
- private JTextField textFieldNorth;
- private JButton add;
- private JButton delete;
- private JButton lookup;
- // West
- private JTextField textFieldWest1;
- private JTextField textFieldWest2;
- private JTextField textFieldWest3;
- private JButton status;
- private JButton picture;
- private JButton friend;
- // Current Profile
- private FacePamphletProfile currentProfile;
- private FacePamphletDatabase data = new FacePamphletDatabase();
- // Canvas
- private FacePamphletCanvas canvas = new FacePamphletCanvas();
- }
复制代码 FacePamphletProfile.java:- /*
- * File: FacePamphletProfile.java
- * ------------------------------
- * This class keeps track of all the information for one profile
- * in the FacePamphlet social network. Each profile contains a
- * name, an image (which may not always be set), a status (what
- * the person is currently doing, which may not always be set),
- * and a list of friends.
- */
- import acm.graphics.*;
- import java.util.*;
- public class FacePamphletProfile implements FacePamphletConstants {
-
- /**
- * Constructor
- * This method takes care of any initialization needed for
- * the profile.
- */
-
- public FacePamphletProfile(String name) { // input name,
- this.name = name; // left "name" is defined in this class, right "name" is passed in
- this.status = "";
- this.image = null;
- }
- /** This method returns the name associated with the profile. */
- public String getName() {
- return this.name;
- }
- /**
- * This method returns the image associated with the profile.
- * If there is no image associated with the profile, the method
- * returns null. */
- public GImage getImage() {
- return this.image;
- }
- /** This method sets the image associated with the profile. */
- public void setImage(GImage image) {
- this.image = image;
- }
-
- /**
- * This method returns the status associated with the profile.
- * If there is no status associated with the profile, the method
- * returns the empty string ("").
- */
- public String getStatus() {
- return this.status;
- }
-
- /** This method sets the status associated with the profile. */
- public void setStatus(String status) {
- this.status = status;
- }
- /**
- * This method adds the named friend to this profile's list of
- * friends. It returns true if the friend's name was not already
- * in the list of friends for this profile (and the name is added
- * to the list). The method returns false if the given friend name
- * was already in the list of friends for this profile (in which
- * case, the given friend name is not added to the list of friends
- * a second time.)
- */
- public boolean addFriend(String friend) {
- if (friendsList.contains(friend)) return false;
- friendsList.add(friend); return true;
- }
- /**
- * This method removes the named friend from this profile's list
- * of friends. It returns true if the friend's name was in the
- * list of friends for this profile (and the name was removed from
- * the list). The method returns false if the given friend name
- * was not in the list of friends for this profile (in which case,
- * the given friend name could not be removed.)
- */
- public boolean removeFriend(String friend) { // remove one friend from the list
- if (friendsList.contains(friend)) {
- friendsList.remove(friend);
- return true;
- }
- return false;
- }
- /**
- * This method returns an iterator over the list of friends
- * associated with the profile.
- */
- public Iterator<String> getFriends() {
- return friendsList.iterator(); // return an iterator
- }
-
- /**
- * This method returns a string representation of the profile.
- * This string is of the form: "name (status): list of friends",
- * where name and status are set accordingly and the list of
- * friends is a comma separated list of the names of all of the
- * friends in this profile.
- *
- * For example, in a profile with name "Alice" whose status is
- * "coding" and who has friends Don, Chelsea, and Bob, this method
- * would return the string: "Alice (coding): Don, Chelsea, Bob"
- */
- public String toString() {
- String line = "";
- line = "\"" + name + " (" + status + "): ";
- Iterator<String> it = getFriends(); // object of the Iterator: "it"
- if (it.hasNext()) line = line + it.next();
- while (it.hasNext()){
- line = line + "," + it.next();
- }
- line = line + "\"";
- return line;
- }
- /** instance variable*/
- private String name;
- private String status;
- private GImage image;
- ArrayList<String> friendsList = new ArrayList <String>(); // each people's list of friends store in "friendList"
- }
复制代码 FacePamphletDatabase.java:
- /*
- * File: FacePamphletDatabase.java
- * -------------------------------
- * This class keeps track of the profiles of all users in the
- * FacePamphlet application. Note that profile names are case
- * sensitive, so that "ALICE" and "alice" are NOT the same name.
- */
- import java.util.*;
- /** this class has 4 methods: "add, delete,get, contains" */
- public class FacePamphletDatabase implements FacePamphletConstants {
- /**
- * Constructor
- * This method takes care of any initialization needed for
- * the database.
- */
-
- public FacePamphletDatabase() {
- // null
- }
-
- /**
- * This method adds the given profile to the database. If the
- * name associated with the profile is the same as an existing
- * name in the database, the existing profile is replaced by
- * the new profile passed in.
- */
-
- public void addProfile(FacePamphletProfile profile) {
- if (database.containsKey(profile.getName())) database.remove(profile.getName());
- database.put(profile.getName(), profile);
- }
- /**
- * This method returns the profile associated with the given name
- * in the database. If there is no profile in the database with
- * the given name, the method returns null.
- */
-
- public FacePamphletProfile getProfile(String name) {
- if (database.containsKey(name)) return database.get(name);
- return null;
- }
-
- /**
- * This method removes the profile associated with the given name
- * from the database. It also updates the list of friends of all
- * other profiles in the database to make sure that this name is
- * removed from the list of friends of any other profile.
- *
- * If there is no profile in the database with the given name, then
- * the database is unchanged after calling this method.
- */
-
- public void deleteProfile(String name) { // THIS IS IMPORTANT!
- if (database.containsKey(name)){
- Iterator<String> it = database.get(name).getFriends(); // set an iterator "it" which represents all of input name's friends
- while (it.hasNext()){ // it.hasNext() is a String of input name's friend's name
- database.get(it.next()).removeFriend(name);
- }
- }
- database.remove(name);
- }
- /**
- * This method returns true if there is a profile in the database
- * that has the given name. It returns false otherwise.
- */
-
- public boolean containsProfile(String name) {
- if (database.containsKey(name)) return true;
- return false;
- }
- /** instance variable*/
-
- private HashMap<String, FacePamphletProfile> database = new HashMap<String, FacePamphletProfile>(); // correspond name & Profile in HashMap
- }
复制代码 FacePamphletCanvas.java:
- /*
- * File: FacePamphletCanvas.java
- * -----------------------------
- * This class represents the canvas on which the profiles in the social
- * network are displayed. NOTE: This class does NOT need to update the
- * display when the window is resized.
- */
- import acm.graphics.*;
- import java.awt.*;
- import java.util.*;
- public class FacePamphletCanvas extends GCanvas implements FacePamphletConstants {
-
- /**
- * Constructor
- * This method takes care of any initialization needed for
- * the display
- */
- public FacePamphletCanvas() {
- // null
- }
- /**
- * This method displays a message string near the bottom of the
- * canvas. Every time this method is called, the previously
- * displayed message (if any) is replaced by the new message text
- * passed in.
- */
-
- public void showMessage(String msg) { // if called, delete current message and add the new one
- message = new GLabel(msg,getWidth()/2, getHeight()-BOTTOM_MESSAGE_MARGIN);
- message.move(-message.getWidth()/2, 0);
- message.setFont(MESSAGE_FONT);
- if (getElementAt(getWidth()/2,getHeight()-BOTTOM_MESSAGE_MARGIN) != null) {
- remove(getElementAt(getWidth()/2,getHeight()-BOTTOM_MESSAGE_MARGIN));
- }
- add(message);
- }
-
- /**
- * This method displays the given profile on the canvas. The
- * canvas is first cleared of all existing items (including
- * messages displayed near the bottom of the screen) and then the
- * given profile is displayed. The profile display includes the
- * name of the user from the profile, the corresponding image
- * (or an indication that an image does not exist), the status of
- * the user, and a list of the user's friends in the social network.
- */
-
- public void displayProfile(FacePamphletProfile profile) {
- removeAll();
-
- // update name
- name = new GLabel(profile.getName(), LEFT_MARGIN, TOP_MARGIN);
- name.move(0, name.getHeight());
- name.setFont(PROFILE_NAME_FONT);
- name.setColor(Color.blue);
- add(name);
-
- // update picture
- add(new GRect(LEFT_MARGIN, TOP_MARGIN+IMAGE_MARGIN+name.getHeight(), IMAGE_WIDTH, IMAGE_HEIGHT));
- if (profile.getImage() == null){
- noPicture = new GLabel ("NO IMAGE!", LEFT_MARGIN+IMAGE_WIDTH/2, TOP_MARGIN+IMAGE_MARGIN+name.getHeight()+IMAGE_HEIGHT/2);
- noPicture.move(-noPicture.getWidth(), 0);
- noPicture.setFont(PROFILE_IMAGE_FONT);
- add(noPicture);
- } else {
- image = profile.getImage();
- add(image, LEFT_MARGIN, TOP_MARGIN+IMAGE_MARGIN+name.getHeight());
- }
-
- // update status
- if (profile.getStatus()== ""){
- status = new GLabel("No current status", LEFT_MARGIN, TOP_MARGIN+IMAGE_MARGIN+name.getHeight()+IMAGE_HEIGHT+STATUS_MARGIN);
- status.move(0, status.getHeight());
- status.setFont(PROFILE_STATUS_FONT);
- add(status);
- } else {
- status = new GLabel(profile.getName()+" is "+profile.getStatus(), LEFT_MARGIN, TOP_MARGIN+IMAGE_MARGIN+name.getHeight()+IMAGE_HEIGHT+STATUS_MARGIN);
- status.move(0, status.getHeight());
- add(status);
- }
-
- // Friends Label
- friendsLabel = new GLabel("Friends: ", getWidth()/2, TOP_MARGIN+IMAGE_MARGIN+name.getHeight());
- friendsLabel.setFont(PROFILE_FRIEND_LABEL_FONT);
- add(friendsLabel);
-
- // update friend
- Iterator<String> it = profile.getFriends();
- double y = TOP_MARGIN+IMAGE_MARGIN+name.getHeight();
- while (it.hasNext()){
- friends = new GLabel (it.next());
- friends.setFont(PROFILE_FRIEND_FONT);
- y = y + friends.getHeight();
- add(friends, getWidth()/2,y);
- }
- }
-
- /** instance variable*/
- private GLabel message;
- private GLabel name;
- private GLabel noPicture;
- private GImage image;
- private GLabel status;
- private GLabel friendsLabel;
- private GLabel friends;
- }
复制代码 FacePamphletConstants.java:
- /*
- * File: FacePamphletConstants.java
- * --------------------------------
- * This file declares several constants that are shared by the
- * different modules in the FacePamphlet application. Any class
- * that implements this interface can use these constants.
- */
- public interface FacePamphletConstants {
- /** The width of the application window */
- public static final int APPLICATION_WIDTH = 800;
- /** The height of the application window */
- public static final int APPLICATION_HEIGHT = 500;
- /** Number of characters for each of the text input fields */
- public static final int TEXT_FIELD_SIZE = 15;
- /** Text to be used to create an "empty" label to put space
- * between interactors on EAST border of application. Note this
- * label is not actually the empty string, but rather a single space */
- public static final String EMPTY_LABEL_TEXT = " ";
- /** Name of font used to display the application message at the
- * bottom of the display canvas */
- public static final String MESSAGE_FONT = "Dialog-18";
- /** Name of font used to display the name in a user's profile */
- public static final String PROFILE_NAME_FONT = "Dialog-24";
-
- /** Name of font used to display the text "No Image" in user
- * profiles that do not contain an actual image */
- public static final String PROFILE_IMAGE_FONT = "Dialog-24";
-
- /** Name of font used to display the status in a user's profile */
- public static final String PROFILE_STATUS_FONT = "Dialog-16-bold";
- /** Name of font used to display the label "Friends" above the
- * user's list of friends in a profile */
- public static final String PROFILE_FRIEND_LABEL_FONT = "Dialog-16-bold";
- /** Name of font used to display the names from the user's list
- * of friends in a profile */
- public static final String PROFILE_FRIEND_FONT = "Dialog-16";
- /** The width (in pixels) that profile images should be displayed */
- public static final double IMAGE_WIDTH = 200;
- /** The height (in pixels) that profile images should be displayed */
- public static final double IMAGE_HEIGHT = 200;
- /** The number of pixels in the vertical margin between the bottom
- * of the canvas display area and the baseline for the message
- * text that appears near the bottom of the display */
- public static final double BOTTOM_MESSAGE_MARGIN = 20;
- /** The number of pixels in the hortizontal margin between the
- * left side of the canvas display area and the Name, Image, and
- * Status components that are display in the profile */
- public static final double LEFT_MARGIN = 20;
- /** The number of pixels in the vertical margin between the top
- * of the canvas display area and the top (NOT the baseline) of
- * the Name component that is displayed in the profile */
- public static final double TOP_MARGIN = 20;
-
- /** The number of pixels in the vertical margin between the
- * baseline of the Name component and the top of the Image
- * displayed in the profile */
- public static final double IMAGE_MARGIN = 20;
- /** The number of vertical pixels in the vertical margin between
- * the bottom of the Image and the top of the Status component
- * in the profile */
- public static final double STATUS_MARGIN = 20;
- }
复制代码
|
|