Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

transparent gradient   simple

Thread view: 
Qu?bec - 15 Mar 2004 02:14 GMT
   Hi gurus,

                   I am trying to get a blue gradient image  in this small
application.
                   My problem is that there is not enough pixels for a
'nice' gradient.

Jean

=============================================
/*<applet codebase="."  code="Gradient " width=400   height=200 >
</applet>
*/
import java.awt.*;
import java.awt.image.*;
public class Gradient extends java.applet.Applet {
   Image i;
   int width = 400;
   int height = 200;
   public void init () {
       int[] pixels = new int [width*height];
       int c;
       for (int index=0,y=0;y<height/4;y++) {
           c = ((0xff) & (byte)(y* 255/(height/4 - 1)));
           for (int x=0;x<width;x++) {
               pixels[index++] = ((c<<24) | (c<<16) | (c<<8) | 255);
           }
       }
       i = createImage (new MemoryImageSource (width, (height/4), pixels,
0, width));
   }
   public void paint (Graphics g) {
        g.drawImage (i, 0, 3*height/4, this);
   }
}
Andrew Hobbs - 15 Mar 2004 04:53 GMT
>     Hi gurus,
>
>                     I am trying to get a blue gradient image  in this small
> application.
>                     My problem is that there is not enough pixels for a
> 'nice' gradient.

What exactly are you trying to do and what is a 'nice' gradient.  I thought
it gives a reasonably graded color.  From white at the bottom of the image
fading in to transparent blue at the top as you increase the transparency.

Andrew

--
********************************************************
Andrew Hobbs   PhD

MetaSense Pty Ltd     -    www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au

*********************************************************
Qu?bec - 15 Mar 2004 13:00 GMT
A raisonably blue, from the bottom of the image to one quarter of the height
(of the image) increasingly transparent to any color used has a background.
The 'nice' thing is only a feeling....:-)
Andrew Hobbs - 15 Mar 2004 15:56 GMT
> A raisonably blue, from the bottom of the image to one quarter of the height
> (of the image) increasingly transparent to any color used has a background.
> The 'nice' thing is only a feeling....:-)

I not sure exactly what you want but it sounds like you should change the
line

 pixels[index++] = ((c<<24) | (c<<16) | (c<<8) | 255);

to

 pixels[index++] = ((c<<24) |  255);

That will give you a deep blue all over becoming transparent at the top
edge.

If this isn't what you meant then you will need to explain a little more
clearly.

Cheers

Andrew

--
********************************************************
Andrew Hobbs   PhD

MetaSense Pty Ltd     -    www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au

*********************************************************
Qu?bec - 15 Mar 2004 19:20 GMT
Thanks Andrew, your line is better but see below,

We have got the left gradient, I want the right one.

http://www.jeanpierredaviau.com/delme.htm
Andrew Hobbs - 16 Mar 2004 01:39 GMT
> Thanks Andrew, your line is better but see below,
>
> We have got the left gradient, I want the right one.
>
> http://www.jeanpierredaviau.com/delme.htm

I can't tell why you are having a problem since the code gives me the
gradient on the right in your example.

However did you post the actual code or a cut down version.  It looks to me
like the sort of pattern you get if the width parameter of the byte array
generation code is different to the one used to construct the image.  You
then get an interference pattern with bytes being put in inappropriate
places in the array.

Cheers

Andrew

--
********************************************************
Andrew Hobbs   PhD

MetaSense Pty Ltd     -    www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au

*********************************************************
Qu?bec - 16 Mar 2004 11:32 GMT
> I can't tell why you are having a problem since the code gives me the
> gradient on the right in your example.
[quoted text clipped - 4 lines]
> then get an interference pattern with bytes being put in inappropriate
> places in the array.
The width is always the same...  I think.

I tried walking trough hell since that absolutely new to me by adding a
Direct Color Model and have a 'nicer' gradient.
BUT I cant get the blue only color.  If I vary the numbers in the
colormodel I get green, blue and yellow, cyan....
This line does not help too    //c = ((0xff) & (byte)(Math.abs (Math.sin((y
+ height) * radianConversion)) * 255));

Jean

=============================================================
/*<applet codebase="."  code="BgGradient" width=400   height=200 >
</applet>
*/

import java.awt.*;
import java.awt.image.*;

public class BgGradient extends java.applet.Applet {
 Image i;
 int width = 400;
 int height = 50;
 public void init () {
   int[] pixels = new int [width * height];
   int c;
double radianConversion = (0.45*Math.PI / 180.0);
   for (int index = 0, y = 0;y < height;y++) {
     c = ((0xff) & (byte)(y* 255/(height - 1)));
     for (int x = 0;x < width;x++) {
       pixels[index++] =   ((0<<24) | 255);//((c << 24) | (c << 16) | (c <<
8) | 255);

     }
   }
