3 Replies - 10401 Views - Last Post: 22 February 2013 - 07:02 AM

#1 Incanto  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 09-January 12

Layering of 2D images, JLabels and more on JFrame

Posted 29 November 2012 - 08:40 PM

I hope my title is not too confusing.
Anyways, I have a problem. I am relatively new to Java and I am still getting used to everything.
I thought I could just quickly program something with a few graphics and such but then I already encountered my first problems.
I found this more or less tutorial on the import of bufferedimages and thought I can use this as it would be exactly what I need. I encountered the first problem when I started trying to use it.

 //the original idea was to just make a constructor load the image and draw it by adding it to my JFrame
JFrame f = new JFrame("Load Image");  
             
    f.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e) 
      {
        System.exit(0);
      }
    });
    
    
   
   
    f.add(new gphx("path of image", pos_x, pos_y);
f.add(new gphx("path of other image", pos_x_2, pos_y_2);
    
    f.pack();
    f.setVisible(true); 


this was a problem though as it would not overlay images. So, I thought I should just make my image class to where it contains all images that should be drawn like this:
JFrame f = new JFrame("Load Image");
    JLabel label = new JLabel("This does not work");
    
             
    f.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e) 
      {
        System.exit(0);
      }
    });
    
    
    gphx g = new gphx(800,800);
    g.loadImage("strawberry.jpg",50,50);
    g.loadImage("pGlo.jpg",190,100);
   
    f.add(g);
    f.add(label);
    f.pack();
    f.setVisible(true);
    

it works perfectly with the images, but when i want to add another layer (the text) then it only draws either or, (only the one that i add last to my JFrame...so long story short, my question is how do i make it to where i can just have everything in there with pictures/text overlapping?

Is This A Good Question/Topic? 0
  • +

Replies To: Layering of 2D images, JLabels and more on JFrame

#2 tony12095  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 109
  • Joined: 23-October 12

Re: Layering of 2D images, JLabels and more on JFrame

Posted 19 February 2013 - 02:08 PM

Okay, first, you are doing this not all wrong, but it is not completely the way to do things. When you have a JFrame, instead of having a WindowListener, you should use <JFrame var>.setDefaultCloseOperation(JFrame.CLOSE_ON_EXIT); this will say when the "x" button is pressed, close the application. It is much shorter and easier to use instead of WindowListener. Second off, when you do graphics in Java, you usually have a draw method with Graphics as the parameter.

An Example:
public void draw(Graphics g) {
    
     // image information and loading such as
     g.drawImage(<image>, <x>, <y>);
}



this will make the images draw onto your JFrame or JPanel, no matter how many images you draw onto it.

* PS: to make them overlap with the way shown above, just set the x and y variables the same thing. *

This post has been edited by tony12095: 19 February 2013 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: Layering of 2D images, JLabels and more on JFrame

Posted 19 February 2013 - 10:51 PM

Except they won't overlap so much as they'll hide each other; if he wants true overlapping he'll need to use PNG's with some transparency.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Layering of 2D images, JLabels and more on JFrame

Posted 22 February 2013 - 07:02 AM

I don't know what this Gphx class is, maybe you should post it? Seems like it's a Component?

Anyway..
You could be using a JLayeredPane
As it says in its name, you will be able to place components in different layers (depth), and you can in that way make components overlap
You can see how to use a JLayeredPane here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1