Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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 1762 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(),
bi.getWidth(),
bi.getType());

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.transform(rotation);

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

g2d.setTransform(at);

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());
jpMainPanel.setOpaque(false);
setTitle("Display Program");


if(jlBackground != null)
{
//This doesn't help...but I tried it
getLayeredPane().remove(jlBackground);
getLayeredPane().repaint();

this.getContentPane().removeAll();
this.repaint();
}

jlBackground = new JLabel();

ImageIcon backImage = new ImageIcon(bif);

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

setContentPane(jpMainPanel);

//Tried all of the following too...no help
jlBackground.repaint();
getLayeredPane().repaint();
jpMainPanel.repaint();
this.repaint();
this.getContentPane().repaint();
}

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.

Thanks.

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 »

http://java.sun.com/docs/books/tutorial/uiswing/mini/threads.html

Quote

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 »

Thanks.  

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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!