I'm attempting to create a 2D spaceship game from scratch. My problem is that I feel like the way Im rotating images is awkward and just wrong. I would appreciate if I could get some guidance. I believe what I'm doing in the following code is loading an image into a JPanel and rotating the image and moving the JPanel.
The code is unrefined and only partial, but it should give you an idea of how I am manipulating images.
What seems so awkward is that I have to create a new JPanel for every image, I would think there is a better way but I don't know. I have tried to get one image on top of an other with the JPanels to no avail. That said I haven't put much time into trying to get that to work. I want to know if I should continue my attempt with JPanels or to pursue a different method.
I know precious little about images, so this might not work.
Why have you overridden paint and not paintComponent? There is also something not quite right about showing the image in the constructor. Try putting everything in the paintComponent method, which should start like this:-Then apply the affine transform, which can be done simply with the rotate method of Graphics2D and then try painting the image.
I overrode paint because I didn't know about paint component. I changed it to paintComponent and it worked fine. I then tried to move, as you suggested, the show image to paintComponent. When I did this nothing was displayed on my screen. I wrote this a month ago so I don't remember all the logic behind it, so I tried playing around with it for an hour to see what it was doing. I have a vague understanding of it now. In ImagePanel I create my image, then setAngle takes image and rotates it and puts it into rotated (another buffered image), and then finally paint draws rotated. I may have that all wrong, but thats how it seems from the output. I made several attempts to simplify the process, but had no success.
Edit: I might have made an oops, so here is what I changed. I don't get any compilation or run time errors but the image doesn't load.
Yeah, temp did seem odd since BufferedImage has getHeight and Width. I didn't think preferred size would affect it because its the only panel currently in my JFrame. I did override getPrferredSize and nothing seemed to change. I use it quite simply;
As the name implies, I only wrote this to quickly test my ImagePanel.
Thanks for the advice on paintComponent, I'll keep looking through the rest of my code for problems.
posted 5 years ago
I did override getPrferredSize and nothing seemed to change.
Yes, in this case it doesn't make a difference because you added the panel to the "CENTER" of the BorderLayout, which will automatically resize the panel to fit the frame. However, if you added the panel to the "NORTH" then it would make a difference.
You want to draw on the panel so you need to use the Graphics object of the panel.
First you create a Graphs2D object to do the painting from the Graphic object of the panel. But then you override this variable to use the Graphics object of the BufferedImage. So all you are doing is drawing on top of the BufferedImage, but you are not drawing anything to the panel. You don't need the second line.
Thanks! I laughed when I read your explanation, its quite a silly mistake but I didn't know better. The code works well now. I've cleaned up the code quite a bit and I feel much more confident about it now.
I don't always make ads but when I do they're tiny
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop