gpt4 book ai didi

java - 从字符串数组到 block 图

转载 作者:行者123 更新时间:2023-12-01 14:10:59 25 4
gpt4 key购买 nike

我编写了一个程序,它通过 Jena 获取一些 SPARQL 查询的结果并将它们保存在二维字符串(即二维字符串数组)中。我只想获取第一列的值并设计一个 block 图,其中每个 block 包含第一列的每个值并将它们依次相互链接。

据我所知,JGraph似乎对此很有帮助,但我下载了它并尝试这样做,但失败了。

我如何使用 JGraph 做到这一点,或者还有其他方法吗?

something like this

最佳答案

这是我整理的一个方法,它将绘制一个矩形,用颜色填充它,然后将一个字符串放在矩形的中心。

/**
* <p>This method will draw a rectangle and place the text in the center
* of the rectangle.</p>
* @param g - Graphics instance from a JPanel paintComponent method
* @param r - Rectangle (origin and dimension) of the rectangle.
* @param c - Fill color of the rectangle.
* @param s - String to place at the center of the rectangle.
*/
public void drawBox(Graphics g, Rectangle r, Color c, String s) {
g.setColor(c);
g.fillRect(r.x, r.y, r.width, r.height);

Graphics2D g2d = (Graphics2D) g;
FontRenderContext frc = g2d.getFontRenderContext();
TextLayout layout = new TextLayout(s, g.getFont(), frc);
Rectangle2D bounds = layout.getBounds();

int width = (int) Math.round(bounds.getWidth());
int height = (int) Math.round(bounds.getHeight());
int x = r.x + (r.width - width) / 2;
int y = r.y + height + (r.height - height) / 2;

g.setColor(Color.BLACK);
layout.draw(g2d, (float) x, (float) y);
}

您必须弄清楚需要矩形的位置以及如何将它们与瘦矩形连接起来。

关于java - 从字符串数组到 block 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18530965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com