哪些用Swing去绘制电力系统图

TWaver作为一款专业的图形界面控件,能够利用在各行各业中。本文大家就来详细介绍一下TWaver咋样绘制出电力系统的界面。我们现在看望几张电力系统的图: 

图片 1

某变电站设备接线图一

图片 2

某变电所的接线图二

自然这么些是本人在网上搜索的一对资源,那么TWaver是否能促成如此的界面呢?回答是自然的,咱们先来分析一下,电力电气系统中这么些界面都是由众多电气元素组成的,比如变压器,刀闸,开关,接地开关,电容器,发电机等。
接下来大家看看如何去扩张这些电气元素,先整个大概的刀闸,刀闸一般有二种情景,打开关闭,由此我们得以定义一个刀闸的类,继承于ResizableNode,添加turnOn的习性来支配开关状态

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);
        }
    }
}

随之需要在UI上需要依照turnOn的性质来绘制开关的形象,下面贴一下第一的paint方法

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);
    }
}

由此一个main来运行看看扩大出来的坚守:

图片 3

这是简简单单的刀闸元素,扩大起来依然相比易于的。接下来来个稍微复杂一点的–接地开关,同样它和刀闸一样,也有二种情形 

图片 4

其一例子中我们给接地开关设置了多少个不同的主旋律,同样下面的也得以这么做一下处理,我们来探望如何绘制这些接地开关的

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);
        }
    }
}

还有部分其他的要素,比如开关,发电机,变压器这一个足以直接通过TWaver提供的customDraw很容易的配置出来,那么些就相比较简单了,此处代码省略100字,直接上图了 

有了那一个基础的元素,那么来绘制电力系统界面就显示不是那么复杂了,我们透过上面增添的这些电力元素用twaver的工具画出了一个电力系统图,如下:

图片 5

最后我们可以在界面上添加交互,比如双击打开关闭刀闸开关,然后添加自己的事务,这样一个大概的电力系统界面就完了了

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);
        }
    }
});

无异于初始贴出来的两张变电所的图是不是也就很容易的做出来呢!最后再黏附TWaver做的另一张电力界面图

图片 6