Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1716
  • Last Modified:

background image in contentpane

Dear all,

        Is it possible to use a gif or jpg as a background image for the content pane in a JApplet ?
        How to achieve this ?

Many thanks,
Cristy
0
cristy56
Asked:
cristy56
  • 10
  • 6
  • 6
  • +3
5 Solutions
 
zzynxSoftware engineerCommented:
Maybe this can help you:
How do I display an image in the background of a Swing component? http://www.jguru.com/faq/view.jsp?EID=9691
0
 
ashok3sepCommented:
public class MyFrame extends JFrame
{
  class MyContentPane extends JPanel
  {
    public MyContentPane()
    {
      this.setOpaque( false ) ;
    }
  }

  public void paint(Graphics g)
  {
   g.drawImage(backgroundImg.getImage(), 0, 0, this);
   super.paint( g ) ;
  }

  public static void main( String[] args )
  {
    MyFrame f = new MyFrame() ;
    f.setContentPane( new MyContentPane() ) ;
  }
}
0
 
ashok3sepCommented:

public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
   
        // Draw background...
   
        // Set alpha.  0.0f is 100% transparent and 1.0f is 100% opaque.
        float alpha = .3f;
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
   
        // Draw foreground...
    }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CEHJCommented:
You need to set the content pane to an overridden JPanel. You could use an applet parameter to set the image. Compile and then run the following (using your own applet param value) thus:


appletviewer BackImgApplet.java


SNIP=========================================================




/*
 *  <applet codebase="." code="BackImgApplet.class" width="300" height="300">
 *  <param name="imageUrl" value="v-bell-3.jpg" />
 *  </applet>
 */
import javax.swing.*;
import java.awt.*;

public class BackImgApplet extends JApplet {

      public void init() {
            Image image = new ImageIcon(getParameter("imageUrl")).getImage();
            setContentPane(new BackgroundImageDisplayer(image));
      }


      private class BackgroundImageDisplayer extends JPanel {
            private Image image;


            public BackgroundImageDisplayer(Image image) {
                  super();
                  this.image = image;
            }


            public void paintComponent(Graphics g) {
                  super.paintComponent(g);
                  g.drawImage(image, 0, 0, this);

            }
      }

}
0
 
objectsCommented:
CEHJ>  Compile and then run the following (using your own applet param value) thus:

Which is just a duplication of what ashok3sep already posted.
0
 
objectsCommented:
Though a custom panel is not really necessary. You can display an image using a JLabel.

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JLabel.html#JLabel(javax.swing.Icon)

Let me know if you need firther assistance.
0
 
cristy56Author Commented:
I have tried to use JLabel ,

private JLabel bg_image = new JLabel(new ImageIcon("main_bg.jpg"));
  :
  :
bg_image.setBounds(new Rectangle(0, 0, 400, 300));
this.getContentPane().add(bg_image, null);

there is no compilation error but I can see nothing on changing to this. Does anybody know why ?

Cristy
0
 
objectsCommented:
where is the image located?
0
 
cristy56Author Commented:
hte image is located in the same folder as the .java file

Cristy
0
 
zzynxSoftware engineerCommented:
Try

    private JLabel bg_image = new JLabel(new ImageIcon( YourClass.class.getResource("main_bg.jpg") ));

replace "YourClass" with the name of your own class this is in
0
 
cristy56Author Commented:
oh, zzynx, using your method, I could see the image, but it is not really a background image ??
It covers up all my other JLabel and JButtons, what should I do ?

Thanks,
Cristy
0
 
zzynxSoftware engineerCommented:
>> using your method, I could see the image
Good!

>> It covers up all my other JLabel and JButtons, what should I do ?
Did you try any of the previous comments above?
What's your code?
0
 
cristy56Author Commented:
 private JLabel bg_image = new JLabel(new ImageIcon( testMEID.class.getResource("main_bg.jpg") ));
  private JLabel lb_pname = new JLabel();
  private JLabel lb_dob = new JLabel();
  private JLabel lb_place = new JLabel();

    :
    :
    lb_pname.setText("Name :");
    lb_pname.setBounds(new Rectangle(30, 185, 60, 20));
    lb_pname.setForeground(Color.blue);      
    txt_pname.setBounds(new Rectangle(100, 185, 200, 25));
   

    lb_dob.setText("Date of Birth:");
    lb_dob.setBounds(new Rectangle(30, 220, 60, 20));
    lb_dob.setForeground(Color.blue);      
    txt_dob.setBounds(new Rectangle(100, 220, 120, 25));


    lb_place.setText("Place of Birth :");
    lb_place.setBounds(new Rectangle(30, 255, 60, 20));
    lb_place.setForeground(Color.blue);      
    txt_place.setBounds(new Rectangle(100, 255, 120, 25));

  :
  :

    this.getContentPane().add(bg_image, null);
    this.getContentPane().add(lb_pname, null);  
    this.getContentPane().add(lb_dob, null);
    this.getContentPane().add(lb_place, null);    

    this.getContentPane().add(txt_pname, null);
    this.getContentPane().add(txt_dob, null);
    this.getContentPane().add(txt_place, null);
  :
  :


I can only see the textbox but cannot see other JLabels and JButton... pls help.
0
 
cristy56Author Commented:
missing 1 statment :

bg_image.setBounds(new Rectangle(0, 0, 640, 480));

the jpg is 640 x 480 which is supposed to be the size of my background image.


Thanks,
Cristy
0
 
