Results 1 to 4 of 4

Thread: Drawing alpha

  1. #1
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default 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:
    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);
    			}
    		}
    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:
    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;
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Drawing alpha

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    sakratt is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Drawing alpha

    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);
                }        
    }
    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.
    Last edited by sakratt; 03-28-2012 at 10:40 PM.

  4. #4
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: Drawing alpha

    Quote Originally Posted by sakratt View Post
    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);
                }        
    }
    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.
    Thanks, your code helped me. Now i fixed my problem. :))

Similar Threads

  1. Replies: 1
    Last Post: 02-18-2012, 08:47 PM
  2. how do I set the alpha value on Images?
    By gib65 in forum AWT / Swing
    Replies: 4
    Last Post: 10-18-2010, 04:20 PM
  3. Replies: 0
    Last Post: 06-28-2008, 09:29 PM
  4. Collaba 7.1 Alpha 1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 05-01-2008, 04:50 PM
  5. SableCC 4-alpha.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-23-2007, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •