Skip to main content

So close to working SlideShow, Problem description and Code

1 reply [Last post]
Joined: 2009-10-08
Points: 0

Hello to the community, I am a new member and I am very inexperienced with Java. I am writing a simple photo album application that will compile and run, but not really run how I would like it to. I am sure the code is not as pretty as more experienced Java folks are used to, but I am learning.

What I would like it to do is scroll the albumName and caption Strings to the left. When those two Strings reach the left edge I would like the current photo to begin scrolling left and out of the screen while the new image simultaneously begins to scroll left from just outside the right edge towards the left edge. When the new photo begins entering the screen the new albumName and caption strings appear at the original position at HCENTER and then begin scrolling left. I would like to make it as smooth as possible.

Am I in need of adding more methods to the code below, or is there a way to alter this code to get the desired results? Also, if this code is written badly, please let me know (maybe suggest improvements). I am new to programming in Java and j2me. Any advice and insight will be greatly appreciated

Best Regards,

Here is the code for the GameCanvas, let me know if the code for the MIDlet is needed. I removed most of the initializations and tried to leave the functional stuff.

Please let me know if there rules about maximum number of lines in posted code.

The code below is very close to performing as stated. The problem with it is the next image to be displayed does not scroll in from the right as the previous image scrolls out to the left, it just pops up in the middle of the screen.

<br />
<p>import javax.microedition.lcdui.*;<br />
<p>public class PhotoCanvas extends GameCanvas<br />
        implements CommandListener, Runnable {</p>
<p>	/******* Variable declarations removed to save space/time *******/</p>
<p>	/**<br />
         * constructor<br />
         */<br />
    public PhotoCanvas() {<br />
<p>        exitCommand = new Command("Exit", Command.EXIT, 0);<br />
        addCommand(exitCommand);<br />
<p>        initPhotos();<br />
        initParam();<br />
<p>    }</p>
<p>    public void initPhotos() {</p>
<p>	/******* Loads the images into the Image[] photo array and the captions into the String[] captions array. *******/</p>
<p>    }</p>
<p>    public void initParam() {<br />
        if (curSlide > 2) {<br />
            curSlide = 0;<br />
        }<br />
        nextStrX = getWidth() / 2;<br />
        nextImgX = getWidth() + (photo[curSlide].getWidth() / 2);</p>
<p>        if (firstRun == true) {<br />
            strX = getWidth() / 2;<br />
            imgX = getWidth() / 2;<br />
        } else if (firstRun == false) {<br />
            strX = getWidth() / 2;<br />
            imgX = getWidth() / 2 + (photo[curSlide].getWidth());<br />
        }<br />
        aniImage = false;<br />
        nextString = false;<br />
        change = false;<br />
<p>    public void start() {<br />
        if (animation == null) {<br />
            animation = new Thread(this);<br />
        }<br />
        animation.start();<br />
<p>    public void run() {<br />
        Graphics g = getGraphics();<br />
        try {<br />
            while (exitFlag == false) {<br />
                //if (firstRun == true) {<br />
                changeStrX();<br />
                if (aniImage) {<br />
<p>                }</p>
<p>                myPaint(g);<br />
                Thread.sleep(30);<br />
            }<br />
        } catch (Exception ex) {<br />
            System.out.println("Error in run()");<br />
            ex.printStackTrace();<br />
<p>    }</p>
<p>    /**<br />
     * paint<br />
     */<br />
    public void myPaint(Graphics g) {<br />
        //Clear Display<br />
        g.setColor(255, 255, 255);<br />
        g.fillRect(0, 0, getWidth(), getHeight());</p>
<p>        //Draw current Image according to index<br />
        g.drawImage(photo[curSlide], imgX, getHeight() / 2,<br />
                Graphics.VCENTER | Graphics.HCENTER);</p>
<p>        //Draw the current caption according to the index<br />
        g.setColor(255, 0, 0);<br />
        g.drawString(caption[curSlide], strX, 300,<br />
                Graphics.HCENTER | Graphics.BOTTOM);<br />
        g.drawString(albumName, strX, 0, Graphics.HCENTER |<br />
                Graphics.TOP);<br />
        if (nextString == true) {<br />
            g.drawString(albumName, nextStrX, 0, Graphics.HCENTER |<br />
                    Graphics.TOP);<br />
            g.drawString(caption[curSlide], nextStrX, 300, Graphics.HCENTER |<br />
                    Graphics.BOTTOM);<br />
<p>        if (nextImage == true) {<br />
            g.drawImage(photo[curSlide], imgX, getHeight() / 2,<br />
                    Graphics.VCENTER | Graphics.HCENTER);<br />
<p>        flushGraphics();</p>
<p>    }</p>
<p>    public void changeStrX() {<br />
        strX -= 2;<br />
        if (strX <= 0) {<br />
            imgX -= 2;<br />
            aniImage = true;<br />
            nextString = true;<br />
<p>    }</p>
<p>    public void changeImgX() {<br />
        imgX -= 2;</p>
<p>	/**<br />
         * If the left side of the image has reached<br />
         * the left edge of the screen<br />
         */<br />
        if (imgX + photo[curSlide].getWidth() / 2 <= 0) {<br />
            nextStrX -= 2;<br />
            curSlide += 1;<br />
            nextImage = true;<br />
            initParam();<br />
<p>    }<br />
}<br />
{code}<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-10-08
Points: 0