Loading Images #Programming Code Examples #Java/J2EE/J2ME #Applets and Basic Graphics

JavaMan1.java Applet that loads an image from a relative URL.
*************************************************************
import java.applet.Applet;
import java.awt.*;

/** An applet that loads an image from a relative URL. 
 *
>>>>>>>>>>>>>>>>>>>

public class JavaMan1 extends Applet {
  private Image javaMan;

  public void init() {
    javaMan = getImage(getCodeBase(),"images/Java-Man.gif");
  }

  public void paint(Graphics g) {
    g.drawImage(javaMan, 0, 0, this);
  }
}
>>>>>>>>>>>>>>>>>>>>
JavaMan2.java Illustrates loading an image from an absolute URL.
********************
import java.applet.Applet;
import java.awt.*;
import java.net.*;

/** An applet that loads an image from an absolute
 *  URL on the same machine that the applet came from.
 *
***********************

public class JavaMan2 extends Applet {
  private Image javaMan;

  public void init() {
    try {
      URL imageFile = new URL("http://www.corewebprogramming.com" +
                              "/images/Java-Man.gif");
      javaMan = getImage(imageFile);
    } catch(MalformedURLException mue) {
      showStatus("Bogus image URL.");
      System.out.println("Bogus URL");
    }
  }

  public void paint(Graphics g) {
    g.drawImage(javaMan, 0, 0, this);
  }
}
>>>>>>>>>>>>>>>>>>>>>
# JavaMan3.java An application that loads an image from a local file. Uses the following image and two files:

    * Java-Man.gif which should be placed in images subdirectory.
    * WindowUtilities.java Simplifies the setting of native look and feel.
    * ExitListener.java WindowListener to support terminating the application.
*******************
JavaMan3.java
*******************
import java.awt.*;
import javax.swing.*;

/** An application that loads an image from a local file. 
 *  Applets are not permitted to do this.
 *
**********************

class JavaMan3 extends JPanel {
  private Image javaMan;

  public JavaMan3() {
    String imageFile = System.getProperty("user.dir") +
                       "/images/Java-Man.gif";
    javaMan = getToolkit().getImage(imageFile);
    setBackground(Color.white);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(javaMan, 0, 0, this);
  }
  
  public static void main(String[] args) {
    JPanel panel = new JavaMan3();
    WindowUtilities.setNativeLookAndFeel();
    WindowUtilities.openInJFrame(panel, 380, 390);
  }   
}
>>>>>>>>>>>>>>>>>>
Preload.java An application that demonstrates the effect of preloading an image before drawing. Specify -preload as a command-line argument to preload the image. In this case, the prepareImage method is called to immediately start a thread to load the image. Thus, the image is ready to display when the user later selects the Display Image button. 
>>>>>>>>>>>>>>>>>>>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;

/** A class that compares the time to draw an image preloaded
 *  (getImage, prepareImage, and drawImage) vs. regularly
 *  (getImage and drawImage).
 *  

* The answer you get the regular way is dependent on the * network speed and the size of the image, but if you assume * you load the applet "long" (compared to the time the image * loading requires) before pressing the button, the drawing * time in the preloaded version depends only on the speed of * the local machine. * ********************** public class Preload extends JPanel implements ActionListener { private JTextField timeField; private long start = 0; private boolean draw = false; private JButton button; private Image plate; public Preload(String imageFile, boolean preload) { setLayout(new BorderLayout()); button = new JButton("Display Image"); button.setFont(new Font("SansSerif", Font.BOLD, 24)); button.addActionListener(this); JPanel buttonPanel = new JPanel(); buttonPanel.add(button); timeField = new JTextField(25); timeField.setEditable(false); timeField.setFont(new Font("SansSerif", Font.BOLD, 24)); buttonPanel.add(timeField); add(buttonPanel, BorderLayout.SOUTH); registerImage(imageFile, preload); } /** No need to check which object caused this, * since the button is the only possibility. */ public void actionPerformed(ActionEvent event) { draw = true; start = System.currentTimeMillis(); repaint(); } // Do getImage, optionally starting the loading. private void registerImage(String imageFile, boolean preload) { try { plate = getToolkit().getImage(new URL(imageFile)); if (preload) { prepareImage(plate, this); } } catch(MalformedURLException mue) { System.out.println("Bad URL: " + mue); } } /** If button has been clicked, draw image and * show elapsed time. Otherwise, do nothing. */ public void paintComponent(Graphics g) { super.paintComponent(g); if (draw) { g.drawImage(plate, 0, 0, this); showTime(); } } // Show elapsed time in textfield. private void showTime() { timeField.setText("Elapsed Time: " + elapsedTime() + " seconds."); } // Time in seconds since button was clicked. private double elapsedTime() { double delta = (double)(System.currentTimeMillis() - start); return(delta/1000.0); } public static void main(String[] args) { JPanel preload; if (args.length == 0) { System.out.println("Must provide URL"); System.exit(0); } if (args.length == 2 && args[1].equals("-preload")) { preload = new Preload(args[0], true); } else { preload = new Preload(args[0], false); } WindowUtilities.setNativeLookAndFeel(); WindowUtilities.openInJFrame(preload, 1000, 750); } } < <<<<<<<<<<<<<<<<<

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10384
Categories:Programming Code Examples, Java/J2EE/J2ME, Applets and Basic Graphics
Tags:Java/J2EE/J2MEApplets and Basic Graphics
Post Data:2017-01-02 16:04:39

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada