Question

In: Computer Science

Create a java Swing GUI application that presents the user with a “fortune”. Create a java...

Create a java Swing GUI application that presents the user with a “fortune”.

  1. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components:
    1. Top panel:

      A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a constructor that takes a String and the ImageIcon. Figure out from the Swing API or the content in Canvas how to display the text either above or below the ImageIcon.) Select a font face that works with your image and set the size to a larger value (try 36 and 48). Note that you have to add the image file to your Netbeans project directory.
    2. Middle panel:

      A JTextArea within a JScrollPane where the fortunes will be displayed one per line. Again, set the font values so that it works. (Should be smaller than the large text of the Top panel.)
    3. Bottom panel:

      A button with the label “Read My Fortune!”.

A button with the label “Quit”

  1. Create separate fonts for the title, buttons and the fortune display.
  2. I recommend that you model your program on the Big Java examples that we have been following in our Lab practices. The FortuneTellerFrame class inherits from JFrame. The minimal code in the java main class viewer just creates an instance of FortuneTellerFrame and sets the Frame stats.
  3. Create a String array(List) of at least 12 humorous fortunes. When the user clicks on the “Read My Fortune” button your program should randomly select a fortune from your array and append it to the display in the ScrollPanel TextArea. Make sure that you implement the constraint that the program will never return the same fortune as the last one presented. To be clear, the program will repeat fortunes just not the same one twice in a row. (Hint: remember the index of the current fortune and when you generate a new random index, make sure that it is different from the previous one.)

    Note that the textArea will display all the fortunes that the user receives one after another and then will be scrollable.
  4. Use a reasonable visually pleasing arrangement of your components using BorderLayout. Following the example that I have posted in Canvas, get an instance of the Toolkit and set your main JFrame to be ¾ of the width of the display and centered on the screen. (The example is in the Canvas Course Documents folder that contains the Java GUI materials.)
  5. Make sure that you use the new Java 8 Lambda Expressions for the actionlistener code for the buttons.
  6. Test your program thoroughly. Get a screen capture shot that shows at least 5 fortunes in the scrollpane/textarea. Paste the shot here:

Solutions

Expert Solution

Working code implemented in Java and appropriate comments provided for better understanding.

Here I am attaching code for all files:

FortuneTellerFrame.java:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import static javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;

public class FortuneTellerFrame extends JFrame
{
JPanel top, middle, bottom, main;
JLabel topLbl, bottomLbl;
JButton actionBtn, quitBtn;
JTextArea textArea;
JScrollPane scroller;
JScrollBar verticle;
ImageIcon icon;
ArrayList<String> fortunes = new ArrayList<>();
ArrayList<Integer> repeatChecker = new ArrayList<>();

public int index;
  
public FortuneTellerFrame()
{
super("Fortune Teller");
main = new JPanel();
createTopPanel();
createMiddlePanel();
createBottomPanel();
loadFortunes();
  
// Now add sub panels to main
main.setLayout(new BorderLayout());
main.add(top,BorderLayout.NORTH);
main.add(middle,BorderLayout.CENTER);
main.add(bottom,BorderLayout.SOUTH);
  
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
  
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
  
setSize(3 * (screenSize.width / 4), 3 * (screenSize.height / 4));
setLocationRelativeTo(null);
  
  
add(main);
  
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
  
private void loadFortunes()
{
fortunes.add("Silence is golden, ducktape is silver.");
fortunes.add("Friends help you move a couch, best friends help you move a body.");
fortunes.add("You will meet the significant other or your dreams...Never!");
fortunes.add("Tomorrrow you will wake up....what? Did you want something else? You asked for a fortune, not a meaningful one.");
fortunes.add("You will see the light, just not the one you are expecting.");
fortunes.add("Meh....try again later.");
fortunes.add("Hearty laughter is a good way to jog internally without having to go outdoors.");
fortunes.add("The fortune you seek is not here.");
fortunes.add("A palm can say much, especially when it smacks.");
fortunes.add("Today might be a huge improvement over yesterday.");
fortunes.add("Never tease an armed midget witha high five.");
fortunes.add("You will__________ insert quarter for rest of fortune.");
}
  
private void createTopPanel()   
{
top = new JPanel();
icon = new ImageIcon("fortuneTeller.png");
topLbl = new JLabel("Fortune Teller", icon, SwingConstants.CENTER);
topLbl.setFont(new Font("Comic Sans MS",Font.PLAIN, 36));
top.add(topLbl);
}
  
private void createMiddlePanel()
{
middle = new JPanel();
textArea = new JTextArea(10, 50);
scroller = new JScrollPane(textArea);

scroller.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scroller.setPreferredSize(new Dimension(700, 400));
// scroller.setAlignmentX(CENTER_ALIGNMENT);
// verticle = scroller.getVerticalScrollBar();
// verticle.setValue(verticle.getMaximum());

middle.setFont(new Font("Bradley Hand ITC", Font.ITALIC, 20));

middle.add(scroller);
}
  
private void createBottomPanel()
{
bottom = new JPanel();
bottomLbl = new JLabel();
  
actionBtn = new JButton("Read My Fortune!");
actionBtn.addActionListener((ActionEvent ae) -> {
mixFortunes();
});
  
quitBtn = new JButton("Quit");
quitBtn.addActionListener((ActionEvent ae) -> {
System.exit(0);
});
  
bottom.setFont(new Font("Bradley Hand ITC", Font.ITALIC, 12));
bottom.add(actionBtn);
bottom.add(quitBtn);
}

public void mixFortunes() throws ArrayIndexOutOfBoundsException
{
Random random = new Random();
int previousNum;
  
if(repeatChecker.size() > 1)
{
previousNum = repeatChecker.size() -1;
}
else
{
previousNum = 0;
}
  
while (true)
{
index = random.nextInt(fortunes.size());
repeatChecker.add(index);
if (index != repeatChecker.get(previousNum)) break;
}
  
textArea.append(fortunes.get(index) + "\n");
// verticle.setValue(verticle.getMaximum());
}
}

FortuneTellerViewer.java:


import javax.swing.JFrame;

public class FortuneTellerViewer {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
JFrame frame = new FortuneTellerFrame();
frame.setVisible(true);
}
  
}

Sample Output Screenshots:


Related Solutions

Create in java an interactive GUI application program that will prompt the user to use one...
Create in java an interactive GUI application program that will prompt the user to use one of three calculators, label project name MEDCALC. You can use any color you want for your background other than grey. Be sure to label main java box with “MEDCALC” and include text of “For use only by students” On all three calculators create an alert and signature area for each user. The alert should be something like “All calculations must be confirmed by user...
Use the below info to create a java program A GUI interface to ensure a user...
Use the below info to create a java program A GUI interface to ensure a user is old enough to play a game. Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this. Instructions to ensure that the information is displayed back...
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates...
***IN C# ONLY, USING WINDOWS FORMS*** --NO JAVA--. Create a GUI application in C# that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide: • Number of days on the trip • Amount of airfare, if any • Amount of car rental fees, if any • Number of miles driven, if a private vehicle was used • Amount of parking fees, if any • Amount of...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user to type in a set of input. Below that textfield have the following controls to show string manipulations: (1) A button that will change the entire textfield’s current text to uppercase. (2) A button with its own textfield for a search value, that will tell the position the search value appears in the textfield above. (3) A button that reports the current number of...
Create a Java application that will prompt the user for the first name of 6 friends...
Create a Java application that will prompt the user for the first name of 6 friends in any order and store them in an array. First, output the array unsorted. Next, sort the array of friends and then output the sorted array to the screen. Be sure to clearly label the output. See the example program input and output shown below. It does not have to be exactly as shown in the example, however it gives you an idea of...
Create an application that asks a user to answer 5 math questions. JAVA
Create an application that asks a user to answer 5 math questions. JAVA
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
Develop a JavaFX GUI application called Registration that implements a user interface for registering for a...
Develop a JavaFX GUI application called Registration that implements a user interface for registering for a web site. The application should have labeled text fields for the Full Name, User Name, Password, Student Id, and a TextAreafor About Me. Include a button labeled Send. When the Send button is clicked, your program should print the contents of all fields (with labels) to standard output using println()statements.
In java, Create a GUI which works as an accumulator 1. There is a textfield A...
In java, Create a GUI which works as an accumulator 1. There is a textfield A which allows user to enter a number 2. There is also another textfield B with value start with 0. 3. When a user is done with entering the number in textfield A and press enter, calculate the sum of the number in textfield B and the number user just entered in textfield A, and display the updated number in textfield B
In C# Create a GUI application that calculates and displays the total travel expenses of a...
In C# Create a GUI application that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide: Number of days on the trip Amount of airfare, if any Amount of car rental fees, if any Number of miles driven, if a private vehicle was used Amount of parking fees, if any Amount of taxi charges, if any Conference or seminar registration fees, if any Lodging charges, per...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT