Please write a program that does the same thing as the program (compiles and exe
ID: 3632548 • Letter: P
Question
Please write a program that does the same thing as the program (compiles and execute as well of course) below but uses scanner instead of SimpleIO.promt and another method to create the frame instead of the DrawableFrame from the jpb package.
jpb ( package written by Dr. King): knking.com/books/java/jpb/index.html
The website has the jpb package to execute the program below and see how it looks.
//Program
import java.awt.*;
import jpb.*;
public class MessageWindowModified {
public static void main(String[] args) {
// Prompt user to enter font name
SimpleIO.prompt("Enter font name: ");
String fontName = SimpleIO.readLine().trim();
// Prompt user to enter font size; convert to integer form
SimpleIO.prompt("Enter font size: ");
String fontSizeString = SimpleIO.readLine().trim();
int fontSize = Integer.parseInt(fontSizeString);
// Create drawable frame and font objects;
// obtain font metrics
DrawableFrame df = new DrawableFrame("Message Window");
Font f = new Font(fontName, Font.PLAIN, fontSize);
FontMetrics fm = df.getFontMetrics(f);
//Prompt user to input single line
SimpleIO.prompt("Enter message: ");
String messageString = SimpleIO.readLine().trim();
//Split the single line into an array of lines using the '/' character as the marker
String[] lines = messageString.split("/");
//Number of lines is equal to the length of array
int numLines = lines.length;
int maxLineWidth = 0;
// Loop through each of the lines in our array
for (int i = 0; i < numLines; i++) {
// Compare width of line with maximum width read so
// far, replacing maximum width if necessary.
int lineWidth = fm.stringWidth(lines[i]);
if (lineWidth > maxLineWidth)
maxLineWidth = lineWidth;
}
// Open frame and set its size
df.show();
int borderSize = fm.getDescent();
int height = fm.getHeight();
df.setSize(maxLineWidth + borderSize * 2,
numLines * height + fm.getLeading());
// Obtain graphics context; set drawing color and font
Graphics g = df.getGraphicsContext();
g.setColor(Color.blue);
g.setFont(f);
// Display lines in message
int firstBaseline = fm.getLeading() + fm.getAscent();
for (int i = 0; i < numLines; i++) {
int lineWidth = fm.stringWidth(lines[i]);
g.drawString(lines[i],
borderSize + (maxLineWidth - lineWidth) / 2,
firstBaseline + i * height);
}
// Repaint frame
df.repaint();
}
}
Explanation / Answer
import java.awt.*;
import java.util.*;
public class MessageWindow
{
public static void main(String[] args)
{
//declaring scanner object
Scanner input =new Scanner(System.in);
// Prompt user to enter font name
System.out.println("Enter font name: ");
String fontName = input.nextLine();
// Prompt user to enter font size; convert to integer form
System.out.println("Enter font size: ");
String fontSizeString = input.nextLine();
int fontSize = Integer.parseInt(fontSizeString);
// Create drawable frame and font objects;
// obtain font metrics
Frame df = new Frame("Message Window");
Font f = new Font(fontName, Font.PLAIN, fontSize);
FontMetrics fm = df.getFontMetrics(f);
// Prompt user to input single line
System.out.println("Enter message: ");
String messageString = input.nextLine();
// Split the single line into an array of lines using the '/' character as the marker
String[] lines = messageString.split("/");
// Number of lines is equal to the length of array
int numLines = lines.length;
int maxLineWidth = 0;
// Loop through each of the lines in our array
for (int i = 0; i < numLines; i++)
{
// Compare width of line with maximum width read so
// far, replacing maximum width if necessary.
int lineWidth = fm.stringWidth(lines[i]);
if (lineWidth > maxLineWidth)
maxLineWidth = lineWidth;
}
// Open frame and set its size
df.show();
int borderSize = fm.getDescent();
int height = fm.getHeight();
df.setSize(maxLineWidth + borderSize * 2,
numLines * height + fm.getLeading());
// Obtain graphics context; set drawing color and font
Graphics g = df.getGraphics();
g.setColor(Color.blue);
g.setFont(f);
// Display lines in message
int firstBaseline = fm.getLeading() + fm.getAscent();
for (int i = 0; i < numLines; i++)
{
int lineWidth = fm.stringWidth(lines[i]);
g.drawString(lines[i],
borderSize + (maxLineWidth - lineWidth) / 2,
firstBaseline + i * height);
}
// Repaint frame
df.repaint();
}
}