Java Code Examples for javax.swing.JLabel.repaint()

The following are Jave code examples for showing how to use repaint() of the javax.swing.JLabel class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: cognitive-services-java-computer-vision-tutorial   File: MainFrame.java   View Source Code Vote up 5 votes
/**
 * Scales the given image to fit the label dimensions.
 * @param bImage: The image to fit.
 * @param label: The label to display the image.
 */
private void scaleAndShowImage(BufferedImage bImage, JLabel label) {
    int bImageHeight = bImage.getHeight();
    int bImageWidth = bImage.getWidth();
    int labelHeight = label.getHeight();
    int labelWidth = label.getWidth();
    
    // Does this need to be scaled?
    if (labelHeight >= bImageHeight && labelWidth >= bImageWidth) {
        // If not, display the image and return.
        ImageIcon image = new ImageIcon(bImage);
        label.setIcon(image);
        return;
    }
    
    // Calculate the new width and height for the image.
    int newHeight;
    int newWidth;
    double bImageAspect = (double)bImageHeight / (double)bImageWidth;
    double labelAspect = (double)labelHeight / (double)labelWidth;
    
    if (bImageAspect > labelAspect) {
        newHeight = labelHeight;
        newWidth = (int)(((double)labelHeight / (double)bImageHeight) * (double)bImageWidth);
    } else {
        newWidth = labelWidth;
        newHeight = (int)(((double)labelWidth / (double)bImageWidth) * (double)bImageHeight);
    }
    
    // Create a new image scaled to the correct size.
    Image newImage = bImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    
    // Display the scaled image.
    ImageIcon labelImage = new ImageIcon(newImage);
    label.setIcon(labelImage);
    label.validate();
    label.repaint();
}
 
Example 2
Project: cognitive-services-java-computer-vision-tutorial   File: MainFrame.java   View Source Code Vote up 5 votes
/**
 * Scales the given image to fit the label dimensions.
 * @param bImage: The image to fit.
 * @param label: The label to display the image.
 */
private void scaleAndShowImage(BufferedImage bImage, JLabel label) {
    int bImageHeight = bImage.getHeight();
    int bImageWidth = bImage.getWidth();
    int labelHeight = label.getHeight();
    int labelWidth = label.getWidth();
    
    // Does this need to be scaled?
    if (labelHeight >= bImageHeight && labelWidth >= bImageWidth) {
        // If not, display the image and return.
        ImageIcon image = new ImageIcon(bImage);
        label.setIcon(image);
        return;
    }
    
    // Calculate the new width and height for the image.
    int newHeight;
    int newWidth;
    double bImageAspect = (double)bImageHeight / (double)bImageWidth;
    double labelAspect = (double)labelHeight / (double)labelWidth;
    
    if (bImageAspect > labelAspect) {
        newHeight = labelHeight;
        newWidth = (int)(((double)labelHeight / (double)bImageHeight) * (double)bImageWidth);
    } else {
        newWidth = labelWidth;
        newHeight = (int)(((double)labelWidth / (double)bImageWidth) * (double)bImageHeight);
    }
    
    // Create a new image scaled to the correct size.
    Image newImage = bImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    
    // Display the scaled image.
    ImageIcon labelImage = new ImageIcon(newImage);
    label.setIcon(labelImage);
    label.validate();
    label.repaint();
}
 
Example 3
Project: brModelo   File: FrameSobre.java   View Source Code Vote up 5 votes
void Inicie(ParteAjuda sel) {
        this.setTitle(sel.getTitulo());
        if (sel.getByteImage() != null || sel.getHtml() != null) {//.isEmpty()) {
            Pan.removeAll();
            int H = 0;
            int W = 0;
            if (!sel.getHtml().isEmpty()) {
                JLabel htmLbl = new JLabel();
                htmLbl.setText(sel.getHtml());
                htmLbl.repaint();
                Dimension d = htmLbl.getPreferredSize();
                int x = (getPreferredSize().width - d.width) / 2;
                if (getPreferredSize().width < d.width) {
                    x = 0;
                }
//                int y = (getPreferredSize().height - d.height - subPan.getPreferredSize().height) / 2;
//                if (getPreferredSize().height - subPan.getPreferredSize().height < d.height) {
//                    y = 0;
//                }
                htmLbl.setBounds(x, 0, d.width, d.height);
                //Pan.setBackground(Color.yellow);
                Pan.add(htmLbl);
                H = d.height + 10;
                W = d.width;
            }
            if (sel.getByteImage() != null) {
                ImageIcon img = new ImageIcon(sel.getByteImage());
                JLabel picLabel = new JLabel(img);
                W = W > img.getIconWidth() ? W : img.getIconWidth();
                picLabel.setBounds(0, H, img.getIconWidth(), img.getIconHeight());
                picLabel.setPreferredSize(new Dimension(img.getIconWidth(), img.getIconHeight()));
                H += img.getIconHeight();
                Pan.setPreferredSize(new Dimension(W, H));
                Pan.add(picLabel);
            }
            Pan.revalidate();
            Pan.repaint();
        }
    }