1

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

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

有没有办法将多个生成的图纸组合成一个drawString?

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

4

2 回答 2

2

示例代码。

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);
    }
}
于 2012-02-10T18:55:44.307 回答
0

不,没有办法做到这一点。但是你希望通过这样的结合达到什么目的?更好的性能?一些特定的布局?

于 2012-02-09T23:59:42.933 回答