Results 1 to 4 of 4
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Graphics2D, setting AlphaComposite, drawing Background and Image doesn't work

    Hello!

    I'm making an RPG Game and my main game class extends Canvas, and it also uses JFrame. I have a tick loop, that my game updates every 60 seconds and renders either every 60 seconds or as fast as the computer can handle, I have a boolean variable that defines whether rendering is limited or unlimited. I am having issues with rendering, especially, working with Alpha Composite. First I'll show related snippets of my code:

    Some object definitions in the main class:
    Java Code:
    private BufferedImage testImage = GraphicsHandler.getImage("Test Image.png");
    private Graphics2D gfx;
    private BufferStrategy strategy;
    private AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.DST_IN, 1.0F);
    public static Color gameStageColor = new Color(66, 255, 66, 0); //For the alpha (last integer arguement) I tried 0, 128 and 255.
    Render method, called rapidly by the game loop:
    Java Code:
    strategy = getBufferStrategy();
    if (strategy == null) { createBufferStrategy(3); return; }
    gfx = (Graphics2D) strategy.getDrawGraphics();
    
    gfx.setComposite(composite);
    gfx.setColor(gameStageColor);
    gfx.fillRect(0, 0, gameWidth, gameHeight);
    gfx.drawImage(testImage, 100, 100, 148, 148, null);
    gfx.dispose();
    strategy.show();
    What I'm trying to achieve here:
    Firstly, the background color is drawn, the stage color, which I defined 66, 255, 66, 0, this being a shade of green. Then overlapped in position X: 100 to 148 and Y: 100 to 148, I want a test image to be drawn. This image has been loaded with ImageIO.read(imageFile), and the image file exists in a Java Build Path: Class Folder, linked folder to the project. The test image is a PNG image with some semi-transperant areas, now I want these areas to mix with the green background. Instead of that, all I get is a black screen, I tried Color.RED instead of gameStageColor and I got a Black and White rapidly flashing screen.

    What's the problem with this?
    How do I get it to do what I want it to do?

    Regards
    Augustas656

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Graphics2D, setting AlphaComposite, drawing Background and Image doesn't work

    Thread moved from New to Java.

    Have you discovered the Oracle tutorials? Compositing Graphics (The Java™ Tutorials > 2D Graphics > Advanced Topics in Java2D)

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

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Graphics2D, setting AlphaComposite, drawing Background and Image doesn't work

    Although I have used the AlphaComposite class I am not sufficiently versed in it to help you with what you submitted.
    If you could submit a http:/SSCCE.org which demonstrates the problem I may be able to help. However, why are you
    using Canvas? That is AWT and you are mixing it with Swing (e.g. JFrame). Probably not a good idea. You should just
    use JPanels and do your painting in them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Graphics2D, setting AlphaComposite, drawing Background and Image doesn't work

    I'm using Canvas so I can make the Buffer Strategy triple buffering rather than the integrated double buffering in JPanels. It's just the problem with AlphaComposite

Similar Threads

  1. Setting JFrames Background To An Image?
    By Alerhau in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2011, 10:38 PM
  2. Why the priority setting doesn't work?
    By ggyyree in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-01-2011, 08:06 AM
  3. Setting up proxy for Maven mojo doesn't work
    By zdenek.zikan in forum Advanced Java
    Replies: 1
    Last Post: 01-12-2010, 12:14 PM
  4. How to drawing free hand on background image ?
    By it.manveer.singh in forum Java Applets
    Replies: 0
    Last Post: 04-18-2009, 02:48 PM
  5. Replies: 2
    Last Post: 03-24-2008, 05:29 AM

Posting Permissions

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