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();
    }
    	
}
Member Avatar for iamthwee

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.