Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31162 > unrolled thread

paintComponent(Graphics graphics)

Started byDoug Mika <dougmmika@gmail.com>
First post2015-02-20 11:13 -0800
Last post2015-02-20 20:14 -0500
Articles 7 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  paintComponent(Graphics graphics) Doug Mika <dougmmika@gmail.com> - 2015-02-20 11:13 -0800
    Re: paintComponent(Graphics graphics) Jeff Higgins <jeff@invalid.invalid> - 2015-02-20 18:58 -0500
      Re: paintComponent(Graphics graphics) Brixomatic <brixomatic@yahoo.com> - 2015-02-21 18:55 +0100
    Re: paintComponent(Graphics graphics) Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2015-02-20 16:15 -0800
      Re: paintComponent(Graphics graphics) Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2015-02-20 16:42 -0800
        Re: paintComponent(Graphics graphics) Jeff Higgins <jeff@invalid.invalid> - 2015-02-20 19:55 -0500
          Re: paintComponent(Graphics graphics) Jeff Higgins <jeff@invalid.invalid> - 2015-02-20 20:14 -0500

#31162 — paintComponent(Graphics graphics)

FromDoug Mika <dougmmika@gmail.com>
Date2015-02-20 11:13 -0800
SubjectpaintComponent(Graphics graphics)
Message-ID<3dba8d3b-34bb-4bd3-8768-78b138daca92@googlegroups.com>
Hi 

I hope I can explain this clearly without getting into too much detail as the code in question is a couple of thousand of lines.  So to begin, I have a JPanel to which I add JLabels and JComponents using JPanel's add() method.  It works fine, but now I would like to print an image ON TOP of the added JLabels and JComponents.  For this I manipulate my JPanel's paintComponent(Graphics graphics) method.  In it I have something of the following sort:

paintComponent(Graphics graphics){
  Graphics2D g = (Graphics2D)graphics.create();
  g.drawImage(image, 200, 200, null);
  g.dispose
}


This works in that in my JPanel I now have my "image" appear at position (200,200), BUT it appears underneath all my JLabels and JComponents.
HOW can I make it appear ON TOP of the JLabels and JComponents in my JPanel?

Many Thanks
Doug

[toc] | [next] | [standalone]


#31164

FromJeff Higgins <jeff@invalid.invalid>
Date2015-02-20 18:58 -0500
Message-ID<mc8i00$qk7$1@dont-email.me>
In reply to#31162
On 02/20/2015 02:13 PM, Doug Mika wrote:
> Hi
>
> I hope I can explain this clearly without getting into too much detail
> as the code in question is a couple of thousand of lines.

It would be very easy to create a small example program that
demonstrates your situation.

> So to begin, I have a JPanel to which I add JLabels and JComponents
> using JPanel's add() method.  It works fine, but now I would like to print
> an image ON TOP of the added JLabels and JComponents.  For this I
> manipulate my JPanel's paintComponent(Graphics graphics) method.

In it I have something of the following sort:
>
> paintComponent(Graphics graphics){
>    Graphics2D g = (Graphics2D)graphics.create();
>    g.drawImage(image, 200, 200, null);
>    g.dispose
> }
>

Why are you creating and disposing of a Graphics object?

>
> This works in that in my JPanel I now have my "image" appear at position
> (200,200), BUT it appears underneath all my JLabels and JComponents.
> HOW can I make it appear ON TOP of the JLabels and JComponents in my JPanel?
>

Have you looked at any of the descendants of JComponent that
allow to manipulate the component Z order?

[toc] | [prev] | [next] | [standalone]


#31169

FromBrixomatic <brixomatic@yahoo.com>
Date2015-02-21 18:55 +0100
Message-ID<MPG.2f52e503ae9028639896a1@news.aioe.org>
In reply to#31164
In article <mc8i00$qk7$1@dont-email.me>, Jeff Higgins 
(jeff@invalid.invalid) says...

> >
> > paintComponent(Graphics graphics){
> >    Graphics2D g = (Graphics2D)graphics.create();
> >    g.drawImage(image, 200, 200, null);
> >    g.dispose
> > }
> >
> 
> Why are you creating and disposing of a Graphics object?

It's generally the right way to do it.

Using the provided graphics object and altering its state should, but 
could potentially cause problems with objects relying on the fact that 
it's unchanged by child components.

Kind regards,
Wanja

-- 
..Alesi's problem was that the back of the car was jumping up and down 
dangerously - and I can assure you from having been teammate to 
Jean Alesi and knowing what kind of cars that he can pull up with, 
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]

[toc] | [prev] | [next] | [standalone]


#31165

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2015-02-20 16:15 -0800
Message-ID<mc8ikv$sig$1@dont-email.me>
In reply to#31162
On 2/20/2015 11:13 AM, Doug Mika wrote:
> Hi
>
> I hope I can explain this clearly without getting into too much detail as the code in question is a couple of thousand of lines.  So to begin, I have a JPanel to which I add JLabels and JComponents using JPanel's add() method.  It works fine, but now I would like to print an image ON TOP of the added JLabels and JComponents.  For this I manipulate my JPanel's paintComponent(Graphics graphics) method.  In it I have something of the following sort:
>
> paintComponent(Graphics graphics){
>    Graphics2D g = (Graphics2D)graphics.create();
>    g.drawImage(image, 200, 200, null);
>    g.dispose
> }
>
>
> This works in that in my JPanel I now have my "image" appear at position (200,200), BUT it appears underneath all my JLabels and JComponents.
> HOW can I make it appear ON TOP of the JLabels and JComponents in my JPanel?
>
> Many Thanks
> Doug
>

I'm assuming you have a translucent image you want to draw over the 
whole JPanel.  Draw on the JFrame's glasspane.  I think you will have to 
make the glasspane visible.


-- 
Knute Johnson

[toc] | [prev] | [next] | [standalone]


#31166

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2015-02-20 16:42 -0800
Message-ID<mc8k87$1cl$1@dont-email.me>
In reply to#31165
On 2/20/2015 4:15 PM, Knute Johnson wrote:
> On 2/20/2015 11:13 AM, Doug Mika wrote:
>> Hi
>>
>> I hope I can explain this clearly without getting into too much detail
>> as the code in question is a couple of thousand of lines.  So to
>> begin, I have a JPanel to which I add JLabels and JComponents using
>> JPanel's add() method.  It works fine, but now I would like to print
>> an image ON TOP of the added JLabels and JComponents.  For this I
>> manipulate my JPanel's paintComponent(Graphics graphics) method.  In
>> it I have something of the following sort:
>>
>> paintComponent(Graphics graphics){
>>    Graphics2D g = (Graphics2D)graphics.create();
>>    g.drawImage(image, 200, 200, null);
>>    g.dispose
>> }
>>
>>
>> This works in that in my JPanel I now have my "image" appear at
>> position (200,200), BUT it appears underneath all my JLabels and
>> JComponents.
>> HOW can I make it appear ON TOP of the JLabels and JComponents in my
>> JPanel?
>>
>> Many Thanks
>> Doug
>>
>
> I'm assuming you have a translucent image you want to draw over the
> whole JPanel.  Draw on the JFrame's glasspane.  I think you will have to
> make the glasspane visible.
>
>

http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html

-- 
Knute Johnson

[toc] | [prev] | [next] | [standalone]


#31167

FromJeff Higgins <jeff@invalid.invalid>
Date2015-02-20 19:55 -0500
Message-ID<mc8lad$4vq$1@dont-email.me>
In reply to#31166
On 02/20/2015 07:42 PM, Knute Johnson wrote:

>> I'm assuming you have a translucent image you want to draw over the
>> whole JPanel.  Draw on the JFrame's glasspane.  I think you will have to
>> make the glasspane visible.
>>
>>
>
> http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html
>

Thank you. Your response prompted me to have another look.
<http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html#drawing>

So that from the example:

class WallpaperLayerUI extends LayerUI<JComponent> {
   @Override
   public void paint(Graphics g, JComponent c) {
     super.paint(g, c);

     Graphics2D g2 = (Graphics2D) g.create();

     int w = c.getWidth();
     int h = c.getHeight();
     g2.setComposite(AlphaComposite.getInstance(
             AlphaComposite.SRC_OVER, .5f));
     g2.setPaint(new GradientPaint(0, 0, Color.yellow, 0, h, Color.red));
     g2.fillRect(0, 0, w, h);

     g2.dispose();
   }
}

Which answers my question to the OP regards
create and dispose of graphics object.

[toc] | [prev] | [next] | [standalone]


#31168

FromJeff Higgins <jeff@invalid.invalid>
Date2015-02-20 20:14 -0500
Message-ID<mc8md5$7rm$1@dont-email.me>
In reply to#31167
On 02/20/2015 07:55 PM, Jeff Higgins wrote:
>
> Which answers my question to the OP regards
> create and dispose of graphics object.
>
Although scaling an image to fit a resize-able
container will present its own set of complexities.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web