Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.applet.Applet;
  3. import java.awt.*;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8.  
  9. /**
  10. * ************************************************************************
  11. * Copyright Slendy (c) 2018. All Right Reserved.
  12. * Any code contained within this document, and any associated APIs with similar branding
  13. * are the sole property of Slendy. Distribution, reproduction, taking snippets, or
  14. * claiming any contents as your own will break the terms of the license, and void any
  15. * agreements with you, the third party.
  16. * Thanks
  17. * ************************************************************************
  18. */
  19. public class WordHunt extends Applet implements Runnable {
  20.  
  21. private static ArrayList<String> words = new ArrayList<>();
  22.  
  23. private TextField textField1;
  24.  
  25. public static void main(String[] args) {
  26. File f = new File("words.txt");
  27.  
  28. Scanner s = null;
  29. try {
  30. s = new Scanner(f);
  31. } catch (FileNotFoundException e) {
  32. JOptionPane.showMessageDialog(null, "You need to create a file called words.txt in the same directory");
  33. }
  34. ArrayList<String> words = new ArrayList<>();
  35. while(s.hasNext()){
  36. words.add(s.nextLine());
  37. }
  38. s.close();
  39. WordHunt wordHunt = new WordHunt(words);
  40. wordHunt.setPreferredSize(new Dimension(800, 800));
  41. wordHunt.init();
  42.  
  43. JFrame mainFrame = new JFrame();
  44. mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  45.  
  46. mainFrame.getContentPane().add(wordHunt);
  47. mainFrame.pack();
  48. mainFrame.setVisible(true);
  49.  
  50. wordHunt.start();
  51. }
  52.  
  53. private WordHunt(ArrayList<String> _words){
  54. words = _words;
  55. }
  56.  
  57. public void init() {
  58. setFocusable(true);
  59. Label label1 = new Label("Word: ");
  60. textField1 = new TextField("");
  61. textField1.setColumns(50);
  62. Thread t = new Thread(this);
  63. t.start();
  64.  
  65.  
  66. add(label1);
  67. add(textField1);
  68. }
  69.  
  70. @Override
  71. public void run() {
  72. for(;;){
  73. try {
  74.  
  75. updateText();
  76. repaint();
  77. Thread.sleep(10);
  78. } catch (InterruptedException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. }
  83.  
  84. public void paint(Graphics g){
  85. g.setFont(new Font("Arial", Font.PLAIN, 15));
  86. g.setColor(Color.black);
  87. int y = 60;
  88. int x = 0;
  89. int index = 0;
  90. int collumn = 0;
  91. int test = 0;
  92. for(String s : output.toString().split(";")){
  93. test++;
  94. index++;
  95. if(index > 45){
  96. index = 0;
  97. y = 60+15;
  98. collumn++;
  99. }
  100.  
  101. g.drawString(s, (getWidth()/2 - (test > 1 ? 375 : 150)) + 85 * collumn, y);
  102. y+= 15;
  103. }
  104. }
  105.  
  106. private Image i;
  107.  
  108. public void update(Graphics g) {
  109. if ( i == null ) {
  110. i = createImage(getHeight(), getHeight());
  111. }
  112. Graphics g2 = i.getGraphics();
  113.  
  114. g2.setColor(getBackground());
  115. g2.fillRect(0, 0, getWidth(), getHeight());
  116.  
  117. g2.setColor(getForeground());
  118. paint(g2);
  119. g.drawImage(i, 0, 0, null);
  120. }
  121.  
  122.  
  123.  
  124. private StringBuilder output = new StringBuilder();
  125.  
  126. private void updateText(){
  127. String text = textField1.getText();
  128. ArrayList<String> possibleMatches = new ArrayList<>();
  129. for(String f : words){
  130. if(f.length() == text.length()){
  131. possibleMatches.add(f);
  132. }
  133. }
  134. ArrayList<String> matches = new ArrayList<>();
  135. for(String f : possibleMatches){
  136. int wildcards = 0;
  137. int matchingLetters = 0;
  138. for(int i = 0; i < f.length(); i++){
  139. if(text.charAt(i) != '_'){
  140. if (text.charAt(i) == f.charAt(i)) {
  141. matchingLetters++;
  142. }
  143. } else {
  144. wildcards++;
  145. }
  146. }
  147.  
  148. if(wildcards == f.length()){
  149. matches.add(f);
  150. } else if(wildcards + matchingLetters == f.length()){
  151. matches.add(f);
  152. }
  153. }
  154. output = new StringBuilder();
  155. output.append("Found ").append(matches.size()).append(" matches;");
  156. for(String m : matches){
  157. output.append(m).append(";");
  158. }
  159. repaint();
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement