简体   繁体   中英

Why the repaint() don't work properly in this method

The proposal of this code is use the method getStringHeight without use Graphics g in your requirement. Then I make this code but when this execute the command System.out.println(this.getStringHeight(font, string)); the return is always 0, because the repaint() don't work properly. I want to know why and how to fix it.

Basically, before the return occurs I need to use the paintComponent to calculate the String Height and then return the value.

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Panel extends JPanel {

    String string = "HELLO WORLD";
    Font font = new Font("arial", Font.LAYOUT_LEFT_TO_RIGHT, 30);
    private int height;
    private boolean gsh;

    public Panel() {

        System.out.println(this.getStringHeight(font, string));

    }

    public int getStringHeight(Font font, String string){
        this.gsh = true;
        repaint();
        return this.height;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (this.gsh == true) {
            Graphics2D g2d = (Graphics2D) g;
            FontMetrics metrics = g2d.getFontMetrics(font);
            this.height = (int)font.createGlyphVector(metrics.getFontRenderContext(), string).getVisualBounds().getHeight();
            this.gsh = false;
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame display = new JFrame();
        display.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        display.setSize(600,400);
        display.add(new Panel());
        display.setVisible(true);

    }

}

repaint() don't work properly.

There are at least two reasons why repaint() doesn't do anything:

  1. The repaint() method does not paint the component right away. A paint request is made to the RepaintManager which will schedule the painting of the component to be done later.

  2. In this case your code is executed in the constructor of your class. The component has not even been added to the frame yet so the RepaintManager will ignore the request since a panel can only be painted on a visible frame.

So the method returns without your painting code being executed and the value will always be 0.

Once the frame is made visible the panel will be painted and the painting code will execute.

I need to use the paintComponent to calculate the String Height and then return the value. import java.awt.Font;

A painting method is used for painting only. It is not used to set properties of the component. It should not calculate a height or set a switch.

If you are trying to do some kind of size calculation for your component, then the logic should be added to the getPreferredSize() method of your class. This is the method that calculates the size of the component based on the properties of the component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM