BackGround picture for a Swing application??

hi,
once I got a nice idea while watching some webpages. we can give a background picture for a webpage in <body> tag?
I want to do same thing for any Swing application. Currently I am working on that issue. Any suggestions are welcome!!
santhosh

Hi everybody!
Finally I got it. now Using this We can enable background for any swing application.
the complete code is shown here. If you have any problems regarding this code, please mail to [email protected]
/******************************[TexturedImageIcon.java]***********************/
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
/* this is used to generate a tiled image from a given image file.*/
public class TexturedImageIcon extends ImageIcon
     private Dimension size = new Dimension(10, 10);
     BufferedImage bimg1,bimg;
     Graphics2D g2;
     ComponentListener cl = new ComponentAdapter(){
          public void componentResized(ComponentEvent ce){
               Component c = (Component)ce.getSource();
               size = c.getSize();
               createImage();
     public void setImage(String filename){
          super.setImage(new ImageIcon(filename).getImage());
          bimg1=null;
          createImage();
     public TexturedImageIcon(Component comp, Image img){
          super(img);
          addListener(comp);
     public TexturedImageIcon(Component comp, String filename){
          super(filename);
          addListener(comp);
     public TexturedImageIcon(Component comp, URL url){
          super(url);
          addListener(comp);
     private void addListener(Component comp){
          comp.addComponentListener(cl);
     private void createImage(){
          bimg = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
          g2 = bimg.createGraphics();
          Rectangle2D rect = new Rectangle2D.Float(0,0,size.width-1, size.height-1);
          Rectangle2D tr = new Rectangle2D.Double(0,0,super.getIconWidth(), super.getIconHeight());
          if(bimg1==null){
               bimg1 = new BufferedImage(super.getIconWidth(), super.getIconHeight(), BufferedImage.TYPE_INT_RGB);
               Graphics2D g = bimg1.createGraphics();
               g.drawImage(super.getImage(), null, null);
          TexturePaint tp = new TexturePaint(bimg1, tr);
          g2.setPaint(tp);
          g2.fill(rect);
     public int getIconWidth(){ return size.width; }
     public int getIconHeight(){ return size.height; }
     public Image getImage(){
          System.out.println("asked");
          return bimg;
     public void paintIcon(Component c, Graphics g, int x, int y){
          Graphics2D g2d =(Graphics2D)g;
          g2d.drawImage(bimg, null, null);
     public static void main(String[] args){
          JFrame f = new JFrame();
          f.setSize(300,300);
          JLabel label = new JLabel();
          label.setBackground(Color.white);
          label.setBorder(BorderFactory.createRaisedBevelBorder());
          label.setIcon(new TexturedImageIcon(label, "world2.gif"));
          f.getContentPane().setLayout(new BorderLayout());
          f.getContentPane().add(label, BorderLayout.CENTER);
          f.show();
/*********************************[JFCUtils.java]************************/
/*The main logic to enable background picture lies in this class*/
public class JFCUtils{
     public static ContainerListener cl = new ContainerAdapter(){
          public void componentAdded(ContainerEvent ce){
               JComponent child = (JComponent)ce.getChild();
               child.setOpaque(false);
               child.addContainerListener(this);
               addlisteners(child);
     public static TexturedImageIcon enableBackGround(JFrame f, String filename){
          ((JPanel)f.getContentPane()).setOpaque(false);
          JLayeredPane lp = f.getLayeredPane();
          JLabel label = new JLabel();
          TexturedImageIcon icon = new TexturedImageIcon(label, filename);
          label.setIcon(icon);
          JPanel panel = new JPanel(new BorderLayout());
          panel.add(label, BorderLayout.CENTER);
          lp.add(panel, new Integer(Integer.MIN_VALUE));
          Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
          panel.setBounds(0, 0, screen.width,screen.height);
          addlisteners((JComponent)f.getContentPane());
          return icon;
     private static void addlisteners(Component c){
          c.toString();
          if(c instanceof JComponent) ((JComponent)c).setOpaque(false);
          if(c instanceof Container){
               Container ct = (Container)c;
               ct.addContainerListener(cl);          
          for(int i=0; i<ct.getComponentCount(); i++){ //recursivly make all subcomponents transparent
               Component child = (Component)ct.getComponent(i);
               addlisteners(child);
     public static void main(String[] args){
          JFrame f = new JFrame();
          enableBackGround(f, "bg.jpg");
          JButton b = new JButton("fdfdfdfd");
          f.getContentPane().add(b, BorderLayout.NORTH);
          f.setSize(300,300);
          f.show();
/*************************************[UserDialog.java]**************************/
//to check how a swing application with background looks like
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class UserDialog extends JDialog
     JPanel contents = (JPanel)getContentPane();
     JTextField shortField = new JTextField(20);
     JTextField nameField = new JTextField(20);
     JTextField emailField = new JTextField(20);
     JTextField smtpServerField = new JTextField(20);
     JTextField pwdField = new JPasswordField(20);
     JTextField pwdField1 = new JPasswordField(20);
     boolean okay = false;
     public UserDialog(JFrame owner){
          super(owner, "New User Details", true);
          initComponents();
          pack();
          setResizable(false);
     public UserDialog(JDialog owner){
          super(owner, "New User Details", true);
          initComponents();
          pack();
          setResizable(false);
     private void initComponents(){
          JPanel west = new JPanel(new GridLayout(0, 1));
          west.add(new JLabel("Short Name"));
          west.add(new JLabel("Full Name"));
          west.add(new JLabel("Email"));
          west.add(new JLabel("SMTP Server"));
          west.add(new JLabel("Password"));
          west.add(new JLabel("Confirm Password"));
          JPanel east = new JPanel(new GridLayout(0, 1));
          east.add(shortField);
          east.add(nameField);
          east.add(emailField);
          east.add(smtpServerField);
          east.add(pwdField);
          east.add(pwdField1);
          JPanel south = new JPanel();
          JButton ok = new JButton("Ok");
          JButton cancel = new JButton("Cancel");
          south.add(ok);
          south.add(cancel);
          contents.setBorder(JFCUtils.border);
          contents.setLayout(new BorderLayout(10, 10));
          contents.add(west, BorderLayout.WEST);
          contents.add(east, BorderLayout.EAST);
          contents.add(south, BorderLayout.SOUTH);
          ActionListener al = new ActionListener(){
               public void actionPerformed(ActionEvent ae){
                    okay = ae.getActionCommand().equals("Ok");
                    setVisible(false);
          ok.addActionListener(al);
          cancel.addActionListener(al);
     private void clearFields(){
          shortField.setText("");
          nameField.setText("");
          emailField.setText("");
          smtpServerField.setText("");
          pwdField.setText("");
          pwdField1.setText("");
     public User getUser(){
          clearFields();
          okay = false;
          show();
          if(okay) return new User(shortField.getText(), nameField.getText(), emailField.getText(), smtpServerField.getText(), pwdField.getText());
          else return null;
     public static void main(String[] args){
          Dialog dlg = new UserDialog();
          TexturedImageIcon ticon = JFCUtils.enableBackGround(f, "bg.jpg");.show();
          //we can change the background picture by calling ticon.setImage(...) at runtime.

Similar Messages

  • Accelarate the Linux ATI Graphics card  for java Swing application

    Hi All,
    I am using a ATI Radeon 9550 Graphic card in LFS (Linux from the scratch) environment. I want to enable the OpenGL-based pipeline for Java Swing application. I tried the -Dsun.java2d.opengl=true . But the Java swing application getting very slow.
    How to overcome this problem?
    Any one give the procedure to Accelerate ATI graphics card for Java Swing Application
    How to verify Java swing use ATI graphics card ?
    Thanks in advance..
    Prabhu.S

    Hi All,
    I am using a ATI Radeon 9550 Graphic card in LFS (Linux from the scratch) environment. I want to enable the OpenGL-based pipeline for Java Swing application. I tried the -Dsun.java2d.opengl=true . But the Java swing application getting very slow.
    How to overcome this problem?
    Any one give the procedure to Accelerate ATI graphics card for Java Swing Application
    How to verify Java swing use ATI graphics card ?
    Thanks in advance..
    Prabhu.S

  • Where is the background picture for a window stored?

    Greetings,
    Where is the background picture for a window stored? What is the file called? I am trying to find the image used on a DVD.
    Thanks

    when you set a picture to be a background of some folder that picture is not moved from its original location. instead a record is made in the .DS_Store file of that folder indicating that this picture is the one to be used for the background. in case of the DVD it is likely sitting in some hidden folder right on the DVD.
    run the following terminal command to enable showing hidden files in finder
    defaults write com.apple.finder AppleShowAllFiles 1; killall Finder
    then look on the dvd and the picture should be there somewhere. if you still can't find it open the .DS_store file of the folder in question using Text Editor. a lot of this file will be unreadable but the picture path will be present in plain text.
    when done rerun the above command after changing 1 to 0.

  • Book Recommendation for Building Swing Applications

    I'm looking for recommendations on books for building swing applications from the ground up.
    I'm not a strong GUI developer. 95% of my experience has been strictly on back end development in many other languages. What little GUI experience I have has been with C++ (years ago) and most recently with HTML.
    I know what I want to develop, and even have the GUI design for my application drawn out. I just need a good book that can walk me through developing the interface in Java.
    I already have several books on Java. But, I find them somewhat limiting because they don't help me build the app from the ground up.
    Yes, I've tried the online book on the Sun site, "The Jfc Swing Tutorial: Guide to Constructing Gui's".
    Please offer some recommendations and reasons on why you like the book.
    Thanks.

    A few comments to that ....
    the first thing is understanding the LayoutManagers, that are available.
    I will give you a short guideline where they are usefull:
    FlowLayout - usefull for JLabel-JTextField combinations or several JButtons
    BorderLayout - usefull for the structure of basic containers
    CardLayout - usefull for every area of the screen, where you want to appear different panels
    GridLayout - usefull for a group of same-sized components laid out in a grid
    GridBagLayout - usefull for a group of components, that have different sizes, very flexible
    JTabbedPane - a special container, that is similar to CardLayout but with visible tabs to switch panels
    Normally you can say "I want that group at the bottom of the frame, that other group at its left side, that toolbar at its top" - if you can say so - that shouts for BorderLayout. If you can say "in this area I want to use several panels" that means CardLayout or a JTabbedPane.
    You see, if you have an idea, what the LayoutManagers do, you know exactly which area needs what Layout - so you have a guideline, which LayoutManager to use in that panel.
    To make an example:
    You want 3 buttons centered at the bottom of a frame - this 3 buttoms should be of that size, that is needed by the button texts. So, what to do:
    1. create a JPanel with FlowLayout
    2. create the buttons and add it to that JPanel
    3. create another JPanel with BorderLayout
    4. add that first JPanel to the second JPanel at BorderLayout.CENTER
    5. add this Panel to the ContentPane of the frame at BorderLayout.SOUTH
    that is a simple panel in panel construct - placing 3 buttons centered at the bottom of the frame. You have to play with that different LayoutManagers a little bit - the way you stick one panel in another changes the look of the GUI - if you know, how it changes (by playing with the examples of the tutorial), you will have that "from the ground"-experience, you are looking for - believe me.
    greetings Marsian

  • How to disable html for whole swing application.

    Hi,
    As we can disable html for individual swing components , for example JLabel
    label.putClientProperty("html.disable",Boolean.TRUE);
    BasicHTML.updateRenderer(label,"html.disable");
    is there any way to disable html rendering in swing components for system wide / whole application?.
    Thanks
    Kiran

    Better use a utility method in the application like this
    public static void disableHtml(JComponent component)
    component.putClientProperty("html.disable",Boolean.TRUE);
    BasicHTML.updateRenderer(component,"html.disable");
    call disableHtml(Component u want to set html disable) for any component in the application.

  • How do I add a background picture til my swing CUI

    How do I add a background picture til my CUI. I�m using Swing - not using any JPanel.
    Here my code:
    class Start extends JFrame implements ActionListener
         JButton soeg, tilfoj, afslut;
         JTextField beskedText;
         JLabel besked;
         private Image baggrund;
         public Start()
              setTitle("Video Registrerings System");
              setSize(300,250);
              setVisible(true);
              addWindowListener(new WindowAdapter()               // Opretter vinues "lytter"
                   public void windowClosing(WindowEvent e)
                        System.exit(0);
              Container c;
              c = getContentPane();
              c.setLayout(null);
              try
                   URL imagename = new URL("file:/c:/jdk1.3.1_02/java/vrs_background.jpg");
                   baggrund = createImage((ImageProducer) imagename.getContent());
              catch (Exception e) {}
              // Tilf�jer knapperne
              tilfoj = new JButton("Tilf�j ny video");
              tilfoj.setBounds(75,110,150,25);
              tilfoj.setVisible(true);
              tilfoj.addActionListener(this);
              c.add(tilfoj);
              soeg = new JButton("Video s�gning");
              soeg.setBounds(75,145,150,25);
              soeg.setVisible(true);
              soeg.addActionListener(this);
              c.add(soeg);
              afslut = new JButton("Afslut");
              afslut.setBounds(75,180,150,25);
              afslut.setVisible(true);
              afslut.addActionListener(this);
              c.add(afslut);
              //Tilf�jer tekstfelt og label
              besked = new JLabel("Besked:");
              besked.setBounds(10,45,50,25);
              besked.setVisible(true);
              c.add(besked);
              beskedText = new JTextField(" V�lg en af de nedenst�ende knapper");
              beskedText.setBounds(65,45,215,25);
              beskedText.setVisible(true);
              c.add(beskedText);
         }

    Here is a link that might help you:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=219443

  • Dynamic change in Background Picture for Smartform

    Hi,
    I need to keep a background Image in smartform only in specific condition.
    I have done like this :
    I have taken one variable in the intialization and I am filling with the image name for a specific condition.
    This variable , I am giving it in the Background Picture Tab.
    But I am getting run time error as Graphic cannot be displayed.
    Here I am giving the code In the initialization:
    Here p_obnam is my variable of type TDOBNAME
    SAMPLE is my image name which is in SE78
    CASE p_biltyp.
        WHEN 'ZRE' OR
             'G2'.
          p_obnam = space.
        WHEN 'ZF5'.
          p_obnam = 'SAMPLE'.
        WHEN OTHERS.
          p_obnam = 'SAMPLE'.
      ENDCASE.
    Please let me know...how can I do this.
    Regards
    Sandeep

    Hi,
    In the Initialization tab specify the condition..
    ex:
        if you want to print the symbol of the currency in the background according to the currency..
    if w_currency = 'USD'. "---->Data received
    w_name = 'DOLLAR'   "--->Image in SE78
    elseif w_currency = 'INR'.
    w_name = 'RUPEE'.   "--->Image in SE78
    endif.
    now you mention w_name as &w_name& in the background picture of the smartform.
    This will print the symbol according to the currency you received from the driver program or in the smartform itself.
    Thanks & Regards
    Sarves

  • Design Patterns for java swing application front a J2EE server

    Hi,
    i dont nkow that i stay in the correct forum. I am development a java swing client application, no web, and i dont know that if there are any patterns for a client of this type.
    I have readed http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html
    but after of Buissnes Delegate pattern, the others one, over ejb tier, are applicated only for web clients, using servlets and jsp pages.
    If i have a swing client application, what patterns i must folow for implement the client logic of my swing application??
    thanks

    MVC pattern is one of the most used
    http://en.wikipedia.org/wiki/MVC
    http://csis.pace.edu/~bergin/mvc/mvcgui.html
    ...

  • How do I set a background picture for my homepage, not just the edges but the entire screen?

    When I use other web browsers I have the ability to set a background picture on my homepage. How do I do that on Firefox?

    Is this picture to be used in a drop zone or used as the total background of a theme?  If the former it's not possible to do that as that's the nature of the drop zone .
    For a general background for the menu the image should have a 4:3 size ratio in the landscape orientatikon.  For a portrait oriented photo that would mean either cropping to a 4:3 landscape ratio or creating  a 4:3 landscape canvas and putting the portrait image in the center, edgo or wherever and using the composite image.  There will be some white space but that can be filled with a color that goes with the image.
    Of course you will need to use 3rd party image editor that an handle layers or Pages.
    Some Image Editors That Support layers:
    Photoshop Elements 11 for Mac - $79
    Rainbow Painter - $30
    Imagerie - $38
    Acorn - $50
    Pixelmator - $60 
    Seashore - Free
    GIMP for Mac - Free
    Xee 2.1 - free
    You will get something like this:
    One could move the image to the right or left edge for better esthetics and have the buttons in the "white" area.
    OT

  • Best setup for a swing application

    Hello,
    I have developed an Swing application for a EPOS machine.
    The machine has around 512 ram.
    What's the best setup for me in terms of performance, for example, which JVM to use etc.....
    Cheers
    Bobby

    bsbiran wrote:
    Hi,
    Well the app require alot of images and I parts of it do run 'slow'
    ...I'm currently going through a book about Swing and read that many times the "slowness" of a Swing-application can be credited to the programmer for not using the API efficiently/correctly (letting the app repaint too much, or repainting large parts that don't need repainting at all, to name just two things). So, I don't know how much of a Swing-guru you are, but it might be better to read a few decent Swing tutorials or pick up a good Swing book.

  • Problem while creating JAR file for my swing application

    Hi...
    Using my swings application I am trying to Run different software�s .
    My program is Running fine from command prompt. If I create JAR file
    It is giving error like �Failed to load Main-Class manifest attribute from .jar�
    Can anybody help me to creating JAR file
    Thanks in advance
    Cheers
    Mallik

    hi,
    User following command
    jar-cmf textfile_name.txt Jarfile_name *
    here you have to make manifest file
    and making jar file you have reach there by command promt
    and manifest file have some constraint also as well as format
    format are
    Manifest-Version: 1.0
    Class-path: jar file name which are using in app separed by space
    Created-By: 1.5.0 (Sun Microsystems Inc.)
    Main-Class: Main class name
    and end of file is not have carriage return

  • Javaw does not work for my SWING application

    Dear all,
    I try to use 'javaw ClassName' (from a batch test.bat) to hide command prompt window when a Swing application starts. Unfortunately, the CMD still appears. So I recommend the command java and javaw has no difference in my situation.
    I use jdk1.6.0_02 runs on Windows XP Prof Service Pack 2.
    Could anyone give me some hint please?
    Regards,
    Byron
    The Swing code is attached
    import java.awt.*;
    import javax.swing.*;
    public class Reminder extends JFrame
         public static void main(String[] args)
              new Reminder();
         public Reminder()
              super("Reminder");
              JDesktopPane desktop = new JDesktopPane();
    this.setContentPane(desktop);
    this.setLocation(350, 300);
    this.setSize(300, 80);
    this.setResizable(false);
    JLabel reminder = new JLabel("Do not forget delivering reports!", SwingConstants.CENTER);
    reminder.setForeground(new Color(125, 125, 220));
    reminder.setFont(new Font("SansSerif", Font.BOLD, 18));
         this.getContentPane().setLayout(new BorderLayout());
         this.getContentPane().add("Center", reminder);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// set frame closeable
    //     this.setExtendedState(JFrame.MAXIMIZED_BOTH);// set maximized frame
              this.setVisible(true);
    }

    Hi Andrew,
    In my case, it does not require any web server. I could not use javaws.
    There is way work around by using Runtime.exec(). The MS Windows CMD hides when I double click the start.bat file.
    File start.bat:------------------------------------------------------------------------------------
    java Test
    File Test.java:-----------------------------------
    public class Test
         public static void main(String[] args)
              try{
                   Runtime r = Runtime.getRuntime();
                   r.exec("java Reminder");
              }catch(Exception e)
                   e.printStackTrace();
    File Reminder.java has been attached in the original post.
    Regards,
    Byron

  • How to setup JAWS for Java Swing applications

    Hello All,
    I am having one swing application and need to test with JAWS.Could you please let me know the configuration steps.I tried with the following steps and it is not working properly.
    1. Install JDK1.5.0_08
    2. Install JAWS 7.0
    3. Install SUN accessbridge-2_0_1
    4. Copied access-bridge, jaccess-1_4 and accessibility.properties to jre\lib\ext
    it is not identifying even buttons also.
    Thanks & Regards,
    Gana

    Hello there,
    Gana, Ana
    I have the same problem
    1. Install JDK1.5.0_08
    2. Install JAWS 7.0
    3. Install SUN accessbridge-2_0_1
    4. Copied access-bridge, jaccess-1_4 and accessibility.properties to jre\lib\ext
    and nothing

  • Any ideas for this Swing application

    i was asked to make a desktop application that takes use-cases as an input and the output should be a generated use-case diagram. the problem is that iam confused how to generate the use-case diagram using swing and awt. so if u have any ideas, it would be appreciated.
    thanks in advance.

    from the google images of what a "use case diagram" is,
    this looks like a perfect job for NetBeans' Visual Library: [http://graph.netbeans.org/|http://graph.netbeans.org/]
    this is a great video on the topic: [netbeans rcp video|http://www.parleys.com/display/PARLEYS/blueMarine?showComments=true]

  • What is the best MVC approach for designing Swing application

    Hi...
    I am designing an client/server application. In this I am using java swing for the front-end development. We decided on using MVC approach for the front-end design. I found that there are more than one approachs for designing in MVC.
    Which is the best way to model. To create your model taking View into consideration or Creating the Model taking the tables into consideration.
    Can anybody give help me out in this this is urgent. Thanks
    sai

    I'm not sure what you are asking, so I'll just ramble a bit and hope it helps.
    Create your model taking the view and the data into consideration. :-)
    Design a class to hold instances of the rows in your database that correspond (more or less) to the structure of the data. Add any needed methods to that data object to support the data that you might need for the view.
    For example, suppose your database stores a name in two columns (firstname and lastname). You pull that data from the database into a collection of Person objects into your model. Your Person class has two fields (and set/get methods) to hold that data. Now, in your view, you want to display the name as one string, so add a method to the Person object to get the full name (just by concatenating the two data elements).
    You have to take everything into consideration. How you are going to view the data as well as how it might be structured.

Maybe you are looking for