Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

***This is a JAVA question*** --------------------------------------------------

ID: 3599415 • Letter: #

Question

***This is a JAVA question***

------------------------------------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;

public class DrawingPanel implements ActionListener {
public static final int DELAY = 50; // delay between repaints in millis

private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save";
private static String TARGET_IMAGE_FILE_NAME = null;
private static final boolean PRETTY = true; // true to anti-alias
private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file

private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse position
private long createTime;

static {
TARGET_IMAGE_FILE_NAME = System.getProperty(DUMP_IMAGE_PROPERTY_NAME);
DUMP_IMAGE = (TARGET_IMAGE_FILE_NAME != null);
}

// construct a drawing panel of given width and height enclosed in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

this.statusBar = new JLabel(" ");
this.statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));

this.panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
this.panel.setBackground(Color.WHITE);
this.panel.setPreferredSize(new Dimension(width, height));
this.panel.add(new JLabel(new ImageIcon(image)));

// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
DrawingPanel.this.statusBar.setText("(" + e.getX() + ", " + e.getY() + ")");
}

public void mouseExited(MouseEvent e) {
DrawingPanel.this.statusBar.setText(" ");
}
};
this.panel.addMouseListener(listener);
this.panel.addMouseMotionListener(listener);

this.g2 = (Graphics2D)image.getGraphics();
this.g2.setColor(Color.BLACK);
if (PRETTY) {
this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
this.g2.setStroke(new BasicStroke(1.1f));
}

this.frame = new JFrame("Building Java Programs - Drawing Panel");
this.frame.setResizable(false);
this.frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (DUMP_IMAGE) {
DrawingPanel.this.save(TARGET_IMAGE_FILE_NAME);
}
System.exit(0);
}
});
this.frame.getContentPane().add(panel);
this.frame.getContentPane().add(statusBar, "South");
this.frame.pack();
this.frame.setVisible(true);
if (DUMP_IMAGE) {
createTime = System.currentTimeMillis();
this.frame.toBack();
} else {
this.toFront();
}

// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}

// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
this.panel.repaint();
if (DUMP_IMAGE && System.currentTimeMillis() > createTime + 4 * DELAY) {
this.frame.setVisible(false);
this.frame.dispose();
this.save(TARGET_IMAGE_FILE_NAME);
System.exit(0);
}
}

// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return this.g2;
}

// set the background color of the drawing panel
public void setBackground(Color c) {
this.panel.setBackground(c);
}

// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
this.frame.setVisible(visible);
}

// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}

// take the current contents of the panel and write them to a file
public void save(String filename) {
String extension = filename.substring(filename.lastIndexOf(".") + 1);

// create second image so we get the background color
BufferedImage image2 = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
Graphics g = image2.getGraphics();
g.setColor(panel.getBackground());
g.fillRect(0, 0, this.width, this.height);
g.drawImage(this.image, 0, 0, panel);

// write file
try {
ImageIO.write(image2, extension, new java.io.File(filename));
} catch (java.io.IOException e) {
System.err.println("Unable to save image: " + e);
}
}

// makes drawing panel become the frontmost window on the screen
public void toFront() {
this.frame.toFront();
}
}

------------------------------------------------------------------------------------------------

import java.awt.*;

public class Facebook{

private String name;

private String content;

DrawingPanel panel;

private Graphics g;

public Facebook(String nm){

content = "undefined";

name = nm;

// Create the drawing panel

panel = new DrawingPanel(200, 150);

g = panel.getGraphics();

// display name

g.drawString(name+"'s mood is undefined.", 20, 75);

}

public void setContent(String newContent){

content = newContent;

if(content.equals("happy")){

g.setColor(Color.red);

g.fillRect(0, 0, 200, 150);

g.setColor(Color.black);

// display mood

g.drawString(name+"'s mood is:"+ "happy", 20, 75);

}

else{

g.setColor(Color.white);

g.fillRect(0, 0, 200, 150);

g.setColor(Color.black);

g.drawString(name+"'s mood is:"+ content, 20, 75);

}

}

public String getContent(){

return content;

}

}

------------------------------------------------------------------------------------------------

public class FacebookPerson{

private String myName;

protected String myMood;

protected Facebook myfacebook;

public FacebookPerson(String name){

myName = name;

myfacebook = new Facebook(myName);

//System.out.println("FacebookPerson's constructor");

}

  

public FacebookPerson(){

  

}

public String getName(){

return myName;

}

public void setMood(String newMood){

myMood = newMood;

myfacebook.setContent(myMood);

}

public String getMood(){

return myMood;

}

  

}

------------------------------------------------------------------------------------------------

public class simple_test{

public static void main (String[] args){

Facebook fb = new Facebook("Dr. Hu");

fb.setContent("happy");

} // end main

}

------------------------------------------------------------------------------------------------

import java.util.*;

public class testFacebook{

public static void main (String[] args){

// Prompt user to enter the number of facebookpresons

Scanner userInput = new Scanner(System.in);

System.out.println("Enter the number of facebookpresons to be created: ");

int numP=0;

while(true){

try{

String inputString = userInput.nextLine();

numP = Integer.parseInt(inputString);

if(numP>0 && numP<=5) // accept the number if it is within range. Here we define the range to be from 1 to 5.

break;

else

System.out.println("the number is out of range [1, 5]! enter again");

} catch (NumberFormatException e){

System.out.println("invalid input. Enter an integer number!");

}

}

FacebookPerson[] fbp = new FacebookPerson[numP];

//Ask the user to enter the name for each person, and create the persons

for(int i=0; i< numP; i++){

System.out.println("Enter the name for person "+ (i+1)+ ":");

String name = userInput.nextLine();

fbp[i] = new FacebookPerson(name);

}

System.out.println("-------select a person and type the mood below--------");

//Ask the user to set the mood for a person, and update the mood, enter "####" to exit

while(true){

System.out.println("Enter the name for a person (enter #### to exit):");

String name = userInput.nextLine();

if(name.equals("####"))

System.exit(0);

int personID = -1;

for(int i=0; i< numP; i++){

if(fbp[i].getName().equals(name)){

personID = i;

break; // break the for loop

}

}

if(personID!=-1){ // found the person, otherwise personID should still be -1

System.out.println("Enter the mood for the person:");

String mood = userInput.nextLine();

fbp[personID].setMood(mood);

}

else

System.out.println("unrecognized name!");

} // end while

} // end main

}

Your HW5 is to modify the Facebook example so that instead of each person has its own Facebook window, all persons are displayed in the same Facebook window. Different persons are shown at different regions of the window as illustrated by the figure at the end of this document. The size of the Facebook window is 700 x 550 and the size of each person's region is 200 x 150 The program will ask the user to enter the number of facebookpresons to be created. The maximum number of person that can be created is 9 (i.e. maximum three rows in the window). If the entered number is greater than 9 or smaller than 1, the program will ask the user to enter a new number until the entered number is between 1 and 9 (inclusive). Then the program will ask the user to enter the person names and create their corresponding regions in the window witlh their default mood displayed (see the illustrative figure at the end of this document). The user can then select a person (the program will prompt an "unrecognized name!" message if the typed name is not recognizable). After the person is selected, the user can do the following two things 1) type in the mood for that person; or 2) type "SS$" to delete the person. In the first case ie, the user's input is not "SS$"), the selected person's region is updated in the following way: » When the mood is happy, the region has a red background and the mood is displayed. » When the mood is sad, the region has a green background and the mood is displayed. » Otherwise, the region has a white background and the mood is displayed. In the second case (i.e., the user's input is "SS$"), the person is deleted and the person's region (including the black boundary of the region) in the window is erased. An illustration of how the program runs and the corresponding window display are given at the end of this document. What to submit: Package all the java files in a "Facebook_HW5" package and submit the entire Facebook_HW5 package. Your package should include a "testFacebook.java", which we will use to run your programs.

Explanation / Answer

the code has fixed and changed

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;

public class DrawingPanel implements ActionListener {
public static final int DELAY = 50; // delay between repaints in millis

private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save";
private static String TARGET_IMAGE_FILE_NAME = null;
private static final boolean PRETTY = true; // true to anti-alias
private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file

private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse position
private long createTime;

static {
TARGET_IMAGE_FILE_NAME = System.getProperty(DUMP_IMAGE_PROPERTY_NAME);
DUMP_IMAGE = (TARGET_IMAGE_FILE_NAME != null);
}

// construct a drawing panel of given width and height enclosed in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

this.statusBar = new JLabel(" ");
this.statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));

this.panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
this.panel.setBackground(Color.WHITE);
this.panel.setPreferredSize(new Dimension(width, height));
this.panel.add(new JLabel(new ImageIcon(image)));

// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
DrawingPanel.this.statusBar.setText("(" + e.getX() + ", " + e.getY() + ")");
}

public void mouseExited(MouseEvent e) {
DrawingPanel.this.statusBar.setText(" ");
}
};
this.panel.addMouseListener(listener);
this.panel.addMouseMotionListener(listener);

this.g2 = (Graphics2D)image.getGraphics();
this.g2.setColor(Color.BLACK);
if (PRETTY) {
this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
this.g2.setStroke(new BasicStroke(1.1f));
}

