Image as a background for a JFrame

Hi all,
Is it possible to have an image as a background for a JFrame?
How do you do it?
plz help

i personally inherit a panel...
  class ImagePanel extends JPanel {
      Image image;
      public ImagePanel(Image image) {
          this.image = image;
      public void paintComponent(Graphics g) {
          super.paintComponent(g); //paint background
          //Draw image at its natural size first.
          g.drawImage(image, 0, 0, this); //85x62 image
  } and call it with
  private Image icLogo =
Toolkit.getDefaultToolkit().getImage(getClass().getResource("icLogo.gif"));
  private JPanel imgPanel = new ImagePanel(icLogo);Well, this should work with frames, too. In case it doesn't, you can simply add this panel to the frame.
greets
Jochen

Similar Messages

  • Image as a background for XY graph

    Is there a way to have an image imported as backgroud for an XY graph? I
    want to plot the tragectory of something moving on that image and update it
    in real time.
    Thanks,
    Ioan Tudosa

    Thank you, it works.
    Ioan Tudosa
    "Greg McKaskle" wrote in message
    news:[email protected]..
    > > Is there a way to have an image imported as backgroud for an XY graph? I
    > > want to plot the tragectory of something moving on that image and update
    it
    > > in real time.
    > >
    >
    > You can do this, but the most direct approach doesn't work due to
    > optimizations in the graph. The direct approach would be to take the
    > graph into the Customize mode and replace the normally black rectangle
    > with your image. The problem is that the graph cheats a bit and will
    > erase the plot using the background color of the plot area rectangle,
    > meaning that it will still erase over your picture with a rectangle.
    >
    > The solution is to
    make the plot area transparent and place the image
    > behind the plot area. You might even want to place the picture behind
    > the graph and make the graph frame transparent too. If this placement
    > is done inside the control editor, the image is automatically a part of
    > the graph control. If done on a regular panel, you might want to group
    > the graph and picture together.
    >
    > Greg McKaskle
    >

  • Saving image with transparent background for web/Dreamweaver

    How do I save a little image, so that the background stays transparent. I already have the image set up in Photoshop with a transparent background and have saved it as gif and png. When I import it into Dreamweaver the background stays white instead of transparent. What am I not doing right, since I read several posts saying that png and gif are the right formats to save in, if you want a transparent background?
    Thanks for any help with this,
    Anne

    Well now that you have told me to save with some sort of an alpha channel, it works perfectly.
    In CS5:
    Select layer,"Composition,Make movie". Then navigate to "Output module" and choose an option that has "alpha channel" in the title.Choose a name for your project "Output to". I now use "lossless alpha".I don't know if that is the best one, but it seems to be working fine.
    There is no "Quicktime" option in CS5 as in older versions of AE.
    I was trying to save as a video in general or for photoshop and that just wasn't working out well.
    I don't know what the "Frames to Layers import assistant" is that you mentioned though. I still tend to drag and drop, rather then import.
    Thanks for your help.

  • How do I set an image as a background for a course homepage?

    I am trying to set a background image for when people visit the homepage (like MOMA and others). Yet, I can't find where/how to do that. Is there any documentation that people of found for the design side of creating an iTunes U course and nav?
    thx,
    Matthew

    Since you are asking in the Course Manager forum, I am puzzled by your question. As a teacher, you can set an image for your course, but there aren't background images for individual courses.
    If you are talking about the institutional pages in the iTunes U section of the iTunes Store, your institution's page is managed by your site administrator (using Public Site Manager), so contact him or her to make the change.

  • How can I use an image as background for the entire page?

    With CSS mill, the solid color was defined in the color.X.property file and there is no place to define an image to use as the background? Is it doable to use an image as the background for teh entire page? If so, where to add it and how to implement? Thank you!
    Hao Pan ([email protected])

    While I have been able to add the image to a JLabel and insert it into frames, so that it looks like a suitable background, I have found it impossible to place any other component over the top of said imageDid you set a layout manager for the label?
    Check out the [Background Panel|http://www.camick.com/java/blog.html?name=background-panel] for two different solutions depending on your exact requirement.

  • How to set an image as background for a jPanel in an applet

    hi,
    I want to set an image as background for a jPanel component in an applet. I am using netbeans environment. I found that to set an image as a background for a jPanel is not possible. Could someone help me .
    Thanks in advance,
    Joshua

    public class ImagePanel extends JPanel
        private Image image = null;
        public void setImage(Image image)
            this.image = image;
        protected void paintComponent(Graphics g)
            super.paintComponent(g);
            g.drawImage(this.image, 0, 0, this);
    }Adjust to taste.

  • Using image or icon as background for JFrame instead of setBackground()

    hello every one,
    please, i'm trying to change the background of my FRAME using an icon or an image as the backgound whle still having my controls(JButton,JLabel,JTextField etc.) still on top of the image. Just like in HTML, for instace, the body tag can be use as follows:
    <body background="c:\images\fly.gif">
    and the image becomes the background.
    Is this possible in java, if so please, how can i do this?
    thank a lot for the help.

    Check out this thread:
    http://forum.java.sun.com/thread.jsp?forum=31&thread=353504

  • How to set a   background for jFrame?

    Hai.i have a code for background image.i.e
    * TextOver.java
    * Created on June 23, 2008, 1:53 PM
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    public class TextOver
    private static final String IMAGE_PATH =
    "http://upload.wikimedia.org/wikipedia/commons/b/b5/HMS_Cardiff_%28D108%29_1.jpg";
    private BufferedImage image;
    private JTextArea textarea = new JTextArea(20, 40);
    private JPanel mainPanel = new JPanel()
    @Override
    protected void paintComponent(Graphics g)
    super.paintComponent(g);
    if (image != null)
    g.drawImage(image, 0, 0, this);
    public TextOver()
    URL imageUrl;
    try
    imageUrl = new URL(IMAGE_PATH);
    image = ImageIO.read(imageUrl);
    Dimension imageSize = new Dimension(image.getWidth(), image.getHeight());
    mainPanel.setPreferredSize(imageSize);
    JScrollPane scrollpane = new JScrollPane(textarea);
    textarea.setOpaque(false);
    scrollpane.setOpaque(false);
    scrollpane.getViewport().setOpaque(false);
    mainPanel.add(scrollpane);
    catch (MalformedURLException e)
    e.printStackTrace();
    catch (IOException e)
    e.printStackTrace();
    public JPanel getPanel()
    return mainPanel;
    private static void createAndShowGUI()
    admin_login_code a=new admin_login_code();
    a.setVisible(false);
    JFrame frame = new JFrame("TextAreaOverImage Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new TextOver().getPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(false);
    public static void main(String[] args)
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    public void run()
    createAndShowGUI();
    i want to give this backgground to my existing jFrame something like
    import java.sql.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class admin_login_code extends javax.swing.JFrame {
    String admin_name;
    String password;
    Connection con;
    Statement stmt;
    ResultSet rs;
    public admin_login_code() {
    initComponents();
    jPasswordField1.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
    char c = e.getKeyChar();
    if (!(Character.isDigit(c) ||
    (c == KeyEvent.VK_BACK_SPACE) ||
    (c == KeyEvent.VK_DELETE))) {
    getToolkit().beep();
    e.consume();
    jFormattedTextField1.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
    char c = e.getKeyChar();
    if (!(Character.isLetter(c) ||
    (c == KeyEvent.VK_BACK_SPACE) ||
    (c == KeyEvent.VK_DELETE))) {
    getToolkit().beep();
    e.consume();
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
    jLabel1 = new javax.swing.JLabel();
    jPanel1 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();
    jFormattedTextField1 = new javax.swing.JFormattedTextField();
    jLabel4 = new javax.swing.JLabel();
    jPasswordField1 = new javax.swing.JPasswordField();
    jPanel2 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36));
    jLabel1.setForeground(new java.awt.Color(255, 0, 0));
    jLabel1.setText("ADMIN LOGIN");
    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24));
    jLabel2.setText("Admin Name");
    jFormattedTextField1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jFormattedTextField1ActionPerformed(evt);
    jLabel4.setFont(new java.awt.Font("Tahoma", 1, 24));
    jLabel4.setText("Password");
    jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jPasswordField1ActionPerformed(evt);
    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPanel1Layout.createSequentialGroup()
    .add(39, 39, 39)
    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jLabel4)
    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 161, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .add(43, 43, 43)
    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPasswordField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
    .add(jFormattedTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE))
    .addContainerGap())
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPanel1Layout.createSequentialGroup()
    .add(47, 47, 47)
    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(jFormattedTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .add(60, 60, 60)
    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jLabel4)
    .add(jPasswordField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    jButton1.setText("Login");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    jButton3.setText("Exit");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton3ActionPerformed(evt);
    org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
    .add(38, 38, 38)
    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 110, Short.MAX_VALUE)
    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(55, 55, 55))
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(jPanel2Layout.createSequentialGroup()
    .add(38, 38, 38)
    .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(30, Short.MAX_VALUE))
    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(574, 574, 574)
    .add(jLabel1))
    .add(layout.createSequentialGroup()
    .add(459, 459, 459)
    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
    .addContainerGap(521, Short.MAX_VALUE))
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(149, 149, 149)
    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(43, 43, 43)
    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(42, 42, 42)
    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(250, Short.MAX_VALUE))
    pack();
    }// </editor-fold>
    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
    private void jFormattedTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Object src=evt.getSource();
    if(src==jButton3)
    dispose();
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Object src=evt.getSource();
    if(src==jButton1)
    admin_name=jFormattedTextField1.getText();
    password=jPasswordField1.getText();
    try
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcle","scott","root");
    stmt=con.createStatement();
    rs=stmt.executeQuery("select admin_name,password from admin_registration where admin_name='chandana' and password='8989' ");
    while(rs.next())
    if(admin_name.equals(rs.getString(1))&&password.equals(rs.getString(2)))
    admin_registration a=new admin_registration();
    a.setVisible(true);
    dispose();
    else
    JOptionPane.showMessageDialog(null,"Please enter admin name & password ");
    catch(ClassNotFoundException e)
    e.printStackTrace();
    catch(SQLException e)
    e.printStackTrace();
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    admin_login_code a=new admin_login_code();
    a.setVisible(true);
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordField1;
    // End of variables declaration
    Can any one can help me how to set a background for jFrame?
    Thank you in advance.
    Edited by: forums.com on Jul 14, 2008 1:40 AM

    90% of the code you posted is not relevant to your question.
    If you want further help post a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the problem.
    And don't forget to use code tags when posting code.

  • How do you set an image into the background of a JPanel or JFrame?

    How do you set an image into the background of a JPanel or JFrame?

    Something like this, Ive thrown in an ImageIcon on a
    button too for good measure.
    import java.awt.*;
    import javax.swing.*;
    public class JFrameImage extends JFrame {
    public JFrameImage() {
    Container c    = getContentPane();
    JPanel panel = new JPanel(){
                 public void paintComponent(Graphics g)     {
    ImageIcon img = new
    = new ImageIcon("background.jpg");
                      g.drawImage(img.getImage(), 0, 0, null);
                      super.paintComponent(g);
            panel.setOpaque(false);
    ImageIcon icon = new ImageIcon("onButton.jpg");
    JButton button = new JButton(icon);
    panel.add(button);
    c.add(panel);
    public static void main(String[] args) {
    JFrameImage frame = new JFrameImage();
    frame.setSize(200,200);
    frame.setVisible(true);
    Going totally fancy pants
    ImageIcon bigImage = new ImageIcon(bgImage.getImage().getScaledInstance(getWidth(), getHeight(),Image.SCALE_REPLICATE));
    g.drawImage(bigImage.getImage(), 0, 0, this); Will scale the image to the size of the panel
    whereas
    for (int y = 0; y  < getHeight(); y = y + image.getHeight(null))
    for (int x = 0; x< getWidth(); x = x + image.getWidth(null))
    g.drawImage(image, x, y, this); Will give a tiled effect
    Try tiling with an animated gif and bring your processor to a standstill.

  • Image size and dpi for iDVD menu custom background?

    I created a custom background for my iDVD menu. When showing
    on my TV, part of the words and image is cut off on each side, even after adjusting my tv picture between wide screen and standard screen.
    What is the correct size and dpi to make a photograph to use as a background
    image in the menus? This may have been answered before, so I apologize for posting the same question, if so.

    iDVD will try to downsize the image to work, but your TV often will overscan with the result being edges clipped off. The amount of clipping can vary, so the "TV Safe" settings will typically be the center 80-90% of the image. So, if you add a border that makes the image 10-15% larger, that should display the whole image. For example, if you had a 150 dpi image that was 640x480, you could put a 10% border on it so the final image was 704x528.
    John

  • Setting the Background Graphics for a JFrame

    Hi,
    I am designing a GUI-based game and intend to set the background of my JFrame to a specific jgp graphic...I can't seem to find any method for setting the background in the JFrame API.
    Kindly assist!
    Cheers.

    Your search is perhaps marred by being too specific to frame. It is generally considered a bad idea to do any 'custom' thing directly to a frame. Try this search instead.
    [http://www.google.com/search?q=background+panel+java]

  • Background for JFrame

    hi
    I want to apply an image as a background to my application designed over the JFrame component. I want my other components like buttons and labels over that background. How is that possible?
    I am not using JApplet.
    I am new to Java & Swing so if anybody can send me some code ...
    Thanx

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    public class BackgroundImage extends JFrame
         JScrollPane scrollPane;
         ImageIcon icon;
         public BackgroundImage()
              icon = new ImageIcon("apache.jpg");
              JPanel panel = new JPanel()
                   public void paintComponent(Graphics g)
                        //  Approach 1: Dispaly image at at full size
                        g.drawImage(icon.getImage(), 0, 0, null);
                        //  Approach 2: Scale image to size of component
                        // Dimension d = getSize();
                        // g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null);
                        //  Approach 3: Fix the image position in the scroll pane
                        // Point p = scrollPane.getViewport().getViewPosition();
                        // g.drawImage(icon.getImage(), p.x, p.y, null);
                        setOpaque( false );
                        super.paintComponent(g);
              JButton button = new JButton( "Hello" );
              panel.add( button );
              scrollPane = new JScrollPane( panel );
              setContentPane( scrollPane );
         public static void main(String [] args)
              BackgroundImage frame = new BackgroundImage();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(300, 300);
              frame.setVisible(true);
    }

  • Black background for transparent PNG images - Xcelsius 2008 SP4

    Hi Experts,
    I am using Xcelsius 2008 SP4. I have used Image component to show a PNG image with transparent background. When I export the flash, the image is being shown with a black background.
    In Xcelsius 2008 SP3, this problem was not there. I am facing this issue after installing SP4.
    Did anyone else face similar issue ? Is there any workaround ?
    regards,
    pasg

    Hi all,
    I was facing the very same issue. I succeeded in finding a solution to it. This is happening coz of the dimension and pixels of the .png image. I imported the image in the image component, which had the dimensions, 175 x 70, and was of 9.26 kb and the black background was visible. I then changed the dimension of the image to 428 x 140, and 19.2 kb (double the size), and VOILA!! to my surprise IT WORKED !
    Don’t forget to “check” the “embed file” and  “Hide SWF Background Color” check boxes in the image component's, general tab.
    Try the same, I hope this helps.
    Regards,
    Sara

  • Framing image with transparent background png frame

    hi,
    i'm trying to find a way to frame images with transparent background png frames...
    what i'm doing now is
    1-drawing my image on a panel
    2-creating a 2nd image using the frame's filename, stretching this 'frame-image' to a size slighlty larger than that of my main image and drawing the 'frame-image'
    the problems with this method are:
    1-depending on the width of the frame, the frame sometimes hides parts of the image (thick frame), and sometimes there is a gap between the frame and the image (thin frame).
    2-if the image file containing the frame is larger than the frame (Ex: The image is 300x300, the frame is centered in this image and is 200x200; as opposed to the image is 200x200 and the frame takes up all the space), when i position the 'frame-image' near the top left corner of the image i want to frame, the frame appears at the wrong place (shifted down and to the right). This is due to the fact that i'm placing the top corner of the created 'frame-image' and not the frame, who is not in the top corner of my 'frame-image'.
    Is there a way to do what i'm trying to do???
    My ideas (which i don't know how to achieve are)
    1-To 'analyse' my transparent background png file and
         1-only keep the frame,
         2-calculate the frame's thickness
    OR
    2-Let java do the analyzing for me and tell it to frame my image with the frame in the png file
    please feel free to ask for more explanations if my description/question is confusing,
    thanks.

    Have you looked into the Border interface? If what you really want to do
    is put a custom border on a component, you may be able to do it this way.
    Anyway, here is some code that stacks and centres 2 images. It's not hard to do.
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.*;
    public class Example extends JComponent {
        private BufferedImage backgroundImage;
        private BufferedImage foregroundImage;
        public Example(BufferedImage backgroundImage, BufferedImage foregroundImage) {
            this.backgroundImage = backgroundImage;
            this.foregroundImage = foregroundImage;
        public Dimension getPreferredSize() {
            int w = backgroundImage.getWidth();
            int h = backgroundImage.getHeight();
            return new Dimension(w, h); //assuming this is bigger
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = getWidth();
            int h = getHeight();
            //paint both, centred
            int x0 = (w-backgroundImage.getWidth())/2, y0 = (h-backgroundImage.getHeight())/2;
            g.drawImage(backgroundImage, x0, y0, null);
            int x1 = (w-foregroundImage.getWidth())/2, y1 = (h-foregroundImage.getHeight())/2;
            g.drawImage(foregroundImage, x1, y1, null);
        public static void main(String[] args) throws IOException {
            URL url1 = new URL("http://weblogs.java.net/jag/Image54-large.jpeg");
            URL url2 = new URL("http://weblogs.java.net/jag/DukeSaltimbanqueSmall.jpeg");
            JComponent comp = new Example(ImageIO.read(url1), ImageIO.read(url2));
            final JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(comp);
            f.pack();
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
    }

  • Background for JButton

    Hi all I am doing design a background for JButton and also doing design a metal look and place this as
    backgroung rather than using the plain colours eg black, red.
    I dont want to use setIcon because I would like to place an Icon on top eg an arrow for previous or next..
    Here is my sample code:
    import java.awt.*;
    import javax.swing.*;
    public class JImageButton extends JButton
    Image backgroundImage;
    public JImageButton()
    super();
    public JImageButton( Action a )
    super( a );
    public JImageButton( Icon icon )
    super( icon );
    public JImageButton( String text )
    super( text );
    public JImageButton( String text, Icon icon )
    super( text, icon );
    public void setBackgroundImage( Image image )
    MediaTracker mt = new MediaTracker( this );
    mt.addImage( image, 0 );
    try
    mt.waitForAll();
    backgroundImage = image;
    catch( InterruptedException x )
    System.err.println(
    "Specified background image could not be loaded." );
    public void paintComponent( Graphics g )
    super.paintComponent( g );
    Color saved = g.getColor();
    g.setColor( getBackground() );
    g.fillRect( 0, 0, getWidth(), getHeight() );
    g.setColor( saved );
    if( backgroundImage != null )
    int imageX = ( getWidth() - backgroundImage.getWidth( this ) ) / 2;
    int imageY = ( getHeight() - backgroundImage.getHeight( this ) ) / 2;
    g.drawImage( backgroundImage, imageX, imageY, this );
    if( !getText().equals( "" ) )
    g.drawString( super.getText(), getWidth() / 2, getHeight() / 2 );
    if( getIcon() != null )
    Icon icon = getIcon();
    icon.paintIcon( this, g, 10, 10 );
    public Dimension getpreferredSize()
    Dimension oldSize = super.getPreferredSize();
    Dimension newSize = new Dimension();
    Dimension returnSize = new Dimension();
    if( backgroundImage != null )
    newSize.width = backgroundImage.getWidth( this ) + 1;
    newSize.height = backgroundImage.getHeight( this ) + 1;
    if( oldSize.height > newSize.height )
    returnSize.height = oldSize.height;
    else
    returnSize.height = newSize.height;
    if( oldSize.width > newSize.width )
    returnSize.width = oldSize.width;
    else
    returnSize.width = newSize.width;
    return( returnSize );
    And here's a tester to show how it works...
    code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JImageButtonTest extends JFrame
    JButton jb1, jb2;
    JImageButton jib1, jib2, jib3;
    Image backgroundImage;
    ImageIcon icon;
    public JImageButtonTest()
    super( "Test for JImageButton" );
    Toolkit tk = Toolkit.getDefaulttoolkit();
    // Replace "someImage" and "anotherImage" with your own
    // images...
    backgroundImage = tk.getImage( "someImage.gif" );
    Image iconImage = tk.getImage( "anotherImage.gif" );
    icon = new ImageIcon( iconImage );
    jib1 = new JImageButton( "Button 1" );
    jib2 = new JImageButton( "Button 2" );
    jib3 = new JImageButton( "Button 3", icon );
    jib2.setBackgroundImage( backgroundImage );
    jib3.setBackgroundImage( backgroundImage );
    jb1 = new JButton( "JButton 1" );
    jb2 = new JButton( "JButton 2", icon );
    JPanel p1 = new JPanel();
    p1.add( jib1 );
    p1.add( jib2 );
    p1.add( jib3 );
    JPanel p2 = new JPanel();
    p2.add( jb1 );
    p2.add( jb2 );
    getContentPane().add( p1, BorderLayout.SOUTH );
    getContentPane().add( p2, BorderLayout.NORTH );
    public static void main( String[] args )
    JImageButtonTest jibt = new JImageButtonTest();
    jibt.addWindowListener( new ExitHandler() );
    jibt.pack();
    jibt.setVisible( true );
    class ExitHandler extends WindowAdapter
    public void windowClosing( WindowEvent e )
    System.exit( 0 );

    Hi noamt3,
    Please look this my test case's below it's working fine here.
    import java.awt.*;
    import javax.swing.*;
    public class JImageButton extends JButton
    Image backgroundImage;
    public JImageButton()
    super();
    public JImageButton( Action a )
    super( a );
    public JImageButton( Icon icon )
    super( icon );
    public JImageButton( String text )
    super( text );
    public JImageButton( String text, Icon icon )
    super( text, icon );
    public void setBackgroundImage( Image image )
    MediaTracker mt = new MediaTracker( this );
    mt.addImage( image, 0 );
    try
    mt.waitForAll();
    backgroundImage = image;
    catch( InterruptedException x )
    System.err.println(
    "Specified background image could not be loaded." );
    public void paintComponent( Graphics g )
    super.paintComponent( g );
    Color saved = g.getColor();
    g.setColor( getBackground() );
    g.fillRect( 0, 0, getWidth(), getHeight() );
    g.setColor( saved );
    if( backgroundImage != null )
    int imageX = ( getWidth() - backgroundImage.getWidth( this ) ) / 2;
    int imageY = ( getHeight() - backgroundImage.getHeight( this ) ) / 2;
    g.drawImage( backgroundImage, imageX, imageY, this );
    if( !getText().equals( "" ) )
    g.drawString( super.getText(), getWidth() / 2, getHeight() / 2 );
    if( getIcon() != null )
    Icon icon = getIcon();
    icon.paintIcon( this, g, 10, 10 );
    public Dimension getPreferredSize()
    Dimension oldSize = super.getPreferredSize();
    Dimension newSize = new Dimension();
    Dimension returnSize = new Dimension();
    if( backgroundImage != null )
    newSize.width = backgroundImage.getWidth( this ) + 1;
    newSize.height = backgroundImage.getHeight( this ) + 1;
    if( oldSize.height > newSize.height )
    returnSize.height = oldSize.height;
    else
    returnSize.height = newSize.height;
    if( oldSize.width > newSize.width )
    returnSize.width = oldSize.width;
    else
    returnSize.width = newSize.width;
    return( returnSize );
    Here tester testcase:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JImageButtonTest extends JFrame
    JButton jb1, jb2;
    JImageButton jib1, jib2, jib3;
    Image backgroundImage;
    ImageIcon icon;
    public JImageButtonTest()
    super( "Test for JImageButton" );
    Toolkit tk = Toolkit.getDefaultToolkit();
    // Replace "someImage" and "anotherImage" with your own
    // images...
    backgroundImage = tk.getImage( "someImage.gif" );
    Image iconImage = tk.getImage( "anotherImage.gif" );
    icon = new ImageIcon( iconImage );
    jib1 = new JImageButton( "Button 1" );
    jib2 = new JImageButton( "Button 2" );
    jib3 = new JImageButton( "Button 3", icon );
    jib2.setBackgroundImage( backgroundImage );
    jib3.setBackgroundImage( backgroundImage );
    jb1 = new JButton( "JButton 1" );
    jb2 = new JButton( "JButton 2", icon );
    JPanel p1 = new JPanel();
    p1.add( jib1 );
    p1.add( jib2 );
    p1.add( jib3 );
    JPanel p2 = new JPanel();
    p2.add( jb1 );
    p2.add( jb2 );
    getContentPane().add( p1, BorderLayout.SOUTH );
    getContentPane().add( p2, BorderLayout.NORTH );
    public static void main( String[] args )
    JImageButtonTest jibt = new JImageButtonTest();
    jibt.addWindowListener( new ExitHandler() );
    jibt.pack();
    jibt.setVisible( true );
    class ExitHandler extends WindowAdapter
    public void windowClosing( WindowEvent e )
    System.exit( 0 );
    I hope this will help you out.
    Regards,
    Tirumalarao
    Developer Technical Support,
    Sun Microsystems,
    http://www.sun.com/developers/support.

Maybe you are looking for

  • My iphone will no longer sync to itunes?

    I need help.  I have done everything in the diagnostic program but my iphone will still not sync.

  • Error: Using CustomAuthenticationIF to confg EPM singal sign on

    We want to config the EPM SSO custom module for other systems I have configed the sso in sharedservice by the document [Oracle?Enterprise Performance Management System - Security Administration Guide] in section [Using a Custom Authentication Module]

  • OMBPlus TCL-script in OWB Designer at startup?

    Is there a way to run a OMBPlus script in OWB Designer automatically at startup? What I would like is a small tcl script that just contains a procedure like: proc run_script {} { source "I://OMB//SCRIPTS//2009//DEPLOY//START_DEPLOY.tcl" Then the user

  • Integrating with SiteMinder

    Hi, We are trying to integrate our Forte UDS web application with SiteMinder for user authentication. When SiteMinder is enabled and we run the EnableAccess method to register the web access service we get the following error: SYSTEM ERROR: This obje

  • Why do I get an error when trying to export a large PDF to Excel?

    The ExportPDF process is only returning a simple message. The file has not been processed. An error has occurred which stopped the processing of the PDF.