Forums Register Login

Passing images to an applet with getParameter.

+Pie Number of slices to send: Send
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
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1038 times.
Similar Threads
Problem with custom painting and multiple JPanel
Generat JAR file with resouces such as picture and music!
Image Display Error
Alignment of JTextArea in OverlayLayout
How to properly create an applet with an Input field and some shapes.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:27:29.