Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,465 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,558 people online right now. Registration is fast and FREE... Join Now!




How do I separate my functions and call them in an applet?

 
Reply to this topicStart new topic

How do I separate my functions and call them in an applet?

nombre
2 Jul, 2008 - 07:22 AM
Post #1

New D.I.C Head
*

Joined: 7 Jun, 2008
Posts: 49

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
User is offlineProfile CardPM
+Quote Post

nombre
RE: How Do I Separate My Functions And Call Them In An Applet?
2 Jul, 2008 - 09:46 AM
Post #2

New D.I.C Head
*

Joined: 7 Jun, 2008
Posts: 49

I got my code closer to what I want it to be. Now it doesn't display anything tho. Can someone tell me what I'm doing wrong?

CODE
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;
        char x=' ';
        char y=' ';
        String state;
        try
        {
            url = new URL("ftp://example");
        }
        catch(MalformedURLException e){e.printStackTrace();}
        while(true){
        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());
          x = strBuff.charAt(5);
          System.out.println(x);
          state = strBuff.substring(0, 3);
          if(state!="LIVE"){break;}
          if(x==y){continue;}
          y=x;
          
          }
          catch(IOException e){
            e.printStackTrace();
          }
          
        
        
      
        try
        {
            url2 = new URL("http://example"+x+".jpg");
        }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();
    }

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:53PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month