import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
import javax.swing.*;
import ket.MathCollection;
import ket.display.ColourScheme;
import ket.display.ImageTools;
import ket.display.box.Box;
import ket.math.Argument;
import ket.math.KnownArguments;
import ket.math.convert.ArgumentParser;
public class Buttons implements ActionListener, KeyListener {
static final ColourScheme colourScheme = ColourScheme.WHITEBOARD;
static final KnownArguments knownArguments = new KnownArguments();
static final MathCollection mathCollection = new MathCollection(knownArguments);
JButton[] list;
Map<Integer, String> map;
int count;
String[][] options;
JFrame frame;
String title;
static final Integer[] UNITS = new Integer[]{
7, 8, 9,
4, 5, 6,
1, 2, 3};
public Buttons(String title, String[][] options) {
this.title = title;
this.options = options;
count = 0;
setup();
}
public void setup() {
list = new JButton[9];
Vector<String> fragments = new Vector<String>(
Arrays.asList(options[count]));
map = randomize(fragments);
frame = new JFrame(title);
frame.setLayout(new GridLayout(3, 3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (Integer i : UNITS) {
ImageIcon imageIcon = asIcon(map.get(i), ""+i);
JButton button = new JButton(imageIcon);
button.addKeyListener(this);
button.addActionListener(this);
frame.add(button);
list[i-1] = button;
}
frame.pack();
frame.setVisible(true);
}
public void suggest(Integer c) {
//- System.out.println("guess = " + c);
//- System.out.println("map: " + map.get(c));
//- if (true) { //; Check you picked the right string.
if (options[count][0].equals(map.get(c))) {
System.out.println(" !!! good !!! ");
count += 1;
if (count==options.length) {
// done.
System.exit(0);
}
frame.setVisible(false); // ignore, but don't dispose
setup();
} else {
System.out.println(" !!! no !!! ");
}
}
public void actionPerformed(ActionEvent ae) {
Integer c = find((JButton) ae.getSource());
suggest(c);
}
public void keyPressed(KeyEvent keyEvent) {
char c = keyEvent.getKeyChar();
if ("123456789".indexOf(c)==-1)
return;
suggest((int) c - (int) '0');
}
public void keyReleased(KeyEvent keyEvent) { }
public void keyTyped(KeyEvent keyEvent) { }
public static void labelImage(BufferedImage image, String label) {
int dx = 1;
int dy = 1;
Graphics2D g2D = image.createGraphics();
FontMetrics fontMetrics = g2D.getFontMetrics();
int width = fontMetrics.stringWidth(label);
int ascent = fontMetrics.getAscent();
int decent = fontMetrics.getDescent();
g2D.setColor(Color.WHITE);
g2D.fillRect(0, 0, width+2*dx, ascent+decent+2*dy);
g2D.setColor(Color.BLACK);
g2D.drawRect(0, 0, width+2*dx, ascent+decent+2*dy);
g2D.drawString(label, dx, ascent+dy);
}
public static ImageIcon asIcon(String expression, String label) {
Argument argument = ArgumentParser.parseArgument(expression, knownArguments, null, mathCollection); // null clipboard
Box box = argument.toBox(Box.X_CENTRE_ALIGN|Box.Y_CENTRE_ALIGN, colourScheme);
BufferedImage image = ImageTools.boxToBufferedImage(box, 160, 120, colourScheme, false);
labelImage(image, label);
ImageIcon imageIcon = new ImageIcon(image);
return imageIcon;
}
public static void main(String[] args) {
String title = "Solve for r wrt G, M and a.";
/* String[][] options = new String[][]{
{"a'x", "b'x", "c'x",
"d'x", "e'x", "f'x",
"g'x", "h'x", "i'x"},
{"a'X", "b'X", "c'X",
"d'X", "e'X", "f'X",
"g'X", "h'X", "i'X"}};*/
String[][] options = new String[][]{
{"[F=G*m*M/r^2,F=m*a]", "[F=G*m*M/r^2,v=u+a*t]", "F=m*a", "[a=x't^2, F=m*a]",
"s=u*t+1//2*a*t^2", "v^2=u^2+2*a*s", "v=u+a*t", "[v=f*\\lambda,F=G*m*M/r^2]", "E=m*c^2"},
{"m*a=G*m*M/r^2", "F=G*m*M/r^2", "F=m*a", "?", "?", "?", "?", "?", "?"},
{"a=G*M/r^2", "0=G*M*m/r^2-a*m", "m*a=G*m*M/r^2", "m*a/G=G*m*M/G*r^2", "?", "?", "?", "?", "?"},
{"r^2=G*M/a", "a", "b", "c", "d", "e", "f", "g", "h"},
{"r=sqrt(G*M/a)", "a", "b", "c", "d", "e", "f", "g", "h"}};
new Buttons(title, options);
}
// A random map between 1-9 and the contents of 'fragments'.
public Map<Integer, String> randomize(Vector<String> fragments) {
//- System.out.println(" --- randomize --- ");
Vector<Integer> ints = new Vector<Integer>(Arrays.asList(UNITS));
Vector<Integer> pert = new Vector<Integer>();
Random rand = new Random(System.currentTimeMillis());
Map<Integer, String> map = new TreeMap<Integer, String>();
while ( ! ints.isEmpty() ) {
int j = rand.nextInt(ints.size());
pert.add(ints.remove(j));
}
//- System.out.println("pert.size() = " + pert.size());
//- System.out.println("fragments.size() = " + fragments.size());
for (int i=0; i<9; i++) {
map.put(pert.get(i), fragments.get(i));
}
//- System.out.println(map);
return map;
}
public Integer find(JComponent c) {
for (Integer i : UNITS) {
if (list[i-1]==c)
return i;
}
return 0;
}
}