I am getting started on a game in JAVA, which eventually shoould become an android app. anyway, i was wondering How to rotate an image (I eventually want to rotate a character to face the mouse, if you know how to do this it would be AMAZING!). Anyway, this is what i use to load the image:
ImageIcon i = new ImageIcon("C:/image.jpg");
Any ideas? a rotate(Image image, int angle){} method would be great!
If you knew how to rotate an image towards the mouse, that would be even better! Thanks for your time
you can't make a game with ImageIcons. you have to use gava.awt.Paint2D and paint to an image. then you can rotate it by setting the affline transform. here i answered a similar wuestion in more depth:
Platform Game - Getting the Object to adjust angle according to mouse position
and a tutorial on java game programming in general:here
Take a look at this tutorial illustrate how to Rotate an image using Java 2D AffineTransform class .
Related
I am currently working on a solution for an automatic image edit.
And I have used Canny Edge Detection and Closing.
But What I ultimately want to accomplish is to find all the rectangles from blueprint and fill them with an image what I have.
I want to know the process that I need to take, not the exact code or solution!
Please suggest me what steps should I take to accomplish it, thx.
Image that has the rectangles
Image that needs to go into all the rectangles
what i have done so far
2018-02-12 EDITTED(clean rectangle detected)
// I have done finding the rectangles and drawing lines over them, but the result is not really reliable than I expected(it draws line on rectangles those are not a parking space), and I do not know how to put an image on those rectangle instead of drawing line on them. please help me out!
P.S : Only in JAVA please !
In your case, you don't need Canny. Your image is really clean and edges are really visible already. A simple Threshold, will work better.
For rectangle detection take a look at this example (included with opencv) that uses findcontours and checks the angles: https://github.com/opencv/opencv/blob/master/samples/cpp/squares.cpp
In your case, you can skip the Canny step because you don't have gradients. You may need to modify the filtering, like side dimensions for your case.
Once the rectangles look good, you just need to copy your image in the location. If rectangles are rotated, you will have to rotate your image as well.
EDIT:
To copy the small image onto the big image you can do the following
Mat submatImg = bigImage.submat(new Rect(x, y, smallImage.width(), smallImage.height());
smallImage.copyTo(submatImg);
If you need to do resizing and rotations, take a look at geometric transformations
I want to turn my player in the direction he is looking at. The whole is so similar to the behavior of the Bird in FlappyBird. He always looks in the direction in which he moves. I am programming this in Android Studio with libGDX and I have a class in which the game is played and one in which I have information about my player.
I have unfortunately no code example, because everything I have, would explain nothing and would make the situation only more complicated.
Please write a comment if you are confused.
Thanks for any help! :)
I think what you are looking for is a way to flip the sprite. Sadly, I don't think libgdx supports this. But what you could do is draw 2 images of your sprite(one in each direction) or use some drawing program to flip your current image. Then in your sprite class, in your update or render method you would change the sprite image to the corresponding direction using sprite.set(new Texture())
I'm trying to make asteroids that spin in my 2D game.
Using the graphics class I am drawing an image I created onto the screen (the asteroid) and moving it across the screen.
My next step was to make it rotate however I am lost at to how to do this.
I was able to make it rotate when I was simply drawing a polygon by changing the vertices of the asteroid however there are no vertices when drawing images, only (x,y) and (length,width).
How can I rotate an image? Is there any inbuilt functionality that does it?
I'm not asking for someone to tell me exactly how to do this, I'm just looking for a push in the right direction as I am a bit lost.
Either use a AffineTransform or use Graphics#rotate. In either case, you should make a copy of the Graphics context first (Graphics#create) first, which will preserve the state of the original context. Just make sure you dispose of the copy when you're done (Graphics#dispose)
Something like this perhaps
I am using png's in my tower defense game. How would I rotate a picture so it follows the movement of another picture (like a turret always pointing at a target)?
Look into the AffineTransform class. The rotate method is what you're looking for. Here's an example!
If you've got some other specific game-development-related questions, there's a Game Development StackExchange site too.
I need to make a game in Java for a project.
What I'm trying to do is a game where you have to go through a maze without touching the walls.
Is there a way to get the color of the pixel the mouse is over?
You probably want java.awt.Robot.getPixelColor(x, y). It'll be faster than grabbing an image.
This post answers your question precisely including a complete working code example. Basically, you can create an image from canvas on which you draw and call getRGB() on a pixel on the image.
You want to implement the MouseMotionListener interface and do what Artelius mentioned, namely, get the coordinates of the mouse position and calculate the color of the pixel.