Draw Rectanlge perfectly centered in Java - java

i want to draw objects (incl. putting components) in a container drawn perfectly in the middle.
The problem is, using the middle x and y and subtracting/adding the half of the windows width and height is not enough.
When i draw a rectangle using this code:
g.drawRect( 10, 10, this.getWidth() - 20, this.getHeight() - 20);
Then this wont work. With the line above i want to draw a rectangle having a padding of 10 pixels on all sides.
So i start it at 10x10, then i take the full width and full height of the window and subtract 20 of it, because 10 on the left side and 10 on the right side.
In this case i should get a rectangle which is perfectly centered having 10 pixels distance to the windows border.
But the right and bottom side of the rectangle is out of the window, so the width and the height given to the drawRect method is too high.
I think this comes from the windows border.
The point 0x0 is not the point where you SEE the drawings. I guess 0x0 starts at the very top-left point of the window itself (no the drawing area).
So in this case i need to know the drawing area. Something like a rectangle giving me coordinates where i need to start drawing, so i can map my coordinates to the windows coordinates.
So when i want to draw a point at 0x0 (of the visible drawing area), then this point must be mapped to something like 8x30.
I did a test by adding a mouse listener printing the mouse position whenever the mouse is moved. When i move my mouse to the very top-left position of the drawing area, then my app prints the coord 8x30. this means the left border has 8px width and the top bar (showing the windows title and close/minimize buttons) has 30px height.
How can i get this working now. I would like to say something like
g.drawRect( 10, 10, this.getWidth() - 20, this.getHeight() - 20);
so i get my rectangle drawn perfectly centered even when i resize my window.
I think its easier to understand my problem when you try to paint this:
Font font = g.getFont();
FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
Rectangle2D rect = font.getStringBounds( "Hello", frc);
g.drawString( "Hello", (int) ( this.getWidth() - rect.getWidth()), (int) ( this.getHeight() - rect.getHeight()));
This takes the size of the Text "Hello" then tries to print it at the bottom-right. The code is correct, but the Text "Hello" appears to far on the bottom-right, i need to resize my window (making it bigger) to see the text.

The getInsets() method of your container should provide the coordinate offsets you need to apply when drawing.
Here is a link to some example code.

Are you drawing directly on the JFrame? It would be easier if you created a JPanel to draw on and added the JPanel to the JFrame. The origin at the top left hand side of the panel will not be bothered by the border of the frame or the title bar.

Related

Draw circle on canvas using strokeOval - how to let it expand from mouse click grid position

Double pytha = (Math.pow(e.getX() - circ.getCenterX(),2) + Math.pow(e.getY() - circ.getCenterY(),2));
Double pythaSqr = Math.sqrt(pytha);
circ.setRadius(pythaSqr);
gc.strokeOval(circ.getCenterX()-circ.getRadius(), circ.getCenterY()-circ.getRadius(), circ.getRadius(), circ.getRadius());
This is part of my paint program code and I can not get the oval to draw from the circle center, it alway uses my mouse click as its borders. I am trying to click on the screen and from the should the circle borders grow accoring to the radius in all directins equally. Hope you understand my questin (here I have done strokeOval(X-r,Y-r,r,r) but the it just grows in -x,y direction and if strokeOval(X,Y,r,r) then it grows in X,-Y dircetion in stead of growin equally to all directions using X,Y as center).

Basing the Recangle in upper-left side of the rootPane

I am very new at JavaFX. Currently working with 8th version. I am facing the following problem. When I am trying to draw a rectangle, it is center aligned. I added a eventHandler, which gives me the coordinates of the mouse click. So I just need this rectangle to be drawn from upper-left, but not from center, so I can just pass the coordinates of the mouse' clicks and then this rectangle
so the y axis would be from the upper left to south, and the y axis from the upper left to east. So
carRect = new Rectangle(0,0,70,36);
This code means, that this rectangle would be drawn upper-left aligned but not center aligned with width 70 and height 36. What am I missing ? I know, that I can pass negative values, but I need them just in the way I get them from the eventHandler.
Just used Normal Pane instead of StackPane and that solved the problem.

Java fillRect on coords and mouseMove point coords are off on the Y axis