zzynxSoftware engineerCommented:
>> this.getContentPane().add(bg_image, null);
That's not how you add a **background** image.
Now, you simply add your JLabel like all other components.

Please apply what you read in the link of my first comment or ashok's comment.
0
 
CEHJCommented:
>>Which is just a duplication of what ashok3sep already posted.

No it isn't. In Swing paintComponent should be overridden not paint

You don't use JLabel to display background images. The code i posted is correct and works fine
0
 
zzynxSoftware engineerCommented:
>> In Swing paintComponent should be overridden not paint
I thought for top level containers (eg. JFrame) you should only override paint(),
while for others you should override paintComponent().
Isn't JApplet also a top level container?
0
 
CEHJCommented:
>>as a background image for the content pane in a JApplet ?

The content pane of a JApplet is a JPanel - i'm not sure where the JFrame needed to come in - it's not relevant
0
 
objectsCommented:
> You don't use JLabel to display background images. The code i posted is correct and works fine

The code you posted reinvents the wheel, the JLabel class alrfeady supports displaying images (with more control than the code you posted).

>  i'm not sure where the JFrame needed to come in - it's not relevant

No it the top level container that is irrelevant.
0
 
cristy56Author Commented:
Dear all,

I have tried to write a test.java using the following code :

import javax.swing.*;
import java.awt.*;


public class test {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Background Example");
    final ImageIcon imageIcon = new ImageIcon("main_bg.jpg");
    JTextArea textArea = new JTextArea() {
      Image image = imageIcon.getImage();
      Image grayImage = GrayFilter.createDisabledImage(image);
      {setOpaque(false);}  // instance initializer
      public void paintComponent (Graphics g) {
        g.drawImage(grayImage, 0, 0, this);
        super.paintComponent(g);
      }
    };
    JScrollPane scrollPane = new JScrollPane(textArea);
    Container content = frame.getContentPane();
    content.add(scrollPane, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(3);
    frame.setSize(640, 480);
    frame.setVisible(true);
  }
}


I was able to compile it successfully, but when I use appletview test.java, I cannot see anything, how should I test my code to see if it is working properly ??

Sorry for so many question caused,
but that's why I am here...
Cristy
0
 
cristy56Author Commented:
typo, should be  appletviewer test.java, but no response.

Cristy
0
 
objectsCommented:
thats an appliation, not an applet. run it with

java -classpath . test
0
 
cristy56Author Commented:
ok. I test that successfully.

But what I am writing is something like this :

public class test extends JApplet
{
     :
     :
     'some code like I posted in my above reply
    :
    :

   and I already have

  public void init()
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }


  }

}
 
but I have no public static void main(String args[]) { } in my codings,  how can I integrate the above exmaple into my code ? Would u mind to explain a bit more?

Regard,
Cristy
0
 
cristy56Author Commented:
Maybe base on the example from http://www.jguru.com/faq/view.jsp?EID=9691
can someone show me how I can add my JLabels, JButtons, and JTextArea on it ?

Thanks a lot,
Cristy
0
 
CEHJCommented:
You would add your fields something like this:


/*
 *  <applet codebase="." code="BackImgApplet.class" width="300" height="300">
 *  <param name="imageUrl" value="main_bg.jpg" />
 *  </applet>
 */
import javax.swing.*;
import java.awt.*;

public class BackImgApplet extends JApplet {
  private JLabel lb_pname;
  private JTextField tf_pname;
  private JLabel lb_place;


  public void init() {
    lb_pname = new JLabel("Name :");
    tf_pname = new JTextField(20);

    Image image = new ImageIcon(getParameter("imageUrl")).getImage();
    setContentPane(new BackgroundImageDisplayer(image));
    Container contentPane = getContentPane();
    contentPane.add(lb_pname);
    contentPane.add(tf_pname);
  }


  private class BackgroundImageDisplayer extends JPanel {
    private Image image;


    public BackgroundImageDisplayer(Image image) {
      super();
      this.image = image;
    }


    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawImage(image, 0, 0, this);

    }
  }

}
0
 
cristy56Author Commented:
Hi CEHJ,

I copy paste your code and compile succesffuly, but when I load it in the html as following, it said applet noinited :

<HTML>
<BODY bgcolor=black>
<APPLET CODE="BackImgApplet"
      HEIGHT="480"
      WIDTH="640"
        ALIGN="bottom">
This browser does not appear to support Applets.
</APPLET>
</BODY>
</HTML>

I have also tried to replace the html with this:

<applet codebase="." code="BackImgApplet.class" width="300" height="300">
 <param name="imageUrl" value="main_bg.jpg" />
 </applet>

but the same error message. any idea ?

Cristy
0
 
CEHJCommented:
First run the code (as posted) as i mentioned earlier:

appletviewer BackImgApplet.java

Your background image file should be in the applet's codebase.
0
 
zzynxSoftware engineerCommented:
Here are my remarks:

I consider my first comment as useful.
Apparently my hint about class.getResource("main_bg.jpg") was also useful:
>> using your method, I could see the image
- So I expect to be in the points split.
- Furthermore, CEHJ's comment also deserves to be in the split.

- although ashok was correct in overwriting paint() for a JFrame, using a JFrame was not relevant for the Q about JApplet.
- objects talked about using a JLabel, but although
     * he said     >> Let me know if you need firther assistance
     * it was clear that the user needed that / had problems with it  >> I have tried to use JLabel...
  none of his further comments explained how to use JLabel for a background image
  (too busy explaining why others' comments were not OK ;°)
0
 
objectsCommented:
split between all

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 10
  • 6
  • 6
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now