1

I am using netbeans to create a GUI. In gui i have used an image by inserting a jpanel in jframe. in jpanel i inserted a label and changed the label icon as my image of width 800*800.

Now the problem is i need to draw graphic objects over this image.

I am using

g.setColor(Color.RED);
    g.drawRect(x-7, y+7, 15, 15);
    g.fillRect(x-7, y+7, 15, 15);

The problem is , my graphic object is shown only when x,y values are more than 800 ie not lying on image coordinates. But i want to display it over my image.

How can i do it ? i believe this is happening as the graphic object for x,y<=800 is beneath the images and gets supressed

typedef1
  • 247
  • 2
  • 6
  • 14
  • 1
    May be you should also draw your image in paintComponent() method where you are drawing shapes, instead of setting image as an Icon to JLabel. – Harry Joy Mar 15 '12 at 09:14
  • Could you tell us where you are invoking this code? paint? on which object? JPanel, JFrame, JLabel? – Guillaume Polet Mar 15 '12 at 09:15
  • It has to do with the z-ordering of the components in your application. Have a look here http://stackoverflow.com/q/3763136/964592 – Fredrik LS Mar 15 '12 at 09:16
  • there is a button on frame. i am invoking the code there. Yes i believe it has smthng to do with z-order. Looking for it. – typedef1 Mar 15 '12 at 09:19
  • @HarryJoy hwo can i use paintComponent here? – typedef1 Mar 15 '12 at 09:25
  • Use it immediately before the 3 lines of code that are shown. It should usually be in the `paintComponent(Graphics)` method of a `JComponent` or `JPanel`. Don't paint directly to top-level containers such as frames, applets or windows. – Andrew Thompson Mar 15 '12 at 09:41
  • Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. [`FauxImage`](http://stackoverflow.com/a/8090328/230513) may be a convenient adjunct. – trashgod Mar 15 '12 at 19:16

1 Answers1

0

Got solved.. used an Graphics object for the top layer. i was using this.graphics earlier. Replaced it by layer1.graphics object and it solved the problem,

typedef1
  • 247
  • 2
  • 6
  • 14