03-21-2013, 03:44 PM
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 -->
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 -->