Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Slideshow problems

Status
Not open for further replies.

kosmokramer

Programmer
Sep 8, 2002
73
US
Hey everyone. You'll have to forgive me if I made some dumb mistakes. It has been a while since I did anything with Java. Anyways, I can't seem to figure out what is wrong with this program.
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class rotatePictures extends Applet implements ActionListener
{
	private Button back,forward;
	private int [] imageArray;
	private int index = 0;
	private Image image;

	public void init()
	{
		back = new Button("Back");
		add(back);
		back.addActionListener(this);

		forward = new Button("Forward");
		add(forward);
		forward.addActionListener(this);
		
		imageArray = getParameterValues("loadPictures");
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == back)
		{
			if (index!=0)
			{
				index--;
				image = getImage(getDocumentBase(), imageArray[index]);
			}
			else
			{
				index = imageArray.length;
				image = getImage(getDocumentBase(), imageArray[index]);
			}
		}
		else if (e.getSource() == forward)
		{
			if (index!=imageArray.length)
			{
				index++;
				image = getImage(getDocumentBase(), imageArray[index]);
			}
			else
			{
				index = 0;
				image = getImage(getDocumentBase(), imageArray[index]);
			}
		}
		repaint();
	}
	public void paint(Graphics g)
	{
		g.drawImage(image, 50, 50, 150, 150, this);
	}
};

What I want to do is create a simple applet that will take a parameter list of images from a webpage and create a slideshow. I will probably change the layout and stuff like that, but right now I am just trying to get it working!!

Thanks ahead of time,
Paul
 
here is my code and html
// I use Toolkit to run my applet at home, you can change it back to getImage(getDocumentBase(), imageArray[index]);
// after you upload the class to the server
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class rotatePictures extends Applet implements ActionListener
{
private Button back,forward;
private String [] imageArray;
private int index = 0;
private Image image;
private int iCount = 1;
Toolkit tk;
public void init()
{
back = new Button("Back");
add(back);
back.addActionListener(this);

forward = new Button("Forward");
add(forward);
forward.addActionListener(this);
iCount = Integer.parseInt(getParameter("imageCount"));
imageArray = new String[iCount];
for (int i=1; i<=iCount; i++)
{
imageArray[i-1] = getParameter((&quot;loadPictures&quot;+i));
}
tk = Toolkit.getDefaultToolkit() ;
image = tk.getImage(imageArray[0]);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == back)
{
if (index!=0)
{
index--;
image = tk.getImage(imageArray[index]);
System.out.println(index);
}
else
{
index = (imageArray.length-1);
image = tk.getImage(imageArray[index]);
//image = getImage(getDocumentBase(), imageArray[index]);
System.out.println(index);
}
}
else if (e.getSource() == forward)
{
if (index<(imageArray.length-1))
{
index++;
image = tk.getImage(imageArray[index]);
System.out.println(index);
}
else
{
index = 0;
image = tk.getImage(imageArray[index]);
//image = getImage(getCodeBase(),imageArray[index]);
System.out.println(index);
}
}
repaint();
}
public void paint(Graphics g)
{
g.drawImage(image, 50, 50, 150, 150, this);
}
};

//html
<html>
<head>
</head>
<body>
<APPLET CODE=rotatePictures.class HEIGHT=400 WIDTH=400>
<PARAM NAME=imageCount VALUE=2>
<PARAM NAME=loadPictures1 VALUE=1.jpg>
<PARAM NAME=loadPictures2 VALUE=2.jpg>
</APPLET>
</body>
</html>
 
Alright. I got it working (thanks!), but now I am having another problem. I am trying to customize the size of the image for each picture so that they don't all show up the same size. I can get it to display one or two pictures, but it is very erratic in its behavior.

Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class rotatePictures extends Applet implements ActionListener
{
    private Button back,forward;
    private String [] imageArray;
    private int index = 0;
    private Image image;
    private int iCount = 1; 
   
    public void init()
    {
        setLayout(new BorderLayout());
		
		Panel bottom = new Panel();
		back = new Button(&quot;Back&quot;);
		forward = new Button(&quot;Forward&quot;);
        
		bottom.add(back);
		bottom.add(forward);
        add(&quot;South&quot;, bottom);
		
		back.addActionListener(this);
        forward.addActionListener(this);
		
        iCount = Integer.parseInt(getParameter(&quot;imageCount&quot;));
        imageArray = new String[iCount];
		
        for (int i=1; i<=iCount; i++)
        {
			imageArray[i-1] = getParameter((&quot;loadPictures&quot;+i));
        }
		
		image = getImage(getDocumentBase(),imageArray[index]);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == back)
        {
            if (index!=0)
            {
                index--;
				image = getImage(getDocumentBase(),imageArray[index]);
				//int someWidth = Integer.parseInt(getParameter(&quot;width&quot;+index++));
				//int someHeight = Integer.parseInt(getParameter(&quot;height&quot;+index++));
				//int centeringNumb = (400 - someWidth)/2;
				System.out.println(index);
				//String newString = Integer.toString(centeringNumb);
				//showStatus(newString);
				//repaint(0,centeringNumb,50,someWidth,someHeight);
            }
            else
            {
                index = (imageArray.length-1);
                image = getImage(getDocumentBase(), imageArray[index]);
				//int someWidth2 = Integer.parseInt(getParameter(&quot;width&quot;+index++));
				//int someHeight2 = Integer.parseInt(getParameter(&quot;height&quot;+index++));
				//int centeringNumb2 = (400 - someWidth2)/2;
				System.out.println(index);
				//String newString2 = Integer.toString(centeringNumb2);
				//showStatus(newString2);
				//repaint(0,centeringNumb2,50,someWidth2,someHeight2);
            }
        }
        else if (e.getSource() == forward)
        {
            if (index<(imageArray.length-1))
            {
                index++;
				image = getImage(getDocumentBase(),imageArray[index]);
				//int someWidth3 = Integer.parseInt(getParameter(&quot;width&quot;+index++));
				//int someHeight3 = Integer.parseInt(getParameter(&quot;height&quot;+index++));
				//int centeringNumb3 = (400 - someWidth3)/2;
				System.out.println(index);
				//String newString3 = Integer.toString(centeringNumb3);
				//showStatus(newString3);
				//repaint(0,centeringNumb3,50,someWidth3,someHeight3);
            }
            else
            {
                index = 0;
                image = getImage(getDocumentBase(),imageArray[index]);
				//int someWidth4 = Integer.parseInt(getParameter(&quot;width&quot;+index++));
				//int someHeight4 = Integer.parseInt(getParameter(&quot;height&quot;+index++));
				//int centeringNumb4 = (400 - someWidth4)/2;
				System.out.println(index);
				//String newString4 = Integer.toString(centeringNumb4);
				//showStatus(newString4);
				//repaint(0,centeringNumb4,50,someWidth4,someHeight4);
            }
        }
        repaint();
    }
	
    public void paint(Graphics g)
    {
		int someWidth = Integer.parseInt(getParameter(&quot;width&quot;+index++));
		int someHeight = Integer.parseInt(getParameter(&quot;height&quot;+index++));
		int centeringNumb = (400 - someWidth)/2;
		showStatus(&quot;width: &quot; +Integer.toString(someWidth)+ &quot; height: &quot; + Integer.toString(someHeight) + &quot; centeringNumb: &quot; + Integer.toString(centeringNumb));
        g.drawImage(image, centeringNumb, 50, someWidth, someHeight, this);
    }
};

here's my html page:
Code:
<html>
<body>
<applet code=&quot;rotatePictures.class&quot; height=&quot;400&quot; width=&quot;400&quot;>
<Param Name=&quot;imageCount&quot; value=&quot;4&quot;>
<Param Name=&quot;loadPictures1&quot; value=&quot;1.jpg&quot;>
<Param Name=&quot;width1&quot; value=&quot;180&quot;>
<Param Name=&quot;height1&quot; value=&quot;225&quot;>
<Param Name=&quot;loadPictures2&quot; value=&quot;2.jpg&quot;>
<Param Name=&quot;width2&quot; value=&quot;138&quot;>
<Param Name=&quot;height2&quot; value=&quot;232&quot;>
<Param Name=&quot;loadPictures3&quot; value=&quot;3.jpg&quot;>
<Param Name=&quot;width3&quot; value=&quot;180&quot;>
<Param Name=&quot;height3&quot; value=&quot;225&quot;>
<Param Name=&quot;loadPictures4&quot; value=&quot;4.jpg&quot;>
<Param Name=&quot;width4&quot; value=&quot;180&quot;>
<Param Name=&quot;height4&quot; value=&quot;215&quot;>
</applet>
</body>
</html>

 
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class rotatePictures2 extends Applet implements ActionListener
{
private Button back,forward;
private String [] imageArray;
private int index = 0, index2 = 0;
private Image image;
private int iCount = 1;
Toolkit tk;

public void init()
{
setLayout(new BorderLayout());

Panel bottom = new Panel();
back = new Button(&quot;Back&quot;);
forward = new Button(&quot;Forward&quot;);

bottom.add(back);
bottom.add(forward);
add(&quot;South&quot;, bottom);

back.addActionListener(this);
forward.addActionListener(this);

iCount = Integer.parseInt(getParameter(&quot;imageCount&quot;));
imageArray = new String[iCount];

for (int i=0; i<iCount; i++)
{
imageArray = getParameter((&quot;loadPictures&quot;+i));
}
tk = Toolkit.getDefaultToolkit() ;
image = tk.getImage(imageArray[0]);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == back)
{
index--;
if (index==-1)
{
index=(imageArray.length-1);
}
image = tk.getImage(imageArray[index]);
}
else if (e.getSource() == forward)
{
index++;
if (index==imageArray.length)
{
index=0;
}
image = tk.getImage(imageArray[index]);
}
repaint();
}

public void paint(Graphics g)
{
if (index==(imageArray.length-1))
index2 = 0;
else
index2 = index + 1;

int someWidth = Integer.parseInt(getParameter(&quot;width&quot;+index2));
int someHeight = Integer.parseInt(getParameter(&quot;height&quot;+index2));
int centeringNumb = (400 - someWidth)/2;
showStatus(&quot;width: &quot; +Integer.toString(someWidth)+ &quot; height: &quot; + Integer.toString(someHeight) + &quot; centeringNumb: &quot; + Integer.toString(centeringNumb));
g.drawImage(image, centeringNumb, 50, someWidth, someHeight, this);
}
}
//html image name start from 0
<html>
<body>
<applet code=&quot;rotatePictures2.class&quot; height=&quot;400&quot; width=&quot;400&quot;>
<Param Name=&quot;imageCount&quot; value=&quot;3&quot;>
<Param Name=&quot;loadPictures0&quot; value=&quot;0.jpg&quot;>
<Param Name=&quot;width0&quot; value=&quot;180&quot;>
<Param Name=&quot;height0&quot; value=&quot;225&quot;>
<Param Name=&quot;loadPictures1&quot; value=&quot;1.jpg&quot;>
<Param Name=&quot;width1&quot; value=&quot;138&quot;>
<Param Name=&quot;height1&quot; value=&quot;232&quot;>
<Param Name=&quot;loadPictures2&quot; value=&quot;2.jpg&quot;>
<Param Name=&quot;width2&quot; value=&quot;138&quot;>
<Param Name=&quot;height2&quot; value=&quot;232&quot;>
</applet>
</body>
</html>
 
Hey. Thanks for all your help, but while I was waiting, I figured it out on my own. Actually, I was able to do it without all the height and width parameters (I just used image.getHeight(this) and image.getWidth(this) and omitted the width and height from the drawImage in the paint method. It made it much less cluttered...always a good thing!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top