Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Change opacity in paint image?


    Before

    Recommended Posts

    Posted

    Right now I'm drawing my paint with the (deprecated) onPaint function, and I have this code right here:

    @Override
        public void onPaint(Graphics2D g)
        {
            g.setColor(Color.WHITE);
            g.drawImage(mainPaint, 0, 0, null);
            ...
    
    

     and my "mainPaint" is just an image I'm loading through. However, the image itself is opaque.. Is there anyway (through code) I can reduce the opacity of the image?

    Posted


    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f); // 50% opacity

    @Override
    public void onPaint(Graphics2D g){
    g.setComposite(ac);
    g.setColor(Color.WHITE);
    g.drawImage(mainPaint, 0, 0, null);
    }

     

    Posted
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f); // 50% opacity
    
    @Override
    public void onPaint(Graphics2D g){
        g.setComposite(ac);
        g.setColor(Color.WHITE);
        g.drawImage(mainPaint, 0, 0, null);
    }
    

    Wait does that just like.. make it less opaque? Will try.

     

    edit: Works a charm

    Posted

    The Color constructor relevant to this is (float r, float g, float B) and (float r, float g, float b, float alpha).

     

    You would use the second one I mentioned above in a range from 0.0-1.0 for each value. For example, if you want a black, 30% opacity background for your UI:

    // Assume 'g' is passed in as an instance of Graphics2D or Graphics
    Color myColor = new Color(0f, 0f, 0f, .3f); // R, G, B, Alpha - The last value, .3f, is opacity.
    g.setColor(myColor);
    g.fillRect(x, y, width, height);
    
    
    

    The solutions that the others have provided above may work but are not a 'standard' way of doing it, considering they require Graphics2D, but it honestly does not matter. Graphics and Graphics2D are the user's preference.

     

    You can set the opacity from 0% to 100% on a scale from 0.0f-1.0f with the method I have shown in this post.

    Posted

    The Color constructor relevant to this is (float r, float g, float B) and (float r, float g, float b, float alpha).

     

    You would use the second one I mentioned above in a range from 0.0-1.0 for each value. For example, if you want a black, 30% opacity background for your UI:

    // Assume 'g' is passed in as an instance of Graphics2D or Graphics
    Color myColor = new Color(0f, 0f, 0f, .3f); // R, G, B, Alpha - The last value, .3f, is opacity.
    g.setColor(myColor);
    g.fillRect(x, y, width, height);
    
    
    

    The solutions that the others have provided above may work but are not a 'standard' way of doing it, considering they require Graphics2D, but it honestly does not matter. Graphics and Graphics2D are the user's preference.

     

    You can set the opacity from 0% to 100% on a scale from 0.0f-1.0f with the method I have shown in this post.

    The difference is that your approach wont work when you draw an image

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.