Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (623)
Games in Android Showcase (176)
games submitted by our members
Games in WIP (676)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Showing old BufferedImage after rotate. Why?  (Read 2022 times)
0 Members and 1 Guest are viewing this topic.
Offline darrinps

Senior Newbie

Java games rock!

« Posted 2003-03-07 15:19:54 »

After I rotate a BufferedImage, I still see the old one.

I can flip (vertical, and horizontal) an image but when I try to scale or rotate it, the old image is still showing up. I've tried every repaint, remove, removeall, etc that I can think of but still have double vision!  Shocked

Note that if I resize the window large enough ( just a little movement won't do it) then the old image does go away!

Here is the code that I use to rotate a loaded BufferedImage:

public static BufferedImage rotateImage(BufferedImage bi)
BufferedImage new_bi = new BufferedImage( bi.getHeight(),

Graphics2D g2d = new_bi.createGraphics();
AffineTransform at = g2d.getTransform();
AffineTransform rotation = new AffineTransform();
rotation.rotate(Math.PI / 2, bi.getWidth() / 2, bi.getHeight() / 2);


g2d.drawImage( bi,
                           bi.getWidth() / 2 - new_bi.getWidth() / 2,
                          (bi.getHeight() / 2 - new_bi.getHeight() / 2) * -1);


return new_bi;

The code that displays the new BufferedImage that is returned from the rotateImage method looks like this

private void displayBufferedImage(BufferedImage bif)
jpMainPanel = new JPanel();
jpMainPanel.setLayout(new BorderLayout());
setTitle("Display Program");

if(jlBackground != null)
//This doesn't help...but I tried it


jlBackground = new JLabel();

ImageIcon backImage = new ImageIcon(bif);

jlBackground.setBounds(0, 0, backImage.getIconWidth(), backImage.getIconHeight());
getLayeredPane().add(jlBackground, new Integer(Integer.MIN_VALUE));


//Tried all of the following help

I think if I can get the Rotate to work, then the scale will follow the same principle.

Can anyone help me cure my double vision please?
Offline trembovetski

Senior Devvie

If only I knew what I'm talking about!

« Reply #1 - Posted 2003-03-10 04:55:51 »

well, your rotation code works fine, so it must be that you're not using the updated image or something - probably a simple logic bug somewhere.

Also, you seem to be modifying swing components from a thread other than EventDispatchThread, which is a big no-no.

All you need to do is to set a new image to that JLabel, there's no need to remove/add it, and call all those repaint methods.
Offline darrinps

Senior Newbie

Java games rock!

« Reply #2 - Posted 2003-03-10 10:50:41 »

Thanks for the reply, but it brings up a quesiton:

Given that the application is single threaded, do you still think that invokeLater or invokeAndWait are needed to update the swing components?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline markuskidd

Junior Devvie

Medals: 1

« Reply #3 - Posted 2003-03-10 15:34:53 »

with swing -- yes. your code may only involve one thread, but there are plenty more running in the periphory (sp?)
Offline darrinps

Senior Newbie

Java games rock!

« Reply #4 - Posted 2003-03-11 11:51:38 »

That isn't the way I understood it.  I was always under the impression that you were safe in a single threaded application, and have never read anything that  mentions the use of invokeAndWait or invokeLater outside the context of a multi-threaded application.

Yes, there are multiple threads being run in a single threaded app, but these, it is my understanding, do not mess with the EventDispatch thread to cause it problems.

Thanks for setting me straight!  

Still, can someone point me to some documentation  (I looked on the Net, but couldn't find any) that says that all references to a Swing component to do things like show() must be done using invokeAndWait or invokeLater for a single threaded application?  I'd like to read it for myself to understand what is going on to require this in a single threaded application.


BTW, I've never had a problem with this, but then again I normally use multi-threaded apps too!
Offline markuskidd

Junior Devvie

Medals: 1

« Reply #5 - Posted 2003-03-11 13:45:16 »


Swing components can be accessed by only one thread at a time, generally the event-dispatching thread. Thus, the single-thread rule is as follows.

Rule: Once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread.

This rule might sound scary, but for many simple programs, you don't have to worry about threads.
Offline darrinps

Senior Newbie

Java games rock!

« Reply #6 - Posted 2003-03-11 15:10:49 »


I read that one years ago but I assumed at the time that it applied to multi-threaded apps since it was titled "Threads and Swing".
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

BurntPizza (9 views)
2015-10-07 02:11:23

KaiHH (13 views)
2015-10-06 20:22:20

KaiHH (11 views)
2015-10-06 19:41:59

BurntPizza (23 views)
2015-10-06 19:04:48

basil_ (44 views)
2015-09-30 17:04:40

shadowstryker (22 views)
2015-09-29 15:55:06

TheSpaceHedgehog (28 views)
2015-09-29 01:58:48

GamerC4 (54 views)
2015-09-24 21:10:38

GamerC4 (71 views)
2015-09-24 21:09:48

htuy (28 views)
2015-09-24 04:57:24
Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!