Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  how do i draw a normal image at 50% alpha  (Read 1824 times)
0 Members and 1 Guest are viewing this topic.
Offline JuddMan

Senior Devvie


Medals: 1


Your Ad Here


« Posted 2003-09-10 07:45:33 »

i had a look thru the API's and searched on this forum, coultn't find an answer, so i'll ask.

how do i draw a square image eg:
1  
getClass().getResource("images/SPiece.gif")

which has no alpha or transparency, at something like 50% transparency (to give it a ghost effect).

it's not necessary to get high performance from it.

what i'm using now is the usual g.drawImage(img, x, y, null).

using PNG works, but it means having to have 2 image files with the game, and then the alpha level can't be altered for things like fading...
Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2003-09-10 08:02:13 »

You need to cast your Graphics instance into a java.awt.Graphics2D. Then you can set the composite to a java.awt.AlphaComposite:

1  
2  
3  
4  
Graphics2D g = (Graphics) g1d;

g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC,0.5f));
g.drawImage(image,0,0,null);


Just typed this code, so it may not be dead on. Also you might want to play with SRC to get the effect you want.

Kev

Offline JuddMan

Senior Devvie


Medals: 1


Your Ad Here


« Reply #2 - Posted 2003-09-10 09:56:34 »

didnt seem to be doing anything...

getInstance(int rule, float alpha)
         Creates an AlphaComposite object with the specified rule and the constant alpha to multiply with the alpha of the source.

does that mean that if my image has zero alpha it will always be zero?

anyway i'll have another look and fiddle with it.

edit: it even makes my 25% transparent png's that i made up as a workaround totally opaque.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JuddMan

Senior Devvie


Medals: 1


Your Ad Here


« Reply #3 - Posted 2003-09-10 10:11:26 »

thanks. read the diferent rules and tried a couple of them out... tried XOR and the image disappeared. SRC_OVER did what i wanted. it says this:

public static final int SRC_OVERPorter-Duff Source Over Destination rule. The source is composited over the destination.

thanks for your help. the api is really hard to find stuff in when you don't know exactly what it is you should be looking for


render(Graphics g)
{
   Graphics2D g2d = (Graphics2D)g
   g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
   g2d.drawImage((piece[j]).image, x+(i*24), y+(j*24), owner.mainFrame);
}
Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #4 - Posted 2003-09-10 10:33:58 »

Great, thanks for providing the correct version aswell  Smiley

Kev

Offline JuddMan

Senior Devvie


Medals: 1


Your Ad Here


« Reply #5 - Posted 2003-09-10 13:06:04 »

thanks. here's it in action in my game, source included.

http://www.adam.com.au/kellyjones/java/

just download the JAR file and run it however you usually do. if you want the music grab the midi zip as well, or use your own midi files in a midi subdirectory in the same place the JAR is run from.

the code is used in the Tetroid object's renderGhost() method. let me know what you think
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (18 views)
2017-11-21 16:21:00

xxMrPHDxx (11 views)
2017-11-21 16:14:31

xxMrPHDxx (14 views)
2017-11-21 16:10:57

Ecumene (114 views)
2017-09-30 02:57:34

theagentd (148 views)
2017-09-26 18:23:31

cybrmynd (248 views)
2017-08-02 12:28:51

cybrmynd (247 views)
2017-08-02 12:19:43

cybrmynd (244 views)
2017-08-02 12:18:09

Sralse (258 views)
2017-07-25 17:13:48

Archive (878 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!