this.frame = new JFrame("Building Java Programs - Drawing Panel");
this.frame.setResizable(false);
this.frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (DUMP_IMAGE) {
DrawingPanel.this.save(TARGET_IMAGE_FILE_NAME);
}
System.exit(0);
}
});
this.frame.getContentPane().add(panel);
this.frame.getContentPane().add(statusBar, "South");
this.frame.pack();
this.frame.setVisible(true);
if (DUMP_IMAGE) {
createTime = System.currentTimeMillis();
this.frame.toBack();
} else {
this.toFront();
}

// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}

// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
this.panel.repaint();
if (DUMP_IMAGE && System.currentTimeMillis() > createTime + 4 * DELAY) {
this.frame.setVisible(false);
this.frame.dispose();
this.save(TARGET_IMAGE_FILE_NAME);
System.exit(0);
}
}

// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return this.g2;
}

// set the background color of the drawing panel
public void setBackground(Color c) {
this.panel.setBackground(c);
}

// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
this.frame.setVisible(visible);
}

// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}

// take the current contents of the panel and write them to a file
public void save(String filename) {
String extension = filename.substring(filename.lastIndexOf(".") + 1);

// create second image so we get the background color
BufferedImage image2 = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
Graphics g = image2.getGraphics();
g.setColor(panel.getBackground());
g.fillRect(0, 0, this.width, this.height);
g.drawImage(this.image, 0, 0, panel);

// write file
try {
ImageIO.write(image2, extension, new java.io.File(filename));
} catch (java.io.IOException e) {
System.err.println("Unable to save image: " + e);
}
}

// makes drawing panel become the frontmost window on the screen
public void toFront() {
this.frame.toFront();
}
}

------------------------------------------------------------------------------------------------

import java.awt.*;

public class Facebook{

private String name;

private String content;

DrawingPanel panel;

private Graphics g;

public Facebook(String nm){

content = "undefined";

name = nm;

// Create the drawing panel

panel = new DrawingPanel(200, 150);

g = panel.getGraphics();

// display name

g.drawString(name+"'s mood is undefined.", 20, 75);

}

public void setContent(String newContent){

content = newContent;

if(content.equals("happy")){

g.setColor(Color.red);

g.fillRect(0, 0, 200, 150);

g.setColor(Color.black);

// display mood

g.drawString(name+"'s mood is:"+ "happy", 20, 75);

}

else{

g.setColor(Color.white);

g.fillRect(0, 0, 200, 150);

g.setColor(Color.black);

g.drawString(name+"'s mood is:"+ content, 20, 75);

}

}

public String getContent(){

return content;

}

}

------------------------------------------------------------------------------------------------

public class FacebookPerson{

private String myName;

protected String myMood;

protected Facebook myfacebook;

public FacebookPerson(String name){

myName = name;

myfacebook = new Facebook(myName);

//System.out.println("FacebookPerson's constructor");

}

public FacebookPerson(){

}

public String getName(){

return myName;

}

public void setMood(String newMood){

myMood = newMood;

myfacebook.setContent(myMood);

}

public String getMood(){

return myMood;

}

}

------------------------------------------------------------------------------------------------

public class simple_test{

public static void main (String[] args){

Facebook fb = new Facebook("Dr. Hu");

fb.setContent("happy");

} // end main

}

------------------------------------------------------------------------------------------------

import java.util.*;

public class testFacebook{

public static void main (String[] args){

// Prompt user to enter the number of facebookpresons

Scanner userInput = new Scanner(System.in);

System.out.println("Enter the number of facebookpresons to be created: ");

int numP=0;

while(true){

try{

String inputString = userInput.nextLine();

numP = Integer.parseInt(inputString);

if(numP>0 && numP<=5) // accept the number if it is within range. Here we define the range to be from 1 to 5.

break;

else

System.out.println("the number is out of range [1, 5]! enter again");

} catch (NumberFormatException e){

System.out.println("invalid input. Enter an integer number!");

}

}

FacebookPerson[] fbp = new FacebookPerson[numP];

//Ask the user to enter the name for each person, and create the persons

for(int i=0; i< numP; i++){

System.out.println("Enter the name for person "+ (i+1)+ ":");

String name = userInput.nextLine();

fbp[i] = new FacebookPerson(name);

}

System.out.println("-------select a person and type the mood below--------");

//Ask the user to set the mood for a person, and update the mood, enter "####" to exit

while(true){

System.out.println("Enter the name for a person (enter #### to exit):");

String name = userInput.nextLine();

if(name.equals("####"))

System.exit(0);

int personID = -1;

for(int i=0; i< numP; i++){

if(fbp[i].getName().equals(name)){

personID = i;

break; // break the for loop

}

}

if(personID!=-1){ // found the person, otherwise personID should still be -1

System.out.println("Enter the mood for the person:");

String mood = userInput.nextLine();

fbp[personID].setMood(mood);

}

else

System.out.println("unrecognized name!");

} // end while

} // end main

}