Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JLabel;


public class ResizeLabelFont
  extends JLabel
{
  private static final long serialVersionUID = 8642870745555129506L;
  public static final int MIN_FONT_SIZE = 1;
  public static final int MAX_FONT_SIZE = 30;
  Graphics a;
  
  public ResizeLabelFont(String text) {
    super(text);
    init();
  }
  
  protected void init() {
    addComponentListener(new ComponentAdapter(this)
        {
          public void componentResized(ComponentEvent e) { this.a.adaptLabelFont(this.a); }
        });
  }
  
  protected void adaptLabelFont(JLabel l) {
    Font f = l.getFont();
    int maxHeight = l.getHeight() - 10;
    int maxWidth = l.getWidth() - 10;
    int fontHeight = 1;
    int fontWidth = 1;
    int fontSize = 1;
    while (fontHeight < maxHeight && fontWidth < maxWidth && fontSize < 30) {
      l.setFont(l.getFont().deriveFont(fontSize));
      fontHeight = l.getFontMetrics(l.getFont()).getHeight();
      fontWidth = l.getFontMetrics(l.getFont()).stringWidth(l.getText());
      fontSize++;
    } 

    setFont(f.deriveFont(f.getStyle(), fontSize));
    repaint();
  }
  
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    this.a = g;
  }
}


What I have tried:

Can I please have some help converting the following Java code into C++?
Posted
Updated 23-Dec-19 8:05am
v2

You cant convert it directly because the JLabel isnt a C++ class and there are other classes in C++.

You must first check which control in C++ you want to change and then understands its functions for changing the wanted properties. My tip is that you may use an CStatic, but you may write the app at first. Take the time to understand the GetTextExtend explained in this article.
 
Share this answer
 
Converting from one language to another is not always straightforward. You can try online converters (Google will find them), or if you are skilled at both languages then doing it line by line may be the best option.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900