Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to show pics from array.  RSS feed

 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program which responds to button presses with a series of sounds and associated pictures, each with it's own pause duration.

The problem was that no pictures would be displayed until the code exited the button's action listener thread.

As such someone suggested creating an animator control - basically a label with a timer and an associated array of images - they even wrote the class for me.

Unfortunately I'm such a novice I still can't seem to get a basic working example i.e. in the following code no images are shown.

Can anyone see why?

Tower Class is the main form with button action listener.
Animator Class is the extended label with Timer.

ProcessBtn simply generates an array of pics and images, but has been omitted as it is fairly lengthy.



EDIT by mw: Added Code Tags.
[ December 08, 2006: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like the body of ProcessButton should execute as its own thread. Basically, whatever ProcessButton does could be moved into the run method of a Runnable. Then calling ProcessButton would create a new thread using that Runnable, and call start.

(Note: By convention, Java method names begin with lowercase, so it would be a little more clear if this were "processButton.")
 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still struggling with threads ... I Tried moving the inner workings into a thread but still no joy

In the following example I get the messages:
Before Test Thread
Outside TestThread
I get a 4 second pause then
C:/bazaar.jpgBefore PauseAfter PauseC:beast.jpgBefore PauseAfter PauseC:bazaar.jpgAfter Test Thread

Only the last image change is effective i.e. the screen is still not aware of changes until the thread finishes.

That's why someone suggested the Animator which now works

Would still like to know why the thread doesn't change the image though.



private JPanel CreateTowerButtons(){

final JButton[] towerButtons = new JButton[btnCaptions.length];

for (int buttonNo=0;buttonNo<towerButtons.length; ++buttonNo) {
final int buttonNo2 = buttonNo;
towerButtons[buttonNo] = new JButton(btnCaptions[buttonNo]);

towerButtons[buttonNo].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tc.pressOK) {
System.out.println("Before Test Thread");

Thread t = new Thread() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestThread();
System.out.println("After Test Thread");
}
});
}
};
t.start();
}

System.out.println("Outside TestThread");
}
});
}
return createPane("Whatever", towerButtons);
}




final void TestThread() {
Tower.picLabel.setIcon(new ImageIcon("C:/beast.jpg"));
Tower.picLabel.repaint();

System.out.print("C:/bazaar.jpg");
Tower.picLabel.setIcon(new ImageIcon("C:/bazaar.jpg"));
Tower.picLabel.repaint();

System.out.print("Before Pause");
tc.Pause(2);
System.out.print("After Pause");

System.out.print("C:beast.jpg");
Tower.picLabel.setIcon(new ImageIcon("C:/beast.jpg"));
Tower.picLabel.repaint();

System.out.print("Before Pause");
tc.Pause(2);
System.out.print("After Pause");

System.out.print("C:bazaar.jpg");
Tower.picLabel.setIcon(new ImageIcon("C:/bazaar.jpg"));
Tower.picLabel.repaint();
}


public void Pause(long s) {
try {
Thread.currentThread().sleep(s * 1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show how you tried to move it to a Thread?
 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, still early days for me.

The last example shows what I tried to do i.e. run the method TestThread through a thread created in the buttons' action listener, but as I said no screen repainting until this thread completes.

Regards

Paul.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried removing the invokeLater call and just have as the body of the run method the call to the method that displays the images?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!