I am working an a basic java Applet slideshow and I am trying to set a textfield where a user can paste an image URL and this image is showed on the slideshow (and saved locally too).
I am stuck on how to get the value inserted in the text field (the URL) and use it on the piece of code that show and save the image.
The following is the applet code am I working on
package javaapplication7;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//==================================================================
// This applet presents a slide show of images.
//==================================================================
public class SlideShow extends Applet {
private AudioClip sound1;
private SlideShowGUI gui;
//-----------------------------------------------------------
// Creates the GUI.
//-----------------------------------------------------------
public void init () {
gui = new SlideShowGUI (this);
//-----------------------------------------------------------
// Making the audioclip playing in loop
//-----------------------------------------------------------
sound1 = getAudioClip(getCodeBase(), "sound1.wav");
sound1.play();
sound1.loop();
} // method init
} // class SlideShow
//==================================================================
// The GUI is made up of two primary panels, one for the images
// and one for the slide show controls.
//==================================================================
class SlideShowGUI {
private ImagePanel imagePanel;
private ControlPanel controlPanel;
//-----------------------------------------------------------
// Sets up the two primary panels.
//-----------------------------------------------------------
public SlideShowGUI (SlideShow app) {
app.setLayout (new BorderLayout());
app.setBackground (Color.white);
imagePanel = new ImagePanel(app);
app.add (imagePanel, "Center");
controlPanel = new ControlPanel(imagePanel);
app.add (controlPanel, "South");
app.setSize (700, 500);
} // constructor SlideShowGUI
} // class SlideShowGUI
//==================================================================
// The center panel contains the slide images.
//==================================================================
class ImagePanel extends Panel {
private CardLayout ourCardLayout;
private SlideShow applet;
private Slide s1, s2, s3;
//-----------------------------------------------------------
// Creates the slides and adds them to the card layout.
//-----------------------------------------------------------
public ImagePanel (SlideShow applet) {
this.applet = applet;
ourCardLayout = new CardLayout();
setLayout (ourCardLayout);
s1 = new Slide (applet, "pic1.jpg");
s2 = new Slide (applet, "pic2.jpg");
s3 = new Slide (applet, "pic3.jpg");
add (s1, "Pic1");
add (s2, "Pic2");
add (s3, "Pic3");
ourCardLayout.first (this);
setSize (700, 500);
} // constructor ImagePanel
//-----------------------------------------------------------
// Moves to the previous slide.
//-----------------------------------------------------------
public void goBack() {
ourCardLayout.previous (this);
} // method goBack
//-----------------------------------------------------------
// Moves to the next slide.
//-----------------------------------------------------------
public void goForward() {
ourCardLayout.next (this);
} // method goForward
} // class ImagePanel
//==================================================================
// Respresents one slide in the slide show.
//==================================================================
class Slide extends Canvas {
SlideShow applet;
Image image;
TextField inputBox;
//-----------------------------------------------------------
// Gets the image.
//-----------------------------------------------------------
public Slide (SlideShow applet, String filename) {
image = applet.getImage (applet.getDocumentBase(), filename);
setSize (700, 500);
} // constructor Slide
//-----------------------------------------------------------
// Draws the image.
//-----------------------------------------------------------
public void paint (Graphics page) {
page.drawImage (image, 0, 0, 700, 500, this);
String url = inputBox.getText();
} // method paint
} // class Slide
//==================================================================
// Creates the control panel of buttons.
//==================================================================
class ControlPanel extends Panel implements ActionListener{
private static ImagePanel imagePanel;
private static Button backButton;
private static Button forwardButton;
private static TextField inputBox;
private static Button loadButton;
//-----------------------------------------------------------
// Sets up all buttons and creates the player thread.
//-----------------------------------------------------------
public ControlPanel(ImagePanel imagePanel) {
this.imagePanel = imagePanel;
setBackground (Color.red);
backButton = new Button ("Previous image");
backButton.addActionListener (this);
forwardButton = new Button ("Next image");
forwardButton.addActionListener (this);
add (backButton);
add (forwardButton);
inputBox = new TextField("insert URL",40);
add (inputBox);
loadButton = new Button("Load");
loadButton.addActionListener(this);
add (loadButton);
} // constructor ControlPanel
//-----------------------------------------------------------
// The control panel also serves as the listener for all
// buttons. This method is invoked when any button is
// pressed.
//-----------------------------------------------------------
public void actionPerformed (ActionEvent event) {
Object obj = event.getSource();
if (obj == backButton)
imagePanel.goBack();
else if (obj == forwardButton)
imagePanel.goForward();
} // method actionPerformed
} // class ControlPanel
and this is the piece od code to retrieve and save the image
try {
URL url = new URL("http://www.iki.rssi.ru/IPL/show14.gif");
image = ImageIO.read(url);
ImageIO.write(image, "gif",new File("C:/xampp/htdocs/img/show14.gif"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
public void paint(Graphics g) {
g.drawImage(picture, 20 ,30, this);
}
So in the slideshow window on the bottom there is the text field to paste the URL and a "load" button to perform the action.
Screenshot textfield and button for action
Any hints?