Off The Top Of My Head

The GreekLetterWidget

Posted in Uncategorized by waltermilner on June 17, 2009

I’m currently writing some maths software in which I wanted to be able to use Greek letters as well as Roman. Since Java uses Unicode, this was no problem internally. But how could the user input Greek as well as Roman characters, through a standard US/UK keyboard, and without obscure and slow character codes?

So I wrote a widget which had 25 buttons, labelled with the lower case Greek letters. It has an instance variable which is a reference to a textfield. When the user clicks a button, the corresponding character is appended to that textfield, and focus is placed on that textfield, ready for the next keystroke.

Here it is:

/**
 * This is a UI widget supporting the input of Greek letters
 * It produces a 5 X 5 grid of buttons, labelled with lower case Greek letters
 * When a button is clicked, the corresponding letter is appended to a supplied
 * text field
 * @author Walter Milner
 */
public class GreekLetterWidget extends JComponent implements ActionListener {
    private MyButton[] buttons = new MyButton[25];
    private MyTextField textField;
/**
 * make an instance, of size 100 by 100
 * @param textField The textfield to which the letter will be appended
 */
    public GreekLetterWidget(MyTextField textField) {
        this.textField = textField;
        setLayout(new GridLayout(5, 5));
        setSize(100, 100);
        // Greek letter buttons
        char[] c = new char[1];
        c[0] = ‘\u03B1′;
        String s = new String(c);
        Insets inset = new Insets(1, 1, 1, 1);
        for (int i = 0; i < 25; i++) {
            buttons[i] = new MyButton(s);
            buttons[i].setToolTipText(s);
            c[0]++;
            s = new String(c);
            buttons[i].setMargin(inset);
            buttons[i].addActionListener(this);
            add(buttons[i]);
        }
    }
    public void setField(MyTextField target)
    {
        textField=target;
    }
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        textField.setText(textField.getText() + ((MyButton) obj).getText());
        textField.requestFocus();
    }
}

This looks like:

screenshot

screenshot

The widget is used (in a sub-classed JDialog using a GridBagLayout) with code like this
glw = new GreekLetterWidget(funcTextField);
c.gridx = 3;
c.gridy = 0;
c.gridheight = 2;
add(glw, c);

The setTextField() method is provided so that this can be used with more than one text field in a dialog. The textfields need to have a focuslistener, and in the gainFocus, setTextField is called with that text field as the parameter. That means that when the user clicks on a textfield, from then on GreekLetter button clicks go into that text field.

Padding a JLabel

Posted in Uncategorized by waltermilner on June 17, 2009

Googling for this was pretty fruitless, but I eventually got a hint, which worked out OK. The hack is to have an ‘empty’ border around the text. If you also want a visible border, you can combine that with the empty border in a compound border. So you have something like:

public class MyLabel extends JLabel {
    static Font labelFont = new Font(“SansSerif”, Font.PLAIN, 12);
    static Border gap = BorderFactory.createEmptyBorder(4, 2, 4, 2);
    static Border blueline = BorderFactory.createLineBorder(Color.blue);
    static Border compound = BorderFactory.createCompoundBorder(blueline, gap);
    MyLabel(String text) {
        super(text);
        setFont(labelFont);
        setBorder(compound);
    }
}

which looks like:

screenshot

screenshot

Tagged with: , , , ,

How people learn

Posted in Uncategorized by waltermilner on February 1, 2009

This is an attempt to describe how ‘adults’ learn new concepts in a formal setting, such as a college course. I’m mostly thinking about ’scientific’ subjects like maths physics and computing. I’m using learning Java as the source of examples.

It uses some other ideas. Two crucial ones are frames and mental spaces . Read that link if you are interested in this and want to make sense of this blog.  It also uses the idea of a compression.

Psychologists have usually treated concepts as categories, delineated by a definition. Work by people like Rosch showed this was simplistic, with ‘real’ concepts being more complex than this. This led to the idea of concept being based on an ‘ideal’ instance (prototype theory), a set of remembered examples (exemplars), and degree of possession of typical features (typicality).

Moving beyond concept as category, Neisser and others developed the idea of concept as implicit theory – which is very relevant to ‘understanding’ an idea. To understand what a Java object is, we need a lot more than simply to be able to decide if something is an object or not.

Typical college teaching shows all of the aspects of concepts. Often definitions are given, examples, descriptions (usually of typical instances) and so on. In addition, students will usually work through ‘exercises’ in which they are obliged to engage actively with some examples.

