4 Replies Latest reply: Aug 5, 2012 5:41 PM by gimbal2 RSS

    Adding an image to a specific location in jframe?

    893337
      ok so i have an image:
      ImageBackgroundPanel temp =null;
      temp = new ImageBackgroundPanel(friend);
      
      frame.add(temp);
      frame.setVisible(true);
      where my variable "friend" is just a bufferedimage (temp is a wrapper class mostly). So, what i want is simple: i want that image to be put in a different place!
      As the code is now, the image is put at the top left corner of the frame. how do i move it? how do i do this dynamically? (i wanna animate the image by redrawing it several times a second in a different position? I know how to animate it, how do you move it?!?!?!?!
      Someone please help me :(...
        • 1. Re: Adding an image to a specific location in jframe?
          gimbal2
          http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
          • 2. Re: Adding an image to a specific location in jframe?
            893337
            please dont consider me dumb...
            i need my image to appear at -50 when the timer reaches 0.25 secs...then i need it to advance 10 pixels to the right for each 10 miliseconds. i know what layouts are and no they will not help me! i need to paint an image at coordinates x,y not using layouts.
            • 3. Re: Adding an image to a specific location in jframe?
              893337
              quick reply: the idea was that inside the ImageBackgroundPanel class i had to just override the paintComponent method and TADAAAAM. An example:
              ImageBackgroundPanel temp = new ImageBackgroundPanel(friend);
              frame.add(temp);
              this will add the picture inside the frame at coordinates X and Y. Now for the ImageBackgroundPanel class:
              public class ImageBackgroundPanel extends JPanel
              {
               BufferedImage image;
               int x=0;
               int y=0;
              
               public void setImage(BufferedImage image) 
                  {
                   this.image = image;
                  }
              
               ImageBackgroundPanel(BufferedImage image) 
                  {
                   this.image = image;
                  }
              
               protected void paintComponent(Graphics g) 
                  {
                   super.paintComponent(g);
                   g.drawImage(image, X, Y, this);
                  }
              }
              
               public void setXY(int x, int y)
               {
                this.x=x;
                this.y=y;
               }
              thanks for trying though :D
              • 4. Re: Adding an image to a specific location in jframe?
                gimbal2
                890334 wrote:
                please dont consider me dumb...
                i need my image to appear at -50 when the timer reaches 0.25 secs...then i need it to advance 10 pixels to the right for each 10 miliseconds. i know what layouts are and no they will not help me! i need to paint an image at coordinates x,y not using layouts.
                Very late but I can't resist:

                http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html