import java.applet.*; import java.awt.*; import java.net.*; import java.util.*; import java.lang.*; /** * A Java applet that simulates the chorded keyboard of * Merlin 4.7 */ public class ChordSim extends Applet { protected Image background; protected Image bUp, bDown; protected boolean bA, bE, bI, bO; protected Image chordTips[] = new Image[10]; protected String codeTab[][] = { {"c","m","n","r","s","u","v","w","x","z"}, {"g","j","p","q","y",",",".",";","[","]"}, {"b","d","f","h","k","l","t","shift","space","symbol"}, {"1","2","3","4","5","6","7","8","9","0"}, {"'","`","/","\\","=","-"," "," "," "," "}, {"C","M","N","R","S","U","V","W","X","Z"}, {"G","J","P","Q","Y","<",">",":","{","}"}, {"B","D","F","H","K","L","T","shift lock","space","shift symbol"}, {"!","@","#","$","%","^","&","*","(",")"}, {"\"","~","?","|","+","_"," "," "," "," "} }; protected int chords[] = {0, // none 0, // o 0, // i 3, // io 0, // e 5, // eo 2, // ei 8, // eio 0, // a 6, // ao 4, // ai 9, // aio 1, // ae 10, // aeo 7, // aei 0}; // aeio protected int currentCol; protected boolean sample; protected boolean slock; protected String scratch[] = { "Welcome to the Chord Simulator!", " by Jecel Assumpcao Junior", "", "testing: ", "", "", "", "", "", "", "", "", "" }; protected int currentRow; protected String prefix; protected boolean redrawAll, redrawKeys, redrawStatus, redrawLine; protected boolean loaded; public void init() { background = this.getImage(this.getDocumentBase(),"merlin4a.gif"); bUp = this.getImage(this.getDocumentBase(),"buttonUp.gif"); bDown = this.getImage(this.getDocumentBase(),"buttonDown.gif"); bA = false; bE = false; bI = false; bO = false; currentCol = -1; sample = true; slock = false; currentRow = 3; prefix = ""; redrawAll = true; redrawKeys = false; redrawStatus = false; redrawLine = false; loaded = false; for (int i = 0; i < 10; ++i) { chordTips[i] = this.getImage(this.getDocumentBase(), "chord"+Integer.toString(i+1)+".jpeg"); }; } protected int keys() { int k = 0; if (bA) k = k + 8; if (bE) k = k + 4; if (bI) k = k + 2; if (bO) k = k + 1; return k; } public void paint(Graphics g) { redrawAll = true; update(g); }; public void update(Graphics g) { if (!loaded) redrawAll = true; if (redrawAll) g.drawImage(background, 0, 0, this); g.setColor(Color.white); if (redrawLine) g.fillRect(188, 130+20*currentRow, 235, 20); if (redrawStatus) g.fillRect(170, 405, 250, 50); g.setColor(Color.black); if (redrawKeys || redrawAll) { g.drawImage(bA?bDown:bUp, 109, 104, this); g.drawImage(bE?bDown:bUp, 109, 209, this); g.drawImage(bI?bDown:bUp, 109, 319, this); g.drawImage(bO?bDown:bUp, 109, 425, this); }; if (redrawLine || redrawAll) { for (int i = 0; i < 13; ++i ) { if (i == currentRow || redrawAll) g.drawString(scratch[i], 188, 150+20*i); if (i == currentRow) { g.setColor(Color.red); g.drawString(prefix, 188+g.getFontMetrics().stringWidth(scratch[i]), 150+20*i); g.setColor(Color.black); } }; }; if (redrawStatus || redrawAll) { if ( currentCol >= 0 ) { for (int i = 0; i < 10; ++i) { g.drawImage(chordTips[i], 180+22*i, 435, this); if (codeTab[currentCol][i].length() < 2) g.drawString(codeTab[currentCol][i], 188+22*i, 450); } if (slock) g.setColor(Color.red); switch (currentCol) { case 0: case 1: case 2: case 3: g.drawString(" ", 188, 425); break; case 4: g.drawString(" symbol", 188, 425); break; case 5: case 6: case 7: case 8: g.drawString("shift ", 188, 425); break; case 9: g.drawString("shift symbol", 188, 425); break; }; g.setColor(Color.black); } }; redrawAll = false; redrawKeys = false; redrawStatus = false; redrawLine = false; } public boolean keyDown(Event e, int code) { switch (code) { case 52 : case 55 : bA = true; break; case 51 : case 56 : bE = true; break; case 50 : case 57 : bI = true; break; case 49 : case 48 : bO = true; break; } loaded = true; // just a guess redrawKeys = true; this.update(this.getGraphics()); return true; } protected void addChar (String c) { if (this.getGraphics().getFontMetrics().stringWidth(scratch[currentRow]+c) > 220) { if (currentRow < 12) { ++currentRow; scratch[currentRow] = scratch[currentRow]+c; redrawAll = true; } } else { scratch[currentRow] = scratch[currentRow]+c; redrawLine = true; } }; protected void doChord (int k) { String s; int prevCol; if (currentCol >= 0) { prevCol = currentCol; s = codeTab[currentCol][chords[k]-1]; if (s.length() < 2) addChar(s); if (s == "space") addChar(" "); if (s == "symbol") currentCol = 4; if (currentCol > 4) currentCol -= 5; if (s == "shift") currentCol += 5; if (s == "shift lock") { slock = !slock; redrawStatus = true; }; if (slock) currentCol += 5; if (s == "shift symbol") currentCol = 9; if (prevCol != currentCol) redrawStatus = true; }; prefix = ""; }; public boolean keyUp(Event e, int code) { int k; if (sample) { switch(keys()) { case 8: addChar(prefix); redrawLine = true; redrawStatus = true; if (slock || (currentCol > 4 && prefix == "")) { currentCol = 5; prefix = "A"; } else { currentCol = 0; prefix = "a"; }; break; case 4: addChar(prefix); redrawLine = true; redrawStatus = true; if (slock || (currentCol > 4 && prefix == "")) { currentCol = 6; prefix = "E"; } else { currentCol = 1; prefix = "e"; }; break; case 2: addChar(prefix); redrawLine = true; redrawStatus = true; if (slock || (currentCol > 4 && prefix == "")) { currentCol = 7; prefix = "I"; } else { currentCol = 2; prefix = "i"; }; break; case 1: addChar(prefix); redrawLine = true; redrawStatus = true; if (slock || (currentCol > 4 && prefix == "")) { currentCol = 8; prefix = "O"; } else { currentCol = 3; prefix = "o"; }; break; case 15: currentCol = -1; redrawAll = true; if (prefix != "") prefix = ""; else if (scratch[currentRow].length()>0) scratch[currentRow] = scratch[currentRow].substring(0, scratch[currentRow].length()-1); else if (currentRow > 0) { if(scratch[--currentRow] != "") scratch[currentRow] = scratch[currentRow].substring(0, scratch[currentRow].length()-1); }; break; default: doChord(keys()); break; } sample = false; } switch (code) { case 52 : case 55 : bA = false; break; case 51 : case 56 : bE = false; break; case 50 : case 57 : bI = false; break; case 49 : case 48 : bO = false; break; } redrawKeys = true; this.update(this.getGraphics()); if (keys() == 0) sample = true; return true; } }