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

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


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

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

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

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
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..

new Thread(
  new Runnable() {
	public void run() {
		panel.shuffle ();
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
macrumors 6502
Join Date: Dec 2006
Just invalidate the rect. It will redraw itself immediately.
garethlewis2 is offline   0 Reply With Quote

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
How to convince my parents to let me buy a 17" MacBook Pro Ulf1103 Buying Tips and Advice 58 Nov 27, 2013 11:45 AM
Tutorial: How to get PCSX2 to run on Mac (without BootCamp) Mirrors Mac Applications and Mac App Store 22 Jun 14, 2013 08:19 PM
IPHONE 4 how to JAILBREAK AND UNLOCK bs2511feist Jailbreaks and iOS Hacks 6 Apr 14, 2012 05:52 PM
Does anybody know how to get TweetTask? xmelissaxmayhem Jailbreaks and iOS Hacks 3 Jul 3, 2011 01:21 AM
[HOW TO] DIY iPhone 4 Swivel Car Mount QuarterSwede iPhone Accessories 13 Mar 8, 2011 02:51 PM

All times are GMT -5. The time now is 06:42 AM.

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

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