For the game I'm currently writing for Android devices, I've got a class called RenderView, which is a Thread and updates and renders everything. Occasionally the class logs the message "Game thread is only updating the update method and is not rendering anything". The game is running at 30 fps on my nexus s. And I get the message a couple of times throughout the session. Could someone tell me how I could optimize the class or if I'm forgetting something or that it's totally normal?
Here's my code:
public class RenderView extends SurfaceView implements Runnable {
public final String classTAG = this.getClass().getSimpleName();
Game game;
Bitmap framebuffer;
Thread gameloop;
SurfaceHolder holder;
boolean running;
int sleepTime;
int numberOfFramesSkipped;
long beginTime;
long endTime;
long lastTime;
int differenceTime;
int framePeriod;
Canvas canvas;
int frameCount;
WSLog gameEngineLog;
public RenderView(Game game, Bitmap framebuffer) {
super(game);
this.game = game;
this.framebuffer = framebuffer;
this.holder = getHolder();
framePeriod = 1000/game.getFramesPerSecond();
lastTime = System.currentTimeMillis();
gameEngineLog = game.getGameEngineLog();
}
#Override
public void run() {
while(running == true) {
if(holder.getSurface().isValid()) {
beginTime = System.currentTimeMillis();
numberOfFramesSkipped = 0;
game.getCurrentScreen().update();
game.getCurrentScreen().render(); // Draw out everything to the current virtual screen (the bitmap)
game.getGraphics().renderFrameBuffer(); // Actually draw everything to the real screen (combine both bitmaps)
canvas = holder.lockCanvas();
if(canvas != null) { // Fix for mysterious bug ( FATAL EXCEPTION: Thread)
// The viewing area of our virtual screen on our real screen
canvas.drawBitmap(framebuffer, null, game.getWSScreen().getGameScreenextendeddst(), null);
holder.unlockCanvasAndPost(canvas);
}
else {
gameEngineLog.e(classTAG, "Surface has not been created or otherwise cannot be edited");
}
endTime = System.currentTimeMillis();;
differenceTime = (int) (endTime - beginTime);
sleepTime = (int) (framePeriod - differenceTime);
if(sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
else {
while(sleepTime < 0 && numberOfFramesSkipped < game.getMaxFrameSkippes()) {
gameEngineLog.d(classTAG, "Game thread is only updating the update method and is not rendering anything");
try {
Thread.sleep(5);
}
catch (InterruptedException exception) {
exception.printStackTrace();
}
game.getCurrentScreen().update();
sleepTime += framePeriod;
numberOfFramesSkipped++;
}
}
// Frame Per Second Count
frameCount++;
if(lastTime + 1000 < System.currentTimeMillis()) {
game.getGameEngineLog().d(classTAG, "REAL FPS: " + frameCount);
lastTime = System.currentTimeMillis();
frameCount = 0;
}
}
}
}
public void resume() {
running = true;
gameloop = new Thread(this);
gameloop.start();
}
public void pause() {
running = false;
while(running == true) {
try {
gameloop.join();
running = false;
}
catch (InterruptedException e) {
}
}
gameloop = null;
}
}
Here's the code for the Graphics class (the getGraphics() just return an graphics object):
public class Graphics {
public final String classTAG = this.getClass().getSimpleName();
Game game;
Canvas frameBuffer;
Canvas canvasGameScreenextended;
Canvas canvasGameScreen; // Used for customeScreen
Bitmap gameScreenextended;
Bitmap gameScreen;
Rect gameScreendst;
Rect gameScreenextendeddst;
WSLog gameEngineLog;
Graphics(Game game, Bitmap framebuffer, Bitmap gameScreen) {
this.game = game;
// Initialize canvases to render to
frameBuffer = new Canvas(framebuffer);
canvasGameScreen = new Canvas(gameScreen);
// Initialize images to be rendered to our composition
this.gameScreen = gameScreen;
// Set up the Log
gameEngineLog = game.getGameEngineLog();
}
public void resetCanvasGameScreenextended() {
// This method has to be called each time the screen scaling type changes
canvasGameScreenextended = new Canvas(game.getWSScreen().getGameScreenextended());
gameScreenextended = game.getWSScreen().getGameScreenextended();
}
public Canvas getCanvasGameScreenextended() {
return canvasGameScreenextended;
}
public Canvas getCanvasGameScreen() {
return canvasGameScreen;
}
public void renderFrameBuffer() {
// Composition
// First layer (bottom)
frameBuffer.drawBitmap(gameScreen, null, game.getWSScreen().getGameScreendst(), null);
// Second layer (top)
frameBuffer.drawBitmap(gameScreenextended, null, game.getWSScreen().getGameScreenextendeddst(), null);
}
public void clearFrameBuffer() {
canvasGameScreen.drawColor(Color.BLACK);
//canvasGameScreenextended.drawColor(Color.BLACK);
gameScreenextended.eraseColor(Color.TRANSPARENT); // Make top layer transparent
}
}
Here's the code for the screen class (the getCurrentScreen() method returns a screen object):
public class Screen {
public final String classTAG = this.getClass().getSimpleName();
protected final Game game;
protected final Graphics graphics;
protected Screen(Game game) {
this.game = game;
this.graphics = game.getGraphics();
//game.getInput().reset();
}
public void update() {
}
public void render() {
}
/** Initialize all the sensory that should be used within this screen.*/
public void resume() {
}
public void pause() {
game.getInput().useAccelerometer(false);
game.getInput().useKeyboard(false);
game.getInput().useTouchscreen(false);
}
public void onDispose() {
game.getGraphics().clearFrameBuffer();
}
public void setScreenResizeType(int screenResizeType) {
}
The Screen class is extended and the render() method is shadowed with methods like:
graphics.getCanvasGameScreen().drawRect(play, red);
The funny thing is, when I override the render() method and don't place any code in it, the logger fires constantly with the message: "Game thread is only updating the update method and is not rendering anything". What kind of sorcery is this?!
Help is hugely appreciated!
As far as I understand from your updated post, there is no rendering problem actually. Instead, your code mistakenly prints that message.
This is because you check if(sleepTime > 0) , so if the rendering is very fast and sleepTime is zero, you get that message. Just change it to if(sleepTime >= 0).
Related
I am trying to render a loading animation on top of a nattable. I use the OverLayPainter mechanism to draw a "glasspane" and some text on top of the table, and this works perfect:
public class MessageOverlay
implements IOverlayPainter {
....
#Override
public void paintOverlay(final GC gc, final ILayer layer) {
this.currentGC = gc;
this.currentLayer = layer;
if (visible) {
currentGC.setAlpha(200);
currentGC.fillRectangle(0, 0, currentLayer.getWidth(), currentLayer
.getHeight());
drawMessage();
if (withLoadingAnimation) {
showAnimation = true;
}
} else {
showAnimation = false;
}
}
}
However, the paintOverlay method is not called regularely but rather everytime the table changes.
To be able to display a smoothe animation, I added a new thread
final Thread animatorThread = new Thread(new Runnable() {
#Override
public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(1000 / fps);
} catch (final InterruptedException e) {
break;
}
display.asyncExec(new Runnable() {
#Override
public void run() {
if (showAnimation && !currentGC.isDisposed()) {
final Image currentImage = getNextImage();
final int posX = currentGC.getClipping().width / 2
- currentImage.getBounds().width;
final int posY = currentGC.getClipping().height / 2
- currentImage.getBounds().height;
currentGC.drawImage(currentImage, posX, posY);
}
}
});
}
}
});
animatorThread.start();
As you can see it tries to access the graphics context this.currentGC set in the paintOverlay method. My problem is, that currentGC within the animatorThread is always disposed.
How can I a.) ensure that the context is not disposed in the thread or b.) solve this in an alternative way?
Thanks for the help.
You could try to create a new GC with the current NatTable instance and if needed pass the config from the passed in GC instance. Then you are in charge of disposing the GC instance and should not have the risk of a disposed GC outside your thread.
A simple example could look like the following snippet that simply shows the pane for 1000ms and then removes it again. You need of course to change the logic to be more dynamic with regards to your loading operation then:
AtomicBoolean paneThreadStarted = new AtomicBoolean(false);
...
natTable.addOverlayPainter(new IOverlayPainter() {
#Override
public void paintOverlay(GC gc, ILayer layer) {
if (this.paneThreadStarted.compareAndSet(false, true)) {
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
GC currentGC = new GC(natTable);
currentGC.setForeground(GUIHelper.COLOR_WHITE);
currentGC.setBackground(GUIHelper.COLOR_BLACK);
currentGC.setAlpha(200);
currentGC.fillRectangle(0, 0, layer.getWidth(), layer.getHeight());
String load = "Loading data ...";
Point textExtent = currentGC.textExtent(load);
currentGC.drawText(load,
layer.getWidth() / 2 - textExtent.x / 2,
layer.getHeight() / 2 - textExtent.y / 2,
true);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentGC.dispose();
natTable.redraw();
}
});
}
}
});
This way you are able to show the pane again by changing the AtomicBoolean from the outside:
Button showPaneButton = new Button(buttonPanel, SWT.PUSH);
showPaneButton.setText("Show Pane");
showPaneButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
this.paneThreadStarted.set(false);
natTable.redraw();
}
});
In my game the kid is collecting fruits but if he hits the rock 3 times or time is up before collecting the required points, the kid will jump to the half of the screen then fall down and start fail music then move to game over screen but I have two problems here:
when time is up the boy jump to top and continue out of the screen not the bottom and the music of fail is slow and choppy.
when kid hits rock 3 times it start fail music well and jump to the center of screen and fall down out of the screen from bottom and this is very good then go to game over screen, but when i go back from game over screen,the music of the Play screen is not working:
here is the code :
public boolean Rockhit()
{
if(Rocks.isRock) {
rocknum--;
Hud.collectorLives(rocknum);
Rocks.isRock=false;
if (rocknum == 0)
return true;
else
return false;
}
else
Hud.collectorLives(rocknum);
return false;
}
public State getState()
{
//Gdx.app.log(Float.toString(b2body.getLinearVelocity().x),"hi");
if ((Hud.getTime()==0&& Hud.getScore()<(level*30)+50)) {
Fruits.manager.get("music/Backmusic.ogg", Music.class).stop();
collectoIsDead=true;
Filter filter = new Filter();
filter.maskBits = Fruits.NOTHING_BIT;
for (Fixture fixture : b2body.getFixtureList())
fixture.setFilterData(filter);
b2body.applyLinearImpulse(new Vector2(0, 5f), b2body.getWorldCenter(), true);
//b2body.applyLinearImpulse(new Vector2(0,-2.5f), b2body.getWorldCenter(), true);
Fruits.manager.get("music/fail.mp3", Sound.class).play();
return State.DEAD;
}
if ( Rockhit()) {
Fruits.manager.get("music/Backmusic.ogg", Music.class).stop();
collectoIsDead=true;
Filter filter = new Filter();
filter.maskBits = Fruits.NOTHING_BIT;
for (Fixture fixture : b2body.getFixtureList())
fixture.setFilterData(filter);
b2body.applyLinearImpulse(new Vector2(0, 5f), b2body.getWorldCenter(), true);
//b2body.applyLinearImpulse(new Vector2(0,-2.5f), b2body.getWorldCenter(), true);
Fruits.manager.get("music/fail.mp3", Sound.class).play();
return State.DEAD;
}
if((Hud.getTime()==0&& Hud.getScore()>=(level*30)+50)|| (Hud.getScore()>=(level*30)+50)) {
Fruits.manager.get("music/Backmusic.ogg", Music.class).stop();
Fruits.manager.get("music/cheering.mp3", Sound.class).play();
return State.SUCCESS;
}
this is the code of gameover screen:
public class GameOverScreen implements Screen {
private Viewport viewport;
private Stage stage;
private Fruits game;
private float level;
public GameOverScreen(Fruits game,float level)
{
this.level=level;
this.game=game;
viewport=new FitViewport(Fruits.V_WIDTH,Fruits.V_HIEGT,new OrthographicCamera());
stage=new Stage(viewport,((Fruits) game).batch);
Label.LabelStyle font = new Label.LabelStyle(new BitmapFont(), Color.WHITE);
Table table=new Table();
table.center();
table.setFillParent(true);//the table will take all the stage
Label gameOverLabel = new Label("Game Over",font);
Label playAgainLabel = new Label("Click Here",font);
table.add(gameOverLabel).expandX();
table.row();
table.add(playAgainLabel).expandX().padTop(10f);// make it bellow Game over text by 10 pixels
stage.addActor(table);
}
#Override
public void render(float delta) {
if(Gdx.input.isTouched()) {
game.setScreen(new Worlds(game,level));
dispose();
}
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
}
#Override
public void show() {
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
here where I am processing the state of the kid in the play screen:
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
kid.draw(batch);
if(gameOver())//24
{
gameisover=true;
globalcounter--;
Gdx.app.log(Float.toString(globalcounter), "counter");
if(globalcounter==0) {
game.setScreen(new GameOverScreen(game, level));
dispose();
}
}
if(gameisover==true) {
globalcounter--;
Gdx.app.log(Float.toString(globalcounter), "counter");
if (globalcounter == 0) {
game.setScreen(new GameOverScreen(game, level));
dispose();
}
}
if(nextLevel())
{
prefs = Gdx.app.getPreferences("levels");
String name=prefs.getString("level", " ");
if(name==" ") {
prefs.putString("level", Float.toString(level + 1));
prefs.flush();
}
else {
prefs.putString("level", Float.toString(level+1));
prefs.flush();
}
game.setScreen(new Celebration(game,level+1));
dispose();
}
}
public boolean gameOver()//25
{
if(player.currentState== Collector.State.DEAD )// will show the game over screen after 3 seconds
{
return true;
}
return false;
}
public boolean nextLevel()//25
{
if(player.currentState== Collector.State.SUCCESS)// will show the game over screen after 3 seconds
{
return true;
}
return false;
}
Well, to start off with, your structure is flawed.
Your getState does not just return a state. A lot of things are happening before you return a state. You should really move those things and do them AFTER you have changed state.
I don't see a State.PLAYING? You only have 2 states DEAD and SUCCESS
You stop your background music:
Fruits.manager.get("music/Backmusic.ogg", Music.class).stop();
But it looks like you never start it again.
My code is used to draw an image onto the screen by g.draw(img);. Is there a way to make the image cycle through different images instead of being static? I've tried .gif files but they don;t work. Here is my code:
static BufferedImage img = null;
{
try {
img = ImageIO.read(new File("assets/textures/bird.png"));
} catch (IOException e) {
System.out.println(e.getMessage();
}
}
Is there a way to animate the textures?
You can create a small class to do this for you:
public class SimpleImageLoop {
private final BufferedImage[] frames;
private int currentFrame;
public SimpleImageLoop(BufferedImage[] frames) {
this.frames = frames;
this.currentFrame = 0;
}
/**
* Moves the loop to the next frame.
* If we are on the last frame, this loops back to the first
*/
public void nextFrame() {
this.currentFrame++;
if (this.currentFrame >= frames.length) {
this.currentFrame = 0;
}
}
/**
* Draws the current frame on the provided graphics context
*/
public void draw(Graphics g) {
g.draw(this.frames[this.currentFrame];
}
}
Then you need a simple animation loop to call through update() and draw():
final SimpleImageLoop imageLoop = new SimpleImageLoop(frames);
while (true) {
imageLoop.nextFrame();
imageLoop.draw(g);
}
If you need to smooth out the result, you can include additional parameters like how many loops should you perform, the time duration of the frames, etc.
I think I've got the jframe right in this program, but why is nothing appearing when I run it?
I have two different classes, here is my first. Just ignore the last method where I'm going to draw a rectangle with circles in it for a stoplight.
Here's my code.
package trafficlight;
import java.awt.Color;
import java.awt.Graphics;
public class TrafficLight {
private int goDuration;
private int stopDuration;
private int warnDuration;
public enum State {STOP, GO, WARN};
public Color GO_COLOR = Color.green;
public Color STOP_COLOR = Color.red;
public Color OFF_COLOR = Color.darkGray;
public Color WARNING_COLOR = Color.yellow;
private State currentState;
public TrafficLight() {
goDuration = 2;
stopDuration = 2;
warnDuration =1;
currentState = State.GO;
}
public void changeLight(){
if(currentState == State.GO){
currentState = State.WARN;
}
if(currentState == State.WARN){
currentState = State.STOP;
}
if(currentState == State.STOP){
currentState = State.GO;
}
}
public int getGoDuration() {
return goDuration;
}
public void setGoDuration(int goDuration) {
this.goDuration = goDuration;
}
public int getStopDuration() {
return stopDuration;
}
public void setStopDuration(int stopDuration) {
this.stopDuration = stopDuration;
}
public int getWarnDuration() {
return warnDuration;
}
public void setWarnDuration(int warnDuration) {
this.warnDuration = warnDuration;
}
public State getCurrentState() {
return currentState;
}
public void setCurrentState(State currentState) {
this.currentState = currentState;
}
public int getCurrentDuration(){
int duration = 0;
if (currentState == State.STOP){
duration = stopDuration;
}
if (currentState == State.GO){
duration = goDuration;
}
if (currentState == State.WARN){
duration = warnDuration;
}
return duration;
}
public void draw(Graphics canvas) {
canvas.drawRect(125,185,100,250);
canvas.drawOval(145,200,60,60);
canvas.drawOval(145,280,60,60);
canvas.drawOval(145,360,60,60);
if (currentState == State.STOP){
}
}
}
Here's my second class.
package trafficlight;
import java.awt.*;
import javax.swing.*;
public class TrafficLightDriver extends JFrame {
private static TrafficLight light;
public void message() {
}
public static void main(String[] args) {
TrafficLightDriver myFrame = new TrafficLightDriver();
int delay, answer;
String valueString;
do {
valueString = JOptionPane.showInputDialog("What is the green light delay? (1.. 10)");
light.setGoDuration(Integer.parseInt(valueString));
valueString = JOptionPane.showInputDialog("What is the yellow light delay? (1.. 10)");
light.setWarnDuration(Integer.parseInt(valueString));
valueString = JOptionPane.showInputDialog("What is the red light delay? (1.. 10)");
light.setStopDuration(Integer.parseInt(valueString));
for (int i = 1; i <= 10; i++) {
delay = light.getCurrentDuration();
Wait.manySec(delay);
light.changeLight();
myFrame.repaint();
}
answer = JOptionPane.showConfirmDialog(null, "Would you like to run the light again?",
null, JOptionPane.YES_NO_OPTION);
} while (answer == JOptionPane.YES_OPTION);
System.exit(0);
}
#Override
public void paint(Graphics canvas) {
light.draw(canvas);
}
public TrafficLightDriver() { //constructor
setSize(350, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
light = new TrafficLight();
setVisible(true);
}
}
here's my wait class
package trafficlight;
public class Wait {
public static void oneSec() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.err.println(e);
}
}
public static void manySec(long s) {
try {
Thread.sleep(s * 1000);
} catch (InterruptedException e) {
System.err.println(e);
}
}
public static void tenthOfSec(long s) {
try {
Thread.sleep(s * 100);
} catch (InterruptedException e) {
System.err.println(e);
}
}
}
You should avoid overriding paint of top level containers, instead, you should use something like JPanel and override it's paintComponent method.
The paint process is a chained series of methods, all which build ontop of each other to generate the output, the moment you break this change, you allow for introduction of artifacts and irregularities to appear. Make sure you are honouring the paint chain by always calling super.paintXxx
Swing is a single threaded framework. That is, there is a single thread that is responsible for processing all the events within the system, including repaint requests. This means that if you block this thread for any reason, you will prevent from processing any new events which will make you program appear as if it has hung. You are also required to ensure that any updates to the UI are made from within the context of this thread.
Start by having a read through
Concurrency in Swing
Performing Custom Painting
Painting in AWT and Swing
Now, I don't know how you Wait class works so I can't comment on that portion, but your TrafficLight isn't updating itself to reflect it's current state...
Updated...
You also have two main methods which is very confusing. The application logic appears to be in TrafficLightDriver, you should make sure you are running this class when you execute your program.
There is a logic problem in your changeLight method
public void changeLight(){
if(currentState == State.GO){
currentState = State.WARN;
}
if(currentState == State.WARN){
currentState = State.STOP;
}
if(currentState == State.STOP){
currentState = State.GO;
}
}
Basically, what this is says is...
if currentState is GO, set currentState to WARN...
if currentState is WARN, set currentState to STOP...
if currentState is STOP, set currentState to GO...
Given the fact that the default state is GO, when you call this method, the state will never be changed to anything other the GO. Instead, you should be using an if-else statement
public void changeLight() {
if (currentState == State.GO) {
currentState = State.WARN;
} else if (currentState == State.WARN) {
currentState = State.STOP;
} else if (currentState == State.STOP) {
currentState = State.GO;
}
}
Updated
Rendering the lights themselves comes a lot down to personal preferences, for example, I might be tempted to do something like....
switch (getCurrentState()) {
case GO:
canvas.setColor(GO_COLOR);
canvas.drawOval(145,360,60,60);
break;
case WARN:
canvas.setColor(WARNING_COLOR);
canvas.drawOval(145,280,60,60);
break;
case STOP:
canvas.setColor(STOP_COLOR);
canvas.drawOval(145,200,60,60);
break;
}
canvas.setColor(OFF_COLOR);
canvas.drawRect(125,185,100,250);
canvas.drawOval(145,200,60,60);
canvas.drawOval(145,280,60,60);
canvas.drawOval(145,360,60,60);
This will fill the light that is active, but then renders everything else over the top, so the light is always outlined
You call System.exit(). That kills the java VM and ends the program. Remove that line.
If you want to control the behavior of your program when the JFrame is closed use frame.setDefaultCloseOperation().
I am putting together a slideshow program that will measure a user's time spent on each slide. The slideshow goes through several different magic tricks. Each trick is shown twice. Interim images are shown between the repetition. Transition images are shown between each trick.
On the first repetition of a trick the JPanel color flashes on the screen after a click before the next image is shown. This doesn't happen during the second repetition of the same trick. It's possible that the image is taking too long to load.
Is there an easy way to pre-load the images so that there isn't a delay the first time through?
NOTE: Original code deleted.
EDIT 1/10/2013: This code now works on slower machines. trashgod's second addendum helped the most. The mouseClick control structure periodically asks SwingWorker classes to load 40 images or less of the current trick while also setting the used images to null. I have simplified my code down for this to just two Image[]s and added a main method so it stands alone. Images are still required to run though. This is now pretty bare bones code, and if you're trying to make a slideshow with a lot of images I think it would be a good place to start.
NOTE: I think I figured out how to properly implement SwingWorker while still using multiple Image[]s. trashgod and kleopatra is this implementation in-line with what you were suggesting? I didn't end up using publish and process since I couldn't figure out how to get that to work appropriately with an indexed array, but because the StringWorker doesn't load all images in the array (only 40), and the code calls StringWorker every 20 images, there should be a pretty good buffer.
EDIT 1/10/2013 Changed out MouseListener by instead extending MouseAdapter on my Mouse class. Also fixed my paintComponent method to include a call to super.paintComponent(g).
Added publish/process methods to my SwingWorker class ImageWorker. Added a wrapper class, ArrayWrapper to allow passing imageArray[i] and its corresponding index int i with publish to process.
package slideshow3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.List;
public class SlideShow3 extends JFrame
{
//screenImage will be replaced with each new slide
private Image screenImage;
private int width;
private int height;
//Create panel for displaying images using paintComponent()
private SlideShow3.PaintPanel mainImagePanel;
//Used for keybinding
private Action escapeAction;
//Image array variables for each trick
private Image[] handCuffs; //h
private Image[] cups; //c
//Used to step through the trick arrays one image at a time
private int h = 0;
private int c = 0;
//Used by timeStamp() for documenting time per slide
private long time0 = 0;
private long time1;
public SlideShow3()
{
super();
//Create instance of each Image array
handCuffs = new Image[50];
cups = new Image[176];
//start(handCuffsString);
start("handCuffs");
try
{
screenImage = ImageIO.read(new File("images/begin1.jpg"));
}
catch (IOException nm)
{
System.out.println("begin");
System.out.println(nm.getMessage());
System.exit(0);
}
/******************************************
* Removes window framing. The next line sets fullscreen mode.
* Once fullscreen is set width and height are determined for the window
******************************************/
this.setUndecorated(true);
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
width = this.getWidth();
height = this.getHeight();
//Mouse click binding to slide advance control structure
addMouseListener(new Mouse());
//Create panel so that I can use key binding which requires JComponent
mainImagePanel = new PaintPanel();
add(mainImagePanel);
/******************************************
* Key Binding
* ESC will exit the slideshow
******************************************/
// Key bound AbstractAction items
escapeAction = new EscapeAction();
// Gets the mainImagePanel InputMap and pairs the key to the action
mainImagePanel.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "doEscapeAction");
// This line pairs the AbstractAction enterAction to the action "doEnterAction"
mainImagePanel.getActionMap().put("doEscapeAction", escapeAction);
/******************************************
* End Key Binding
******************************************/
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run()
{
SlideShow3 show = new SlideShow3();
show.setVisible(true);
}
});
}
//This method executes a specific SwingWorker class to preload images
public void start(String e)
{
if(e.equals("handCuffs"))
{
new ImageWorker(handCuffs.length, h, e).execute();
}
else if(e.equals("cups"))
{
new ImageWorker(cups.length, c, e).execute();
}
}
//Stretches and displays images in fullscreen window
private class PaintPanel extends JPanel
{
#Override
public void paintComponent(Graphics g)
{
if(screenImage != null)
{
super.paintComponent(g);
g.drawImage(screenImage, 0, 0, width, height, this);
}
}
}
/******************************************
* The following SwingWorker class Pre-loads all necessary images.
******************************************/
private class ArrayWrapper
{
private int i;
private Image image;
public ArrayWrapper(Image image, int i)
{
this.i = i;
this.image = image;
}
public int getIndex()
{
return i;
}
public Image getImage()
{
return image;
}
}
private class ImageWorker extends SwingWorker<Image[], ArrayWrapper>
{
private int currentPosition;
private int arraySize;
private String trickName;
private Image[] imageArray;
public ImageWorker(int arraySize, int currentPosition, String trick)
{
super();
this.currentPosition = currentPosition;
this.arraySize = arraySize;
this.trickName = trick;
}
#Override
public Image[] doInBackground()
{
imageArray = new Image[arraySize];
for(int i = currentPosition; i < currentPosition+40 && i < arraySize; i++)
{
try
{
imageArray[i] = ImageIO.read(new File("images/" + trickName + (i+1) + ".jpg"));
ArrayWrapper wrapArray = new ArrayWrapper(imageArray[i], i);
publish(wrapArray);
}
catch (IOException e)
{
System.out.println(trickName);
System.out.println(e.getMessage());
System.exit(0);
}
}
return imageArray;
}
#Override
public void process(List<ArrayWrapper> chunks)
{
for(ArrayWrapper element: chunks)
{
if(trickName.equals("handCuffs"))
{
handCuffs[element.getIndex()] = element.getImage();
}
else if(trickName.equals("cups"))
{
cups[element.getIndex()] = element.getImage();
}
}
}
#Override
public void done()
{
try
{
if(trickName.equals("handCuffs"))
{
handCuffs = get();
}
else if(trickName.equals("cups"))
{
cups = get();
}
}
catch(InterruptedException ignore){}
catch(java.util.concurrent.ExecutionException e)
{
String why = null;
Throwable cause = e.getCause();
if(cause != null)
{
why = cause.getMessage();
}
else
{
why = e.getMessage();
}
System.err.println("Error retrieving file: " + why);
}
}
}
/******************************************
* End SwingWorker Pre-Loading Classes
******************************************/
//Prints out time spent on each slide
public void timeStamp()
{
time1 = System.currentTimeMillis();
if(time0 != 0)
{
System.out.println(time1 - time0);
}
time0 = System.currentTimeMillis();
}
/******************************************
* User Input Classes for Key Binding Actions and Mouse Click Actions
******************************************/
private class EscapeAction extends AbstractAction
{
#Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public class Mouse extends MouseAdapter
{
#Override
public void mouseClicked(MouseEvent e)
{
if(!(h<handCuffs.length) && !(c<cups.length))
{
timeStamp();
System.exit(0);
}
else if(h<handCuffs.length)
{
timeStamp();
screenImage = handCuffs[h];
repaint();
System.out.print("handCuffs[" + (h+1) + "]\t");
h++;
//purge used slides and refresh slide buffer
if(h == 20 || h == 40)
{
for(int i = 0; i < h; i++)
{
handCuffs[i] = null;
}
start("handCuffs");
}
if(h == 45)
{
start("cups");
}
}
else if(c<cups.length)
{
timeStamp();
screenImage = cups[c];
repaint();
System.out.print("cups[" + (c+1) + "]\t");
c++;
//purge used slides and refresh slide buffer
if(c == 20 || c == 40 || c == 60 || c == 80 || c == 100 || c == 120 || c == 140 || c == 160)
{
for(int i = 0; i < c; i++)
{
cups[i] = null;
}
start("cups");
}
}
}
}
/******************************************
* End User Input Classes for Key Binding Actions and Mouse Click Actions
******************************************/
}
This example uses a List<ImageIcon> as a cache of images returned by getImage(). Using getResource(), the delay is imperceptible. The next and previous buttons are bound to the Space key by default.
Addendum: You can control navigation by conditioning a button's setEnabled() state using an instance of javax.swing.Timer, for example.
Addendum: Your second example waits until the mouse is clicked to begin reading an image, an indeterminate process that may return a copy immediately or may not complete until after repaint(). Instead, begin reading the images in the background using ImageIO.read(), as shown here. You can process() your List<Image> and show progress, as seen here. The SwingWorker can be launched from the initial thread, running while you subsequently build your GUI on the EDT. You can display the first image as soon as it is processed.