Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Passing images to an applet with getParameter.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I am writing an applet that will allow the user to display images either via a JTextField or as a parameter in an HTML file. Here is the current code:
<code>
// Package import statements.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
// Main method declaration. Inherits from JApplet and implements
// the ActionListener interface.
public class Assign11 extends JApplet
implements ActionListener
{
// Declare 4 private instance variables.
private ImagePanel imagePanel;
private JTextField filename;
private JButton display;
private JPanel myPanel;
private String firstImage,
secondImage;

// Initialize the applet.
public void init()
{
// Instantiate four graphical components. The JTextField
// has a maximum width of 25 characters and the JButton
// has the enclosed string displayed on it.
imagePanel = new ImagePanel();
filename = new JTextField( 25 );
display = new JButton( "Display Image" );
myPanel = new JPanel();
firstImage = getParameter("image1");
secondImage = getParameter("image2");

// Set the new layout manager. Add a JLabel to the panel
// in the Western side. It is identified by the string 'Image'.
// Add both the JTextField and the file name onto the JPanel.
myPanel.setLayout( new BorderLayout() );
myPanel.add( new JLabel( "Image: " ), BorderLayout.WEST );
myPanel.add( filename, BorderLayout.CENTER );
myPanel.add( display, BorderLayout.EAST );
// Add the imagePanel and the JPanel to the contentPane.
getContentPane().add( imagePanel, BorderLayout.CENTER );
getContentPane().add( myPanel, BorderLayout.SOUTH );
// Create a red lined border around the imagePanel
imagePanel.setBorder( new LineBorder( Color.white, 1 ));
// 'Enable' the action listener for the JButton
// component.
display.addActionListener( this );
}
// Define the actionPerformed method. Call the displayImage
// method if either the JButton or JTextField is utilized in
// the input of the image file.
public void actionPerformed( ActionEvent e )
{
if( ( e.getSource() instanceof JButton ) )
displayImage();

}

// displayImage method. Remove whitespace from the JTextField
// before obtaining the input w/ the getText method. Retrieve
// the image and store it in 'image'.
private void displayImage()
{
Image image = getImage( getCodeBase(), filename.getText().trim() );

// Display the image.
imagePanel.showImage( image );

}
} // End of the main method.
// ImagePanel class. Inherits from JPanel.
class ImagePanel extends JPanel
{
// Define and initialize a private instance variable called
// image that is a reference to an instance of Image.
private Image image = null;
// Default constructor.
public ImagePanel()
{
}
// showImage method. Receives an Image object as a parameter.
public void showImage( Image image )
{
// Assign the parameter to the local instance variable.
this.image = image;
// Call the repaint method.
repaint();
}
// paintComponent method. Receives a Graphics object as a
// parameter and assigns it to the reference variable 'g'.
public void paintComponent( Graphics g )
{
// Call the paintComponent method in the base class and pass
// it the graphics object as a parameter
super.paintComponent( g );
// Ascertain that the image was obtained.
if( image != null )
// Draw the image on the applet after obtaining the necessary
// dimensions and originating class.
g.drawImage( image, 0, 0, getWidth(), getHeight(), this );
}
} // End of ImagePanel class.
</code>
I have never worked with getParameter before so I'm not where to place the code that will display the images(s) if passed as a parameter.
Thanks,
-CaitlinG
 
Are you here to take over the surface world? Because this tiny ad will stop you!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic