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.
OP is drawing a BufferedImage, and that is not a Component.
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.
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.
posted 2 months ago
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.
posted 2 months ago
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