------------------------------------------------------------>
>------
   i = createImage (new MemoryImageSource (width, height, new
DirectColorModel((255 << 16) , (255 << 8), 255,(255 << 24) ) ,pixels, 0,
width));
   setFont (new Font ("TimesRoman", Font.BOLD | Font.ITALIC, 32));
 }
 public void paint (Graphics g) {
   //setBackground(Color.black);
   g.drawImage (i, 0, 3*height, this);
   g.drawString ("JeanPierreDaviau.com", 30, 80);
 }
}
Andrew Hobbs - 16 Mar 2004 12:35 GMT
I am not sure what you are doing now.  However what I am saying is that the
code you originally posted worked well for me, just as in your example.
Have you tried running that exact code.  I don't mean running code that you
think is the same, but actually cut and pasted it into a file and run it.

How about cutting and pasting the actual code that is giving you the problem
into your reply.  If it really is too big how about cutting it down and
making sure it isn't working and then cutting and pasting.

Cheers

Andrew

--
********************************************************
Andrew Hobbs   PhD

MetaSense Pty Ltd     -    www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au

*********************************************************
Andrew Thompson - 16 Mar 2004 13:02 GMT
....
> I am not sure what you are doing now.  However what I am saying is that the
> code you originally posted worked well for me, just as in your example.

Or, to put that another way..
<http://www.physci.org/test/screenshot/Gradient1.png>

On the basis that, a picture paints a thousand words..   ;-)

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

Andrew Hobbs - 16 Mar 2004 15:05 GMT
...
> Or, to put that another way..
> <http://www.physci.org/test/screenshot/Gradient1.png>

Quite

Andrew

--
********************************************************
Andrew Hobbs   PhD

MetaSense Pty Ltd     -    www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au

*********************************************************
Jon A. Cruz - 16 Mar 2004 18:17 GMT
Québec wrote:
> I tried walking trough hell since that absolutely new to me by adding a
> Direct Color Model and have a 'nicer' gradient.
>  BUT I cant get the blue only color.  If I vary the numbers in the
> colormodel I get green, blue and yellow, cyan....
> This line does not help too    //c = ((0xff) & (byte)(Math.abs (Math.sin((y
> + height) * radianConversion)) * 255));

Try breaking it down to more manageable and understandable code.
(Despite any rumors going around out there, shoving stuff to a single
line does not make a program any faster)

int r = 0x0ff & something;
int g = 0x0ff & or;
int b = 0x0ff & another;

c = (r << 16) | (g << 8) | (b << 0);

Once you have it broken down to something like that, it should be easier
to isolate b and what values you're getting for it.
Alex Hunsley - 16 Mar 2004 11:10 GMT
Québec wrote:

>     Hi gurus,
>
>                     I am trying to get a blue gradient image  in this small
> application.
>                     My problem is that there is not enough pixels for a
> 'nice' gradient.
[snip]

Just a question... why do you want to create an image from a buffer of
ints? You're asking for problems that way. Why not create a buffered
image in memory, and then use regular Graphics2D drawing operations to
draw to it?

alex
Qu?bec - 16 Mar 2004 13:43 GMT
> I am not sure what you are doing now.  However what I am saying is that
the
> code you originally posted worked well for me, just as in your example.
I just copy paste it.(14 mars, 2004 20:14)Ha!
How come it dont work in jdk1.1.8? I have what you have seen on the left
part of the image on the web.

> Just a question... why do you want to create an image from a buffer of
> ints? You're asking for problems that way. Why not create a buffered
> image in memory, and then use regular Graphics2D drawing operations to
> draw to it?
You mean create an image in a a kind of Photoshop and upload it in a
Graphics g (1.1.8) buffer?
:-) How about learning?

Thanks for your attention.
Alex Hunsley - 17 Mar 2004 17:50 GMT
Québec wrote:
>>I am not sure what you are doing now.  However what I am saying is that
>
[quoted text clipped - 13 lines]
> You mean create an image in a a kind of Photoshop and upload it in a
> Graphics g (1.1.8) buffer?

Nah, I mean entirely in java, in such a way that is explained here:

http://makeashorterlink.com/?K42B110C7

I'm sure there's lots of other example if you google for them.

> :-) How about learning?

Well, learning is ok if that's the aim! :)

> Thanks for your attention.

All rights reserved, all wrongs reversed! or something.
alex


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.