?
Solved

background image in contentpane

Posted on 2005-03-07
33
Medium Priority
?
1,699 Views
Last Modified: 2013-11-23
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
Comment
Question by:cristy56
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
  • 6
  • +3
33 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 400 total points
ID: 13475045
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
 
LVL 5

Assisted Solution

by:ashok3sep
ashok3sep earned 400 total points
ID: 13476956
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
 
LVL 5

Expert Comment

by:ashok3sep
ID: 13476966

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 13477104
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
 
LVL 5

Assisted Solution

by:Naeemg
Naeemg earned 400 total points
ID: 13477818
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 13481280
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
 
LVL 92

Expert Comment

by:objects
ID: 13481303
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
 
LVL 5

Author Comment

by:cristy56
ID: 13484205
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
 
LVL 92

Expert Comment

by:objects
ID: 13484254
where is the image located?
0
 
LVL 5

Author Comment

by:cristy56
ID: 13494003
hte image is located in the same folder as the .java file

Cristy
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13494032
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
 
LVL 5

Author Comment

by:cristy56
ID: 13494072
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13494114
>> 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
 
LVL 5

Author Comment

by:cristy56
ID: 13494208
 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
 
LVL 5

Author Comment

by:cristy56
ID: 13494236
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13494258
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13496508
>>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
 
LVL 37

Expert Comment

by:zzynx
ID: 13496767
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13497050
>>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
 
LVL 92

Expert Comment

by:objects
ID: 13501508
> 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
 
LVL 5

Author Comment

by:cristy56
ID: 13503514
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
 
LVL 5

Author Comment

by:cristy56
ID: 13503517
typo, should be  appletviewer test.java, but no response.

Cristy
0
 
LVL 92

Expert Comment

by:objects
ID: 13503579
thats an appliation, not an applet. run it with

java -classpath . test
0
 
LVL 5

Author Comment

by:cristy56
ID: 13503662
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
 
LVL 5

Author Comment

by:cristy56
ID: 13503836
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13509904
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
 
LVL 5

Author Comment

by:cristy56
ID: 13513714
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13515018
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13704618
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
 
LVL 92

Expert Comment

by:objects
ID: 13704873
split between all

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question