Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file chooser to GUI #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist/
4 changes: 4 additions & 0 deletions build/built-jar.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Sun, 30 Oct 2016 13:30:07 +0530


D\:\\OSS\\Hacktoberfest\\Random-Text-Generator=
Binary file added build/classes/random/text/generator/Gui$1.class
Binary file not shown.
Binary file added build/classes/random/text/generator/Gui$2.class
Binary file not shown.
Binary file added build/classes/random/text/generator/Gui.class
Binary file not shown.
Binary file modified build/classes/random/text/generator/RandomTextGenerator.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion nbproject/private/private.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
compile.on.save=true
user.properties.file=C:\\Users\\Ma Jing\\AppData\\Roaming\\NetBeans\\8.1\\build.properties
user.properties.file=C:\\Users\\Work\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
6 changes: 5 additions & 1 deletion nbproject/private/private.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/Ma%20Jing/Documents/NetBeansProjects/Random%20Text%20Generator/src/random/text/generator/SpaceEfficientGenerator.java</file>
<file>file:/D:/OSS/Hacktoberfest/Random-Text-Generator/src/random/text/generator/SpaceEfficientGenerator.java</file>
<file>file:/D:/OSS/Hacktoberfest/Random-Text-Generator/src/random/text/generator/SpeedEfficientGenerator.java</file>
<file>file:/D:/OSS/Hacktoberfest/Random-Text-Generator/src/random/text/generator/RandomTextGenerator.java</file>
<file>file:/D:/OSS/Hacktoberfest/Random-Text-Generator/src/random/text/generator/WordSet.java</file>
<file>file:/D:/OSS/Hacktoberfest/Random-Text-Generator/src/random/text/generator/Gui.java</file>
</group>
</open-files>
</project-private>
61 changes: 53 additions & 8 deletions src/random/text/generator/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class Gui {
private RandomTextGenerator generator;
//private static final int DEFAULT_WORD_AMOUNT;

public Gui() {
init();
}
Expand All @@ -28,18 +34,57 @@ public void init() {
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
frame.add(panel);

JLabel label = new JLabel("Click the button on the bottom!");
label.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(label);

JTextArea textField = new JTextArea(5, 5);
textField.setText("Not randomly generated text.");
textField.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(textField);
JLabel headerLabel = new JLabel("Click the button on the bottom!");
headerLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(headerLabel);

JComboBox<String> generatorSelectBox = new JComboBox<>();
generatorSelectBox.addItem("SpaceEfficientGenerator");
generatorSelectBox.addItem("SpeedEfficientGenerator");
generatorSelectBox.setAlignmentX(Component.RIGHT_ALIGNMENT);
panel.add(generatorSelectBox);

JTextArea inputTextArea = new JTextArea(5, 5);
inputTextArea.setText(RandomTextGenerator.getDefaultText());
inputTextArea.setAlignmentX(Component.CENTER_ALIGNMENT);
inputTextArea.setLineWrap(true);
inputTextArea.setWrapStyleWord(true);
panel.add(inputTextArea);

JTextArea outputTextArea = new JTextArea(5, 5);
outputTextArea.setText("Result");
outputTextArea.setEditable(false);
outputTextArea.setAlignmentX(Component.CENTER_ALIGNMENT);
outputTextArea.setLineWrap(true);
outputTextArea.setWrapStyleWord(true);
panel.add(outputTextArea);

JButton generateTextButton = new JButton("Generate text");
generateTextButton.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(generateTextButton);

JLabel executionTimeLabel = new JLabel();
panel.add(executionTimeLabel);

generateTextButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String generatorType = (String)generatorSelectBox.getSelectedItem();
String inputText = inputTextArea.getText();
switch(generatorType){
case "SpeedEfficientGenerator":
generator = new SpeedEfficientGenerator(inputText);
break;
default:
generator = new SpaceEfficientGenerator(inputText);
}
long startTime = System.currentTimeMillis();
outputTextArea.setText(generator.generateText(200));
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
executionTimeLabel.setText("Time taken: " + elapsedTime + "ms");
}
});

frame.pack();
frame.setLocationRelativeTo(null);
Expand Down
20 changes: 19 additions & 1 deletion src/random/text/generator/RandomTextGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,32 @@
* to form a new text. This technique is called the Markov chain.
*/
public abstract class RandomTextGenerator {

protected static final String defaultText = "She is into superstitions, black cats and voodoo dolls. "
+ "I feel a premonition that girl is gonna make me fall. "
+ "She is into new sensations, new kicks in the candle light. "
+ "She has got a new addiction for every day and night. "
+ "She will make you take your clothes off and go dancing in the rain. "
+ "She will make you live her crazy life but she will take away your pain like a bullet to your brain. "
+ "Come On! Upside, inside out she is livin la vida loca. "
+ "She will push and pull you down, livin la vida loca. "
+ "Her lips are devil red and her skin is the color mocha. "
+ "She will wear you out livin la vida loca. Come On! Livin la vida loca, Come on! "
+ "She is livin la vida loca. Woke up in New York City in a funky cheap hotel. "
+ "She took my heart and she took my money. She must have slipped me a sleeping pill. "
+ "She never drinks the water and makes you order French Champagne. "
+ "Once you have had a taste of her you will never be the same. "
+ "Yeah, she will make you go insane.";
public final String[] text;
public final int SETSIZE = 3;

public RandomTextGenerator(String text) {
this.text = text.split("\\s+");
}

public static String getDefaultText(){
return defaultText;
}

public String generateText(int wordAmount) {
if (wordAmount <= SETSIZE) return null;

Expand Down
15 changes: 0 additions & 15 deletions src/random/text/generator/SpaceEfficientGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@ public ArrayList<String> getFollowingWords(WordSet set) {
* @param args the command line arguments
*/
public static void main(String[] args) {
final String defaultText = "She is into superstitions, black cats and voodoo dolls. "
+ "I feel a premonition that girl is gonna make me fall. "
+ "She is into new sensations, new kicks in the candle light. "
+ "She has got a new addiction for every day and night. "
+ "She will make you take your clothes off and go dancing in the rain. "
+ "She will make you live her crazy life but she will take away your pain like a bullet to your brain. "
+ "Come On! Upside, inside out she is livin la vida loca. "
+ "She will push and pull you down, livin la vida loca. "
+ "Her lips are devil red and her skin is the color mocha. "
+ "She will wear you out livin la vida loca. Come On! Livin la vida loca, Come on! "
+ "She is livin la vida loca. Woke up in New York City in a funky cheap hotel. "
+ "She took my heart and she took my money. She must have slipped me a sleeping pill. "
+ "She never drinks the water and makes you order French Champagne. "
+ "Once you have had a taste of her you will never be the same. "
+ "Yeah, she will make you go insane.";
SpaceEfficientGenerator gen = new SpaceEfficientGenerator(defaultText);
System.out.println(gen.generateText(100));
}
Expand Down
15 changes: 0 additions & 15 deletions src/random/text/generator/SpeedEfficientGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,6 @@ public ArrayList<String> getFollowingWords(WordSet set) {
* @param args the command line arguments
*/
public static void main(String[] args) {
final String defaultText = "She is into superstitions, black cats and voodoo dolls. "
+ "I feel a premonition that girl is gonna make me fall. "
+ "She is into new sensations, new kicks in the candle light. "
+ "She has got a new addiction for every day and night. "
+ "She will make you take your clothes off and go dancing in the rain. "
+ "She will make you live her crazy life but she will take away your pain like a bullet to your brain. "
+ "Come On! Upside, inside out she is livin la vida loca. "
+ "She will push and pull you down, livin la vida loca. "
+ "Her lips are devil red and her skin is the color mocha. "
+ "She will wear you out livin la vida loca. Come On! Livin la vida loca, Come on! "
+ "She is livin la vida loca. Woke up in New York City in a funky cheap hotel. "
+ "She took my heart and she took my money. She must have slipped me a sleeping pill. "
+ "She never drinks the water and makes you order French Champagne. "
+ "Once you have had a taste of her you will never be the same. "
+ "Yeah, she will make you go insane.";
SpeedEfficientGenerator gen = new SpeedEfficientGenerator(defaultText);
// Uncomment the line below to print all the WordSets and their following words
// gen.printWordMap();
Expand Down