I have this applet that I want to get information from the server about which picture to display. The loop I made a while loop only shows the last picture. How do I make sure that all the pictures are visible when they are supposed to be and how do I separate the functions so that I can call them in the end. I'm new to applets so please be patient with me.
CODE
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import javax.swing.*;
import java.applet.*;
import javax.imageio.*;
import java.io.*;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageObserver;
import javax.swing.ImageIcon;
public class applet extends JApplet
implements ActionListener {
ImageIcon icon=null;
URL url2=null;
private JButton exitButton = null;
private URL url = null;
JLabel label=null;
Container contentPane=null;
/**
* Noarg constructor
*/
public void init() {
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());
StringBuffer strBuff;
String line;
TextArea txtArea= new TextArea();
Image image=null;
File fima;
URL url2=null;
try
{
url = new URL("http://example");
}
catch(MalformedURLException e){e.printStackTrace();}
try{
InputStream in = url.openStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
strBuff = new StringBuffer();
while((line = bf.readLine()) != null){
strBuff.append(line + "\n");
}
// System.out.println(strBuff.toString());
txtArea.append(strBuff.toString());
}
catch(IOException e){
e.printStackTrace();
}
int x=0;
while(x<4)
{
try
{
url2 = new URL("ftp://example");
}catch (MalformedURLException e){e.printStackTrace();}
ImageIcon icon= new ImageIcon();
icon= new ImageIcon(Toolkit.getDefaultToolkit().createImage(url2));
// ---------------------------------------------------------------------
// Exit button
// ---------------------------------------------------------------------
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
// ---------------------------------------------------------------------
// Finally create a ImagePanel to display the image and exit button
// ---------------------------------------------------------------------
//ImagePanel i = new ImagePanel(image);
// txtArea.setSize(5, 5);*/
contentPane.add(new JLabel(icon), BorderLayout.CENTER);
contentPane.add(exitButton, BorderLayout.SOUTH);
contentPane.add(new JScrollPane(txtArea), BorderLayout.NORTH);
// i.setAlignmentY(JLabel.CENTER_ALIGNMENT);
repaint();
x++;}
// ------------------------------------------------------------
// Window listener to close application when Window gets closed
// ------------------------------------------------------------
}
public void start()
{
}
/**
* The action listener for user events like buttons.
* @param ae Event that was performed by the user
*/
public void actionPerformed(ActionEvent ae) {
String action = ae.getActionCommand();
if (action.equals("Exit")) {
System.out.println("Exiting.");
System.exit(0);
} else {
System.out.println(action);
}
}
class loop extends Container
{
}
/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {
applet mainFrame = new applet();
mainFrame.setVisible(true);
mainFrame.setSize(1000,800);
}
}
class ImagePanel extends JPanel {
Image image;
ImageObserver im;
public ImagePanel(Image image) {
this.image = image;
}
public void paintComponent(Graphics g) {
// paint background
super.paintComponent(g);
//Draw image at its natural size
g.drawImage(image, 0, 0, this);
}
}
This post has been edited by Amadeus: 9 Jul, 2008 - 05:42 PM