How to implement sleep to display a slideshow of images - java

Im working in Java Swing using JWindows to display images... Ive made different JLabels contain the images that I want to display.. Im removing previous added components and adding new ones to be displayed on the same JWindow.. The problem is as follows..
The code works perfectly without the sleep function. I can display all the images on different windows or on same windows upon event completion.. However when I use sleep, nothing at all gets displayed during this period...
Is there any way to implement a delay for the images like a slideshow, and have the images painted before the delay?
getContentPane().remove(startLabel);
getContentPane().add(recordLabel1, "Center");
setVisible(true);
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
getContentPane().remove(recordLabel1);
getContentPane().add(recordLabel2, "Center");
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
getContentPane().remove(recordLabel2);
getContentPane().add(recordLabel3, "Center");
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
getContentPane().remove(recordLabel3);
getContentPane().add(recordLabel4, "Center");
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
getContentPane().remove(recordLabel4);
getContentPane().add(pausedLabel, "Center");
setVisible(false);

This is a good time to use Swing Timers.
You should:
store your labels/pictures in an array rather than in different variables
set up a timer as indicated in the tutorial above
in the timer event, just rotate through your array of labels
All you need for that is an extra member in your class that stores what picture number you're currently displaying. When the timer fires, use that member to remove the current item from the pane, increase it (modulo the total number of elements you have), and insert the new one.

Related

Mouse-listener that stops a block from executing when the mouse is released

In Java, I need to integrate a mouse Listener that moves a motor. However here is the catch, there is no stop function. Thereby, the mousepressed should move the motor, while the mouseReleased should stop it, without actually sending a stop command but rather by stopping the mouse pressed block from executing. Any ideas on how this can be implemented? And is it possible to repeat the command under mousePressed as long as the mouse is pressed?
The following code is to illustrate my current program, which uses a stop function, which i would rather avoid.
n2Button.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
try {
// moveUp();
session.motion(0, 0, -500);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void mouseReleased(MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
try {
session.stop();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
You rather want to have mouse motion listener
[Example][1] [1]: https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html
And i also do not see the problem to have session.stop(); only the movement function i guess must be executed in another thread

How to add background music to gamescene in andengine

bg music plays on splash,menu and gamescene when i had put the below code, but i want to make bg music to play only on my gamescene, can you please help me. Thank you.
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException
{
MusicFactory.setAssetBasePath("mfx/");
try {
music = MusicFactory.createMusicFromAsset(mEngine
.getMusicManager(), this, "abcd.wav");
music.setLooping(true);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
{
music.play();
SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
}
I usually use music.pause() and music.resume() (because after stop the play does not work any more, I do not know why). When you leave a scene, you can call music.pause(). When you enter a scene, you can call music.resume();

SwingUtilities.invokeLater() displaying just during one frame

I'm using Swing in order to create a little java 2D game. I only try to display an image. Since i'm not on the EDT I'm using SwingUtilities.invokeLater() to do the stuff. When I use it, image is not displayed (in fact it's displayed during few miliseconds and disapear).
When I don't use SwingUtilities.invokeLater() the image is correctly displayed but I need to use invokeLater().
Here's my basic code :
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
#Override
public void run() {
final BreizhFrame frame = new BreizhFrame();
File imgFile = new File("src/test/lvl1-800x450.jpg");
System.out.println(String.valueOf(imgFile.exists()));
System.out.println(SwingUtilities.isEventDispatchThread());
Image i;
try {
i = ImageIO.read(imgFile);
frame.getGraphics().drawImage(i, 0, 0, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (InvocationTargetException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Any idea ?
Thanks.
That's not how you do it in Java. Yes, you are drawing the image, but after a while the control gets invalidated and it's re-drawn without your image.
This tutorial should help you

frame shows black screen

btnnew.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
System.out.println("Hello");
packetListener.listener();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
I get a black screen when it runs. But when the packetListener.listener(); calls in the constructor it shows.
Can you please explain why this is happening?
Code that is executed from a listener executes on the EDT. I'm guessing that the packetListner.listener() method blocks in which case the GUI will freeze. You should not be blocking the EDT.
Read the section from the Swing tutorial on Concurrency for a full description of this problem and a solution.
I think the packetListener.listener(); method performs some complex operation which blocks your UI.
Better create a thread for listening the packet.
ie,use it like this
try {
System.out.println("Hello");
new Thread(new Runnable() {
public void run() {
packetListener.listener();
}
}).start();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Hope this helps you

Changing color before removing button

I'm trying to create a button that changes color of the background and then removes itself from the JFrame after a set amount of time, but instead of changing color it just stays pressed for the duration of wait.
public void actionPerformed(ActionEvent e) {
setBackground(Color.red);
try{
Thread.sleep(10000);
}
catch (InterruptedException iE) {
}
frame.remove(this);
}
Can anyone see what im doing wrong?
Your sleep is occurring in the main UI thread, hence the reason the button just stays pressed. If you want a sleep you should create a new thread, get that to sleep, then from within that thread you can get the frame to remove the button.
new Thread() {
public void run() {
try {
Thread.sleep(10000);
// Now do what is needed to remove the button.
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();

Categories

Resources