Thursday, February 25, 2016

Word Search Puzzle Maker

This program creates word search puzzle. The result can be saved as an image file. The number of characters displayed is 14x14=196. 10 textfields are used to input words that will be searched.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import javax.imageio.ImageIO;

public class WordSearchMaker extends JFrame{
    JPanel p1=null;
    JPanel p2=null;
    JTextField tf1=null;
    JTextField tf2=null;
    JTextField tf3=null;
    JTextField tf4=null;
    JTextField tf5=null;
    JTextField tf6=null;
    JTextField tf7=null;
    JTextField tf8=null;
    JTextField tf9=null;
    JTextField tf10=null;
    JButton bcreate=null;
    JButton bsave=null;
    
    public WordSearchMaker(){
        super("Word Search Maker");
        setSize(1000,800);
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.insets=new Insets(5,10,0,0);
        
        p1=new JPanel();
        p1.setLayout(new GridBagLayout());
        gbc.gridy++;
        tf1=new JTextField(20);
        p1.add(tf1,gbc);
        gbc.gridy++;
        tf2=new JTextField(20);
        p1.add(tf2,gbc);
        gbc.gridy++;
        tf3=new JTextField(20);
        p1.add(tf3,gbc);
        gbc.gridy++;
        tf4=new JTextField(20);
        p1.add(tf4,gbc);
        gbc.gridy++;
        tf5=new JTextField(20);
        p1.add(tf5,gbc);
        gbc.gridy++;
        tf6=new JTextField(20);
        p1.add(tf6,gbc);
        gbc.gridy++;
        tf7=new JTextField(20);
        p1.add(tf7,gbc);
        gbc.gridy++;
        tf8=new JTextField(20);
        p1.add(tf8,gbc);
        gbc.gridy++;
        tf9=new JTextField(20);
        p1.add(tf9,gbc);
        gbc.gridy++;
        tf10=new JTextField(20);
        p1.add(tf10,gbc);
        gbc.gridy++;
        bcreate=new JButton("Create");
        bcreate.addActionListener(new ButtonHandler());
        p1.add(bcreate,gbc);
        gbc.gridy++;
        bsave=new JButton("Save");
        bsave.addActionListener(new ButtonHandler());
        p1.add(bsave,gbc);
        add("West",p1);
        
        p2=new DrawPanel();
        p2.setPreferredSize(new Dimension(700,700));
        p2.setBackground(Color.BLACK);
        add("East",p2);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class DrawPanel extends JPanel{
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            
            g2.setBackground(Color.LIGHT_GRAY);
            g2.clearRect(0,0,this.getWidth(),this.getHeight());
            
            Font f=new Font("Monospaced",Font.BOLD,14);
            FontMetrics fm=p2.getFontMetrics(f);
            g2.setColor(Color.BLACK);
            g2.setFont(f);
            
            String words[]=new String[10];
            words[0]=tf1.getText();
            words[1]=tf2.getText();
            words[2]=tf3.getText();
            words[3]=tf4.getText();
            words[4]=tf5.getText();
            words[5]=tf6.getText();
            words[6]=tf7.getText();
            words[7]=tf8.getText();
            words[8]=tf9.getText();
            words[9]=tf10.getText();
            
            for(int i=0;i<words.length;i++){
                if(words[i].length()>14){
                    return;
                }
            }
            
            String alphabet="abcdefghijklmnopqrstuvwxyz";            
            
            Character box[][]=new Character[14][14];
            for(int i=0;i<14;i++){
                for(int j=0;j<14;j++){
                    box[i][j]=null;
                }
            }
            
            Random rand=new Random();
            int counter=0;
            do{
                String st=words[counter];
                int stlength=st.length();
                int dir=rand.nextInt(4);
                boolean mark=false;
                //horizontal
                if(dir==0){
                    int x=rand.nextInt(14-stlength);
                    int y=rand.nextInt(14);
                    for(int i=0;i<stlength;i++){
                        if(box[x+i][y]==null||box[x+i][y]==st.charAt(i)){
                            mark=true;
                        }
                        else{
                            mark=false;
                            counter--;
                            break;
                        }
                    }
                    if(mark==true){
                        for(int i=0;i<stlength;i++){
                            box[x+i][y]=st.charAt(i);
                        }
                    }
                }
                //vertical
                if(dir==1){
                    int x=rand.nextInt(14);
                    int y=rand.nextInt(14-stlength);
                    for(int i=0;i<stlength;i++){
                        if(box[x][y+i]==null||box[x][y+i]==st.charAt(i)){
                            mark=true;
                        }
                        else{
                            mark=false;
                            counter--;
                            break;
                        }
                    }
                    if(mark==true){
                        for(int i=0;i<stlength;i++){
                            box[x][y+i]=st.charAt(i);
                        }
                    }
                }
                //diagonal
                if(dir==2){
                    int x=rand.nextInt(14-stlength);
                    int y=rand.nextInt(14-stlength);
                    for(int i=0;i<stlength;i++){
                        if(box[x+i][y+i]==null||box[x+i][y+i]==st.charAt(i)){
                            mark=true;
                        }
                        else{
                            mark=false;
                            counter--;
                            break;
                        }                        
                    }                    
                    if(mark==true){
                        for(int i=0;i<stlength;i++){
                            box[x+i][y+i]=st.charAt(i);
                        }
                    }                    
                }
                if(dir==3){
                    int x=rand.nextInt(14-stlength);
                    int y=rand.nextInt(14-stlength)+stlength;
                    for(int i=0;i<stlength;i++){
                        if(box[x+i][y-i]==null||box[x+i][y-i]==st.charAt(i)){
                            mark=true;
                        }
                        else{
                            mark=false;
                            counter--;
                            break;
                        }                        
                    }                    
                    if(mark==true){
                        for(int i=0;i<stlength;i++){
                            box[x+i][y-i]=st.charAt(i);
                        }
                    }                    
                }
                counter++;
            }while(counter<10);
            
            for(int i=0;i<14;i++){
                for(int j=0;j<14;j++){
                    if(box[i][j]==null){
                        box[i][j]=alphabet.charAt(rand.nextInt(26));
                    }
                }
            }
            
            for(int i=0;i<14;i++){
                for(int j=0;j<14;j++){
                    g2.drawString(box[i][j]+"",50+(30*i),50+(30*j));
                }
            }    
            
            counter=0;
            for(int i=0;i<2;i++){
                for(int j=0;j<5;j++){
                    g2.drawString(words[counter],50+(100*i),500+(30*j));
                    counter++;
                }
            }    
        }
    } 
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand()=="Create"){
                repaint();
            }
            if(e.getActionCommand()=="Save"){
                BufferedImage bufimage = new BufferedImage(p2.getWidth(),p2.getHeight(),BufferedImage.TYPE_INT_ARGB);
                Graphics2D g=(Graphics2D)bufimage.getGraphics();
                
                p2.paint(g);
                try {
                    ImageIO.write(bufimage,"png", new File("wordsearchpuzzle.png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }                 
            }
        }
    }
    
    public static void main(String[] args){
        WordSearchMaker ap=new WordSearchMaker();
    }
}

No comments:

Post a Comment