Results 1 to 4 of 4
- 10-14-2014, 08:06 PM #1
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
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.
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();
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
- 10-14-2014, 08:44 PM #2
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)
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 10-14-2014, 08:44 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-14-2014, 09:14 PM #4
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
Similar Threads
-
Setting JFrames Background To An Image?
By Alerhau in forum AWT / SwingReplies: 2Last Post: 08-13-2011, 11:38 PM -
Why the priority setting doesn't work?
By ggyyree in forum Threads and SynchronizationReplies: 3Last Post: 02-01-2011, 09:06 AM -
Setting up proxy for Maven mojo doesn't work
By zdenek.zikan in forum Advanced JavaReplies: 1Last Post: 01-12-2010, 01:14 PM -
How to drawing free hand on background image ?
By it.manveer.singh in forum Java AppletsReplies: 0Last Post: 04-18-2009, 03:48 PM -
GUI... setting my background to an image, im using a JFrame
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-24-2008, 06:29 AM
Bookmarks