📄 framefactory.java
字号:
if (verts == null)
{
String message = Logging.getMessage("nullValue.BufferIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
int count = verts.remaining() / 2;
drawBuffer(dc, mode, count, verts);
}
//-- Shape creation
//-- Rectangle ------------------------------------------------------------------
private static DoubleBuffer createRoundedRectangleBuffer(double width, double height, int cornerRadius, DoubleBuffer buffer)
{
int numVertices = 9 + (cornerRadius < 1 ? 0 : 4 * (cornerSteps - 2));
buffer = allocateVertexBuffer(numVertices, buffer);
int idx = 0;
// Drawing counter clockwise from bottom-left
// Bottom
buffer.put(idx++, (double)cornerRadius);
buffer.put(idx++, 0d);
buffer.put(idx++, width - cornerRadius);
buffer.put(idx++, 0d);
idx = drawCorner(width - cornerRadius, cornerRadius, cornerRadius, -Math.PI / 2, 0, cornerSteps, buffer, idx);
// Right
buffer.put(idx++, width);
buffer.put(idx++, (double)cornerRadius);
buffer.put(idx++, width);
buffer.put(idx++, height - cornerRadius);
idx = drawCorner(width - cornerRadius, height - cornerRadius, cornerRadius, 0, Math.PI / 2, cornerSteps, buffer, idx);
// Top
buffer.put(idx++, width - cornerRadius);
buffer.put(idx++, height);
buffer.put(idx++, (double)cornerRadius);
buffer.put(idx++, height);
idx = drawCorner(cornerRadius, height - cornerRadius, cornerRadius, Math.PI / 2, Math.PI, cornerSteps, buffer, idx);
// Left
buffer.put(idx++, 0d);
buffer.put(idx++, height - cornerRadius);
buffer.put(idx++, 0d);
buffer.put(idx++, (double)cornerRadius);
idx = drawCorner(cornerRadius, cornerRadius, cornerRadius, Math.PI, Math.PI * 1.5, cornerSteps, buffer, idx);
// Finish up to starting point
buffer.put(idx++, (double)cornerRadius);
buffer.put(idx++, 0d);
buffer.limit(idx);
return buffer;
}
private static DoubleBuffer createRoundedRectangleWithLeaderBuffer(double width, double height, Point leaderOffset, int cornerRadius, DoubleBuffer buffer)
{
int numVertices = 12 + (cornerRadius < 1 ? 0 : 4 * (cornerSteps - 2));
buffer = allocateVertexBuffer(numVertices, buffer);
int idx = 0;
// Drawing counter clockwise from right leader connection at the bottom
// so as to accomodate GL_TRIANGLE_FAN and GL_LINE_STRIP (inside and border)
// Bottom right
buffer.put(idx++, width / 2 + leaderGapWidth / 2);
buffer.put(idx++, 0d);
buffer.put(idx++, width - cornerRadius);
buffer.put(idx++, 0d);
idx = drawCorner(width - cornerRadius, cornerRadius, cornerRadius, -Math.PI / 2, 0, cornerSteps, buffer, idx);
// Right
buffer.put(idx++, width);
buffer.put(idx++, (double)cornerRadius);
buffer.put(idx++, width);
buffer.put(idx++, height - cornerRadius);
idx = drawCorner(width - cornerRadius, height - cornerRadius, cornerRadius, 0, Math.PI / 2, cornerSteps, buffer, idx);
// Top
buffer.put(idx++, width - cornerRadius);
buffer.put(idx++, height);
buffer.put(idx++, (double)cornerRadius);
buffer.put(idx++, height);
idx = drawCorner(cornerRadius, height - cornerRadius, cornerRadius, Math.PI / 2, Math.PI, cornerSteps, buffer, idx);
// Left
buffer.put(idx++, 0d);
buffer.put(idx++, height - cornerRadius);
buffer.put(idx++, 0d);
buffer.put(idx++, (double)cornerRadius);
idx = drawCorner(cornerRadius, cornerRadius, cornerRadius, Math.PI, Math.PI * 1.5, cornerSteps, buffer, idx);
// Bottom left
buffer.put(idx++, (double)cornerRadius);
buffer.put(idx++, 0d);
buffer.put(idx++, width / 2 - leaderGapWidth / 2);
buffer.put(idx++, 0d);
// Draw leader
buffer.put(idx++, leaderOffset.x);
buffer.put(idx++, leaderOffset.y);
buffer.put(idx++, width / 2 + leaderGapWidth / 2);
buffer.put(idx++, 0d);
buffer.limit(idx);
return buffer;
}
private static int drawCorner(double x0, double y0, double cornerRadius, double start, double end, int steps, DoubleBuffer buffer, int startIdx)
{
if(cornerRadius < 1)
return startIdx;
double step = (end - start) / (steps - 1);
for(int i = 1; i < steps - 1; i++)
{
double a = start + step * i;
double x = x0 + Math.cos(a) * cornerRadius;
double y = y0 + Math.sin(a) * cornerRadius;
buffer.put(startIdx++, x);
buffer.put(startIdx++, y);
}
return startIdx;
}
//-- Circle / Ellipse -----------------------------------------------------------
private static DoubleBuffer createEllipseBuffer(double width, double height, int steps, DoubleBuffer buffer)
{
int numVertices = steps + 1;
buffer = allocateVertexBuffer(numVertices, buffer);
// Drawing counter clockwise from bottom-left
double halfWidth = width / 2;
double halfHeight = height / 2;
double halfPI = Math.PI / 2;
double x0 = halfWidth;
double y0 = halfHeight;
double step = Math.PI * 2 / steps;
int idx = 0;
for(int i = 0; i <= steps; i++)
{
double a = step * i - halfPI;
double x = x0 + Math.cos(a) * halfWidth;
double y = y0 + Math.sin(a) * halfHeight;
buffer.put(idx++, x);
buffer.put(idx++, y);
}
buffer.limit(idx);
return buffer;
}
private static DoubleBuffer createEllipseWithLeaderBuffer(double width, double height, Point leaderOffset, int steps, DoubleBuffer buffer)
{
int numVertices = steps + 3;
buffer = allocateVertexBuffer(numVertices, buffer);
// Drawing counter clockwise from right leader connection at the bottom
// so as to accomodate GL_TRIANGLE_FAN and GL_LINE_STRIP (inside and border)
double halfWidth = width / 2;
double halfHeight = height / 2;
double halfPI = Math.PI / 2;
double x0 = halfWidth;
double y0 = halfHeight;
double step = Math.PI * 2 / steps;
double halfGap = leaderGapWidth / 2 / halfWidth;
int idx = 0;
for(int i = 0; i <= steps; i++)
{
double a = step * i - halfPI;
if (i == 0) a += halfGap;
if (i == steps) a -= halfGap;
double x = x0 + Math.cos(a) * halfWidth;
double y = y0 + Math.sin(a) * halfHeight;
buffer.put(idx++, x);
buffer.put(idx++, y);
}
// Draw leader
buffer.put(idx++, leaderOffset.x);
buffer.put(idx++, leaderOffset.y);
buffer.put(idx++, x0 + Math.cos(halfGap - halfPI) * halfWidth);
buffer.put(idx++, y0 + Math.sin(halfGap - halfPI) * halfHeight);
buffer.limit(idx);
return buffer;
}
//-- Utility Methods
private static DoubleBuffer allocateVertexBuffer(int numVertices, DoubleBuffer buffer)
{
int numCoords = 2 * numVertices;
if (buffer != null)
buffer.clear();
if (buffer == null || buffer.capacity() < numCoords)
buffer = BufferUtil.newDoubleBuffer(numCoords);
return buffer;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -