Showing posts with label Event Programming. Show all posts
Showing posts with label Event Programming. Show all posts

Sunday, December 4, 2011

Simple Calculator Applet

Since I've been playing the last few days with Java applets, I decided to create a simple calculator. In the right you can see the result.

How the applet works:
  1. User inputs one or two values (depending of what operations he needs to be done)
  2. The user selects the operation by clicking of the central buttons.
  3. The result appears in the result TextField and the bottom list.
What it can do for now:
  • v1.01
    • The result text box is no longer editable
    • Better log (it now shows which operations were made)
    • The ability to reload automatically operators by clicking a log entry
  • v1.00
    • Basic arithmetic operations (addition, subtraction, multiplication, division, modulo, power, square root, natural logarithm)
    • Trigonometric functions
    • Saving the results in a list

Tuesday, October 18, 2011

Mouse Events in Java AWT

Let's consider the class we had in the article on how to close a frame in Java AWT. To use a mouse event we need to define another adapter class that will extend the MouseAdapter class. The MouseAdapter subclass will also be nested in the "main" class, since it's easier this way to access the members and methods of the "main" class (just like the CloseWindowAdapter class in the previous example).
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestFrame1 extends Frame
{
    //This controls should be declared as internal variables
    //in order to be accessed by the nested classes
    private Button but;
    private Label lbl;
    
    private class CloseWindowAdapter extends WindowAdapter 
    {        
        public void windowClosing(WindowEvent we)
        {
            TestFrame1.this.setVisible(false);
            TestFrame1.this.dispose();
            System.exit(0);
        }
    }
    
    private class TestMouseAdapter extends MouseAdapter
    {
        public void mouseClicked(MouseEvent e) 
        {    
            //Checks if it's right click
            if(e.isMetaDown()==true)
                lbl.setText("RIGHT CLICK");
            //If it's not right-click, then it's left click
            else
                lbl.setText("LEFT CLICK");
        }
    }
    
    public TestFrame1()
    {
        super("Test frame");
        this.addWindowListener(new CloseWindowAdapter());
        this.setLayout(new FlowLayout());
        //Creating a button
        but = new Button("Trigger click event");
        //Adding the a mouse listener for the button
        but.addMouseListener(new TestMouseAdapter());
        //Creating a label to show what event will be triggered
        lbl = new Label("Nothing happened yet");
        //Adding the controls to the frame
        this.add(lbl);
        this.add(but);
        //Packing the frame
        this.pack();
    }
    
}
In the example above, the frame contains two controls : a button and a label. If you would right click the button, the label's text would become "RIGHT-CLICK". If you would left click the button, the label's text would become "LEFT-CLICK". The MouseEvent object can also provide several other checks:
 //Returns true if the ALT key was pressed at the time of the click
 e.isAltDown();
 //Returns true if the ALT GR key was pressed at the time of the click
 e.isAltGraphDown();
 //Returns true if the CTRL key was pressed at the time of the click
 e.isControlDown();
 //Returns true if the SHIFT key was pressed at the time of the click
 e.isShiftDown();
 //Returns a reference to the control who triggered the event
 e.getSource();
 //Returns a Point object containing the x,y coordinates 
 //relative to the source component
 e.getPoint();
The TestMouseAdapter subclass implements only the mouseClicked event trigger. The MouseAdapter ancestor class would had allowed the implementation of several other event triggers:
  • mouseEntered - occurs when the mouse cursor enters the component
  • mouseExited - occurs when the mouse cursor leaves the component
  • mousePressed - occurs when a mouse button is pressed on the component
  • mouseReleased - occurs when a mouse button is no longer pressed on the component 
The mouseClicked event trigger which we had used occurs when the component was clicked (pressed and then released).

Closing a Frame in Java AWT

To close a frame in AWT, you need to define a WindowAdapter object that will call the methods you want when the close button will be clicked. If it often beneficial to declare this new class as a nested type of the frame, since it could access the frame's methods and members.

To practically close the window, you must first make it invisible to the user and then free all the resources it occupies.
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyFrame extends Frame 
{
    public class CloseWindowEvent extends WindowAdapter 
    {        
        public void windowClosing(WindowEvent we)
        {
            //Make the frame invisible
            MyFrame.this.setVisible(false);
            //Free all resources
            MyFrame.this.dispose();
            //If you also want to close the program,uncomment bellow
            //System.exit(0);
        }
    }
    
    public MyFrame(String title)
    {
        super(title);
        this.add(new Label("Click X to Close"));
        //Adds the listener to the class
        this.addWindowListener(new CloseWindowEvent());
        this.pack();
    }
}
Related Posts Plugin for WordPress, Blogger...