Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jashburn

Thread: No text

  1. #1
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Unhappy No text

    Hello, I have one question:
    Why can't I see the text when I compile the java code?

    Java Code:
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class WorldTravel {
    
        public static void main (String[] args){
            JFrame f = new JFrame("World Travel!");
            BG_text p = new BG_text();
            f.add(p);
            try {
                f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("/home/flopet17/Java/Home Practice/WorldTravel/src/world_map.jpg")))));
            } catch (IOException ex) {
                System.out.println("No file!");
            }
            f.pack();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    
    class BG_text extends JPanel {
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.drawString("Europe", 200, 100);
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: No text

    Probably because you are setting the content pane to the JLabel. Just add the JLabel to the Frame. And you're panel has no size and you need to set a layout manager for you Frame (try new FlowLayout()).

    Regards,
    Jim
    Last edited by jim829; 03-07-2014 at 05:11 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: No text

    Judging by the image file name (world_map.jpg) and the string (Europe), it appears that you may need fine-grained control over the positioning of the string. If so, you can do something like this:

    Java Code:
            JFrame f = new JFrame("World Travel!");
    
            BufferedImage background = null;
            try {
                background = ImageIO.read(new File(
                    "/home/flopet17/Java/Home Practice/WorldTravel/src/world_map.jpg"));
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            Graphics2D bgGraphics = background.createGraphics();
            bgGraphics.setColor(Color.BLACK);
            bgGraphics.drawString("Europe", 200, 100);
            
            JLabel label = new JLabel(new ImageIcon(background));
            f.add(label);
            f.pack();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
    In other words, you:
    1. create a BufferedImage using the image file
    2. create a Graphics2D object out of the BufferedImage so that you can draw on it
    3. draw the string onto the Graphics2D object at the desired location
    4. pass the BufferedImage to the JLabel to be displayed

    Hth!
    Flopet17 likes this.

Similar Threads

  1. Replies: 0
    Last Post: 07-24-2012, 08:07 AM
  2. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  3. Replies: 7
    Last Post: 03-05-2011, 09:25 AM
  4. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  5. Replies: 0
    Last Post: 05-26-2008, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •