Results 1 to 4 of 4
Thread: Drawing alpha
- 03-28-2012, 09:55 PM #1
Drawing alpha
Hello. Im trying to draw a graphics from RGBA color, but i have a little problem. I want to make it move to right and when it reaches 100 at x coordinates, it starts decreasing his alpha. But now what i made, it moves to right until x <= 100 and when reaches 100 it stucks and after few seconds disappears. Here is my code at paint(Graphics g) method:
And bomb class (It's defined at the top of my main frame. Bomb bomb = new Bomb(0, 0, new Color(255, 0, 0, 255));):Java Code:if(bomb.isLaunched() == true){ bomb.x +=3; g.setColor(bomb.getColor()); if(bomb.getX() >= 100){ for(int i=0; i<2550; i++){ int alpha = (int)(2550 - i) / 10; bomb.setColor(new Color(255, 0, 0, alpha)); g.setColor(bomb.getColor()); g.fillOval(bomb.getX(), bomb.getY(), radius, radius); } bomb.x = 0; bomb.setLaunched(false); }else{ g.fillOval(bomb.getX(), bomb.getY(), radius, radius); } }
Java Code:import java.awt.Color; public class Bomb { int x, y; Color clr; boolean launched; public Bomb(int x, int y, Color clr){ this.x = x; this.y = y; this.clr = clr; launched = false; } public int getX(){ return x; } public int getY(){ return y; } public Color getColor(){ return clr; } public void setColor(Color clr){ this.clr = clr; } public boolean isLaunched(){ return launched; } public void setLaunched(boolean tf){ this.launched = tf; } }
- 03-28-2012, 10:15 PM #2
Re: Drawing alpha
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-28-2012, 10:37 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: Drawing alpha
I'm no expert but I'm pretty sure this is much more efficient than doing a loop that loops 2550 rounds each time the game updates. It will get the alpha of the bomb and decrease it by 1 each time the game updates. What happened in your method was that it updated the bomb's alpha inside the for loop without actually redrawing it.Java Code:paintMethod(Graphics g) { if(bomb.isLaunched()){ // you don't have to manually compare booleans bomb.x +=3; // personally I would update the game logic in another method g.setColor(bomb.getColor()); if(bomb.getX() >= 100){ int alpha = bomb.getColor().getAlpha() - 1; bomb.setColor(new Color(255, 0, 0, alpha)); g.setColor(bomb.getColor()); g.fillOval(bomb.getX(), bomb.getY(), radius, radius); } bomb.x = 0; bomb.setLaunched(false); }else{ g.fillOval(bomb.getX(), bomb.getY(), radius, radius); } }Last edited by sakratt; 03-28-2012 at 10:40 PM.
- 03-28-2012, 10:56 PM #4
Similar Threads
-
Drawing monochrome/alpha channel sprites in different colors
By kjkrum in forum Java GamingReplies: 1Last Post: 02-18-2012, 08:47 PM -
how do I set the alpha value on Images?
By gib65 in forum AWT / SwingReplies: 4Last Post: 10-18-2010, 04:20 PM -
Drawing with transformations, paths and alpha blending
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:29 PM -
Collaba 7.1 Alpha 1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 05-01-2008, 04:50 PM -
SableCC 4-alpha.1
By JavaBean in forum Java SoftwareReplies: 0Last Post: 06-23-2007, 07:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks