GradientPaint with Alpha Color: Broken in Java 7?

I recently added some gradient fill areas, just to polish up some interfaces. In cases where I was filling with translucent colors (Color objects created with alpha value), Java 6 runtime will fill the area with the proper gradient, and the area is translucent, as expected. Running the same code (compiled with Java 6) in a Java 7 runtime (on the same platform) behaves differently. The alpha-gradient areas are not transparent at all.

It appears that Java 7 will not honor the alpha byte of the Color objects when performing a GradientPaint fill. I’ve only tested this in Ubuntu, with Sun/Oracle Java runtimes.

I have not found any articles or bugs reported on this. I’m not well-versed in gradient painting, so can anyone clarify if this is a known issue (or if I’m doing it wrong)? I should code up a simple test case to demonstrate.

The quick work-around was to remove any alpha bytes from my gradient areas, so they paint consistently.

Could you show us code? My game uses alpha gradient fill for the button highlight and it works fine in Java 7.

@ra4king, FYI, your game throws an NPE on startup. Doesn’t seem to affect anything though (I didn’t actually play but title screen renders okay):


java.lang.NullPointerException
	at com.ra4king.jdoodlejump.gui.a.p(Unknown Source)
	at com.ra4king.jdoodlejump.JDoodleJump.g(Unknown Source)
	at com.ra4king.a.e.f(Unknown Source)
	at com.ra4king.a.l.run(Unknown Source)

Yep, I knew that would be the next question. This is pieced together a bit, but shows what I’m doing:


int w = getWidth();
int h = getHeight();
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color top = new Color(0,0,0,64);
Color bot = new Color(255,0,0,64);
GradientPaint gp = new GradientPaint(0,0,top,0,h,bot);
g2.setPaint(gp);
g2.fillRoundRect(0,0,w,h,8,8);

I have not yet tested this in isolation (need to make a test driver). Any insights welcome.

I have no problems playing it (except I have some pending grudge with xjump). Definitely no NPE here.

That error is caused when trying to retrieve the list of cookies (the game stores your name and score in a local cookie). Looks like your browser didn’t want to give it to me :slight_smile:
However that whole area is surrounded with a try-catch so if it fails, it just prints the stack trace and returns quietly :slight_smile:

Anyway, I tried your code on Windows 7 with Java 7 and Ubuntu with OpenJDK 7. I painted the background light green and then used your code: it worked fine. I could see the red tint on the bottom.

Either you’re doing something wrong somewhere or your flavor of Linux is messed up :smiley:

Thanks for running those tests, ra4king. In my context, the bad behavior is definitely triggered by the the alpha color. Maybe it’s specific to my graphics card/driver (ATI Radeon / FGLRX driver).