📄 visofgraph.java
字号:
drawPlaces(PNet, gBB);
drawTransitions(PNet, gBB);
drawEdges(PNet, gBB);
g.drawImage(BackBuffer, 0, 0, ViewDim.width, ViewDim.height, null);
}
public visOfGraph(){
this.setVisible(true);
this.setSize(400, 450);
}
//
//
//
//
//
public boolean drawPlaces(pn PN, Graphics g){
for (int i = 0; i < PN.numberOfPlaces(); i++) {
place n;
n = PN.getPlace(i);
if (iDeleteNode == -1)
g.setColor(n.getColor());
else if (iDeleteNode == i)
g.setColor(Color.lightGray);
else
g.setColor(n.getColor());
g.drawOval(n.getX() - n.getRadius(),
n.getY() - n.getRadius(),
2 * n.getRadius(),
2 * n.getRadius());
if (n.getTokens() == 1) {
g.fillRect(n.getX() - (TokenW / 2), n.getY() - (TokenH / 2), TokenW, TokenH);
}
else if (n.getTokens() == 2) {
g.fillRect(n.getX() + (TokenW / 2), n.getY() - (TokenH / 2), TokenW, TokenH);
g.fillRect(n.getX() - (3 * (TokenW / 2)), n.getY() - (TokenH / 2), TokenW, TokenH);
}
else if (n.getTokens() == 3) {
g.fillRect(n.getX() - (5 * (TokenW / 2)), n.getY() - (TokenH / 2), TokenW, TokenH);
g.fillRect(n.getX() - (TokenW / 2), n.getY() - (TokenH / 2), TokenW, TokenH);
g.fillRect(n.getX() + (3 * (TokenW / 2)), n.getY() - (TokenH / 2), TokenW, TokenH);
}
else if (n.getTokens() > 3 && n.getTokens() < 10) {
Font f = g.getFont();
int size = f.getSize();
g.drawString(Integer.toString(n.getTokens()), n.getX() - (size / 4), n.getY() + (size / 2));
}
else if (n.getTokens() > 9) {
Font f = g.getFont();
int size = f.getSize();
g.drawString(Integer.toString(n.getTokens()), n.getX() - (size / 2), n.getY() + (size / 2));
}
// display name of node
String s = n.getName();
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(s);
int h = fm.getHeight();
n.setXName(n.getX() - (w / 2));
n.setYName(n.getY() - (n.getRadius() / 2) - h);
g.drawString(s, n.getXName(), n.getYName());
}
return true;
}
private boolean drawTransitions(pn PN, Graphics g) {
for (int i = 0; i < PN.numberOfTransitions(); i++) {
transition t;
t = PN.getTransition(i);
if (iDeleteTransition != -1 && iDeleteTransition == i)
g.setColor(Color.lightGray);
else {
if (colorMode == COLORMODE_BLACKANDWHITE)
g.setColor(Color.black);
else if (colorMode == COLORMODE_STANDARD)
g.setColor(Color.blue);
}
if (t.highlight) g.setColor(Color.yellow);
switch(t.getOrientation()) {
case transition.ORIENTATION_VERTICAL:
g.fillRect(t.getX() - (t.getWidth() / 2),
t.getY() - (t.getHeight() / 2),
t.getWidth(),
t.getHeight());
break;
case transition.ORIENTATION_DIAGONAL1:
{
int[] xpoints = new int[4];
int[] ypoints = new int[4];
double Sin45 = 0.707106781;
xpoints[0] = (int) (t.getX() + ((-t.getWidth() / 2.0) * Sin45 + (-t.getHeight() / 2.0) * Sin45));
ypoints[0] = (int) (t.getY() + ((-t.getWidth() / 2.0) * Sin45 + (-t.getHeight() / 2.0) * (-Sin45)));
xpoints[1] = (int) (t.getX() + (( t.getWidth() / 2.0) * Sin45 + (-t.getHeight() / 2.0) * Sin45));
ypoints[1] = (int) (t.getY() + (( t.getWidth() / 2.0) * Sin45 + (-t.getHeight() / 2.0) * (-Sin45)));
xpoints[2] = (int) (t.getX() + (( t.getWidth() / 2.0) * Sin45 + ( t.getHeight() / 2.0) * Sin45));
ypoints[2] = (int) (t.getY() + (( t.getWidth() / 2.0) * Sin45 + ( t.getHeight() / 2.0) * (-Sin45)));
xpoints[3] = (int) (t.getX() + ((-t.getWidth() / 2.0) * Sin45 + ( t.getHeight() / 2.0) * Sin45));
ypoints[3] = (int) (t.getY() + ((-t.getWidth() / 2.0) * Sin45 + ( t.getHeight() / 2.0) * (-Sin45)));
g.fillPolygon(xpoints, ypoints, 4);
}
break;
case transition.ORIENTATION_HORIZONTAL:
g.fillRect(t.getX() - (t.getHeight() / 2),
t.getY() - (t.getWidth() / 2),
t.getHeight(),
t.getWidth());
break;
case transition.ORIENTATION_DIAGONAL2:
{
int[] xpoints = new int[4];
int[] ypoints = new int[4];
double Sin45 = 0.707106781;
xpoints[0] = (int) (t.getX() + ((-t.getWidth() / 2.0) * Sin45 + (-t.getHeight() / 2.0) * Sin45));
ypoints[0] = (int) (t.getY() + ((-t.getWidth() / 2.0) * (-Sin45) + (-t.getHeight() / 2.0) * Sin45));
xpoints[1] = (int) (t.getX() + (( t.getWidth() / 2.0) * Sin45 + (-t.getHeight() / 2.0) * Sin45));
ypoints[1] = (int) (t.getY() + (( t.getWidth() / 2.0) * (-Sin45) + (-t.getHeight() / 2.0) * Sin45));
xpoints[2] = (int) (t.getX() + (( t.getWidth() / 2.0) * Sin45 + ( t.getHeight() / 2.0) * Sin45));
ypoints[2] = (int) (t.getY() + (( t.getWidth() / 2.0) * (-Sin45) + ( t.getHeight() / 2.0) * Sin45));
xpoints[3] = (int) (t.getX() + ((-t.getWidth() / 2.0) * Sin45 + ( t.getHeight() / 2.0) * Sin45));
ypoints[3] = (int) (t.getY() + ((-t.getWidth() / 2.0) * (-Sin45) + ( t.getHeight() / 2.0) * Sin45));
g.fillPolygon(xpoints, ypoints, 4);
}
break;
case transition.ORIENTATION_ALL:
for (int j = 0; j < t.getWidth(); j++) {
g.drawRect(t.getX() - (t.getHeight() / 2) + j,
t.getY() - (t.getHeight() / 2) + j,
t.getHeight() - (2*j),
t.getHeight() - (2*j));
}
break;
}
// display name of transition
String s = t.getName();
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(s);
int h = fm.getHeight();
t.setXName(t.getX() - (w / 2));
t.setYName(t.getY() - (t.getHeight() / 2) - h);
g.drawString(s, t.getXName(), t.getYName());
}
return true;
}
private boolean drawEdges(pn PN, Graphics g) {
int iDel = 0;
for (int i = 0; i < PN.numberOfEdges(); i++) {
edge e;
e = PN.getEdge(i);
if (iDel < nDeleteEdge && iDeleteEdge[iDel] == i) {
g.setColor(Color.lightGray);
iDel++;
}
else
g.setColor(Color.black);
drawEdge(e, g, PN);
}
return true;
}
private boolean drawEdge(edge e, Graphics g, pn PN) {
Polygon points = e.getPoints();
//
if (e.getTFrom() == e.getTTo()) {
if (colorMode == COLORMODE_BLACKANDWHITE)
g.setColor(Color.lightGray);
else if (colorMode == COLORMODE_STANDARD)
g.setColor(Color.red);
if (e.getIFrom() == e.getITo() && points != null) {
if (points.npoints <= 2)
return false;
}
}
// edges that will not be drawn
if (e.getTFrom() == edge.NOTHING)
return false;
if (e.getTTo() == edge.NOTHING && points == null)
return false;
Point pFrom, pTo;
pFrom = e.getXYFrom(PN);
pTo = e.getXYTo(PN);
/* if (e.getTFrom() == edge.TRANSITION) {
transition t = PN.getTransition(e.getIFrom());
if (t.getOrientation() == transition.ORIENTATION_ALL) {
pFrom = e.getXYFrom(PN, 15.0);
pTo = e.getXYTo(PN, 15.0);
} else {
pFrom = e.getXYFrom(PN);
pTo = e.getXYTo(PN);
}
} else {
pFrom = e.getXYFrom(PN);
pTo = e.getXYTo(PN);
}
*/
// if edge is negative, the edge is drawn further away from the transition,
// so it doesnt stick out of the oval at the end of the edge
if (e.isNegated() == true) {
if (e.getTFrom() == edge.TRANSITION) {
pFrom = e.getXYFrom(PN, 6.0);
g.fillOval(pFrom.x - 4, pFrom.y - 4, 8, 8);
}
else if (e.getTTo() == edge.TRANSITION) {
pTo = e.getXYTo(PN, 6.0);
g.fillOval(pTo.x - 4, pTo.y - 4, 8, 8);
}
}
// if edge is build out of more than one line draw, draw all lines except
// the last one (which will be an arrow or a just line again)
if (points != null) {
points.xpoints[0] = pFrom.x;
points.ypoints[0] = pFrom.y;
g.drawPolygon(points);
// x and y position for the last line are adjusted
pFrom.x = points.xpoints[points.npoints-1];
pFrom.y = points.ypoints[points.npoints-1];
}
// draw arrow or line (the line already got the oval at the transition)
if (e.isNegated() == false) {
drawArrow(g, pFrom, pTo);
}
else {
g.drawLine(pFrom.x, pFrom.y, pTo.x, pTo.y);
}
// the weight of the edge will be drawn if the weight of the edge
// is higher than 1 and not negated
if (e.getWeight() > 1 && e.isNegated() == false) {
Point weightPos = e.getWeightPosition(PN);
g.drawString("" + e.getWeight(), weightPos.x, weightPos.y);
}
return true;
}
private static void drawArrow(Graphics g, Point p1, Point p2) {
// System.out.println("drawArrow from (" + p1.x + ", " + p1.y + ") to (" + p2.x + ", " + p2.y + ")");
drawArrow(g, p1.x, p1.y, p2.x, p2.y);
}
private static void drawArrow(Graphics g, int xS, int yS, int xE, int yE) {
// variables to store the x, y koordinates of the polygon
// forming the head of the arrow.
int xP[] = new int[3];
int yP[] = new int[3];
if (xE < 0.0)
xP[0] = (int) (xE - 1.0);
else
xP[0] = (int) (xE + 1.0);
if (yE < 0.0)
yP[0] = (int) (yE - 1.0);
else
yP[0] = (int) (yE + 1.0);
double dx = xS - xE;
double dy = yS - yE;
// System.out.println(xS + " - " + xE + " = " + dx);
// System.out.println(yS + " - " + yE + " = " + dy);
double length = Math.sqrt(dx * dx + dy * dy);
double xAdd = 9.0 * dx / length;
double yAdd = 9.0 * dy / length;
xP[1] = (int) Math.round (xE + xAdd - (yAdd / 3.0));
yP[1] = (int) Math.round (yE + yAdd + (xAdd / 3.0));
xP[2] = (int) Math.round (xE + xAdd + (yAdd / 3.0));
yP[2] = (int) Math.round (yE + yAdd - (xAdd / 3.0));
// System.out.println(xS + " " + yS + ", " + xE + " " + yE + ", " + xAdd + " " + yAdd);
g.drawLine(xS, yS, xP[0], yP[0]);
g.fillPolygon(xP, yP, 3);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -