Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 11, 2009, 07:06 PM   #1
alumac
macrumors member
 
Join Date: Mar 2009
How to paintImmediately() in Java?

Hello,

I'm developing a Slide Image Puzzle in Java. I'm randomly shuffling the Image in a for loop and I want the Image to be painted on the screen after each iteration of the for loop. Using repaint() doesn't work so I'm wondering how I can implement paintImmediately() to do this.

Here's a simplified version of my program structure

1. ImageGame class - main app

Code:
class ImageGame{
public static void main() {
//creates and displays a new ImageFrame (extends JFrame)
}
}
2. ImageFrame class - JFrame

Code:
class ImageFrame extends JFrame{
ImagePanel panel = new ImagePanel(); //creates panel //has a menu with options "loadImage" and "shuffleImage"
public void actionPerformed(ActionEvent evt) { Object source = evt.getSource();
if(source==loadimage) { panel.loadimage(); }
if(source==shuffleimage) { panel.ShuffleImage(); }
}
}
3. ImagePanel class - JPanel

Code:
class ImagePanel extends JPanel{
//setting up the Panel
public void paintComponent(Graphics g) //works fine { super.paintComponent(g); if (image != null) { g.drawImage(image, 0, 0, null); } }
public void loadimage() { //code to load the image }
public void shuffleImage()
{
for(i=0;i<x;i++) { //swap adjacent x and x-1 repaint() Thread.sleep(delay); }
}
}
The repaint() doesn't work in the loop because Java only performs the repaint() at the last iteration. Therefore, I want to use paintImmediately() to paint the panel at every iteration so that the user can see how the tiles are being swapped. I understand that paintImmediately() can only be called from the EDT (event dispatch thread) so my guess is that it needs to be in the ImageFrame() class in the ActionPerformed(). I can't figure out how to do this!

Does anyone know how it can be implemented?

Thanks in advance!
__________________
 Macbook CE  iPhone 3G  iPod (4th generation - still works!!)  iPod Shuffle (3rd generation)
alumac is offline   0 Reply With Quote
Old Apr 12, 2009, 05:07 AM   #2
alumac
Thread Starter
macrumors member
 
Join Date: Mar 2009
figured it out

I figured it out. If the shuffle function runs in a seperate thread, then the repaint() will be synchronous (basically it works!)

So I just changed a little bit of code.

When calling panel.shuffle() in class ImageFrame, I did the following..

Code:
new Thread(
  new Runnable() {
	public void run() {
		panel.shuffle ();
	}
  }
).start();
Then, I simply added Thread.sleep() to in the shuffle function and voila. Probably not the most efficient way of doing this but its what I need at the moment.
__________________
 Macbook CE  iPhone 3G  iPod (4th generation - still works!!)  iPod Shuffle (3rd generation)
alumac is offline   0 Reply With Quote
Old Apr 12, 2009, 05:10 AM   #3
garethlewis2
macrumors 6502
 
Join Date: Dec 2006
Just invalidate the rect. It will redraw itself immediately.
garethlewis2 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 07:56 AM
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 03:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 04:49 PM

Forum Jump

All times are GMT -5. The time now is 10:52 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC