• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

drawImage() draw image everytime I rotated

 
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 25
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed it, forgot to paint the background

 
Ranch Hand
Posts: 85
2
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 73019
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 26625
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 73019
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic