📄 graphics.c
字号:
/*****************************************************************************
*** Project: GUI for a Computer Network Simulator ***
*** Author: Hussein F. Salama ***
*** Date: September 9, 1994 ***
*** File: graphics.c ***
*** A C++ library of classes to represent network nodes, links, paths, ***
*** .... etc graphically
*****************************************************************************/
#include "graphics.h"
graphics::graphics(Widget da) {
// Note: graphics assumes that the drawing area widget has already been
//initialized
Arg al[10];
int ac;
XGCValues vals;
/* get the current fg and bg colors */
w = da;
ac = 0;
XtSetArg(al[ac], XmNforeground, &foreground); ac++;
XtSetArg(al[ac], XmNbackground, &background); ac++;
XtGetValues(da, al, ac);
/* create the copy gc for drawing normal width lines */
vals.foreground = foreground;
vals.background = background;
gcCopy = XtGetGC(da, GCForeground | GCBackground, &vals);
vals.line_width = 3;
gcThickCopy = XtGetGC(da, GCForeground | GCBackground | GCLineWidth, &vals);
};
void graphics::drawPoint(int x, int y, int size) {
// x & y are the node position in pixels
// draws a filled circle of radius 5 pixels
int xx, yy;
xx = x - size;
yy = y - size;
XFillArc(XtDisplay(w), XtWindow(w), gcCopy, xx, yy, (2*size), (2*size),
0, 23040);
};
void graphics::drawSquare(int x, int y, int size) {
// x & y are the node position in pixels
// draws a filled circle of radius 5 pixels
int xx, yy;
xx = x - size;
yy = y - size;
XDrawRectangle(XtDisplay(w), XtWindow(w), gcCopy, xx, yy, (2 * size),
(2 * size));
};
// In the remaining functions, all values are in pixels
// functions to draw and delete straight lines
void graphics::drawLine(int x1, int y1, int x2, int y2) {
XDrawLine(XtDisplay(w), XtWindow(w), gcCopy, x1, y1, x2, y2);
};
void graphics::drawThickLine(int x1, int y1, int x2, int y2) {
XDrawLine(XtDisplay(w), XtWindow(w), gcThickCopy, x1, y1, x2, y2);
};
void graphics::drawName(int x, int y, char* s) {
XDrawString(XtDisplay(w), XtWindow(w), gcCopy, x, y, s, strlen(s));
};
void graphics::drawNode(Node *n, int status) {
drawPoint((int)(n->X() * SIDE + 10), (int)(n->Y() * SIDE + 15), 4);
if (status == True) {
char *s = new char[10];
sprintf(s, "%d", n->name());
drawName((int)(n->X() * SIDE + 17), (int)(n->Y() * SIDE + 8), s);
};
};
void graphics::drawDestination(Node *n) {
drawPoint((int)(n->X() * SIDE + 10), (int)(n->Y() * SIDE + 15), 6);
};
void graphics::drawSource(Node *n, int j) {
drawSquare((int)(n->X() * SIDE + 10), (int)(n->Y() * SIDE + 15),
4 + 2 * j);
};
void graphics::drawLink(Node *n1, Node *n2, double w12, double w21) {
char *s = new char[20];
sprintf(s, "%.1lf/%.1lf", w12 / 1e6, w21 / 1e6);
drawLine((int)(n1->X() * SIDE + 10), (int)(n1->Y() * SIDE + 15),
(int)(n2->X() * SIDE + 10), (int)(n2->Y() * SIDE + 15));
int x = (int)((n1->X() * SIDE + 10 + n2->X() * SIDE + 10) / 2);
int y = (int)((n1->Y() * SIDE + 15 + n2->Y() * SIDE + 15) / 2);
drawName(x + 3, y + 3, s);
};
void graphics::drawLink(Node *n1, Node *n2) {
drawLine((int)(n1->X() * SIDE + 10), (int)(n1->Y() * SIDE + 15),
(int)(n2->X() * SIDE + 10), (int)(n2->Y() * SIDE + 15));
};
void graphics::drawPath(Node *n1, Node *n2) {
drawThickLine((int)(n1->X() * SIDE + 10),
(int)(n1->Y() * SIDE + 15),
(int)(n2->X() * SIDE + 10),
(int)(n2->Y() * SIDE + 15));
};
void graphics::clearScreen() {
int width, height;
int ac;
Arg al[10];
// get the screen size
ac = 0;
XtSetArg(al[ac], XmNheight, &height); ac++;
XtSetArg(al[ac], XmNwidth, &width); ac++;
XtGetValues(w, al, ac);
XClearArea(XtDisplay(w), XtWindow(w), 0, 0, width, height, False);
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -