Today we are launching .simplicity Code. .simplicity Code is a project created to maintain open-source projects from .simplicity that are too small to be considered as full-blown projects, but too big to be considered as simple code snippets. We are launching this project, because we often work on small projects without any other target audience than ourselves. These projects are the typical projects that are started out of self-interest. We recognize, though, that the code we write for these projects can be of interest to our visitors. .simplicity Code offers us the opportunity to publish this code, without having to offer complete support as a full-blown project would require. As a matter of fact most projects on .simplicity Code will have limited to no support. That does not mean that you will find junk on .simplicity Code. On the contrary, you will find that the code will be most of the time of the same quality as our bigger projects.
The first project that you can find on .simplicity Code is the .simplicity Java Sigslot Event System. This is, as the name already says, an implementation of a sigslot event system in Java. A sigslot event system allows the programmer to connect components within a system without that those components know with who they are actually connected. Furthermore this system can also be used as a broadcasting system for sending information to multiple subscribed components. Sigslot event systems can be applied in various different cases, such as component-based entity systems and graphical user interfaces.
But lets look at some example code using the .simplicity Java SigSlot Event System:
// Import classes
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
import net.dotsimplicity.events.awt.AWTEventsContainer;
// EventApplet class
public class EventApplet extends Applet
{
// Public methods
// Initialize the applet.
public void init()
{
// Create an AWT EventsContainer.
AWTEventsContainer container = new AWTEventsContainer("applet", this);
// Add a mouse moved event.
container.addMouseMotionListener();
container.connectEventSlot("mouseMoved", this, "onMouseMoved");
// Add a custom event.
container.createEvent("print", String.class);
container.connectEventSlot("print", this, "onPrint");
container.emitEventSignal("print", "Hello, World!");
}
// Paint the applet.
public void paint(Graphics g)
{
g.drawString("X: " + this.x + ", Y: " + this.y, 10, 15);
g.drawString(this.message, 10, 30);
}
// Handle mouseMoved events.
public void onMouseMoved(MouseEvent event)
{
this.x = event.getX();
this.y = event.getY();
this.repaint();
}
// Handle print events.
public void onPrint(String string)
{
this.message = string;
}
// Private members
private static final long serialVersionUID = 1;
private int x, y;
private String message;
}
// End of File
In the above example we have created an applet that uses our sigslot event system to handle mouseMoved events. Please note that we do not have to implement the MouseMotionListener interface; you are free to only implement those features that you need under whatever name you choose. That means that you no longer have to implement six empty methods if you want to use one feature of the WindowListener, for example.
Furthermore we also use our event system to transmit a signal with the message “Hello, World!”. In this particular appliance this is a rather useless operation, but it demonstrates in a simple way how we can use our system for broadcasting. Note that you can also use the relatively simpler EventsContainer for this appliance; this class provides all methods that the AWTEventsContainer provides except for the AWT specific methods.
Well, that is all for now. We hope that .simplicity Code will be of use to you.
