Java - 在 Swing 中加入多个绘图字符串

发布于 2025-01-04 00:19:55 字数 254 浏览 1 评论 0原文

在 Swing 的面板中,我使用 PaintComponent 使用 Graphics2D 绘制具有不同坐标的字符串:

g2.drawString("one", 0, 0);
g2.drawString("two", 50, 50);

有没有一种方法可以将多个生成的绘图合并到一个绘图字符串中?

编辑:我基本上使用 unicode 字符绘制一个五线谱,并且我想绘制另一个五线谱。我希望有一种干净的方式来复制它。

In Swing, in a panel, I use paintComponent to draw strings with different coordinates using Graphics2D:

g2.drawString("one", 0, 0);
g2.drawString("two", 50, 50);

Is there a way to combine the multiple resulting drawings into one drawString?

Edit: I basically draw a musical stave using unicode characters, and I want to draw another stave. I was hoping there would be a clean way of duplicating it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待圉鍢 2025-01-11 00:19:55

示例代码。

private BufferedImage sample; //declare as class member to reuse instance

@Override
protected void paintComponent(Graphics g) {
    if (sample == null) { // lazy initialization, but you could do it even in constructor
        sample = new BufferedImage(sampleWidth, sampleHeight, bufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = sample.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, sampleWidth, sampleHeight);
        g2d.setColor(Color.BLACK);
        g2d.drawString("Some text", 10, 10);
        g2d.drawWhateverYouNeed(....);
    }

    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    // draw sample image three times, in sequence
    for (int i = 0; i < 3; i++) { 
        g.drawImage(sample, 0, i * sampleHeight, this);
    }
}

Sample code.

private BufferedImage sample; //declare as class member to reuse instance

@Override
protected void paintComponent(Graphics g) {
    if (sample == null) { // lazy initialization, but you could do it even in constructor
        sample = new BufferedImage(sampleWidth, sampleHeight, bufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = sample.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, sampleWidth, sampleHeight);
        g2d.setColor(Color.BLACK);
        g2d.drawString("Some text", 10, 10);
        g2d.drawWhateverYouNeed(....);
    }

    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    // draw sample image three times, in sequence
    for (int i = 0; i < 3; i++) { 
        g.drawImage(sample, 0, i * sampleHeight, this);
    }
}
献世佛 2025-01-11 00:19:55

不,没有办法做到这一点。但您希望通过这种结合实现什么目标?更好的性能?一些具体的布局?

No, there is no way to do it. But what do you wish to achieve with such combining? Better performance? Some specific layout?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文