Move Bitmap in canvas android java - java

My Question is how can I move a bitmap on a canvas?
Recently I'm making a game and There's a button.
When I'm clicking on this button I want to move the bitmap (Only X)
but when i'm doing this the bitmap is gone.
Here's the code :
"main_right" is the button that I'm clicking on.
I also tried to put "invalidate()" there but it also didn't work.
#Override
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (x > width - main_right.getWidth()
&& x < width - main_right.getWidth()
+ main_right.getWidth()
&& y > height
- (main_right.getHeight() + main_right.getHeight() / 2)
&& y < height
- (main_right.getHeight() + main_right.getHeight() / 2)
+ main_right.getHeight()) {
x += 1;
canvas_main.drawBitmap(image, (width / 2) + x2,
height - (image.getHeight() + image.getHeight() / 2),
paint);
} else {
x = 0;
}
return true;
}
return false;
}

Store the x position on your onTouchEvent and draw at your canvas inside the onDraw(Canvas), that is the surface you need to draw.

canvas_main.drawBitmap(image, (width / 2) + x,
height - (image.getHeight() + image.getHeight() / 2),
paint);
The height might be the problem if your bitmap is > (device height/3)
height - (image.getHeight() + image.getHeight() / 2)
In essence means height - 1.5*image height. Since the top left of the bitmap is placed at that coordinate, the image will disappear out of the screen.
It should be
height - (image.getHeight() - image.getHeight() / 2)
or
height - image.getHeight() / 2

Related

Rotate player towards mouse

I want my player rotate towards mouse. Here's the code calculating the angle:
float angle = (float) Math.atan2(MouseInput.getMousePos().y - transform.position.y + transform.size.y / 2,
MouseInput.getMousePos().x - transform.position.x + transform.size.x / 2);
angle = (float) (angle * (180 / Math.PI));
if (angle < 0) {
angle = 360 + angle;
}
transform.rotation = 180 + angle;
And getMousePos() method (it just returns mouse pos relative to window):
public static Vector2 getMousePos() {
Point p = MouseInfo.getPointerInfo().getLocation();
return new Vector2(p.x - Game.w.getAccessToWindow(Acces.WINDOW_JFRAME_ACCES).getLocation().x,
p.y - Game.w.getAccessToWindow(Acces.WINDOW_JFRAME_ACCES).getLocation().y);
}
Can you tell me what's wrong with this code? Player isn't rotating properly.
I tried following this article: https://gamefromscratch.com/gamedev-math-recipes-rotating-to-face-a-point/
Update:
I found this post: Java 2d rotation in direction mouse point
Now I've updated my code to this:
int centerX = (int) (transform.size.x / 2);
int centerY = (int) (transform.size.x / 2);
int mouseX = (int) MouseInput.getMousePos().x;
int mouseY = (int) MouseInput.getMousePos().y;
double angle = Math.atan2(centerY - mouseY, centerX - mouseX) - Math.PI / 2;
transform.rotation = angle;
But still something is off. Try this code for yourself. Maybe I did something wrong somewhere else.

How to draw bitmap image on top in arc

I want to draw image on Top in each Arc of Canvas
private void drawImage(Canvas canvas, float tempAngle, Bitmap bitmap,String mValue) {
//get every arc img width and angle
int imgWidth = (radius / mWheelItems.size());
//int imgWidth = (radius / 3);
float angle = (float) ((tempAngle + 360 / mWheelItems.size() /2) * Math.PI / 180);
//calculate x and y
int x = (int) (center + radius / 2 / 2 * Math.cos(angle));
int y = (int) (center + radius / 2 / 2 * Math.sin(angle));
int top=y - imgWidth/2;
int bottom=y +imgWidth/2;
int left=x - imgWidth /2;
int right=x + imgWidth / 2;
Rect rect = new Rect(left, top, right, bottom);
final Rect rect1 = new Rect(x - imgWidth /2 , y - imgWidth / 2 , bitmap.getWidth() , bitmap.getHeight());
canvas.drawBitmap(bitmap, null, rect, null);
}
the Arc is made according to the size of items
The Result is shown like that
But I want the image bitmap shown on top like that of rect. Red also want to large size of images or bitmap
Solved by Specific Tab and Big Screen Tablets
Change the X and Y coordination According to Angle
if(tempAngle==0) {
x = x + 50;
}
if(tempAngle==60) {
y = y + 50;
}
if(tempAngle==120) {
imgWidth = imgWidth-12;
y = y + 20;
x = x - 40;
}
if(tempAngle==180) {
imgWidth = imgWidth+12;
x = x - 50;
}
if(tempAngle==240) {
y = y - 50;
}
if(tempAngle==300) {
y = y - 20;
x = x + 40;
}

Android- crop square image inside bounding box on surfaceview

I am trying to crop image inside bounding box from camera preview and display in a activity. I have a surfaceview and a square imageview on top of it. I have to capture image that appears only inside imageview. Following is my cropping code, No idea as to what is wrong here,Please help
int width = rotatedBitmap.getWidth();
int height = rotatedBitmap.getHeight();
int newWidth = (height > width) ? width : height;
int newHeight = (height > width)? height - ( height - width) : height;
int x = (width - height) / 2;
x = (x < 0)? 0: x;
int y = (height - width) / 2;
y = (y < 0)? 0: y;
Bitmap cropImg = Bitmap.createBitmap(rotatedBitmap, x, y,newWidth, newHeight);
what's wrong with my code?

How to resize a JFrame manually?

I am trying to resize the JFrame manually by dragging a JPanel. Here is what i have done. At the moment it works, but the screen still flickers, and the resizing is not correct
What i want it to happen is, when i click on the Jpanel and drag it, the JFrame should resize automatically to where the X Coordinates and Y Coordinates are.
public void mouseDragged(MouseEvent e) {
// Sets width, height and moved X and Y coordinates
int currentwidth = this.getWidth();
int currentheight = this.getHeight();
int newwidth = 0;
int newheight = 0 ;
int thisX = this.getX();
int thisY = this.getY();
// Calculates the moving distance
int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);
// Checks which component the mouse is clicked on
if(e.getComponent() == reszingbit){
// Calculates the new height and width
newwidth = 200 + xMoved;
newheight = 200 + yMoved;
// Making sure that the new height and width is not less than actual size
if(newwidth >= 200 && newheight >= 200){
this.setSize(newwidth,newheight);
}
}
200 is the actual height and width.
It is to little informations, but maybe you need, to change the size calculation to:
newwidth = this.getWidth() + xMoved;
newheight = this.getHeight() + yMoved;
also the move size calculation should look like this:
int xMoved = e.getX() - initialClick.x;
int yMoved = e.getY() - initialClick.y;

How do I linearly scale my pixel coloring?

So I have a project for class I am working on in which you have to create a GUI box filled with circles, except the middle 50% of the screen cannot be filled with circles. Also, the red color value of each circle scales linearly from top to bottom of the screen, 0 at the top, 255 at the bottom. Here is what it should look like:
Here's what I have. I tried doing 255/500 (500 is the height) in order to get a scaling factor which I would then use to multiply all my y coordinates by in order to get the specified red value and it worked. The answer to 255 / 500 is 0.51 and when I used 0.51 instead of y * (255 / getHeight()); it worked. I need it to work with any dimensions of the frame, however, so the 0.51 doesn't work. For some reason the y * (255 / getHeight()) does not work, it appears to be returning 0 as the circles are various shades of blue and green. What can i do to fix this?
The code I have:
public class NewJComponent1 extends JComponent {
public void paintComponent(Graphics g) {
int count = 0;
int diameter = 0;
Random rand = new Random();
while (count < 5000) {
int x = rand.nextInt(getWidth() + 1);
int y = rand.nextInt(getHeight() + 1);
int greenValue = rand.nextInt(256);
int blueValue = rand.nextInt(256);
diameter = rand.nextInt(21) + 10;
int redValue = y * (255 / getHeight());
Color random = new Color (redValue, greenValue, blueValue);
if ((x < (getWidth() / 4) && y <= (getHeight() - diameter))
|| ((x > (getWidth() * .75) && (x < getWidth() - diameter)) && y <= (getHeight() - diameter))
|| (x <= (getWidth() - diameter) && y < (getHeight() / 4))
|| (x <= (getWidth() - diameter) && ((y > (getHeight() * .75)) && (y <= getHeight() - diameter)))){
g.setColor(random);
g.fillOval(x, y, diameter, diameter);
count++;
}
}
System.out.println(getHeight());
System.out.println(getWidth());
}
}
I tried various iterations of the redValue code, swapping order, making a double and typecasting to int, and various other things but I couldn't get it to work. I'm sure its a small mistake that is messing everything up, but anyways thank you for the help regardless. I am using Android Studio, not sure if that really would impact anything.
Replace this line
int redValue = y * (255 / getHeight());
with
int redValue = (int) Math.round(y * (255.0 / (double) getHeight()));
Just changing redValue to an double wouldn't change the fact that 255/getHeight() is integer division.

Categories