📄 graphicsdata.java
字号:
package cn.work.zdj.ui;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import cn.work.zdj.sys.AutoMachine;
import cn.work.zdj.sys.IAppConstants;
public class GraphicsData extends ViewPart implements ISelectionListener{
private Color bgColor = new Color(Display.getCurrent(),0xe9,0xf9,0xf9);
private Canvas canvas;
private AutoMachine machine;
private int[][] stateLines;
private int a= 2;
public GraphicsData() {
// TODO 自动生成构造函数存根
}
@Override
public void createPartControl(Composite parent) {
// TODO 自动生成方法存根
canvas = new Canvas(parent,SWT.NONE);
IViewPart vp = getViewSite().getPage().findView(IAppConstants.OLD_ZDJ_VIEW_ID);
Table table = ((OldZdj)vp).getTableViewer().getTable();
machine = (AutoMachine)table.getItem(table.getSelectionIndex()).getData();
machine.placeMap();
stateLines = new int[machine.getStates().size()][machine.getStates().size()];
drawDiagram(parent);
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addSelectionListener(this);
}
@Override
public void setFocus() {
// TODO 自动生成方法存根
}
private void drawDiagram(Composite parent){
int xMargin = 30;
int yMargin = 50;
int ovalLength = 60;
//MessageDialog.openInformation(null,"提示",""+machine.getFunction().getStateLetters("q2").toString());
canvas.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e) {
// TODO 自动生成方法存根
e.gc.setBackground(bgColor);
e.gc.fillRectangle(0,0,canvas.getBounds().width,canvas.getBounds().height);
e.gc.drawText(""+canvas.getBounds().width+"*"+canvas.getBounds().height, 0, 0);
}
});
canvas.redraw();
int xSpan = 103;
int ySpan = 94;
//String test =null;
int xSlot = (xSpan - ovalLength)/2;
int ySlot = (ySpan - ovalLength)/2;
Hashtable stateAxis = new Hashtable();
for(int i=0;i<machine.getMap().length;i++){
for(int j=0;j<machine.getMap()[0].length;j++){
//test = test + machine.getMap()[i][j];
if(machine.getMap()[i][j] != 0){
int x = xMargin+j * xSpan + xSlot;
int y = yMargin+i * ySpan + ySlot;
String state = machine.getStates().get(machine.getMap()[i][j]-1);
boolean isEndState = machine.getEndStates().contains(state);
boolean isFirState = machine.getFirState().contains(state);
if(isFirState)
canvas.addPaintListener(new DrawLine("",x-40,y+ovalLength/2,x,y+ovalLength/2,10));
canvas.addPaintListener(new DrawOval(state,x,y,ovalLength,ovalLength,isEndState));
//MessageDialog.openInformation(null,"提示",""+machine.getEndStates());
Point point = new Point(x,y);
stateAxis.put(state, point);
}
}
//test =test+"|";
}
//MessageDialog.openInformation(null,"提示",""+parent.getBounds().width);
int arcWidth = 20;
int arcHeight = 20;
Enumeration e = stateAxis.keys();
while(e.hasMoreElements()){
String state = (String)e.nextElement();
Point point1 = (Point)stateAxis.get(state);
Hashtable table = (Hashtable)machine.getFunction().outputNextStatePossible(state);
if(table == null)
continue;
String letters = null;
String endState = null;
Enumeration e1 = table.keys();
while(e1.hasMoreElements()){
endState = (String)e1.nextElement();
letters = (String)table.get(endState);
if(state.equals(endState)){
int x = point1.x + (ovalLength-arcWidth)/2;
int y = point1.y - arcHeight;
canvas.addPaintListener(new DrawArc(letters,x,y,arcWidth,arcHeight,-60,330));
continue;
}
Point point2 = (Point)stateAxis.get(endState);
int x1,x2,y1,y2;
if(point1.x < point2.x){
x1 = point1.x + ovalLength;
y1 = point1.y + ovalLength/2;
x2 = point2.x ;
y2 = point2.y + ovalLength/2;
}else{
x1 = point1.x ;
y1 = point1.y + ovalLength/2;
x2 = point2.x + ovalLength;
y2 = point2.y + ovalLength/2;
}
int endStateIndex = machine.getStates().indexOf(endState);
int firStateIndex = machine.getStates().indexOf(state);
boolean isCollision = false;
if(y1 == y2){
if(firStateIndex > endStateIndex){
int temp = endStateIndex;
endStateIndex = firStateIndex;
firStateIndex = temp;
}
for(int k =firStateIndex+1; k<endStateIndex; k++){
if(stateLines[k][endStateIndex] !=0 || stateLines[endStateIndex][k] != 0){
isCollision = true;
break;
}
}
}
if(stateLines[machine.getStates().indexOf(endState)][machine.getStates().indexOf(state)] == 0
&& !isCollision){
canvas.addPaintListener(new DrawLine(letters,x1,y1,x2,y2,10));
stateLines[machine.getStates().indexOf(state)][machine.getStates().indexOf(endState)] =1;
}else{
stateLines[machine.getStates().indexOf(state)][machine.getStates().indexOf(endState)] = 1;
double bevelEdge = Math.sqrt(Math.pow((x2-x1), 2)+Math.pow((y2-y1), 2));
double xEdge = x2 - x1;
if(y2 > y1)
xEdge = Math.abs(x2 - x1);
double rad_angle2 = Math.asin(xEdge/bevelEdge);
double rad_angle = Math.PI/3- rad_angle2;
double deg_angle = Math.toDegrees(rad_angle);
int x3 = x2 + (int)(Math.sin(rad_angle) * bevelEdge);
int y3 = y2 + (int)(Math.cos(rad_angle) * bevelEdge);
if(y2 > y1){
x3 = x1 + (int)(Math.sin(rad_angle) * bevelEdge);
y3 = y1 + (int)(Math.cos(rad_angle) * bevelEdge);
}
int startX = x3 - (int)bevelEdge;
int startY = y3 - (int)bevelEdge;
//MessageDialog.openError(null,"提示",state+":"+endState+":"+letters+":"+(90+deg_angle));
canvas.addPaintListener(new DrawArc(letters,startX,startY,(int)bevelEdge*2,(int)bevelEdge*2,(int)(90+deg_angle),60));
final int xArrow = x3 - (int)(Math.cos(rad_angle2)*bevelEdge);
final int yArrow = y3 - (int)(Math.sin(rad_angle2)*bevelEdge);
double rad_arrow = Math.toRadians(20.0);
double srad_arrow = rad_angle2 - rad_arrow;
double brad_arrow = rad_angle2 + rad_arrow;
int arrowLength = 10;
final int arrowX1 = xArrow - (int)(Math.sin(srad_arrow)*arrowLength);
final int arrowY1 = yArrow + (int)(Math.cos(srad_arrow)*arrowLength);
final int arrowX2 = xArrow - (int)(Math.sin(brad_arrow)*arrowLength);
final int arrowY2 = yArrow + (int)(Math.cos(brad_arrow)*arrowLength);
canvas.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e) {
// TODO 自动生成方法存根
e.gc.drawLine(arrowX1, arrowY1, xArrow, yArrow);
e.gc.drawLine(arrowX2, arrowY2, xArrow, yArrow);
}
});
}
}
}
}
private class Point{
int x;
int y;
public Point(int x,int y){
this.x = x;
this.y = y;
}
}
private class DrawArc implements PaintListener{
private int x,y,width,height,startAngle,arcAngle;
private String letter;
public DrawArc(String letter,int x,int y,int width,int height,int startAngle,int arcAngle){
this.x = x;
this.y = y;
this.letter = letter;
this.width = width;
this.height = height;
this.startAngle = startAngle;
this.arcAngle = arcAngle;
}
public void paintControl(PaintEvent e) {
// TODO 自动生成方法存根
e.gc.drawArc(x, y, width, height, startAngle, arcAngle);
int centerX = x+width/2;
int centerY = y+height/2;
startAngle = startAngle>0?startAngle:startAngle+360;
int tempAngle = 180- startAngle - arcAngle;
int deg_angle = arcAngle/2 + tempAngle;
double rad_angle = Math.toRadians(deg_angle);
int pointX = centerX - (int)(Math.cos(rad_angle) * (height/2));
int pointY = centerY - (int)(Math.sin(rad_angle) * (height/2));
e.gc.drawString(""+letter, pointX, pointY-15);
//e.gc.drawString(""+letter+":"+x+":"+y+":"+(height/2)+":"+pointX+":"+pointY, pointX, pointY-15);
}
}
private class DrawOval implements PaintListener{
private String state;
private int x,y,width,height;
private boolean isEndState,isFirState;
public DrawOval(String state,int x,int y,int width,int height,boolean isEndState){
this.state = state;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.isEndState = isEndState;
}
public void paintControl(PaintEvent e) {
// TODO 自动生成方法存根
e.gc.drawOval(x, y, width, height);
if(isEndState)
e.gc.drawOval(x+5,y+5,width-10,height-10);
int xTxt = x + width/2 -10;
int yTxt = y + height/2 -5;
e.gc.drawString(state, xTxt, yTxt);
}
}
private class DrawLine implements PaintListener{
private String letter;
private int x1,y1,x2,y2;
private int arrowLength;
public DrawLine(String letter,int x1,int y1,int x2,int y2,int arrowLength){
this.letter = letter;
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.arrowLength = arrowLength;
}
public void paintControl(PaintEvent e) {
// TODO 自动生成方法存根
e.gc.drawLine(x1, y1, x2, y2);
int xMidPoint = (x2+x1)/2;
int yMidPoint = (y2+y1)/2;
e.gc.drawString(""+letter, xMidPoint, yMidPoint-15);
int xEdge = x2 - x1;
int yEdge = y2 - y1;
double rad_arrow = Math.toRadians(20.0);
double bevelEdge = Math.sqrt(Math.pow(xEdge,2)+Math.pow(yEdge,2));
double rad_angle = Math.asin((double)(yEdge)/bevelEdge);
double brad_angle = rad_angle+rad_arrow;
double srad_angle = rad_angle-rad_arrow;
int xArrow1 = x2 - (int)(Math.cos(brad_angle)*arrowLength);
int yArrow1 = y2 - (int)(Math.sin(brad_angle)*arrowLength);
int xArrow2 = x2 - (int)(Math.cos(srad_angle)*arrowLength);
if(x2 < x1){
xArrow2 = x2 + (int)(Math.cos(srad_angle)*arrowLength);
xArrow1 = x2 + (int)(Math.cos(brad_angle)*arrowLength);
}
int yArrow2 = y2 - (int)(Math.sin(srad_angle)*arrowLength);
e.gc.drawLine(xArrow1, yArrow1, x2, y2);
e.gc.drawLine(xArrow2, yArrow2, x2, y2);
}
}
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
// TODO 自动生成方法存根
if(!selection.isEmpty()){
refreshView();
}
}
private void refreshView(){
IViewPart vp = getViewSite().getPage().findView(IAppConstants.OLD_ZDJ_VIEW_ID);
Table table = ((OldZdj)vp).getTableViewer().getTable();
machine = (AutoMachine)table.getItem(table.getSelectionIndex()).getData();
machine.placeMap();
stateLines = new int[machine.getStates().size()][machine.getStates().size()];
drawDiagram(getViewSite().getShell());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -