0

I am trying to add an image to an applet and I keep on failing. I am following http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html but first of all, getCodeBase() is not defined. Then I got rid of it and came up with this code, which is giving some error messages and is not working. I am just getting a blank screen.

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;


public class Cricket extends Canvas{
BufferedImage background;
/**
 * 
 */
private static final long serialVersionUID = 1L;

public Cricket() {
    setSize(1000,500);
    setBackground(Color.white);
}

@Override
public void paint(Graphics g) {
    // TODO Auto-generated method stub
    super.paint(g);
    try {
        URL url = new URL("resources/cricket_homescreen.png");
        background = ImageIO.read(url);
    } catch (IOException e) {
    }
    g.drawImage(background, 0, 0, null);
}
}

And I keep on getting these error messages:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at MTProgramming.getImage(MTProgramming.java:18)
at MTProgramming.paint(MTProgramming.java:30)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at MTProgramming.getImage(MTProgramming.java:18)
at MTProgramming.paint(MTProgramming.java:30)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Any ideas of what I am doing wrong here? (by the way my package explorer shows that the files are fine)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2884344
  • 195
  • 1
  • 1
  • 9
  • *"first of all, `getCodeBase()` is not defined.."* Sure it is. But it is defined for **`Applet`** not **`Canvas`**! Also, this has nothing to do with Eclipse, since it is done the exact same way with Eclipse as nay other IDE, or coding using a text editor. Other tips: 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). .. – Andrew Thompson Nov 16 '13 at 01:39
  • .. 3) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Nov 16 '13 at 01:40
  • Where is you resources directory located? – Paul Samsotha Nov 16 '13 at 01:40
  • @peeskillet it is located under the project where it is supposed to be in – user2884344 Nov 16 '13 at 01:43

0 Answers0