I have a String which has been splitted like this

String companyinforstring ="Company name.:Company profile no 1.:Company profile no 2.:Company profile no3.:---------------------------------------------------------------:Company address, :Street number,  :Area,Area no2,  :City. ";




for (String retval: companyinforstring.split(":"))
        {
             System.out.println(retval);
             graphics.drawString(retval.toUpperCase(), 250, 200);               
          }

The Problem is that String which has been splitted, overlaping each String,How can this be solved?

Dani AI

Generated

As pointed out, the overlap was caused by drawing every token at the same coordinates. Two details worth highlighting: Graphics.drawString takes the y coordinate as the text baseline (not the top edge), and fixed pixel steps like 10 are fragile because font metrics (ascent, descent, leading) change with the font and size. A robust approach is to compute the line height from the current font and move the baseline down by that amount for each token.

Example (paint-time approach that uses font metrics and skips empty tokens):

Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
FontMetrics fm = g2.getFontMetrics();
int x = 360;
int y = 50 + fm.getAscent();   // first baseline

for (String part : companyInfoString.split(":+")) { // collapse consecutive colons
    part = part.trim();
    if (part.isEmpty()) continue;
    g2.drawString(part.toUpperCase(), x, y);
    y += fm.getHeight();       // advance by full line height (ascent+descent+leading)
}

Troubleshooting notes: trim tokens and collapse separators (as shown) to avoid empty lines; measure text width with fm.stringWidth() if horizontal wrapping or alignment is needed; for automatic wrapping consider LineBreakMeasurer/TextLayout or a Swing text component (JTextArea) rendered to the Graphics. Also ensure painting happens inside paintComponent (Swing) and call super.paintComponent(g) first. These steps make the layout reliable across fonts and display settings and prevent the overlapping observed by .

Recommended Answers

All 5 Replies

what are you talking about, overlaping each String? what exactly is it you try to get?

You are drawing them all at the same position 250,200
You need to increment that position's x or y value after each print so the next string goes to a different position

String[] str1 = companyinforstring.split(":");
        for(int i=0;i<str1.length;i++)
        {
            //System.out.println("Split1 Element "+i+" :"+str1[i]);
            graphics.drawString(str1[0].toUpperCase(), 360, 50);
            graphics.drawString(str1[1].toUpperCase(), 360, 60);
            graphics.drawString(str1[2].toUpperCase(), 360, 70);
            graphics.drawString(str1[3].toUpperCase(), 360, 80);
            graphics.drawString(str1[4].toUpperCase(), 360, 90);
            graphics.drawString(str1[5].toUpperCase(), 360, 100);
            graphics.drawString(str1[6].toUpperCase(), 360, 110);
            graphics.drawString(str1[7].toUpperCase(), 360, 120);
            graphics.drawString(str1[8].toUpperCase(), 360, 130);
        }

Is there any better/simple way of doing it?
Please let me know.

Yes. Whenever you see code repeated like that, with one value changing from line to line, you just know it should be a loop. Eg::

for(int i=0;i<str1.length;i++) {
   graphics.drawString(str1[i].toUpperCase(), 360, 50 + i*10);
}

Thank you a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.