There is a particular problem if there is no definition of a concept. For example, the Java class/object concept has no semantic definition ( the syntax of a class definition is available, but it is no use to a student trying to understand what a class is). In this situation there is usually some teaching discourse (in a textbook or lecture) which tries to deliver an understanding of the idea through prose – but this is extremely difficult. For example one standard Java student text says that ‘objects belong to classes’. But this strongly suggests that a class is a set, and an object is an element in the set, which supports the partonomy/taxonomy confusion.

In these situations we have general concepts (class/object, or type) and examples ( JButtons, JFrames, classes developed by the student in lab sessions, or for type, ints, chars, doubles, booleans and so on).

A student coming to understand these general concepts does so by taking the examples (a multiplicity of mental spaces which include the examples), and compressing them into a frame. That’s it.

Students are individuals, and there are individual differences. For example one student may be slow to do this – he may be very fluent handling ints and doubles, while denying any recognition of the idea of type. Other students may actually do this too fast – rather as infants over-generalize terms. For example one student had absorbed the idea of inheritance, and had then assumed that ActionListener, WindowListener, MouseMotionListener and so on were sub-classes of a common base class ( semantically correct but syntactically wrong, since these are interfaces and not classes).

An example blend

Posted in Uncategorized by waltermilner on January 28, 2009

How do we really understand ideas? How do we make sense of things beyond the purely concrete? Like time, or jokes, or freedom, or software? That’s my question.

And I think at least part of the answer is as conceptual blends. Not my idea – it comes largely from Gilles Fauconnier. Let me outline one of his examples.

The idea of a ‘computer virus’. An irritating idea, but also creative and imaginative.

Blends have (usually) 2 input spaces. In this case, one is a computer program. This space has several ’slots’
Made by a human (programmer)
Useful
Needs computer hardware to run on

The other input space is a biological virus
Made by evolution
Harmful
Needs host living cell
Reproduces

Then we do a selective projection of these slots. In other words, we pick some from one input and some from the other. This is very precise – its not a blend as in food blender – we don’t get mush. In the output space we have

Made by a programmer
Harmful
Needs host hardware
Reproduces

And there you have a new idea.

But this is not at a conscious explicit level – the first people to write viruses did not work it out like this, in the sense that they were aware of what they were doing in this way. The hypothesis is that the human central nervous system holds, uses and transforms concepts in a way somehow related to the process described here.

What a Java class really is

Posted in Uncategorized by waltermilner on January 27, 2009

At the heart of OOP is a statement like
someObject.someMethod();
This is usually decribed as ‘calling’ or ‘invoking’ someMethod on the object someObject.

It is an imperative, and it is usually described as sending a ‘message’ to someObject, telling it to do someMethod. This is a more or less explict metaphor, built on top of metaphors we are probably unaware of – unless we have read Lakoff. But there are big problems with it. As a minor point, the ‘message’ metaphor has two difficulties
1. Messages are asynchronous. If I send you a message by email or SMS or post, it might be several days before you read it.
2. Messages are optional – they can be ignored.

But the big problem is who is the agent – that is, who is the imperative addressed to – who is supposed to do it? In English, this is implicit, as in
“Stand up!”
or explicit, as in
“No-one leaves the hall, Sargeant!”
Usually the Java form
someObject.someMethod();
is thought of as a message TO someObject, telling IT to do its someMethod. This is wrong, since the agent which executes the method is actually the ’system’, the JRE. Futher, it breaks the metaphor, since often the method is something which happens TO the object, not carried out by him. Eg in a personnel system we might have a method to fire an employee, and we would say
joe.dismiss();
but Joe does not dismiss himself – it is done to him.

So what does the . actually mean? It is to identify the namespace of the method, and the object primarily affected. That is, someMethod() might occur in several classes. Which one do we want? The one in the class that someObject belongs to. And which object will someMethod() primarily affect? someObject.

So a class is a namespace.

JOGLBLOGL has moved again..

Posted in Uncategorized by waltermilner on October 20, 2008

.. to http://jogl.yuku.com/

This is currently ad-free, but they’ll come. If they are too bad I’ll move it again.

Tagged with: , , ,

More JOGL..

Posted in JOGL by waltermilner on October 11, 2008

See Space Balls :

Jupiter

Jupiter, but not as we know it.

Display lists are easy

Posted in JOGL by waltermilner on October 3, 2008

See this

JOGL

Ten rather disturbing torii

Textures and blending

Posted in JOGL by waltermilner on October 1, 2008

JOGLBLOGL is growing..

Looking at JOGL

Posted in JOGL by waltermilner on September 17, 2008

JOGLBLOGL has been moved to here

Tagged with: , , , ,