TWaver作为一款专业的图形界面控件,可以应用在各行各业中。本文我们就来详细介绍一下TWaver如何绘制出电力系统的界面。我们现在看看几张电力系统的图:
某变电站设备接线图一
某变电所的接线图二
当然这些是我在网上搜索的一些资源,那么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); } } }
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); } }
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); } } }
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); } } });