BP Forums
Loading and Displaying Images - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+----- Forum: Code Snippets (https://bpforums.info/forumdisplay.php?fid=32)
+----- Thread: Loading and Displaying Images (/showthread.php?tid=763)



Loading and Displaying Images - brandonio21 - 03-21-2013

This is the source code for my Java tutorial discussing how to load and display images in a Java applet.

Here's the video:
<!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=oXmUp4ZTW2Q">http://www.youtube.com/watch?v=oXmUp4ZTW2Q</a><!-- m -->

Source code:
[code2=java]import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;

public class ImageLoadingTutorial extends Applet
{

private Image spiral = null;

public void paint(Graphics g)
{
this.setSize(640, 480);

if (spiral == null)
spiral = getImage("spiral.png");

Graphics2D g2 = (Graphics2D)g;
g2.drawImage(spiral, 25, 50, 25, 25, this);
}


public Image getImage(String path)
{
Image tempImage = null;
try
{
URL imageURL = ImageLoadingTutorial.class.getResource(path);
tempImage = Toolkit.getDefaultToolkit().getImage(imageURL);
}
catch (Exception e)
{
System.out.println("An error occured - " + e.getMessage());
}
return tempImage;
}

}[/code2]

Pastebin link: <!-- m --><a class="postlink" href="http://pastebin.com/sdBqqdxK">http://pastebin.com/sdBqqdxK</a><!-- m -->