Tuesday, March 29, 2011

Swing Calculator

import groovy.swing.*;
import java.awt.*;
import javax.swing.*;
 
public class Calc{ 
    def sb = new SwingBuilder();
    
    public static void main(String[] args) {
        // create itself
        new Calc(args);
    }

    public Calc(String[] args){

  Toolkit tk = Toolkit.getDefaultToolkit();
  Dimension screenSize = tk.getScreenSize();
  final int WIDTH = screenSize.width;
  final int HEIGHT = screenSize.height;

  int w = WIDTH / 2
  int h = HEIGHT / 2

  int x = (WIDTH / 2) - (w / 2)
  int y = (HEIGHT / 2) - (h / 2) - 50

  sb.frame(id:"calcFrame", title:"XCalc", visible:true, alwaysOnTop:true, resizable:false, defaultCloseOperation:JFrame.EXIT_ON_CLOSE, location:[x, y], size:[w, h]) {
            borderLayout()
            textField(id:"display", editable:false, horizontalAlignment:JTextField.RIGHT, constraints:BorderLayout.NORTH);
            
            panel(constraints:BorderLayout.CENTER){
                gridLayout(cols:4, rows:5, hgap:2, vgap:2)
 
                if (args.size() > 0) 
                     label(args[0])
                else
                     label("")
    
                label("")
                button(text:"<", actionPerformed:{backSpace()})
                button(text:"X", actionPerformed:{clear()})
 
                button(text:"7", actionPerformed:{insertText(it.source.text)})
                button(text:"8", actionPerformed:{insertText(it.source.text)})
                button(text:"9", actionPerformed:{insertText(it.source.text)})
                button(text:"/", actionPerformed:{insertText(it.source.text)}) 
 
                button(text:"4", actionPerformed:{insertText(it.source.text)})
                button(text:"5", actionPerformed:{insertText(it.source.text)})
                button(text:"6", actionPerformed:{insertText(it.source.text)})
                button(text:"*", actionPerformed:{insertText(it.source.text)})
 
                button(text:"1", actionPerformed:{insertText(it.source.text)})
                button(text:"2", actionPerformed:{insertText(it.source.text)})
                button(text:"3", actionPerformed:{insertText(it.source.text)})
                button(text:"-", actionPerformed:{insertText(it.source.text)})
 
                button(text:"0", actionPerformed:{insertText(it.source.text)})
                button(text:".", actionPerformed:{insertText(it.source.text)})
                button(text:"=", actionPerformed:{doEquals()})
                button(text:"+", actionPerformed:{insertText(it.source.text)})                    
            }
        }
        //sb.calcFrame.pack(); //compact display, ignores the size attribute
        sb.calcFrame.show();

    }
 
    public void insertText(text){
        sb.display.text += text
    }
 
    public void backSpace(){
        sb.display.text=sb.display.text[0..sb.display.text.length()-2]        
    }
 
    public void clear(){
        sb.display.text="";
    }
 
    public void doEquals(){
        GroovyShell shell = new GroovyShell();
        sb.display.text = shell.evaluate(sb.display.text);
    }
}
http://groovyconsole.appspot.com/script/449002

No comments:

Post a Comment