Results 1 to 14 of 14
  1. #1
    vytska is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Red face Draw text over background image using JLabel and JFrame

    Hello, could anyone help me to finish this simple program? I need to make the program draw the lable over the background but i cant achieve this. I'm totaly new to java...thx in advance.
    Here's my code so far:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.SwingConstants;
    import javax.swing.ImageIcon;

    /**
    *
    * @author Aidas
    */
    public class Langas extends JFrame {

    JScrollPane scrollPane;
    ImageIcon icon;
    JLabel label;


    public Langas(){

    icon = new ImageIcon("logo.jpg");

    JPanel panel= new JPanel()
    {
    public void paintComponent(Graphics g)
    {

    g.drawImage(icon.getImage(), 0, 0, null);
    super.setOpaque(false);
    super.paintComponent(g);
    }
    };

    panel.setOpaque(false);
    scrollPane = new JScrollPane( panel );
    getContentPane().add( scrollPane );


    }


    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    JLabel label = new JLabel("Microsoft Office 2003 SP3");
    Langas frame = new Langas();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.getContentPane().add(label);
    frame.setSize(640, 480);
    frame.setLocationRelativeTo( null );
    frame.setBackground(Color.red);
    frame.setVisible(true);


    // TODO code application logic here
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    i cant achieve this.
    please explain your problem in more detail.

  3. #3
    vytska is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    I want to draw text over the background image. For now i only made the program to draw background, but if i add label the background dissapears... it doesnt draw on it.
    For now its like in picture number 2, or like in 1 if i add label. I want it to be like in picture 3
    Attached Thumbnails Attached Thumbnails Draw text over background image using JLabel and JFrame-1.jpg   Draw text over background image using JLabel and JFrame-2.jpg   Draw text over background image using JLabel and JFrame-3.jpg  

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    How are you drawing the image and then the text over it?
    Do you do both in the same paintComponent method or how?
    Please post your code showing how the two are being drawn.

  5. #5
    vytska is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    Well that's what i'm trying to achieve... if i would know i wouldnt ask. I havent managed to draw them both. So that's what i'm asking :)

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by vytska View Post
    Well that's what i'm trying to achieve... if i would know i wouldnt ask. I havent managed to draw them both. So that's what i'm asking :)
    After you've drawn the image on the Graphics, why don't you call the drawString( ... ) method on that same Graphics object with the desired text?(i.e. don't use any JLabel object at all).

    kind regards,

    Jos

  7. #7
    vytska is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    That's my homework, i have to use JLabel and JFrame

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Please post your code showing how the two are being drawn.

    How complicated can your code be? Do you know how to create a BufferedImage?
    If so, create one, draw the image on it and then draw the string over the top and then pass that image to the label to be shown.

  9. #9
    vytska is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please post your code showing how the two are being drawn.

    How complicated can your code be? Do you know how to create a BufferedImage?
    If so, create one, draw the image on it and then draw the string over the top and then pass that image to the label to be shown.
    Could you please give me some examples?:)

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Please post your code showing how the two are being drawn.

  11. #11
    vytska is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please post your code showing how the two are being drawn.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.SwingConstants;
    import javax.swing.ImageIcon;

    /**
    *
    * @author Aidas
    */
    public class Langas extends JFrame {

    JScrollPane scrollPane;
    ImageIcon icon;
    JLabel label;


    public Langas(){

    icon = new ImageIcon("logo.jpg");

    JPanel panel= new JPanel()
    {
    public void paintComponent(Graphics g)
    {

    g.drawImage(icon.getImage(), 0, 0, null);
    super.setOpaque(false);
    super.paintComponent(g);
    }
    };

    panel.setOpaque(false);
    scrollPane = new JScrollPane( panel );
    getContentPane().add( scrollPane );


    }


    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    JLabel label = new JLabel("Microsoft Office 2003 SP3");
    Langas frame = new Langas();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.getContentPane().add(label);
    frame.setSize(640, 480);
    frame.setLocationRelativeTo( null );
    frame.setBackground(Color.red);
    frame.setVisible(true);


    // TODO code application logic here
    }

    This is my code so far...
    Last edited by vytska; 09-29-2010 at 11:35 PM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I guess I'm confused about what you are trying to do.

    Where is the drawString method being called? That's what you want, to draw a String of text over an image?

    Looking at the comments in your code I have no idea what you want it to do.

  13. #13
    vytska is offline Member
    Join Date
    Sep 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    I guess I'm confused about what you are trying to do.

    Where is the drawString method being called? That's what you want, to draw a String of text over an image?

    Looking at the comments in your code I have no idea what you want it to do.
    This code is only drawing the picture, i dont know how to draw a string over it...

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    You use the drawImage method and it works ok?
    Add a call to the drawSting method after the drawImage method call

Similar Threads

  1. JLabel wont appear in front of background image
    By Andolph23 in forum AWT / Swing
    Replies: 5
    Last Post: 05-27-2010, 02:42 AM
  2. JFrame image background
    By 851marc in forum NetBeans
    Replies: 5
    Last Post: 03-09-2010, 07:27 PM
  3. How to add a background image to JFrame
    By dunafrothint in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2010, 11:17 PM
  4. Need help with JFrame background image
    By ProGenius in forum New To Java
    Replies: 6
    Last Post: 12-27-2009, 05:17 PM
  5. Replies: 2
    Last Post: 03-24-2008, 06:29 AM

Posting Permissions

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