Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

drawImage() draw image everytime I rotated  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to rotate an image in a loop, but when I do the image multiplies on the screen and stacks up on each other.


   

Result:


How do I rotate my image without multiplying?

 
Elya Matsunomi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it, forgot to paint the background

 
Ranch Hand
Posts: 49
1
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't look at your code carefully but if image multiplies on the screen when you try to rotate it you can use removeAll(); method to clear screen or remove(Component component); for certain component.
 
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Asif
OP is drawing a BufferedImage, and that is not a Component.

@Elya
You are overriding the 'paint' method. That is something that you should not normally do, although in this case it doesn't matter.
Paint does three things: it invokes the method 'paintComponent', in which you would do your user drawings, then it invokes the method 'paintBorder', that draws a border if any, and lastly it invokes the method 'paintChikldren', that draws all your labels, buttons et cetera. So, in normal circumstances you only should override the 'paintComponent' method.

You use two BufferedImages to rotate your image. Nothing wrong with that, but you can draw your image also directly to your panel, where you do a rotation in the Graphics2D of the panel. That would simplify your code a little.

I just created an example where I override the 'paint' method, to give an example where overriding the paint method is useful. I created a JLabel that contains the image, and centered the label in its panel. Then I override the paint method of the panel, where I first invoke the two methods 'paintComponent' and 'paintBorder', then I create a Graphics2D that I give a rotation, and with this g I invoke 'paintChildren'. As a consuquence, the JLabel with the image is then rotated.
This is only a demo, of course, it has no practical value.
 
Marshal
Posts: 63853
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think this discussion would fit better in our GUIs forum, so I shall move it.
 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like Piet is using a JPanel to draw on, whereas Elya is using a JFrame. The problem with drawing on a JFrame is that it has a content pane, and you should really be drawing on the content pane. So I would suggest changing the code to use a JPanel (defined the way Piet defined it) and then to use that JPanel as the JFrame's content pane.
 
Campbell Ritchie
Marshal
Posts: 63853
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, start the paintComponent method like this:-Assuming you have directly extended JPanel, that will cause the panel to revert to its basic appearance before you paint anything.
 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is usually a good idea, but here it is not necessary, since the bufferedimage has the same size as the panel (content pane), the frame is not resizable and the bufferedimage has no transparancy.
 
machines help you to do more, but experience less. Experience this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!