rec.set(0, 0, canvas.getWidth(), canvas.getHeight()/2);
It creates the rectangle as expected, I know 0, 0 is x, y but how do I set y to the bottom so it stays at the bottom on all devices with different size?
you haven't given much information, so I'll state my assumptions
the x,y would be the top-left of your shape, so assuming that the canvas is the full height of the device:
x - 0, as you want the shape to start on the left edge, y - the height of the device minus the height of the shape
your code would be
int width = canvas.getWidth();
int height = canvas.getHeight()/2;
int x = 0;
int y = canvas.getHeight() - height;
rec.set(x, y, width, height);
Related
With AWT I draw a border using java.awt.Graphics#drawOval and java.awt.Graphics2D#setStroke. For situations when the set stroke has a size bigger than the oval's diameter the resulting border is not like expected. In that situation the stroke overlaps the stroke of the other side of the circle: Circles north stroke overlaps the south stroke. AWT renders this overlapping in an XOR way as you can see in the following image.
What I'd expect instead is that the stroke overlapping is drawn in an OR way, so that in all situations when stroke width > circle diameter the center is black.
Is there a simple way I can set to change the behaviour to an OR overlapping mode, even when width or height of the circle (then its an ellipse) is not equal?
Same diameter (10px) with increasing stroke width:
Based on the solution that Marco13 mentioned in his comment I came up with this custom drawOval function. It basically switch from drawOval to fillOval once the stroke width is greater than the diameter. The position and dimensions for the fillOval function are calculated to match the drawOval output.
public static void drawOval(Graphics2D g2d, int strokeWidth, int x, int y, int width, int height) {
int minLength = Math.min(width, height);
int maxLength = Math.max(width, height);
if (minLength >= strokeWidth) {
g2d.drawOval(x, y, width, height);
} else {
int x1 = x - (strokeWidth - maxLength) / 2 - (maxLength / 2);
int y1 = y - (strokeWidth - maxLength) / 2 - (maxLength / 2);
int width1 = width + strokeWidth;
int height1 = height + strokeWidth;
g2d.fillOval(x1, y1, width1, height1);
}
}
This is how it looks like
I'm trying to rotate an image over its x axes but its not working
Texture one = new Texture(Gdx.files.internal("img/one.jpg"));
oneImg = new Image(one);
oneImg.setOrigin(oneImg.getWidth() / 2, oneImg.getHeight() / 2);
oneImg.setPosition(stage.getWidth() / 2-32 , stage.getHeight()/2);
oneImg.setScale(2f,2f);
oneImg.addAction(rotateBy(360, 0.5f));
You can rotate your texture right when you draw it on the batch.
SpriteBatch.draw(textureRegion.getTexture(), x, y, originX, originY, width, height, scaleX, scaleY, rotation, srcX, srcX, srcWidth, srcHeight, false, false);
So above is the exact code that you need to rotate your image in x-axis, y-axis or both.
where :
x - the x-coordinate in screen space
y - the y-coordinate in screen space
originX - the x-coordinate of the scaling and rotation origin relative to the screen space coordinates
originY - the y-coordinate of the scaling and rotation origin relative to the screen space coordinates
width - the width in pixels
height - the height in pixels
scaleX - the scale of the rectangle around originX/originY in x
scaleY - the scale of the rectangle around originX/originY in y
rotation - the angle of counter clockwise rotation of the rectangle around originX/originY
srcX - the x-coordinate in texel space
srcY - the y-coordinate in texel space
srcWidth - the source with in texels
srcHeight - the source height in texels
I am trying to create an oval/circle inside a rectangle. I am trying to do this on canvas for a bitmap image. Here is my code:
int x = (int) (midpoint.x*xRatio);
int y = (int) (midpoint.y*yRatio);
int radius = (int) (distance/2);
int left = x - radius;
int right = x + radius;
int top = y - radius;
canvas.drawRect(left, top, right, bottom, paint);
Now i want to create an oval/circle inside this rectangle. I tried this and been trying for hours cant get it to work:
RectF ovalBounds = new RectF();
//ovalBounds.set(x, y, (right - left)/2, (bottom-top)/2);
ovalBounds.set(x, y-radius, radius * 2, radius * 2);
canvas.drawOval(ovalBounds, paint);
Can someone please help me figure this out?
Here is visual to help what i am trying to achieve:
You should use the same bounds than you used for drawing the rectangle:
RectF rect = new RectF(left, top, right, bottom);
canvas.drawRect(rect, paint);
canvas.drawOval(rect, paint);
I currently have a maze game which draws a 5 x 5 square (takes the width of screen and splits it evenly). Then for each of these boxes using x and y cordinates I user drawRect, to draw a colored background.
The issue I am having is I now need to draw an image within this same location, therefore replacing the current plain background colour fill.
Here is the code I am currently using to drawRect (a few example):
// these are all the variation of drawRect that I use
canvas.drawRect(x, y, (x + totalCellWidth), (y + totalCellHeight), green);
canvas.drawRect(x + 1, y, (x + totalCellWidth), (y + totalCellHeight), green);
canvas.drawRect(x, y + 1, (x + totalCellWidth), (y + totalCellHeight), green);
I would then also need to implement a background image for all the other squares within my canvas. This background will have simple 1px black lines drawn over the top of it, current code to draw in a grey background.
background = new Paint();
background.setColor(bgColor);
canvas.drawRect(0, 0, width, height, background);
Could you please advice if this is at all possible. If so, what is the best way I can go about doing this, whilst trying to minimise memory usage and having 1 image which will expand and shrink to fill the relvent square space(this varies on all the different screen sizes as it splits the overall screen width evenly).
Use the Canvas method public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint). Set dst to the size of the rectangle you want the entire image to be scaled into.
EDIT:
Here's a possible implementation for drawing the bitmaps in squares across on the canvas. Assumes the bitmaps are in a 2-dimensional array (e.g., Bitmap bitmapArray[][];) and that the canvas is square so the square bitmap aspect ratio is not distorted.
private static final int NUMBER_OF_VERTICAL_SQUARES = 5;
private static final int NUMBER_OF_HORIZONTAL_SQUARES = 5;
...
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
int squareWidth = canvasWidth / NUMBER_OF_HORIZONTAL_SQUARES;
int squareHeight = canvasHeight / NUMBER_OF_VERTICAL_SQUARES;
Rect destinationRect = new Rect();
int xOffset;
int yOffset;
// Set the destination rectangle size
destinationRect.set(0, 0, squareWidth, squareHeight);
for (int horizontalPosition = 0; horizontalPosition < NUMBER_OF_HORIZONTAL_SQUARES; horizontalPosition++){
xOffset = horizontalPosition * squareWidth;
for (int verticalPosition = 0; verticalPosition < NUMBER_OF_VERTICAL_SQUARES; verticalPosition++){
yOffset = verticalPosition * squareHeight;
// Set the destination rectangle offset for the canvas origin
destinationRect.offsetTo(xOffset, yOffset);
// Draw the bitmap into the destination rectangle on the canvas
canvas.drawBitmap(bitmapArray[horizontalPosition][verticalPosition], null, destinationRect, null);
}
}
Try the following code :
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawBitmap(bitmap, x, y, paint);
==================
You could also just reference this answer.
How do you draw a filled square box in Java that is exactly in the center of an applet window? and when resizing the window, it is centered horizontally and vertically within the applet window? I want it to adapt to the vertical height of the screen but stay square even as the horizontal width edges. If the window is resized to be too narrow, then the sides might cut off?
Here's an example of a panel that will either make a 30px square in the middle, or resize with the panel. Perhaps it can give you enough to make progress.
private class MyPanel extends JPanel{
int height = 30;//30 pixels high.
int width = 30;//30 pixels wide.
boolean resize = true;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int verticalCenter = this.getHeight()/2;
int horizontalCenter = this.getWidth()/2;
if(!resize){
int topLeftSquareCornerY = verticalCenter - (height/2);
int topLeftSquareCornerX = horizontalCenter - (width/2);
g.setColor(Color.BLUE);
g.drawRect(topLeftSquareCornerX, topLeftSquareCornerY, width, height);
}else{
g.setColor(Color.GREEN);
g.drawRect(15,15,(this.getWidth()-30), this.getHeight()-30);
}
}
}
Sounds like your basic problem is figuring out how to place a given rectangle correctly. You need to have the middle of the screen in the middle of the rectangle.
The distance from the middle of the rectangle to its sides are half the height and length respectively.
So x1, x2 = middle_x ± width/2, and y1, y2 = middle_y ± height/2.
I'm guessing you want to draw a square with a fixed size that stays in the center of the panel as the panel is resized. One approach to such problems is start from the end and work backward. You know about fillRect(), so write down what you need to "fill on the blanks" required by that method. Call the center coordinates x and y. The top corner would be half of size up, and the left corner would be half of size to the left; the square's width and height would be just size:
g.fillRect(left, top, width, height);
g.fillRect(x - size/2, y - size/2, size, size);
Now go back and figure out that x and y are half the panel's width and height, respectively:
int x = getWidth() / 2;
int y = getHeight() / 2;
Now put it all together in your paintComponent() method.