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.
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);
}
}