Opengl blending and lighting in Libgdx - java

Ok, so I am trying to add ligting to my game but am having some problems in open gl using blending.
What I am trying to do, is cover the whole screen with a black square. Then, cut holes in it by applying another shape over it with a lower alpha level.
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_SRC_ALPHA);
Gdx.gl20.glBlendEquation(GL.GL_FUNC_SUBTRACT);
woods.shapes.begin(ShapeType.Filled);
woods.shapes.setColor(255, 255, 255, 200);
woods.shapes.rect(0, 0, 600, 600);
woods.shapes.setColor(255, 255, 255, 10);
woods.shapes.rect(300, 300, 50, 50);
woods.shapes.end();
Gdx.gl20.glBlendEquation(GL.GL_FUNC_ADD);
Gdx.gl.glDisable(GL20.GL_BLEND);
This is what I am using for it right now, but I can't seem to get it right, the results are never what I am expecting. I suspect it has something to do with my blendfunc or blendequation but I have tried tons of different combinations to no avail. Any help at all would be fantastic.

Related

Difference between the fill methods vs. draw methods in graphics

g.drawRect(50, 50, 400, 75);
g.fillRect(50, 50, 200, 200);
Someone told me the first line will draw a rectangle while the second one will draw a square. I understand the parameters, but however wouldn't g.drawRect(50,50,200,200); also draw a square? I thought fill would actually fill the square with a color.
The difference between draw and fill is whether you just get the outline or a "solid" (i.e. filled) shape.
http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics2D.html#draw-java.awt.Shape-
http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics2D.html#fill-java.awt.Shape-

How to make a gradient ring in java for android?

I am trying to draw out a ring with a nice gradient effect in java for an android application. I am trying to use the RadialGradient class found in android.graphics.RadialGradient, but it doesn't really do a gradient effect. I feel like it is just sort of mixing the 2 colors I subbed in for the color parameter, but I would like it to start from the first color I gave it and slowly transition itself to the second color in whatever direction it wants. The below is the code snippet for what I have tried.
paint.setColor(color);
paint.setAntiAlias(true);
int[] colors = {Color.argb(255, 255, 255, 0), Color.argb(255, 219, 219, 7)};
Shader test = new RadialGradient(X, Y, radius, colors, null, Shader.TileMode.CLAMP);
paint.setShader(test);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(thickness);
Don't worry about the variables that I used that aren't shown how it was initialized. That part is not important.

Java - BufferedImage - clear the screen after each iteration

I'm drawing onto a transparent background, but in each loop, I'd like the screen to be cleared, instead of drawing on the screen from before.
Is there a way to wipe the drawing screen in Java?
You may be looking for Graphics#clearRect(). You'll probably need to set the surface background to a transparent color, though:
graphics.setBackground(new Color(255, 255, 255, 0));

Drawing Glassy Buttons

I'm going to draw some sort of glassy buttons in java me (targeting devices with MIDP 2.0).
An Example:
Actually I need to impelement Gradient and Bevel-Emboss effects in Java ME, do you have any opinion or "guideline" on how to implement this?
EDIT: Now I know how to draw gradient background, but That is not sufficient.
Is it possible to draw such glassy button in Java ME?
I've worked with C# and I can draw these kinds of glassy buttons there, but I'm struggling on how to simulate something like these buttons in Java ME or at least something near to them, Note that I'm looking for a good guidance and help to move forward.
Do I need to provide more information? If so, please let me know.
Thanks In advance.
You can do it by using an alpha gradient paint. Heres an example:
Graphics2D g = (Graphics2D)screen.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(new Font("Serif", Font.BOLD, 30));
Color c1 = new Color(0,0,0,0);
Color c2 = new Color(0,0,0,100);
GradientPaint gradient = new GradientPaint(10,8,c1,10,40,c2,true);
g.setColor(Color.GREEN);
g.fillRect(0, 0, screen.width, screen.height);
g.setColor(Color.BLACK);
g.setPaint(gradient);
g.fillRoundRect(100, 100, 200, 50, 25, 25);
g.setPaint(Color.BLACK);
g.drawRoundRect(100, 100, 200, 50, 25, 25);
g.drawString("Hello World!", 118, 135);
it will look like this:
You can use LWUIT framework for developing java-me (MIDP 2.0) applications. Its good GUI framework for java-me application. Here you can create the theme manually using ResourceEdit. For more info see this discussion and LWUIT blog also.
yoy can not do so in me because it dont have such reach effect on UI
get image that have effect like that gredient and make it transparent using
btn.getStyle().setBgTransparency(100) // from 0-255
note: you must use images that is already semi transparent , if not than setBgTransparency would not work properly
I can not recommend you to use canvas
i recommend you to use use LWUIT
it have many more effects ads you required ,like
btnBuzz.getStyle().setBorder(Border.createBevelLowered());
it have different layout as well

Curved sides of triangle path?

I am drawing a triangle on a Canvas, something like:
canvas.moveTo(0, 30);
canvas.lineTo(40, 0);
canvas.lineTo(40, 40);
canvas.lineTo(40, 40);
canvas.lineTo(0, 30);
And get proper triangle on my Canvas. But I need to curve the sides a little and fill that path with specific color. What is the easiest way to do this? Drawing arcs? But how to fill the object?
Thanks!
EDIT: I noticed you were using android's canvas, not HTML Canvas, sorry. The concept is exactly the same except you'll call quadTo() instead of quadraticCurveTo(), so my example should still get you going.
Also on android you use canvas.drawPath(path, paint) and pass in a paint that has its Paint.style set to FILL_AND_STROKE.
You will want to construct the path, fill() it, then stroke() it in order to get both a filled path with the stroke outline.
To get that particular shape, the easiest way is to draw two quadratic curves. A quadratic curve first needs a control point x,y then the end point x,y. The control point for both curves should be around the middle of your desired triangle. Here is an example:
ctx.fillStyle = "lightgray";
ctx.moveTo(0, 100);
ctx.quadraticCurveTo(50, 50, 50, 0);
ctx.quadraticCurveTo(50, 50, 100, 100);
ctx.lineTo(0, 100);
ctx.fill();
ctx.stroke();
Here is that example live for you.

Categories