Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.applet.Applet;
- import java.awt.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- * ************************************************************************
- * Copyright Slendy (c) 2018. All Right Reserved.
- * Any code contained within this document, and any associated APIs with similar branding
- * are the sole property of Slendy. Distribution, reproduction, taking snippets, or
- * claiming any contents as your own will break the terms of the license, and void any
- * agreements with you, the third party.
- * Thanks
- * ************************************************************************
- */
- public class WordHunt extends Applet implements Runnable {
- private static ArrayList<String> words = new ArrayList<>();
- private TextField textField1;
- public static void main(String[] args) {
- File f = new File("words.txt");
- Scanner s = null;
- try {
- s = new Scanner(f);
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(null, "You need to create a file called words.txt in the same directory");
- }
- ArrayList<String> words = new ArrayList<>();
- while(s.hasNext()){
- words.add(s.nextLine());
- }
- s.close();
- WordHunt wordHunt = new WordHunt(words);
- wordHunt.setPreferredSize(new Dimension(800, 800));
- wordHunt.init();
- JFrame mainFrame = new JFrame();
- mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- mainFrame.getContentPane().add(wordHunt);
- mainFrame.pack();
- mainFrame.setVisible(true);
- wordHunt.start();
- }
- private WordHunt(ArrayList<String> _words){
- words = _words;
- }
- public void init() {
- setFocusable(true);
- Label label1 = new Label("Word: ");
- textField1 = new TextField("");
- textField1.setColumns(50);
- Thread t = new Thread(this);
- t.start();
- add(label1);
- add(textField1);
- }
- @Override
- public void run() {
- for(;;){
- try {
- updateText();
- repaint();
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public void paint(Graphics g){
- g.setFont(new Font("Arial", Font.PLAIN, 15));
- g.setColor(Color.black);
- int y = 60;
- int x = 0;
- int index = 0;
- int collumn = 0;
- int test = 0;
- for(String s : output.toString().split(";")){
- test++;
- index++;
- if(index > 45){
- index = 0;
- y = 60+15;
- collumn++;
- }
- g.drawString(s, (getWidth()/2 - (test > 1 ? 375 : 150)) + 85 * collumn, y);
- y+= 15;
- }
- }
- private Image i;
- public void update(Graphics g) {
- if ( i == null ) {
- i = createImage(getHeight(), getHeight());
- }
- Graphics g2 = i.getGraphics();
- g2.setColor(getBackground());
- g2.fillRect(0, 0, getWidth(), getHeight());
- g2.setColor(getForeground());
- paint(g2);
- g.drawImage(i, 0, 0, null);
- }
- private StringBuilder output = new StringBuilder();
- private void updateText(){
- String text = textField1.getText();
- ArrayList<String> possibleMatches = new ArrayList<>();
- for(String f : words){
- if(f.length() == text.length()){
- possibleMatches.add(f);
- }
- }
- ArrayList<String> matches = new ArrayList<>();
- for(String f : possibleMatches){
- int wildcards = 0;
- int matchingLetters = 0;
- for(int i = 0; i < f.length(); i++){
- if(text.charAt(i) != '_'){
- if (text.charAt(i) == f.charAt(i)) {
- matchingLetters++;
- }
- } else {
- wildcards++;
- }
- }
- if(wildcards == f.length()){
- matches.add(f);
- } else if(wildcards + matchingLetters == f.length()){
- matches.add(f);
- }
- }
- output = new StringBuilder();
- output.append("Found ").append(matches.size()).append(" matches;");
- for(String m : matches){
- output.append(m).append(";");
- }
- repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement