2012/08/17

TWaver’s Application in Power-system

As a professional graphical interface component, TWaver can be used in all walks of life. In this article we are going to give an introduction on how to draw an interface of power system with the help of TWaver. Now let's first see several images on power system:
the 1st wiring layout of a specified substation:

the 2nd wiring layout of a specified substation:

Of course the images above are what I have found on the Net. Then is TWaver able to realize it? The answer is absolutely. Let’s first analyze it: in a power system these interfaces are composed of many electric elements, such as transformer, disconnecting link, switch, earthed switch, capacitor and generator, etc.

Then let’s see how to extend these electric elements: first comes a simple disconnecting link which has two statuses: open and closed. So we can define a class for it which inherits from ResizableNode and adding turnOn to control its switch-status.

public class Switch extends ResizableNode{
    private boolean turnOn = true;

    public Switch() {
        super();
        init();
    }

    public Switch(Object id){
        super(id);
        init();
    }

    private void init(){
        this.setSize(5,30);
        this.putBorderColor(Color.black);
        this.putBorderInsets(12);
    }

     public String getUIClassID() {
        return SwitchUI.class.getName();
    }

     public boolean isTurnOn() {
        return turnOn;
    }

     public void setTurnOn(boolean turnOn) {
        if(this.turnOn != turnOn){
            boolean oldValue = this.turnOn;
            this.turnOn = turnOn;
            this.firePropertyChange("turnOn", oldValue, this.turnOn);
        }
    }
}
Next, we need to draw the shape of the swich according to the property of turnOn on UI. Here is the major paint method:

public void paintBody(Graphics2D g) {
    g.setStroke(TWaverConst.DOUBLE_WIDTH_STROKE);

    Switch switzh = (Switch)this.getElement();

    //get position
    final Point location = switzh.getLocation();
    final Dimension size = switzh.getSize();
    boolean trunOn = switzh.isTurnOn();
    final int x = location.x;
    final int y = location.y;
    final int width = size.width;
    final int height = size.height;

    //draw color frame
    g.setColor(new Color(170,0,225));
    g.drawOval(x, y, width, width);
    g.drawOval(x, y+height-width, width, width);

    if(trunOn){
        g.drawLine(x+width/2, y+height-width, x+height/2,y+width);
    }else{
        g.drawLine(x+width/2, y+height-width, x+width/2, y+width);
    }
}
Let’s see the extended effect through main

This is a simple disconnecting-link element, so it is quite easy to extend it. The next one is a little more complicated- disconnecting link. Same as disconnecting link, it also has two statuses.
Since in the example above we have set several different directions to the earthed switches, we can also do the same to disconnecting link. Let’s see how to draw the earthed switch:

public void paintBody(Graphics2D g) {
    g.setStroke(TWaverConst.BASIC_STROKE);
    g.setColor(Color.black)

    EarthingSwitch earthingSwitch = (EarthingSwitch)this.getElement();
    final Point location = earthingSwitch.getLocation();

    boolean turnOn = earthingSwitch.isTurnOn();
    int position = earthingSwitch.getSwitchPosition();
    final int x = location.x;
    final int y = location.y;
    int width = earthingSwitch.getWidth();
    int height = earthingSwitch.getHeight();
    //draw body
    if(turnOn){
        if(position == Utils.SWITCH_POSITION_TOP){
            g.drawLine(x+width/4, y, x+width/4*3, y);
            g.drawLine(x+width/6, y+2, x+width/6*5, y+2);
            g.drawLine(x, y+4, x+width, y+4);
            g.drawLine(x+width/2, y+4, x+width/2, y+7);
            g.drawLine(x+width/2, y+7, x+width, y+height-10);
            g.drawLine(x+width/2, y+height-3, x+width/2, y+height);
        }else if(position == Utils.SWITCH_POSITION_LEFT){
            //transfer width and height
            int middle = width;
            width = height;
            height = middle;

            g.drawLine(x,y+height/4, x, y+height/4*3);
            g.drawLine(x+2, y+height/6, x+2, y+height/6*5);
            g.drawLine(x+4, y, x+4, y+height);
            g.drawLine(x+4, y+height/2, x+7, y+height/2);
            g.drawLine(x+7, y+height/2, x+width-10, y);
            g.drawLine(x+width-3, y+height/2, x+width, y+height/2);
        }else if(position == Utils.SWITCH_POSITION_BOTTOM){
            g.drawLine(x+width/4, y+height, x+width/4*3, y+height);
            g.drawLine(x+width/6, y+height-2, x+width/6*5, y+height-2);
            g.drawLine(x, y+height-4, x+width, y+height-4);
            g.drawLine(x+width/2, y+height-4, x+width/2, y+height-7);
            g.drawLine(x+width/2, y+height-7, x, y+10);
            g.drawLine(x+width/2, y+3, x+width/2, y);
        }else if(position == Utils.SWITCH_POSITION_RIGHT){
            //transfer width and height
            int middle = width;
            width = height;
            height = middle;

            g.drawLine(x+width,y+height/4, x+width, y+height/4*3);
            g.drawLine(x+width-2, y+height/6, x+width-2, y+height/6*5);
            g.drawLine(x+width-4, y, x+width-4, y+height);
            g.drawLine(x+width-4, y+height/2, x+width-7, y+height/2);
            g.drawLine(x+width-7, y+height/2, x+10, y);
            g.drawLine(x+3, y+height/2, x, y+height/2);
        }
    }else{
        if(position == Utils.SWITCH_POSITION_TOP){
            g.drawLine(x+width/4, y, x+width/4*3, y);
            g.drawLine(x+width/6, y+2, x+width/6*5, y+2);
            g.drawLine(x, y+4, x+width, y+4);
            g.drawLine(x+width/2, y+4, x+width/2, y+7);
            g.drawLine(x+width/2, y+7, x+width/2, y+height-3);
            g.drawLine(x+width/2, y+height-3, x+width/2, y+height);
        }else if(position == Utils.SWITCH_POSITION_LEFT){
            //transfer width and height
            int middle = width;
            width = height;
            height = middle;

            g.drawLine(x,y+height/4, x, y+height/4*3);
            g.drawLine(x+2, y+height/6, x+2, y+height/6*5);
            g.drawLine(x+4, y, x+4, y+height);
            g.drawLine(x+4, y+height/2, x+7, y+height/2);
            g.drawLine(x+7, y+height/2, x+width-3, y+height/2);
            g.drawLine(x+width-3, y+height/2, x+width, y+height/2);
        }else if(position == Utils.SWITCH_POSITION_BOTTOM){
            g.drawLine(x+width/4, y+height, x+width/4*3, y+height);
            g.drawLine(x+width/6, y+height-2, x+width/6*5, y+height-2);
            g.drawLine(x, y+height-4, x+width, y+height-4);
            g.drawLine(x+width/2, y+height-4, x+width/2, y+height-7);
            g.drawLine(x+width/2, y+height-7, x+width/2, y+3);
            g.drawLine(x+width/2, y+3, x+width/2, y);
        }else if(position == Utils.SWITCH_POSITION_RIGHT){
            //transfer width and height
            int middle = width;
            width = height;
            height = middle;

            g.drawLine(x+width,y+height/4, x+width, y+height/4*3);
            g.drawLine(x+width-2, y+height/6, x+width-2, y+height/6*5);
            g.drawLine(x+width-4, y, x+width-4, y+height);
            g.drawLine(x+width-4, y+height/2, x+width-7, y+height/2);
            g.drawLine(x+width-7, y+height/2, x+3, y+height/2);
            g.drawLine(x+3, y+height/2, x, y+height/2);
        }
    }
}
There are some other elements, such as switch, generator and transformer. These can be easily configured by customDraw provided by TWaver. These are very easy so here the codes are omitted. See the image directly:
With the basic elements, it seems not so complicated to draw a power-system interface. We have drawn a power-system image with tools in TWaver through the extended electric elements above.
At last, interactions can be added on the interface, for example: you can double-click to open or close the switch of disconnecting link and then add your own business. A simple power-system is thus finished.

network.addElementDoubleClickedActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        Element ele = network.getSelectionModel().lastElement();
        if(ele instanceof Switch){
            boolean turnOn = ((Switch)ele).isTurnOn();
            ((Switch)ele).setTurnOn(!turnOn);
        }
    }
});
In this way, the first two images of a substation can also be easily made. The following is another power-interface image made by TWaver.

No comments:

Post a Comment