[转载]如何用Swing去绘制电力系统图

[转载]如何用Swing去绘制电力系统图 – twaver – 博客园.

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

接着需要在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来运行看看扩展出来的效果:

这是简单的刀闸元素,扩展起来还是比较容易的。接下来来个稍微复杂一点的–接地开关,同样它和刀闸一样,也有两种状态

这个例子中我们给接地开关设置了几个不同的方向,同样上面的也可以这么做一下处理,我们来看看如何绘制这个接地开关的

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的工具画出了一个电力系统图,如下:

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

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做的另一张电力界面图

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