• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can you use multiple fonts on multiple text lines (drawString)

 
Matt Wilcko
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am drawing six lines in a panel using drawString. For each line, I allow the user to select if they the text line bold or not. I set the font for each line using setFont(adFont). Problem is, all 6 lines are drawn the same way(bold or not bold, not mixed). Is there any way to do this so you can have a different font drawn on each line?
Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should work -- why not show us the code so we can tell you where you've gone astray.
 
Matt Wilcko
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is the paintComponent:
public void paintComponent(Graphics g){

//override paid component
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
Toolkit tk = Toolkit.getDefaultToolkit();
Image splash = tk.getImage(previewAd + ".jpg");
//g2D.drawImage(splash, 10, 10, 322,450, this);
g2D.drawImage(splash, 10, 10,this);

//draw text on image
//get font metrics so text can be centered
FontRenderContext fontContext = g2D.getFontRenderContext();
//get font bold selection, if user has bold button pushed, set font to bold
for (int i=0;i<6; i++) {
if (boldLines[i] == true){
setFont(adFontBold);
}
//get length of each line, center it and draw it
bounds = adFont.getStringBounds(customLines[i], fontContext);
double x = (410 - bounds.getWidth()/2);
double y = (i * 15) + 200;
g2D.drawString(customLines[i],(int)x,(int)y);

//return font to unbold for next iteration of loop
setFont(adFont);

}

*** problem is, by doing this, it always has the font unbolded.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this does look like it should work, so one of two things must be true:
  • adFontBold is the same as adFont, either accidentally, or that's just what the font subsystem is giving you; or
  • every element in boldLines is false.


  • So test each hypothesis in turn. To test the first, make adBoldFont red, and six points bigger. To test the second, put a debug-print into the loop which displays the result of each "if bold" decision.
    You could show us more code and we could see if we could spot the error!
     
    Matt Wilcko
    Ranch Hand
    Posts: 65
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Does setFont set the font for the entire panel? I think when I set the font back to the unbolded version after drawing the string it applies to the entire panel. If I remove this line, you can bold a line. If this line is in there, selecting bold does not work at all.
     
    Matt Wilcko
    Ranch Hand
    Posts: 65
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Problem solved:
    instead of setFont(adFontBold);
    I needed to use g2D.setFont(adFontBold);
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!