From: Francois Fleuret Date: Sat, 24 Aug 2013 09:16:15 +0000 (+0200) Subject: Initial commit X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=2ef8b0fca1590fa0722b453a5af3408c353137fd;p=crosswords.git Initial commit --- 2ef8b0fca1590fa0722b453a5af3408c353137fd diff --git a/Grid.class b/Grid.class new file mode 100644 index 0000000..d902c49 Binary files /dev/null and b/Grid.class differ diff --git a/Square.class b/Square.class new file mode 100644 index 0000000..ffcb46f Binary files /dev/null and b/Square.class differ diff --git a/Words.class b/Words.class new file mode 100644 index 0000000..2f6ab99 Binary files /dev/null and b/Words.class differ diff --git a/Words.java b/Words.java new file mode 100644 index 0000000..8b3c0ef --- /dev/null +++ b/Words.java @@ -0,0 +1,611 @@ +/****************************************************************************** + + Cross-Words applet v1.5 (c) Francois FLEURET + + This small applet is an interactive crosswords grid for web + pages. Mail for bug reports and + questions. + + In the past, I put his applet under a pure GPL licence. So, what was + expected to happen happened : commercial sites used it and I saw + people earning money with my work without giving me a single + buck. This is bad. So, now this software is distributed under the FFL + (Francois Fleuret Licence) which is the same as GPL version 2, plus + the following paragraph: + + * + * RESTRICTION OF USE FOR APPLET SOFTWARES + * + * 13 The site the applet is used on must be a NON-PROFIT site, which + * implies it does not contain banners or any other mean to make money + * (including indirectly, for example as an ISP portal). The name of + * all the authors of the software and a link to the URL they provide + * in the source code must appear on any web page that uses the + * applet. If the source code was modified, the modified source must + * be downloadable from the site the applet is used on. + * + + So, if you use this applet on a non-profit site, please add a link to + http://www-rocq.inria.fr/~fleuret and a short note indicating my name + as the author of the applet. For any other usage, please contact + me. Be sure we can make a deal for a reasonnable price. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + Sept 11th 2000, François Fleuret + +******************************************************************************/ + +import java.applet.Applet; +import java.awt.*; +import java.awt.event.*; +import java.lang.Object; + +class Square { + char solution; + char found; + boolean ajour; + boolean holdCursor; + boolean locked; + Color color; + + Square(char c) { + solution = c; + found = ' '; + ajour = false; + locked = false; + color = Color.black; + } + + void setFound(char c) { if(c != found) { found = c; ajour = false; } } + + void putCursor() { if(!holdCursor) ajour = false; holdCursor = true; } + + void remCursor() { if(holdCursor) ajour = false; holdCursor = false; } + + void lock(Color c) { + if(!locked) { + ajour = false; + locked = true; + color = c; + } + } +} + +class Grid extends Panel implements KeyListener, MouseListener { + int gridWidth, gridHeight; + int xCursor, yCursor; + int nbLetters, nbFound; + boolean cursorHorizontal; + + int xMargin, yMargin; + Square grid[][]; + Font font, smallFont; + FontMetrics fontMetrics, smallFontMetrics; + int squareSize; + + TextArea definitionArea; + String[][][] definitions; + + Grid(Font f, Font sf, + FontMetrics fm, FontMetrics sfm, + int w, int h, + String content, + TextArea ta, String def) { + + int x, y, xx, yy, k; + + font = f; fontMetrics = fm; + smallFont = sf; smallFontMetrics = sfm; + squareSize = fontMetrics.getHeight(); + + gridWidth = w; + gridHeight = h; + grid = new Square[gridWidth][gridHeight]; + + yMargin = smallFontMetrics.getHeight(); + xMargin = yMargin; + + nbLetters = 0; nbFound = 0; + k = 0; + for(y = 0; y 0) && ((def.indexOf(':', i) > j) || (def.indexOf(':', i) < 0))) { + direction = def.substring(i, j).toLowerCase(); + j++; + } else j = i; + + + i = j; + y = 0; + while((def.charAt(i) >= 'a') && (def.charAt(i) <= 'z')) + y = y*26 + ((int) def.charAt(i++)) - 'a'; + + x = 0; + while((def.charAt(i) >= '0') && (def.charAt(i) <= '9')) + x = x*10 + ((int) def.charAt(i++)) - '0'; + x--; + i++; + + j = def.indexOf('/', i); + if(j < 0) j = def.length(); + sd = def.substring(i, j); + j++; + + if(direction.equals("horizontal")) { + for(xx = x; (xx>=0) && (grid[xx][y].solution != '.'); xx--) + definitions[xx][y][0] = sd; + for(xx = x; (xx=0) && (grid[x][yy].solution != '.'); yy--) + definitions[x][yy][1] = sd; + for(yy = y; (yy=0) && (grid[xx][y].solution != '.'); xx--) { + grid[xx][y].setFound(grid[xx][y].solution); + grid[xx][y].lock(Color.red); + } + + for(xx = x; (xx=0) && (grid[x][yy].solution != '.'); yy--) { + grid[x][yy].setFound(grid[x][yy].solution); + grid[x][yy].lock(Color.red); + } + + for(yy = y; (yy=0) && (grid[xmin][y].solution != '.'); xmin--) + ok &= (grid[xmin][y].solution == grid[xmin][y].found); + + for(xmax = x; ok && (xmax=0) && (grid[x][ymin].solution != '.'); ymin--) + ok &= (grid[x][ymin].solution == grid[x][ymin].found); + + for(ymax = y; ok && (ymax 0) && (grid[xCursor][y].solution == '.')) y--; + if((y >= 0) && (grid[xCursor][y].solution != '.')) { + grid[xCursor][yCursor].remCursor(); + yCursor = y; + grid[xCursor][yCursor].putCursor(); + } + } + else if(code == KeyEvent.VK_DOWN) { + y = yCursor+1; + while((y < gridHeight-1) && (grid[xCursor][y].solution == '.')) y++; + if((y < gridHeight) && (grid[xCursor][y].solution != '.')) { + grid[xCursor][yCursor].remCursor(); + yCursor = y; + grid[xCursor][yCursor].putCursor(); + } + } + else if(code == KeyEvent.VK_LEFT) { + x = xCursor-1; + while((x > 0) && (grid[x][yCursor].solution == '.')) x--; + if((x >= 0) && (grid[x][yCursor].solution != '.')) { + grid[xCursor][yCursor].remCursor(); + xCursor = x; + grid[xCursor][yCursor].putCursor(); + } + } + else if(code == KeyEvent.VK_RIGHT) { + x = xCursor+1; + while((x < gridWidth-1) && (grid[x][yCursor].solution == '.')) x++; + if((x < gridWidth) && (grid[x][yCursor].solution != '.')) { + grid[xCursor][yCursor].remCursor(); + xCursor = x; + grid[xCursor][yCursor].putCursor(); + } + } + else if(code == Event.ENTER) { + cursorHorizontal = !cursorHorizontal; + grid[xCursor][yCursor].ajour = false; + } + else if(code == Event.BACK_SPACE) { + if((grid[xCursor][yCursor].found == ' ') || grid[xCursor][yCursor].locked) { + if(cursorHorizontal) { + if(xCursor > 0) + if(grid[xCursor - 1][yCursor].solution != '.') { + grid[xCursor][yCursor].remCursor(); + xCursor--; + grid[xCursor][yCursor].putCursor(); + } + } else { + if(yCursor > 0) + if(grid[xCursor][yCursor - 1].solution != '.') { + grid[xCursor][yCursor].remCursor(); + yCursor--; + grid[xCursor][yCursor].putCursor(); + } + } + } + + if(!grid[xCursor][yCursor].locked) { + if(grid[xCursor][yCursor].found == grid[xCursor][yCursor].solution) + nbFound--; + grid[xCursor][yCursor].setFound(' '); + } + } else if(key == ' ') { + if(definitionArea != null) { + if(cursorHorizontal) definitionArea.setText(definitions[xCursor][yCursor][0]); + else definitionArea.setText(definitions[xCursor][yCursor][1]); + } + } else if((key >= 'a') && (key <= 'z')) { + if(!grid[xCursor][yCursor].locked) { + if(grid[xCursor][yCursor].found == grid[xCursor][yCursor].solution) + nbFound--; + grid[xCursor][yCursor].setFound((char) key); + if(grid[xCursor][yCursor].found == grid[xCursor][yCursor].solution) + nbFound++; + } + + if(cursorHorizontal) { + if(xCursor < gridWidth-1) + if(grid[xCursor + 1][yCursor].solution != '.') { + grid[xCursor][yCursor].remCursor(); + xCursor++; + grid[xCursor][yCursor].putCursor(); + } + } else { + if(yCursor < gridHeight-1) + if(grid[xCursor][yCursor + 1].solution != '.') { + grid[xCursor][yCursor].remCursor(); + yCursor++; + grid[xCursor][yCursor].putCursor(); + } + } + + if(nbFound == nbLetters) { + for(y = 0; y= gridWidth) xc = gridWidth-1; + yc = (mouseEvent.getY()-yMargin)/squareSize; + if(yc < 0) yc = 0; + if(yc >= gridHeight) yc = gridHeight-1; + + if(grid[xc][yc].solution != '.') { + grid[xCursor][yCursor].remCursor(); + xCursor = xc; yCursor = yc; + grid[xCursor][yCursor].putCursor(); + } + + repaint(); + } + + public void mouseClicked(MouseEvent mouseEvent) {} + public void mouseReleased(MouseEvent e) {} + public void mouseEntered(MouseEvent e) {} + public void mouseExited(MouseEvent e) {} + + public Dimension getPreferredSize() { + return new Dimension(xMargin + gridWidth*squareSize + 1, + yMargin + gridHeight*squareSize + 1); + } + +} + +public class Words extends Applet implements ActionListener { + Grid grid; + Button checkButton, giveButton; + TextArea definitionArea; + + public void init() { + String fontName, s, definitionString; + int fontSize; + + Font f, sf; + FontMetrics fm, sfm; + int w, h, wd, hd; + + s = getParameter("bgcolor"); + if(s != null) + setBackground(new Color(Integer.parseInt(s))); + + fontName = getParameter("font"); + if(fontName == null) fontName = "Courier"; + + s = getParameter("fontsize"); + if(s == null) fontSize = 48; else fontSize = Integer.parseInt(s); + + f = new Font(fontName, Font.PLAIN, fontSize); + + fm = getGraphics().getFontMetrics(f); + + fontName = getParameter("smallfont"); + if(fontName == null) fontName = "Courier"; + + s = getParameter("smallfontsize"); + if(s == null) fontSize = 20; else fontSize = Integer.parseInt(s); + + sf = new Font(fontName, Font.ITALIC, fontSize); + sfm = getGraphics().getFontMetrics(sf); + + s = getParameter("gridwidth"); + if(s == null) w = 1; else w = Integer.parseInt(s); + + s = getParameter("gridheight"); + if(s == null) h = 1; else h = Integer.parseInt(s); + + definitionString = null; + + s = getParameter("definitions"); + + if(s != null) { + int i, j; + wd = -1; hd = -1; + + // Find the width and the height + i = 0; j = s.indexOf('x', i); + wd = Integer.parseInt(s.substring(i, j)); + i = j+1; j = s.indexOf('/', i); + hd = Integer.parseInt(s.substring(i, j)); + + i = j+1; definitionString = s.substring(i, s.length()); + + if((wd > 0) && (hd > 0)) { + definitionArea = new TextArea("", hd, wd, TextArea.SCROLLBARS_NONE); + definitionArea.setEditable(false); + } + } + + s = getParameter("content"); if(s == null) s = "."; + grid = new Grid(f, sf, fm, sfm, w, h, s, definitionArea, definitionString); + add("north", grid); + + s = getParameter("checkwordlabel"); + if(s != null) { + checkButton = new Button(s); + checkButton.addActionListener(this); + } + + s = getParameter("givewordlabel"); + if(s != null) { + giveButton = new Button(s); + giveButton.addActionListener(this); + } + + if((checkButton != null) || (giveButton != null)) { + Panel buttonPanel; + buttonPanel = new Panel(); + if(checkButton != null) buttonPanel.add(checkButton); + if(giveButton != null) buttonPanel.add(giveButton); + if(definitionArea != null) add("south", definitionArea); + add("south", buttonPanel); + } + } + + public void actionPerformed(ActionEvent event) { + Object source = event.getSource(); + if(source == checkButton) grid.checkWord(); + else if(source == giveButton) grid.giveWord(); + } + + public String getAppletInfo() { + return "Word applet v1.5\n" + + "Written and (c) Francois Fleuret, mail to \n" + + "Check http://www-rocq.inria.fr/~fleuret\n"; + } + + public String[][] getParameterInfo() { + String chose[][] = { + { "bgcolor", "integer", "Background Color (optional)" }, + { "font", "font name", "Grid character font name (optional)" }, + { "fontsize", "integer", "Grid character font size (optional)" }, + { "smallfont", "font name", "Row and column index font name (optional)" }, + { "smallfontsize", "integer", "Row and column index font size (optional)" }, + { "gridwidth", "integer", "Grid width" }, + { "gridheight", "integer", "Grid height" }, + { "content", "string", "Grid content" }, + { "definitions", "string", "Definitions (optional)" }, + { "checkwordlabel", "string", "Label for the 'check word' button (optional)" }, + { "givewordlabel", "string", "Label for the 'give word' button (optional)" } + }; + + return chose; + } +} +