//add function that if you double click then search the word by google package jp.co.learn.dict;
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.Point; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.List; import java.util.Properties;
import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.UIManager; import javax.swing.plaf.FontUIResource;
public class LearnWords extends JFrame implements ActionListener, KeyListener, Runnable {
static final long serialVersionUID = 1L;
static LearnWords frame;
JLabel label;
int sec;
JPopupMenu menubar = new JPopupMenu();
static List<String> wordList = new ArrayList<String>();
String filename = "";
MouseEvent start;
int intTime = 1000;
int intRight = 40;
int intDown = 20;
int CONTENT_LABEL_HEIGHT = 25;
String MODE_AUTO = "AUTO";
Properties properties;
public static void main(String[] args) throws Exception {
System.setProperty("awt.useSystemAAFontSettings", "on");
System.setProperty("swing.aatext", "true");
try {
// Systematic UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } // All local fonts used on the bottom initGlobalFontSetting (new Font ("Meiryo", 0, 12));
frame = new LearnWords();
}
public LearnWords() {
addKeyListener(this);
sec = 0;
JMenu menu1 = new JMenu ("text"); JMenu menu2 = new JMenu ("accidental"); JMenu menu3 = new JMenu ("time"); JMenu menu4 = new JMenu ("Japanese language"); JMenu menu5 = new JMenu ("Kanshu"); JMenu menu6 = new JMenu("help");
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menu4);
menubar.add(menu5);
menubar.add(menu6);
JMenuItem menuitem1_2 = new JMenuItem ("Open");
JMenuItem menuitem2_1 = new JMenuItem ("Start at any time"); JMenuItem menuitem2_2 = new JMenuItem ("End of time");
JMenuItem menuitem3_1 = new JMenuItem ("time +"); JMenuItem menuitem3_2 = new JMenuItem ("time-");
JMenuItem menuitem4_1 = new JMenuItem ("Japanese Language Proficiency Test Questioning Standard Vocabulary Table-N1"); JMenuItem menuitem4_2 = new JMenuItem ("Japanese Language Proficiency Test Questioning Standard Vocabulary Table-N2"); JMenuItem menuitem4_3 = new JMenuItem ("Japanese Language Proficiency Test Questioning Standard Vocabulary Table-N3"); JMenuItem menuitem4_4 = new JMenuItem ("Japanese Language Proficiency Test Questioning Standard Vocabulary Table-N4"); JMenuItem menuitem4_5 = new JMenuItem ("Minna no Japanese Words"); JMenuItem menuitem4_6 = new JMenuItem ("Japanese Language Proficiency Test Questioning Standard Katakana Vocabulary Table");
JMenuItem menuitem5_1 = new JMenuItem("exit");
JMenuItem menuitem6_1 = new JMenuItem ("+ key: frequency 1 second added"); JMenuItem menuitem6_2 = new JMenuItem ("-key: frequency reduced by 1 second"); JMenuItem menuitem6_3 = new JMenuItem ("Spacebar: Start at any time"); JMenuItem menuitem6_4 = new JMenuItem ("CTRL key: end at any time"); JMenuItem menuitem6_5 = new JMenuItem ("ESC key: exit");
menu1.add(menuitem1_2);
menu2.add(menuitem2_1);
menu2.add(menuitem2_2);
menu3.add(menuitem3_1);
menu3.add(menuitem3_2);
menu4.add(menuitem4_1);
menu4.add(menuitem4_2);
menu4.add(menuitem4_3);
menu4.add(menuitem4_4);
menu4.add(menuitem4_5);
menu4.add(menuitem4_6);
menu5.add(menuitem5_1);
menu6.add(menuitem6_1);
menu6.add(menuitem6_2);
menu6.add(menuitem6_3);
menu6.add(menuitem6_4);
menu6.add(menuitem6_5);
menuitem1_2.addActionListener(this);
menuitem1_2.setActionCommand("Open");
menuitem2_1.addActionListener(this);
menuitem2_1.setActionCommand ("random start"); menuitem2_2.addActionListener(this); menuitem2_2.setActionCommand ("random end");
menuitem3_1.addActionListener(this);
menuitem3_1.setActionCommand("addTime");
menuitem3_2.addActionListener(this);
menuitem3_2.setActionCommand("decreaseTime");
menuitem4_1.addActionListener(this);
menuitem4_1.setActionCommand("readJapaneseN1");
menuitem4_2.addActionListener(this);
menuitem4_2.setActionCommand("readJapaneseN2");
menuitem4_3.addActionListener(this);
menuitem4_3.setActionCommand("readJapaneseN3");
menuitem4_4.addActionListener(this);
menuitem4_4.setActionCommand("readJapaneseN4");
menuitem4_5.addActionListener(this);
menuitem4_5.setActionCommand("readJapaneseMinnna");
menuitem4_6.addActionListener(this);
menuitem4_6.setActionCommand("readJapaneseKana");
menuitem5_1.addActionListener(this);
menuitem5_1.setActionCommand("exit");
JPanel p = new JPanel();
label = new JLabel();
label.setBackground(Color.gray);
label.setVerticalAlignment(JLabel.CENTER);
label.setText("Loading......");
p.add(label);
Container contentPane = getContentPane();
contentPane.add(p, BorderLayout.LINE_START);
WindowDrugMove mouseListener = new WindowDrugMove();
// When you first grab the mouse label.addMouseListener(mouseListener); // When you drag the mouse label.addMouseMotionListener(mouseListener);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(intRight, intDown, 400, CONTENT_LABEL_HEIGHT);
setAlwaysOnTop(true);
setUndecorated(true);
setBackground(new Color(0x0, true));
setVisible(true);
// Read the property file. properties = new Properties(); try { String currentPath = new File(".").getAbsoluteFile().getParent(); File settingFile = new File(currentPath + "\LearnWords.ini"); if (!settingFile.exists()) { properties.setProperty("MODE", MODE_AUTO); properties.setProperty("DICT", "jp.co.learn.dict.JapaneseN1"); properties.store(new FileOutputStream(settingFile), "Comments"); } else { properties.load(new FileInputStream(settingFile)); } } catch (Exception e) { }
filename = "C:\\en.txt";
readFile(filename);
Thread thread = new Thread(this);
thread.start();
}
/**
Mouse movement
*/ class WindowDrugMove implements MouseMotionListener, MouseListener { private Point loc;
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
Window window = frame;
loc = window.getLocation(loc);
int x = loc.x - start.getX() + e.getX();
int y = loc.y - start.getY() + e.getY();
window.setLocation(x, y);
}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
try {
// String jisUni = new String(wordList.get(sec).getBytes("Shift_JIS"),"Shift_JIS"); // // Unicode as EUC // String eucjpUni = new String(jisUni.getBytes("UTF-8"),"UTF-8"); // Process p =Runtime.getRuntime().exec("H:\iexplore.exe http://www.google.com/search?q="+wordList.get(sec)+"&ie=euc-jp" + "&hl=ja");
Process p =Runtime.getRuntime().exec("H:\\Chrome\\Application\\chrome.exe http://www.google.com/search?q="+wordList.get(sec-1)+"&ie=euc-jp" + "&hl=ja");
try {
p.waitFor();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
InputStream input = p.getInputStream();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
String lines = "";
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
lines += line + "\n";
}
System.out.println(lines);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
start = e;
showPopup(e);
}
public void mouseReleased(MouseEvent e) {
// Menu menu showPopup(e); } }
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Open") {
JFileChooser jfc1 = new JFileChooser();
// Name setting jfc1.setDialogTitle ("Please select a file."); // Default method jfc1.showOpenDialog(null); jfc1.setVisible(true); // Get all paths filename = jfc1.getSelectedFile().getAbsolutePath(); readFile(filename); } else if (e.getActionCommand () == "random start") { label.setText (" <font face ='MS Mincho, SimSun, Song body' size ='+ 1'color ='green'> random start ... </ font> </ html>"); Collections.shuffle(wordList); return; } else if (e.getActionCommand () == "random end") { label.setText (" <font face ='MS Mincho, SimSun, Song body' size ='+ 1'color ='green'> random end ... </ font> </ html>"); reRadom(); return; } else if (e.getActionCommand() == "addTime") { if (intTime >= 1000) { intTime = intTime + 1000; } else if (250 <= intTime && intTime < 1000) { intTime = intTime * 2; } } else if (e.getActionCommand() == "decreaseTime") { if (intTime > 1000) { intTime = intTime - 1000; } else if (intTime >= 500) { intTime = intTime / 2; } } else if (e.getActionCommand() == "readJapaneseN1") { loadDict("jp.co.learn.dict.JapaneseN1"); sec = 0; } else if (e.getActionCommand() == "readJapaneseN2") { loadDict("jp.co.learn.dict.JapaneseN2"); sec = 0; }
outPutMsgOfchangTime();
//
// else if (e.getActionCommand() == "readJapaneseN3") {
// readJapaneseN3();
// sec = 0;
// } else if (e.getActionCommand() == "readJapaneseN4") {
// readJapaneseN4();
// sec = 0;
// } else if (e.getActionCommand() == "readJapaneseMinnna") {
// readJapaneseMinnna();
// sec = 0;
// } else if (e.getActionCommand() == "readJapaneseKana") {
// readJapaneseKana();
// sec = 0;
// }
if ("exit".equals(e.getActionCommand())) {
int dialogButton = JOptionPane.YES_NO_OPTION;
if (dialogButton == JOptionPane.YES_OPTION) {
writeFile();
System.exit(NORMAL);
}
}
menubar.setVisible(false);
frame.setBounds(frame.getX(), frame.getY(), frame.getWidth(), CONTENT_LABEL_HEIGHT);
}
public void run() {
do {
if (sec + 1 > wordList.size()) {
sec = 0;
} else {
label.setText (" " + wordList.get (sec) .trim () + "</ font> </ html> "); sec++; if (sec == wordList.size()) { sec = 0; } } try { Thread.sleep(intTime); } catch (InterruptedException e) { } } while (true); }
/**
read file Read the specified TXT file
@return None */ public void readFile(String fileName) {
wordList = new ArrayList<String>();
sec = 0;
try {
Files.lines(Paths.get(fileName)).forEachOrdered(s -> {
if (null != s && s.trim().length() > 1 && !s.contains("OK")) {
wordList.add(s);
}
});
menubar.setVisible(false);
if (wordList.size() == 0) {
loadDict("jp.co.learn.dict.JapaneseN1");
}
} catch (IOException e1) {
if (wordList.size() == 0) {
loadDict("jp.co.learn.dict.JapaneseN1");
sec = 0;
}
e1.printStackTrace();
}
}
/**
Keylogger */ @Override public void keyPressed(KeyEvent e) {
// + KEY Add time to repeat if (e.getKeyCode() == 107) { if (intTime >= 1000) { intTime = intTime + 1000; } else if (250 <= intTime && intTime < 1000) { intTime = intTime * 2; } } // --KEY Reduced time to repeat else if (e.getKeyCode() == 109) { if (intTime > 1000) { intTime = intTime - 1000; } else if (intTime >= 500) {
intTime = intTime / 2;
}
}
// ESC key exit else if (e.getKeyCode() == 27) { writeFile(); System.exit(NORMAL); } // SPACE Random start else if (e.getKeyCode() == 32) { Collections.shuffle(wordList); } // CTRL random termination else if (e.getKeyCode() == 17) { label.setText ("random end"); reRadom(); } // ENTER Write the words you understand. Do not display next time else if (e.getKeyCode() == 10) { if (wordList.get(sec - 1) == label.getText()) { wordList.remove(sec - 1); wordList.add(sec - 1, "OK " + label.getText()); System.out.println("OK"); } else { System.out.println("NG"); } } // Lower right single word else if (e.getKeyCode() == 39) { sec++; if (sec == wordList.size()) { sec = 0; } label.setText( " <font face ='MS Mincho, SimSun, Song'size ='+ 1'>" + wordList.get (sec) .trim () + "</ font> </ html>"); } // Downward movement else if (e.getKeyCode() == 40) { intDown = intDown + 50; frame.setBounds(frame.getX(), intDown, frame.getWidth(), CONTENT_LABEL_HEIGHT); } // Top improvement movement else if (e.getKeyCode() == 38) { intDown = intDown - 50; if (intDown > 0) { frame.setBounds(frame.getX(), intDown, frame.getWidth(), CONTENT_LABEL_HEIGHT); } } // Upper left single word else if (e.getKeyCode() == 37) { sec--; if (sec < 0) { sec = 0; } label.setText( " <font face ='MS Mincho, SimSun, Song'size ='+ 1'>" + wordList.get (sec) .trim () + "</ font> </ html>"); }
outPutMsgOfchangTime();
}
private void outPutMsgOfchangTime() {
if (intTime<1000) {
String text = String.valueOf (intTime) + "Changed to milliseconds."; label.setText (" <font face ='MS Mincho, SimSun, Song body' size ='+ 1'color ='green'>" + text + "</ font> </ html>"); }else{ String text = String.valueOf (intTime / 1000) + "Changed to seconds."; label.setText (" <font face ='MS Mincho, SimSun, Song body' size ='+ 1'color ='green'>" + text + "</ font> </ html>"); } }
/**
Random release Random release.
*/
private void reRadom() {
Collections.sort(wordList, new Comparator
/**
File update. Words to understand Add OK before this line. */ public void writeFile() {
FileSystem fs = FileSystems.getDefault();
/ * Write file * / Path outputPath = fs.getPath(filename); try (BufferedWriter bw = Files.newBufferedWriter(outputPath, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) { wordList.forEach(s -> { try { bw.write(s + "\r\n"); } catch (Exception e) { e.printStackTrace(); } }); } catch (IOException e) { e.printStackTrace(); } }
@SuppressWarnings("unchecked")
private void loadDict(String param) {
try {
wordList.clear();
Class<?> clazz = Class.forName(param);
Object obj = clazz.newInstance();
Method method = clazz.getMethod("read");
wordList = (List<String>) method.invoke(obj);
} catch (Exception e) {
}
}
private void showPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
menubar.show(e.getComponent(), e.getX(), e.getY());
}
}
public static void initGlobalFontSetting(Font fnt) {
FontUIResource fontRes = new FontUIResource(fnt);
for (Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource)
UIManager.put(key, fontRes);
}
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}
public static boolean isUTF8(byte[] src)
{
try {
byte[] tmp = new String(src, "UTF8").getBytes("UTF8");
return Arrays.equals(tmp, src);
}
catch(UnsupportedEncodingException e) {
return false;
}
}
public static boolean isSJIS(byte[] src)
{
try {
byte[] tmp = new String(src, "Shift_JIS").getBytes("Shift_JIS");
return Arrays.equals(tmp, src);
}
catch(UnsupportedEncodingException e) {
return false;
}
}
}