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-
Related
My goal is to fill the screen with a black rect with an alpha of 128 so the screen looks dark, then make it so I can render rects to the screen and the place they are rendered to turns fully transparent so you can see right through that rect. I have made the screen get filled partially black but I cannot make it go transparent when I draw a rect on top of that. I have never used AlphaComposites but I assumed that I'm gonna have to use one of those to make this work. Anyone know how I could get this done?
private Color darknessColor = new Color(0,0,0,128), flashlightColor = new Color(255,255,255,128);
public void render(Graphics g) {
// Draws the darkness part of the screen.
Graphics2D g2 = (Graphics2D) g;
g2.setColor(darknessColor);
g2.fillRect(0, 0, handler.getWidth(), handler.getHeight());
g2.setColor(flashlightColor);
g2.setComposite(AlphaComposite.DstOut);
g2.fillRect(200, 200, 300, 200);
g2.dispose();
}
There is no way to "undraw" a rectangle which has already been drawn; if you've already painted over the scene behind the rectangle, then it's gone. The solution is to only draw the black shape where you want it; this means it is not simply a rectangle, but if the part you want to "cut out" is a rectangle, then you could achieve the effect you want by drawing four black rectangles, like so:
Hopefully it's straightforward how to compute the coordinates that these four rectangles should have.
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.
i have a jlable.and i'm going to draw a graph on it.but i realized that a line crated by draw line method ,disappear when resizing frame.
this is my code.i want to know how to avoid from disappearing when resize.i want to stay line even resize jframe.
void graph(JComponent jcom,int thick,int height,int xpos,int ypos,Color col){
Graphics2D gfx=(Graphics2D) jcom.getGraphics();
gfx.setStroke(new BasicStroke(thick));
gfx.setPaint(col);
gfx.drawLine(xpos, ypos, xpos, ypos-height);
}
button click code
graph(jLabel1, 10, 100, 200, 200, Color.GREEN);
You can create your own class which extends from JLabel and has an extra method to decide if it must paint the line or not.
In the overridden paintComponent() method of this new class, draw your line after the super.paintComponent() call.
apply the logic from suggestion 1 in the parent component of you JLabel. (not sure if this will work in all situations)
I've been trying to draw a string in a JFrame, but for some reason it wont show , here is the code with the drawings command:
g.setColor(Color.WHITE);
g.fillRect(0, 0, 900, 500);
g.drawImage(backToMain,0,0,this);
g.setColor(Color.BLACK);
g.drawString("name", 300, 0);
but for some reason the screen I get is white with the Image in the left corner but missing the String.
I thought I might have a problem with the paint() command but I have tried several other things but it seams that the string just refuses to appear.
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.