Adding gif image to a panel

Hi
i have had a look at the link that you have directed to me but i dont understand which code is neccessary for me. please could you help me with the code to add a GIF background image to a Jpanel.
Thanks

hi, no it wasnt this link but the link given was not suitable for my programming i need to add an image to a background using the drawImage() method could you please please please help me.
Thanks

Similar Messages

  • Adding GIF images to a stand-alone program

    To add a GIF image to an applet one can use the "getImage" function defined in the applet class.
    How do i add an image to a stand-alone program in java when one doesn't extend the Applet class ( and so one doesn't have access
    to the "getImage" procedure)
    I have just begun with Java so please reply in simple english and not in some hard-to-understand "Javian" crap!
    Thank u

    You mean a JFrame?
    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)     {
    // puts an image on the whole program as background
                      ImageIcon img = new ImageIcon("myBackground.jpg");
                      g.drawImage(img.getImage(), 0, 0, null);
                      super.paintComponent(g);
            panel.setOpaque(false);
    // puts an image on the button
          ImageIcon icon = new ImageIcon("myButton.jpg");
          JButton button = new JButton(icon);
          panel.add(button);
          c.add(panel);
      public static void main(String[] args) {
        JFrameImage frame = new JFrameImage();
    // puts your own personal image on the frame itself
          Image onFrame = Toolkit.getDefaultToolkit().getImage("myIcon.gif") ;
        frame.setIconImage(onFrame);
        frame.setSize(200,200);
    // stops the thing looking sh*tty and amateurish by putting it nearer the middle
    // of the screen, instead of the default top left hand corner
        frame.setLocation(300,200);
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.setVisible(true);
    }

  • Adding different images to different panels

    If I am having a class which is extending Applet not JApplet and in that I am having CardLayout with 4 different panels. If I want to add 4 different images 2 these Panels what is the solution? Can anyone please help?

    sorry
    myPanel p1, p2, p3, p4;
    class myPanel extends Panel
    Image image;
    public myPanel(Image img)
    image = img;
    public void paint (Graphics g)
    if (image == null)
    return;
    g.drawImage(image, 0, 0, this);
    }

  • Problem in displaying  gif image on to the JFrame

    i added gif image in to the jframe but when i run that image the image get distorted very much.

    public class MyFrame extends JFrame
         private static final String sIMAGE_PATH = "My\\Path\\To\\Image\\fileName.gif";
         public MyFrame()
              super("Frame with image on it");
              init();
         private void init()
              JPanel panel = new JPanel(new BorderLayout());
              ImageIcon image = new ImageIcon(sIMAGE_PATH);
              JLabel label = new JLabel(image);
              panel.add(label, BorderLayout.CENTER);
              this.setContentPane(panel);
         }Hope this helps..

  • Displaying thumbnail images in another panel when it  mouse clicked

    hi,
    I am using JAI, what i am trying to do is i am decoding the tiff file and creating thumbnail of that images and adding thumbnail images to one panel and enlarged images to another panel using DisplayJAI. Now when i will click i thumbnail images on left panel then that image should be display to another panel on right side. Adding both panel to JSplit panel.
    I have no idia how to do it, i tried to do in different way but not able to do that. I hope anybody can give me the hints about this.
    Thanks

    i am adding thumbnail of all images in left panel and enlarged view of each thumbnail is added in right panel , and what i require that when i click thumbnail in left side it's corresponding enlarged image should be visible, suppose in right panel page no. 1 is visible and when i click thumbnail of 13 page in left panel that should be in viewport of right panel.You should put your right panel inside a JScrollPane and call [scrollRectToVisible() |http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#scrollRectToVisible(java.awt.Rectangle)] from your panel. You can put the rectangles corresponding to enlarged images in a hash map with the keys being some unique attribute of your thumbnails in left panel. That will help in getting the rectangle to make visible, real fast. Or, you can just put the rectangles in some array indexed according to the thumbnails in left panel.
    I also want to hide the left panel, i think it's possible by Frame.Yes it is possible, but you said you have both your panels in a split pane, so maybe you can use its setDividerLocation() function to hide your left panel.
    Thanks!

  • Adding Image to a Panel

    Dear All,
    I am having one problem. I am having an Applet .i want to add an Image to a panel and that Panel i have to add to Applet. how can add any one help pls.
    Regards,
    Viswa

    Create an image icon ImageIcon icon = new ImageIcon("someimage.gif") and then add this to a label JLabel label = new JLabel(icon) then just add this label to your panel.
    Cath

  • Adding a gif image

    At the moment I have just drawn checkers but I would like to add a .gif image so it looks alot better. But im really unsure how to do this without completely messing things up. Thanks to any help guys
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class MyDraughts1edited extends Applet
                             implements MouseListener, MouseMotionListener{
      private int top = 0, left1 = 0, bWidth = 200, n = 4;
      private Point counter1, counter2, counter3, counter4, counter5, counter6,
       counter7, counter8, mouse;
      private int select;
      public void init() {
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
        counter1 = new Point(15,15);
        counter2 = new Point(65,15);
        counter3 = new Point(115,15);
        counter4 = new Point(165,15);
        counter5 = new Point(15,165);
        counter6 = new Point(65,165);
        counter7 = new Point(115,165);
        counter8 = new Point(165,165);
      public void paint (Graphics g) {
        Draw_Board(g);
        g.setColor (Color.black);
        g.fillOval(counter1.x, counter1.y, 20,20);
        g.fillOval(counter2.x, counter2.y, 20,20);
        g.fillOval(counter3.x, counter3.y, 20,20);
        g.fillOval(counter4.x, counter4.y, 20,20);
        g.setColor (Color.white);
        g.fillOval(counter5.x, counter5.y, 20,20);
        g.fillOval(counter6.x, counter6.y, 20,20);
        g.fillOval(counter7.x, counter7.y, 20,20);
        g.fillOval(counter8.x, counter8.y, 20,20);
      public void mouseDragged(MouseEvent e) {
        mouse = e.getPoint();
        // continuously change the coordinates of the selected counter
        switch(select){
          case 1 : counter1 = mouse; break;
          case 2 : counter2 = mouse; break;
          case 3 : counter3 = mouse; break;
          case 4 : counter4 = mouse; break;
          case 5 : counter5 = mouse; break;
          case 6 : counter6 = mouse; break;
          case 7 : counter7 = mouse; break;
          case 8 : counter8 = mouse; break;
          default :
        repaint();
      // required for the interface
      public void mouseMoved(MouseEvent e) {}
      public void mousePressed(MouseEvent e) {
        mouse = e.getPoint();
        if (mouse.x > counter1.x - 20 && mouse.x < counter1.x + 20 &&
            mouse.y > counter1.y - 20 && mouse.y < counter1.y + 20){
          select = 1;
        else if (mouse.x > counter2.x - 20 && mouse.x < counter2.x + 20 &&
            mouse.y > counter2.y - 20 && mouse.y < counter2.y + 20){
          select = 2;
        else if (mouse.x > counter3.x - 20 && mouse.x < counter3.x + 20 &&
            mouse.y > counter3.y - 20 && mouse.y < counter3.y + 20){
          select = 3;
        else if (mouse.x > counter4.x - 20 && mouse.x < counter4.x + 20 &&
            mouse.y > counter4.y - 20 && mouse.y < counter4.y + 20){
          select = 4;
        else if (mouse.x > counter5.x - 20 && mouse.x < counter5.x + 20 &&
            mouse.y > counter5.y - 20 && mouse.y < counter5.y + 20){
          select = 5;
        else if (mouse.x > counter6.x - 20 && mouse.x < counter6.x + 20 &&
            mouse.y > counter6.y - 20 && mouse.y < counter6.y + 20){
          select = 6;
        else if (mouse.x > counter7.x - 20 && mouse.x < counter7.x + 20 &&
            mouse.y > counter7.y - 20 && mouse.y < counter7.y + 20){
          select = 7;
        else if (mouse.x > counter8.x - 20 && mouse.x < counter8.x + 20 &&
            mouse.y > counter8.y - 20 && mouse.y < counter8.y + 20){
          select = 8;
        else{
        repaint();
      // required for the interface
      public void mouseClicked(MouseEvent event){}
      public void mouseReleased(MouseEvent event){}
      public void mouseEntered(MouseEvent event){}
      public void mouseExited(MouseEvent event){}
      private void Draw_Board(Graphics g) {
        int sqWidth = bWidth/n; // Rounds down
        for (int row = 0; row < 4; ++row){
          for(int Col=0; Col<4; Col++) {
            g.setColor(posColor(row, Col));
            g.fillRect
             (left1 + Col * sqWidth, top + row * sqWidth, sqWidth, sqWidth);
      private boolean isEven(int x) {
        return(x == 2 * (x / 2));
      private Color posColor(int row, int Col) {
        if ( (isEven(row) && isEven(Col)) ||
            (! isEven(row) && ! isEven(Col))){
          return(Color.GRAY);
        else{
          return(Color.lightGray);
    }

    If you can use Swing instead of AWT, then maybe this posting will give you some ideas:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=518707&start=3

  • Adding gif/jpeg image to frame

    Hi,
    Can anyone please tell me how to add a jpeg/gif image or an icon to the title bar ofa frame such that it appears before the title.
    Thanks

        Image myImg=Toolkit.getDefaultToolkit().getImage(
          getClass().getResource("YourImage.gif"));
    myFrame.setIconImage(myImg);~Bill
    Edited by: abillconsl on Sep 24, 2007 5:07 PM

  • How to drag image in left panel then drop into right panel??

    Dear friends.
    I have following code, it is runnable, just add two jpg image files is ok, to run.
    I tried few days to drag image from left panel then drop into right panel or vice versa, but not success, can any GUI guru help??
    Thanks.
    Sunny
    [1]. main code/calling code:
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class ImagePanelCall extends JComponent {
         public  JSplitPane ImagePanelCall() {
              setPreferredSize(new Dimension(1200,300));
              JSplitPane          sp = new JSplitPane();
              sp.setPreferredSize(new Dimension(1200,600));
              sp.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
              add(sp);
              ImagePanel     ip = new ImagePanel();
              ImagePanel     ip1 = new ImagePanel();
              ip.setPreferredSize(new Dimension(600,300));
              ip1.setPreferredSize(new Dimension(600,300));
              sp.setLeftComponent(ip);// add left part
              sp.setRightComponent(ip1);// add right part
              sp.setVisible(true);
              return sp;
         public static void main(String[] args) {
              JFrame frame = new JFrame("Test transformable images");
              ImagePanelCall  ic = new ImagePanelCall();
              frame.setPreferredSize(new Dimension(1200,600));
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.getContentPane().add(ic.ImagePanelCall(), BorderLayout.CENTER);
              frame.pack();
              frame.setVisible(true);
    }[2]. code 2
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class ImagePanel extends JComponent {
         private static final Cursor DEFAULT_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR);
         private static final Cursor MOVE_CURSOR = new Cursor(Cursor.MOVE_CURSOR);
         private static final Cursor VERTICAL_RESIZE_CURSOR = new Cursor(Cursor.N_RESIZE_CURSOR);
         private static final Cursor HORIZONTAL_RESIZE_CURSOR = new Cursor(Cursor.W_RESIZE_CURSOR);
         private static final Cursor NW_SE_RESIZE_CURSOR = new Cursor(Cursor.NW_RESIZE_CURSOR);
         private static final Cursor NE_SW_RESIZE_CURSOR = new Cursor(Cursor.NE_RESIZE_CURSOR);
         public Vector images;
         * Create an ImagePanel with two images in.
         * A MouseHandler instance is added as mouse listener and mouse motion listener.
         public ImagePanel() {
              images = new Vector();
              images.add(new TransformableImage("swing/dnd/Bird.gif"));
              images.add(new TransformableImage("swing/dnd/Cat.gif"));
              setPreferredSize(new Dimension(600,600));
              MouseHandler mh = new MouseHandler();
              addMouseListener(mh);
              addMouseMotionListener(mh);
         * Simply paint all the images contained in the Vector images, calling their method draw(Graphics2D, ImageObserver).
         public void paintComponent(Graphics g) {
              Graphics2D g2D = (Graphics2D)g;
              for (int i = images.size()-1; i>=0; i--) {     
                   ((TransformableImage)images.get(i)).draw(g2D, this);
         * Inner class defining the behavior of the mouse.
         final class MouseHandler extends MouseInputAdapter {
              private TransformableImage draggedImage;
              private int transformation;
              private int dx, dy;
              public void mouseMoved(MouseEvent e) {
                   Point p = e.getPoint();
                   TransformableImage image = getImageAt(p);
                   if (image != null) {
                        transformation = image.getTransformation(p);
                        setConvenientCursor(transformation);
                   else {
                        setConvenientCursor(-1);
              public void mousePressed(MouseEvent e) {
                   Point p = e.getPoint();
                   draggedImage = getImageAt(p);
                   if (draggedImage!=null) {
                        dx = p.x-draggedImage.x;
                        dy = p.y-draggedImage.y;
              public void mouseDragged(MouseEvent e) {
                   if (draggedImage==null) {
                        return;
                   Point p = e.getPoint();
                   repaint(draggedImage.x,draggedImage.y,draggedImage.width+1,draggedImage.height+1);
                   draggedImage.transform(p, transformation,dx,dy);
                   repaint(draggedImage.x,draggedImage.y,draggedImage.width+1,draggedImage.height+1);
              public void mouseReleased(MouseEvent e) {
                   Point p = e.getPoint();
                   draggedImage = null;
         * Utility method used to get the image located at a Point p.
         * Returns null if there is no image at this point.
         private final TransformableImage getImageAt(Point p) {
              TransformableImage image = null;
              for (int i = 0, n = images.size(); i<n; i++) {     
                   image = (TransformableImage)images.get(i);
                   if (image.contains(p)) {
                        return(image);
              return(null);
         * Sets the convenient cursor according the the transformation (i.e. the position of the mouse over the image).
         private final void setConvenientCursor(int transfo) {
              Cursor currentCursor = getCursor();
              Cursor newCursor = null;
              switch (transfo) {
                   case TransformableImage.MOVE : newCursor = MOVE_CURSOR;
                        break;
                   case TransformableImage.RESIZE_TOP : newCursor = VERTICAL_RESIZE_CURSOR;
                        break;
                   case TransformableImage.RESIZE_BOTTOM : newCursor = VERTICAL_RESIZE_CURSOR;
                        break;
                   case TransformableImage.RESIZE_LEFT : newCursor = HORIZONTAL_RESIZE_CURSOR;
                        break;
                   case TransformableImage.RESIZE_RIGHT : newCursor = HORIZONTAL_RESIZE_CURSOR;
                        break;
                   case TransformableImage.RESIZE_TOP_LEFT_CORNER : newCursor = NW_SE_RESIZE_CURSOR;
                        break;
                   case TransformableImage.RESIZE_TOP_RIGHT_CORNER : newCursor = NE_SW_RESIZE_CURSOR;
                        break;
                   case TransformableImage.RESIZE_BOTTOM_LEFT_CORNER : newCursor = NE_SW_RESIZE_CURSOR;
                        break;
                   case TransformableImage.RESIZE_BOTTOM_RIGHT_CORNER : newCursor = NW_SE_RESIZE_CURSOR;
                        break;
                   default : newCursor = DEFAULT_CURSOR;
              if (newCursor != null && currentCursor != newCursor) {
                   setCursor(newCursor);
         public static void main(String[] args) {
              JFrame frame = new JFrame("Test transformable images");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.getContentPane().add(new ImagePanel(), BorderLayout.CENTER);
              frame.pack();
              frame.setVisible(true);
    }[3]. code 3
    import java.awt.*;
    import javax.swing.*;
    import java.awt.image.*;
    public final class TransformableImage extends Rectangle {
         public static final int MOVE = 0;
         public static final int RESIZE_TOP = 10;
         public static final int RESIZE_BOTTOM = 20;
         public static final int RESIZE_RIGHT = 1;
         public static final int RESIZE_LEFT = 2;
         public static final int RESIZE_TOP_RIGHT_CORNER = 11;
         public static final int RESIZE_TOP_LEFT_CORNER = 12;
         public static final int RESIZE_BOTTOM_RIGHT_CORNER = 21;
         public static final int RESIZE_BOTTOM_LEFT_CORNER = 22;
         public static final int BORDER_THICKNESS = 5;
         public static final int MIN_THICKNESS = BORDER_THICKNESS*2;
         private static final Color borderColor = Color.black;
         private Image image;
         * Create an TransformableImage from the image file filename.
         * The TransformableImage bounds (inherited from the class Rectangle) are setted to the corresponding values.
         public TransformableImage(String filename) {
              ImageIcon ic = new ImageIcon(filename);
              image = ic.getImage();
              setBounds(0,0,ic.getIconWidth(), ic.getIconHeight());
         * Draw the image rescaled to fit the bounds.
         * A black rectangle is drawn around the image.
         public final void draw(Graphics2D g, ImageObserver observer) {
              Color oldColor = g.getColor();
              g.setColor(borderColor);
              g.drawImage(image, x, y, width, height, observer);
              g.draw(this);
              g.setColor(oldColor);
         * Return an int corresponding to the transformation available according to the mouse location on the image.
         * If the point p is in the border, with a thickness of BORDER_THICKNESS, around the image, the corresponding
         * transformation is returned (RESIZE_TOP, ..., RESIZE_BOTTOM_LEFT_CORNER).
         * If the point p is located in the center of the image (i.e. out of the border), the MOVE transformation is returned.
         * We allways suppose that p is contained in the image bounds.
         public final int getTransformation(Point p) {
              int px = p.x;
              int py = p.y;
              int transformation = 0;
              if (py<(y+BORDER_THICKNESS)) {
                   transformation += RESIZE_TOP;
              else
              if (py>(y+height-BORDER_THICKNESS-1)) {
                   transformation += RESIZE_BOTTOM;
              if (px<(x+BORDER_THICKNESS)) {
                   transformation += RESIZE_LEFT;
              else
              if (px>(x+width-BORDER_THICKNESS-1)) {
                   transformation += RESIZE_RIGHT;
              return(transformation);
         * Move the left side of the image, verifying that the width is > to the MIN_THICKNESS.
         public final void moveX1(int px) {
              int x1 = x+width;
              if (px>x1-MIN_THICKNESS) {
                   x = x1-MIN_THICKNESS;
                   width = MIN_THICKNESS;
              else {
                   width += (x-px);
                   x = px;               
         * Move the right side of the image, verifying that the width is > to the MIN_THICKNESS.
         public final void moveX2(int px) {
              width = px-x;
              if (width<MIN_THICKNESS) {
                   width = MIN_THICKNESS;
         * Move the top side of the image, verifying that the height is > to the MIN_THICKNESS.
         public final void moveY1(int py) {
              int y1 = y+height;
              if (py>y1-MIN_THICKNESS) {
                   y = y1-MIN_THICKNESS;
                   height = MIN_THICKNESS;
              else {
                   height += (y-py);
                   y = py;               
         * Move the bottom side of the image, verifying that the height is > to the MIN_THICKNESS.
         public final void moveY2(int py) {
              height = py-y;
              if (height<MIN_THICKNESS) {
                   height = MIN_THICKNESS;
         * Apply a given transformation with the given Point to the image.
         * The shift values dx and dy are needed for move tho locate the image at the same relative position from the cursor (p).
         public final void transform(Point p, int transformationType, int dx, int dy) {
              int px = p.x;
              int py = p.y;
              switch (transformationType) {
                   case MOVE : x = px-dx; y = py-dy;
                        break;
                   case RESIZE_TOP : moveY1(py);
                        break;
                   case RESIZE_BOTTOM : moveY2(py);
                        break;
                   case RESIZE_LEFT : moveX1(px);
                        break;
                   case RESIZE_RIGHT : moveX2(px);
                        break;
                   case RESIZE_TOP_LEFT_CORNER : moveX1(px);moveY1(py);
                        break;
                   case RESIZE_TOP_RIGHT_CORNER : moveX2(px);moveY1(py);
                        break;
                   case RESIZE_BOTTOM_LEFT_CORNER : moveX1(px);moveY2(py);
                        break;
                   case RESIZE_BOTTOM_RIGHT_CORNER : moveX2(px);moveY2(py);
                        break;
                   default :
    }

    I gave you a simple solution in your other posting. You never responded to the suggestion stating why the given solution wouldn't work, so it can't be that urgent.

  • Animated Gif Image does not render correctly on screen

    I have added animated gif image to the scene it does not render correctely.it shakes on the screen. plz give me any suggestion
    i use following code
    Image logo= new Image(getClass().getResourceAsStream("images/image.gif"));
    logoLabel.setGraphic(new ImageView(logo));

    Hello user,
    I think gif are rendered smoothly.
    Are you sure you are not making many object of same images everytime?
    Thanks.
    Narayan

  • Using GIF images in iMovie11 ??

    Admittedly this is now my first attempt at using the iMovie8/9/11 editing structure since I was perfectly satisfied with iMovie06 HD and that timeline approach. In the old program I had GIF formatted images (Maps) that I had available in my "Photos" Media for adding to my Project timeline in the same fashion I would use a still JPG photo.
    After a number of hours searching the forum here and all the iMovie 9/11 documentation I can find, it appears  this new version of iMovie does not display GIF images in your iPhoto Media list to use for projects. Bottomline is I am not able to drag a gif map into my video project. It seems only JPG format files are now displayed in iMovie's iPHOTO Media source list.  I have one graphic that is a collage of photos and graphical objects which I had saved in JPG format to preserve the photo quality and that one shows in my Media list. No GIFs.  What gives?  I can open iPhoto stand alone and see my map graphics that are present in the database in their own Rolls.  I've tried dragging, dropping, importing, etc. with no success.
    This is a significant limitation (or bug) if the current version of iMovie does not allow free use of graphical images in GIF format. Can anyone shed some light on this and help solve my problem before I go back to iMovie HD?  Thanks in advance.

    Klondike Kid wrote:
    Hmm, doesn't seem to make sense …
    I'm not in the position here to 'defend' Apples decision, but an expert as you should keep in mind:
    iM vers.≤6 was a completely diff app than iM vers. ≥08
    same name, from the ground different programs (not only on the outside)
    video isn't based upon limited 256 colors, nor has inserting a file-size-degraded still any effect on the resulting video-file size.
    Photoshop is a >1k$ professional tool, meant for stills - so, it should support a bunch of formats, including obsolete ones. iMovie is a 15$ consumer toy tool, by concept reduced in options to offer convenience - for a broad range of people - I dare to say, the majority of iMovie users dont even know the existence of 'gif', iMovies iLife partner for stills, iPhoto, is biased to jpg.-
    Klondike Kid wrote:
    … I guess I'll have to live without it for iMovie.
    on MacOS, there's little options, I haven't tried, but I wouldn't be surprised, if FCPX doesn't support gif either. pretty sure, Premiere as part of the Adobe CS suite, will handle it - somehow.
    happy movie making!

  • Gif images for iconic button?anybody met success

    I have done everything what is specified in the doc i.e added the lineof code for app.iconpath and referred the same in the HTML FOR cartridge in serverApp param
    and also in the cartridge parameters but the result no the buttons just stare balank at me.
    Instead i tried to replace the buttons with the 1k,2k size gif images oops! Oracle Developer server can't even take a byte of
    image files and the application hangs searching for the file.
    Why can anyone who has met with success please tell me how?
    Mahesh

    I tryed almost everuthing (I'm using WebDB
    listener not OAS)
    The only sollution worked for me is
    in Registry.dat (NOT WINDOWS REGISTRY)
    which located in
    ORACLE_HOME\FORMS60\JAVA\ORACLE\FORMS\REGISTRY
    FOLLOWING SETTINGS
    default.icons.iconpath=/dev60temp/
    default.icons.iconextension=gif
    where /dev60temp/ is virtual directory made
    for WebDB listener.
    Please let me know if your immage looks Ok.
    Mine is shifted to the upper right corner.
    Good luck!

  • Adding background image to JPanal...............PLEASE PLEASE HELP

    Hi,
    i am very very stuck in adding a image to a JPanel. here is my code could any one please show me where and how to add an image to this please. a sample code would be prefered.
    Thanks
    * card.java
    public class card extends javax.swing.JFrame {
    /** Creates new form card */
    public card() {
    initComponents();
    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    getContentPane().setLayout(new java.awt.GridBagLayout());
    setTitle("Card Game");
    addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent evt) {
    exitForm(evt);
    jPanel1.setLayout(new java.awt.GridBagLayout());
    jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    jLabel1.setIcon(new javax.swing.ImageIcon("card game implementation\\headingF1.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.insets = new java.awt.Insets(5, 145, 0, 205);
    jPanel1.add(jLabel1, gridBagConstraints);
    jButton1.setIcon(new javax.swing.ImageIcon("card game implementation\\Nigat\\helpbuttonF.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.ipadx = -36;
    gridBagConstraints.ipady = -10;
    gridBagConstraints.insets = new java.awt.Insets(83, 40, 130, 40);
    jPanel1.add(jButton1, gridBagConstraints);
    jButton2.setIcon(new javax.swing.ImageIcon("card game implementation\\Nigat\\newgame1.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.ipadx = -39;
    gridBagConstraints.ipady = -12;
    gridBagConstraints.insets = new java.awt.Insets(213, 40, 0, 40);
    jPanel1.add(jButton2, gridBagConstraints);
    jButton3.setIcon(new javax.swing.ImageIcon("card game implementation\\Nigat\\quit1.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.ipadx = -39;
    gridBagConstraints.ipady = -12;
    gridBagConstraints.insets = new java.awt.Insets(80, 40, 70, 40);
    jPanel1.add(jButton3, gridBagConstraints);
    jPanel2.setLayout(new java.awt.GridBagLayout());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridheight = 2;
    gridBagConstraints.ipadx = 420;
    gridBagConstraints.ipady = 400;
    gridBagConstraints.insets = new java.awt.Insets(43, 60, 40, 0);
    jPanel1.add(jPanel2, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 1);
    getContentPane().add(jPanel1, gridBagConstraints);
    pack();
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit(0);
    * @param args the command line arguments
    public static void main(String args[]) {
    new card().show();
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration

    My images loaded up just fine. I left them in and commented out. Try using the single forward slash below...
    public class CardTest extends javax.swing.JFrame {
        /** Creates new form card */
        public CardTest() {
            initComponents();
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
        private void initComponents() {
            java.awt.GridBagConstraints gridBagConstraints;
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jPanel2 = new javax.swing.JPanel();
            getContentPane().setLayout(new java.awt.GridBagLayout());
            setTitle("Card Game");
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    exitForm(evt);
            jPanel1.setLayout(new java.awt.GridBagLayout());
            jPanel1.setBackground(new java.awt.Color(255, 255, 255));
            jLabel1.setIcon(new javax.swing.ImageIcon(
                            //"images/T1.gif"));
                            "card game implementation/headingF1.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.gridwidth = 2;
            gridBagConstraints.insets = new java.awt.Insets(5, 145, 0, 205);
            jPanel1.add(jLabel1, gridBagConstraints);
            jButton1.setIcon(new javax.swing.ImageIcon(
                             //"images/T2.gif"));
                             "card game implementation/Nigat/helpbuttonF.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.ipadx = -36;
            gridBagConstraints.ipady = -10;
            gridBagConstraints.insets = new java.awt.Insets(83, 40, 130, 40);
            jPanel1.add(jButton1, gridBagConstraints);
            jButton2.setIcon(new javax.swing.ImageIcon(
                             //"images/T3.gif"));
                             "card game implementation/Nigat/newgame1.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.ipadx = -39;
            gridBagConstraints.ipady = -12;
            gridBagConstraints.insets = new java.awt.Insets(213, 40, 0, 40);
            jPanel1.add(jButton2, gridBagConstraints);
            jButton3.setIcon(new javax.swing.ImageIcon(
                             //"images/T4.gif"));
                             "card game implementation/Nigat/quit1.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 2;
            gridBagConstraints.ipadx = -39;
            gridBagConstraints.ipady = -12;
            gridBagConstraints.insets = new java.awt.Insets(80, 40, 70, 40);
            jPanel1.add(jButton3, gridBagConstraints);
            jPanel2.setLayout(new java.awt.GridBagLayout());
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.gridheight = 2;
            gridBagConstraints.ipadx = 420;
            gridBagConstraints.ipady = 400;
            gridBagConstraints.insets = new java.awt.Insets(43, 60, 40, 0);
            jPanel1.add(jPanel2, gridBagConstraints);
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 1);
            getContentPane().add(jPanel1, gridBagConstraints);
            pack();
        /** Exit the Application */
        private void exitForm(java.awt.event.WindowEvent evt) {
            System.exit(0);
         * @param args the command line arguments
        public static void main(String args[]) {
            new CardTest().setVisible(true);
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        // End of variables declaration
    }

  • Problem adding an image to the display list

    I'm trying to create a component to add images to a Panel (id=hbox). I seem to be loading the images successfully (because the trace statement works), but can't add them to the display.
    I'm using the following line of code, but getting an error:
    hbox.addChild(event.currentTarget)
    What am I doing wrong?
    Thank you!
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" backgroundAlpha=".5"   backgroundColor="0xffffff" creationComplete="init()"  >
        <mx:Script>
        <![CDATA[
        public function placeAnimals():void{
            for(var i:int=0; i<12;i++){
                var imageLoader:Loader = new Loader();
                var url:String = "images/animal"+i+".png";
                var urlReq:URLRequest = new URLRequest(url);
                imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
                imageLoader.x=50*i
                imageLoader.load(urlReq);       
                function imageLoaded(event:Event):void {
                    trace("Success="+event.currentTarget.url)
                   //hbox.addChild(event.currentTarget)
             public function init():void {
                 placeAnimals()
        ]]>
        </mx:Script>
        <mx:Panel  id="hbox"  height="200"   backgroundImage="images/smallWhitePanel.png"  borderStyle="none" />
    </mx:Canvas>

    You cannot add an image directly like that... why not dot the following
    var img:Image = new Image();
    img.source = url;
    this.addChild(img);
    hpefully it should work.. i'v neva seen image being added to the hbox like that... it will work only if you embed it / put it in source element..
    the following link should help you
    http://livedocs.adobe.com/flex/3/html/help.html?content=controls_16.html

  • Please, Please Help Me (.gif Images)

    Hello,
    I want to cry, I've been trying to include .gif images in my swing applications but am having big problems. I've been reading the forums and trying everything. If I try the most basic of examples just to get it working I still have problems. The code compiles correctly and the panels appear but no pics. I've tried including every different file path version, moving .gifs to different folders, what is the problem, why no pics. I'm running J2SE v 1.4.1_02, please help, I would be so greatful, don't make me cry,
    Lucy
    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 ImageIcon("globe.gif");
    g.drawImage(img.getImage(), 0, 0, null);
    super.paintComponent(g);
    panel.setOpaque(false);
    ImageIcon icon = new ImageIcon("cat.gif");
    JButton button = new JButton(icon);
    panel.add(button);
    c.add(panel);
    public static void main(String[] args)
    JFrameImage frame = new JFrameImage();
    Image onFrame = Toolkit.getDefaultToolkit().getImage("icon.gif") ;
    frame.setIconImage(onFrame);
    frame.setSize(200,200);
    frame.setLocation(300,200);
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.setVisible(true);

    sorry but I think it should be something else.
    To locate a File you need it's full name, including the extension. Maybe since the extension wasn't showing it's actual filename was cat.gif.gif? I don't know. Anyway, this is a method I use for all my image retrieving, just put it in some Util-class:  private static HashMap hmImages = new HashMap();
      public static ImageIcon getImage (String name)
        if (hmImages.containsKey(name))
          return (ImageIcon) hmImages.get(name);
        String url = System.getProperty("user.dir") + "/resources/" + name;//or any other directory were all your image-files are under (even in subs, in that case add the subdirectory to the arg name
        try
          ImageIcon ic = new ImageIcon (url);
          if (ic != null)
            hmImages.put(name, ic);
            return ic;
        catch (Exception e)
        System.out.println("Warning: image '" + url + "' not found");
        return null;
      }greetz,
    Stijn

Maybe you are looking for