I'm trying to display text on GlassPane in front of image which I read in. There is no problem with image which does display but my text doesn't appear. What is wrong????

import java.io.File;
import java.io.IOException;
import java.awt.*;
import java.awt.image.RenderedImage;
import javax.swing.*;
import javax.media.jai.widget.ScrollingImagePanel;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;

public class FlashText extends JFrame
{		   
    public void runFlashText() throws IOException
    {
    	JFrame viewImg = new JFrame();
    	Dimension dim = new Dimension(676, 900);
    	viewImg.setSize(dim);
        viewImg.setTitle("Multi page TIFF Reader");
        File file = new File("colour_img.tif");
        SeekableStream s = new FileSeekableStream(file);
        TIFFDecodeParam param = null;
        ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param); 
        
        System.out.println("Number of images in this TIFF: " +
                           dec.getNumPages());      

        // Which of the multiple images in the TIFF file do we want to load
        // 0 refers to the first, 1 to the second and so on.
        int imageToLoad = 0;

        RenderedImage op =
            new NullOpImage(dec.decodeAsRenderedImage(imageToLoad),
                            null,
                            OpImage.OP_IO_BOUND,
                            null);

        ScrollingImagePanel myImg = new ScrollingImagePanel(op, 706, 910);
        viewImg.add(myImg);
        
        MyGlassPane myGlassPane = new MyGlassPane();
        myGlassPane.setVisible(true);
        viewImg.setGlassPane(myGlassPane);
        viewImg.setVisible(true);
    }
    
    public class MyGlassPane extends JComponent
    {
    	protected void painComponent(Graphics g)
    	{
    		g.setFont(new Font("Serif", Font.BOLD, 50 ));
    		String str = "MY TEXT";
    		g.drawString(str, 200, 200);
    	}
    }
    
    public static void main(String[] args) throws IOException
    {
    	FlashText flashText = new FlashText();
    	flashText.runFlashText();
    }
    	
}

Dani AI

Generated

A few useful points that build on , and : the original misspelling of paintComponent is the common first trap (already fixed here). After that, the usual causes for an overlay not appearing are: the glass pane not being visible/sized, the glass pane filling its background (hiding the image), or heavyweight/lightweight mixing where the image component paints above a lightweight glass pane.

Glass panes are part of the root pane and must be visible and transparent to let the image show through. Use setOpaque(false) on a JPanel-based glass pane (or extend JComponent and avoid filling the background), and call frame.getGlassPane().setVisible(true() after the frame is shown so layout/size are correct. See the Swing root-pane/glass-pane guidance: Root pane and glass pane.

If the image viewer is a heavyweight AWT component (common with older JAI widgets), the heavyweight will always paint on top of lightweight Swing overlays. That explains why the overlay shows only when the image panel is removed. The mixing rules are covered here: .

Practical options (in order of preference):

  • Paint the annotation directly in the same component that draws the image (subclass or wrap the image panel so its paintComponent draws the text last).
  • Replace the image viewer with a lightweight Swing component that draws the TIFF into a BufferedImage and paints it in paintComponent.
  • Use a top-level overlay (a transparent JWindow or heavyweight popup) if converting the image component is not possible.
  • On Java 7+, use JLayer/LayerUI to draw an overlay for lightweight components. Example overlay pattern with LayerUI:
class OverlayUI extends LayerUI<JComponent> {
  @Override
  public void paint(Graphics g, JComponent c) {
    super.paint(g, c);
    g.setFont(g.getFont().deriveFont(Font.BOLD, 36f));
    g.setColor(Color.RED);
    g.drawString("MY TEXT", 50, 50);
  }
}
JLayer<JComponent> wrapped = new JLayer<>(imagePanel, new OverlayUI());
frame.setContentPane(wrapped);

Checklist: correct paintComponent signature, glass pane setOpaque(false), setVisible(true) after showing frame, verify whether the image widget is heavyweight.

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

Are you trying to paint/ render text on a bitmap or tiff file?

yes, something like that, any advice?

are you sure your text is being painted in a visible colour, that the text colour isn't somethintg transparent?

I did some update on that class, but no difference. There was a mirror mistake in the code as I called painComponent instead of paintComponent :lol:

public class MyGlassPane extends JPanel
 {
   	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		g.setColor(Color.BLACK);
    		g.setFont(new Font("Serif", Font.BOLD, 20 ));
    		String str = "MY TEXT";
    		g.drawString(str, 200, 200);
    	}
}

Text is only visible once I replace method setGlassPane() with method add() and coment out viewImg.add(myImg)

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.