I am trying to draw a gradient rectangle that goes from red to blue. I have the following code:
g2d.setPaint(new GradientPaint(0, 0, Color.RED, 1000, 1000, Color.BLUE));
g2d.fillRect(0, 0, 1000, 1000);
This is working. However, the direction of the gradient is diagonal, from the top left point of the rectangle to the bottom right point (another way to look at this is that the gradient follows the line of y=-x + windowHeight)
I would like my gradient to go from top to bottom. So the entire top of the rectangle is red, and the entire bottom is blue. In other words, the color should only change with the y coordinate, given any y=point line the color should be uniform across that line.
I have included the following images also to give a general idea of what I am trying to do:
How can I accomplish this?
It all has to do with the vector of your gradient. Here: (0, 0, Color.RED, 1000, 1000, Color.BLUE) you're vector is a diagonal vector that originates at [0, 0], and then ends or points at [1000, 1000] or on a 45 degree angle.
Change that to straight down: [0, 0] going to [0, 1000] should work well. e.g,
new GradientPaint(0, 0, Color.RED, 0, 1000, Color.BLUE)
Related
I studied this question, but I still don't get it. The shortest possible code below shows a Pyramid totally grey, whereas I try to give the 6 triangles making up the pyramid different colors. So ... why don't these colors show up?
Note that I borrowed the getTexCoords().addAll(..) statement from that question, but clearly I still am doing something wrong. Is it the uv mapping? What is that anyway? I have seen a topological explanation (sphere <-> map), but what has that got to do with textures/colors...?
Appreciate your help - Michael
public class ColoredPyramid extends Application {
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 200, 200, true);
primaryStage.setTitle("Colored Pyramid");
primaryStage.setScene(scene);
primaryStage.show();
TriangleMesh colouredPyramid = new TriangleMesh();
float height = 100;
float hypotenuse = 150;
colouredPyramid.getPoints().addAll(0, 0, 0); //0-index:: top
colouredPyramid.getPoints().addAll(0, height, -hypotenuse / 2); //1-index:: x=0, z=-hyp/2 ==> Closest to user
colouredPyramid.getPoints().addAll(-hypotenuse / 2, height, 0); //2-index:: x=hyp/2, z=0 ==> Leftest
colouredPyramid.getPoints().addAll(hypotenuse / 2, height, 0); //3-index:: x=hyp/2, z=0 ==> rightest
colouredPyramid.getPoints().addAll(0, height, hypotenuse / 2); ////4-index:: x=0, z=hyp/2 ==> Furthest from user
//Next statement copied from stackoverflow.com/questions/26831871/coloring-individual-triangles-in-a-triangle-mesh-on-javafx
colouredPyramid.getTexCoords().addAll(
0.1f, 0.5f, // 0 red
0.3f, 0.5f, // 1 green
0.5f, 0.5f, // 2 blue
0.7f, 0.5f, // 3 yellow
0.9f, 0.5f // 4 orange
);
colouredPyramid.getFaces().addAll(0, 0, 2, 0, 1, 0); //Left front face ---> RED
colouredPyramid.getFaces().addAll(0, 1, 1, 1, 3, 1); //Right front face ---> GREEN
colouredPyramid.getFaces().addAll(0, 2, 3, 2, 4, 2); //Right back face ---> BLUE
colouredPyramid.getFaces().addAll(0, 3, 4, 3, 2, 3); //Left back face ---> RED
colouredPyramid.getFaces().addAll(4, 4, 1, 4, 2, 4); //Base: left triangle face ---> YELLOW
colouredPyramid.getFaces().addAll(4, 0, 3, 0, 1, 0); //Base: right triangle face ---> ORANGE
MeshView meshView = new MeshView(colouredPyramid);
Group group = new Group(meshView);
group.setTranslateX(100);
group.setTranslateY(80);
root.getChildren().add(group);
}
public static void main(String[] args) {
launch(args);
}
}
To understand how JavaFX 3D defines the color of any given 3D shape, have a look at the PhongMaterial javadoc (bold is mine):
The PhongMaterial class provides definitions of properties that represent a Phong shaded material. It describes the interaction of light with the surface of the Mesh it is applied to. The PhongMaterial reflects light in terms of a diffuse and specular component together with an ambient and a self illumination term. The color of a point on a geometric surface is mathematical function of these four components.
That means that you need to supply a material in the first place, and then you need to specify any of those components, for instance the diffuse component.
If you copy the image from the cited question:
and create a material instance with it:
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(new Image(getClass().getResourceAsStream("bB2jV.png")));
meshView.setMaterial(material);
you can see that this image is used to apply colors to your pyramid:
If you modify the texture indices for the faces, you will get different colors, based on the texture coordinates.
To know more about this, you can have a look at the FXyz3D library, that provides a TexturedMesh class based in this concept. There you will find many different 3D shape "textured" primitives, that can use different texture "modes". Most of those modes don't even require an image, as this is created internally. This allows creating for instance color gradients based on mathematical functions.
This is an example of a TetrahedraMesh, that makes use a 3D function to define the density map:
TetrahedraMesh tetra = new TetrahedraMesh(10, 5, null);
tetra.setTextureModeVertices3D(1530, p -> p.magnitude());
I've seen similar questions before but none helped me.
The player in my game can go to the right forever until he dies, but he can also go back.
I have a wall texture that I need to repeat forever on the top and bottom of the screen. I've seen answers where they say you need to put different values when drawing the texture at srcX, srcY, srcWidth and srcHeight, but when I do that the texture doesn't look anyting like the original. I set the texture to repeat with wallTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat)
The code I have to draw the wall on the top and bottom of the screen:
spriteBatch.draw(wallTexture, -Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT - Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH / 2, Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH, Constants.WALL_HEIGHT, 1, 1, 0, 0, 0, 720, 64, false, false);
spriteBatch.draw(wallTexture, -Constants.WORLD_WIDTH, -Constants.WORLD_HEIGHT - Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH / 2, Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH, Constants.WALL_HEIGHT, 1, 1, 0, 0, 0, 720, 64, false, false);
Thanks
I'm making a game in which there will be red and blue Shapes moving around on screen. I've looked high and low for how to make anywhere they overlap a different color (purple). I am only using Java2D, which to my understanding does not support Shaders. I looked into drawing the red shapes to one BufferedImage and the blue shapes to another, then trying to use AlphaComposite to combine the colors and draw it to the screen, but it never produced correct results. I'm using 127,0,0 and 0,0,127 for red and blue instead of 255 because 255,0,255 looks, in my opinion, terrible for purple. I would effectively like this.
Thanks to copeg's suggestion, I was able to figure it out. Here is the code segment (Context: the shapes I'm drawing are attacks):
//Attacks
BufferedImage attackImg = new BufferedImage(S_WIDTH, S_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D ag = (Graphics2D) attackImg.getGraphics();
//Make all of attackImg a transparent image
ag.setComposite(AlphaComposite.Clear);
ag.fillRect(0, 0, S_WIDTH, S_HEIGHT);
ag.setComposite(AlphaComposite.SrcOver);
//Render red attacks to attackImg
ag.setColor(new Color(127, 0, 0, 255));
for(Shape s : redAttacks)
ag.fill(s);
//Render overlap areas using composites to attackImg
ag.setColor(new Color(127, 0, 127, 255));
ag.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
g.setColor(new Color(0, 0, 127, 255));
for(Shape s : blueAttacks)
{
ag.fill(s);
g.fill(s); //Render blue attacks
}
//Render red and purple attacks
g.drawImage(attackImg, 0, 0, null);
I have a triangle like this;
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(1, 1, 0, 1);
shapeRenderer.polygon(new float[] { -10, 0, 10, 0, 0, 200 });
shapeRenderer.rotate(0, 0, 1, 1);
shapeRenderer.end();
and I rotate 1 degree in each render. But I want to fix rotation (e.g. 45) to an angle. How can I do this?
Thanks.
To have a fixed rotation you hav to rotate the ShapeRenderer only once.
There are 2 possible ways i can think about:
call shapeRenderer.rotate(0, 0, 1, 45); in the constructor or in create() / show() method
This call rotates your ShapeRenderer by 45° (last parameter) arround the Z-Axis (The 3rd parameter)
call shapeRenderer.rotate(0, 0, 1, 45); in the rendermethod, only if you did not rotate yet. So you have to keep a boolean rotated and only if it is false you call rotate() and set it to true.
To answer the question in your comment: You cannot directly set the rotation, you can only rotate (relative to the current rotation). So i would suggest to store a float rotation, and everytime you rotate your ShapeRenderer you set the new value. To set a rotation in degrees you have to rotate like:
shapeRenderer.rotate(0, 0, 1, newRotation - rotation);
rotation = newRotation;
This works only if you always rotate arround the same axis, in your case the Z-axis. Else you would have to store 3 rotations (x,y,z). If you rotate arround a custom axis, defined by for example (0.1, 0.3, 0.6) you would need to calculate the rotation for all axes. But i don't really know how to do that. I think some Vectormath would do that. But i don't think you need that.
I have a drawn a filled circle using ShapeRenderer and now I want to draw this circle as a transparent one. I am using the following code to do that: But the circle is not coming as transparent. Also, I checked th libgdx API and from the wiki, it says that, need to Create CameraStrategy. Has somebody faced similar issue ever before? If so, please give me some clues. Thanks in advance.
Gdx.gl.glEnable(GL10.GL_BLEND);
Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
drawFilledCircle();
Gdx.gl.glDisable(GL10.GL_BLEND);
private void drawFilledCircle(){
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.FilledCircle);
shapeRenderer.setColor(new Color(0, 1, 0, 1));
shapeRenderer.filledCircle(470, 45, 10);
shapeRenderer.end();
}
The following code is working for me in this case, maybe it will help someone else:
Gdx.gl.glEnable(GL10.GL_BLEND);
Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.FilledCircle);
shapeRenderer.setColor(new Color(0, 1, 0, 0.5f));
shapeRenderer.filledCircle(470, 45, 10);
shapeRenderer.end();
Gdx.gl.glDisable(GL10.GL_BLEND);
First we need to enable blending:
Gdx.gl.glEnable(GL10.GL_BLEND);
And make sure that you don't call SpriteBatch.begin() and SpriteBatch.end() between that line of code and your Shaperender.drawSomething() line of code. I don't know why but that's what works in my case
Only this worked for me :
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
//Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); // <<< this line here makes the magic we're after
game.shapeRenderer.setProjectionMatrix(camera.combined);
game.shapeRenderer.begin(ShapeType.Filled);
go.drawShapes();
game.shapeRenderer.end();
//Gdx.gl.glDisable(GL20.GL_BLEND);
Well, there is not really a point in drawing something fully transparent. If you did want to make a half transparent circle, you would have to clear the color buffer by glClearColor before each frame and set Color alpha component to 0.5f.
If you wouldn't clear the buffer, after few render draws, the circle would blend into one with almost solid color.
private void drawFilledCircle(Camera camera){
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.FilledCircle);
shapeRenderer.setColor(new Color(0, 1, 0, 0.5f)); // last argument is alpha channel
shapeRenderer.filledCircle(470, 45, 10);
shapeRenderer.end();
}