I came across this problem in a game I'm writing but have reproduced the issue on a separate jar that consists only of a frame, a panel, and a mouse motion listener.
The issue is that I draw a rectangle - for example at x:512, y:384 (48x48).
Using a mouse motion listener on the frame, it always reports the Y axis around 25 pixels less. So while the coord of the rectangle should be x:512, y:384, the mouse motion listener reports as x:512, y:409.
I theoretically could just add the difference to the mouse Y, but I need to understand why this happening.
Full code for the three example classes
it always reports the Y axis around 25 pixels less
Maybe you added the MouseListener to the frame instead of the panel.
You draw your Rectangles on the panel, but the panel is located (on my OS) 30 pixels down from the top of the frame so your coordinates don't match. The X value should also be out by the width of the frame border.
Try adding the listener to the panel.
The origin of Frame and Board is different. The origin of Board is the (0, 25) of Frame.
Not a good image, though. First, screenshot won't show cursor, so I drew it. Second, I didn't put my cursor exactly on (0, 25) but just around it.

libGDX Coordinate System

I'm trying to properly configure my Camera and Sprites in libGDX to show up in a 2D coordinate system properly with the origin at the bottom left hand corner.
I set up my Camera like this:
cameraWidth = Gdx.graphics.getWidth();
cameraHeight = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, cameraHeight/cameraWidth);
And I set up my Sprites like this:
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setScale(scale);
sprite.setPosition(startX,startY);
My problem is with sprite.setSize(x,y). If I set all the sprites to have a size of (1, texture aspect ratio), then everything draws with the right display ratio (not smushed or stretched), but nothing draws in the correct place. For example, if I draw something at (0,0), it will draw with its bottom left corner off the left side of the screen and up a number of pixels.
I've noticed by changing around the ratio I can get things to draw in different places - namely if I set it to (1, display aspect ratio) things look pretty close to drawing in the right place - they just draw from their center, not their bottom left corner, as LibGDX specifies. The only problem is that the images all appear as smushed or stretched, which is no good.
This seems like a simple problem and I just want to know how to set this up so I can have a sensible coordinate system that draws things in the right place and in the right aspect ratio. Thanks.
Once you change your viewport to match the screen's aspect ratio then (0, 0) will no longer be at the bottom left of the screen unless the screen is square. If the screen is wider than it is high then the visible portion of the x axis will still go from 0.0 to 1.0, but 0.0 on the y axis will now be somewhere off the bottom of the screen.
If you adjust the camera so that (0, 0) is at the bottom left of the screen, and remember that the visible y axis will only go up to grapicsHeight / graphicsWidth then that should solve your coordinate problem.
I would recommend setting the camera to point to the middle of the screen rather than the bottom left. There's an example here that does exactly that, drawing a 2:1 rectangle which is always in the centre of the screen, always with a 2:1 ratio no matter how much you resize it.
I've found a solution to this problem:
Set the camera to ortho (even though it's already an orthographic camera)
camera.setToOrtho(false,1,screen height / screen width);
Also, each sprite must have its position set to (x - sprite.getWidth()/2, y - sprite.getHeight()/2. I extended the Sprite class and overrode the setPosition method to account for this. Now, every time the position is set, the Sprites end up going where you "would think they'd go", with setPosition(0,0) putting it in the bottom left and setPosition(1,height/width) in the top left.
Oddly enough, this draws every sprite centered around the (x,y) point, which would make sense since width/2 and height/2 were subtracted from the position, except not subtracting the values does not make setPosition center the sprite via the bottom left corner - it's centered in a way I haven't figured out.

how to get stage in the center in libgdx?

My image is of size 720x480 and stage is of same size. The boolean parameter, whether to stretch the stage or not is kept false, so that aspect ratio is maintained, now the problem i am getting that a black area is shown on either side of the screen I want my scene to be centered. The link below refers a file, which shows the problem more clearly, that image is aligned to the bottom left i want it to be centered so that the black area is divided equally on top and the bottom.
what is the approach to solve this?
http://badlogicgames.com/forum/viewtopic.php?f=11&t=3398
You have to specify X and Y coordinate as the following way
X = (Gdx.graphics.getWidth() - sprite.getRegionWidth()) / 2.0f;
Y = (Gdx.graphics.getHeight() - sprite.getRegionHeight()) / 2.0f, sprite.getRegionWidth()/2.0f;
You have to calaculate center of the image. Using this way you can put your image in center of the screen. Also here I user Sprite to display image.

Categories

Resources