0

I'm trying to load Images into this program and it won't work and I've played around to make sure that I had the directories right and everything, but now I'm kinda at a lost. The draw function called in by the PersonFront class works fine, but not the Image that is supposed to be in it as well. I can change dimensions and all that, but it continues to not show any images.

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;

 public class GameViewer extends Applet implements KeyListener
{
   final static int NUM_WIDTH=14;
   final static int NUM_HEIGHT=7;
   private int startX = 100;
   private int startY = 40;

public void init()
{
    setBackground(Color.GRAY);
    addKeyListener(this);
    setFocusable(true);
    requestFocusInWindow();
}

public void start()
{}

public void paint(Graphics g)
{
    Graphics2D g2 =(Graphics2D) g;

    Image wood = getImage(getCodeBase(), "Resources/Images/Environment/woodfloor.png");
    Image wall = getImage(getCodeBase(), "wall.jpg");
    Image counter = getImage(getCodeBase(), "resources/images/environment/counter.jpg");
    Image cab_right = getImage(getCodeBase(), "resources/images/environment/cabinet_right.jpg");
    Image cab_left = getImage(getCodeBase(), "resources/images/environment/cabinet_left.jpg");
    Image shirt = getImage(getCodeBase(), "resources/Images/Character_Wear/Shirts/shirt_red.png");
    Image pants = getImage(getCodeBase(),"resources/Images/Character_Wear/Pants/pants_limegreen.png");

    g2.drawImage(wood,0,0,null);

    //for(int q=0; q<NUM_WIDTH; q++)
     // for(int z=0; z<NUM_HEIGHT; z++)
     // {
        //  g2.drawImage(wood, q*64, z*64, null);
       //   g2.drawImage(wall, 0, 128+(16*z), null);
   //   }

    PersonFront stat = new PersonFront(startX,startY,shirt,pants);
    stat.draw(g2);
}

public void keyPressed(KeyEvent key)
{
    if (key.getKeyCode() == key.VK_D)
    {
        startX += 32;

        repaint();
    }
    if(key.getKeyCode() == key.VK_A)
    {
        startX -= 32;

        repaint();
    }

}

public void keyReleased(KeyEvent e)
{

}

public void keyTyped(KeyEvent e)
{

}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
CJ Jacobs
  • 299
  • 1
  • 16
  • It is highly unrecommended that you should be loading images in the `paint` method. The paint method should run as fast as possible. Instead, use the `init` method – MadProgrammer Nov 26 '13 at 23:26
  • Thanks for the tip, but unfortunately that didn't have the images load in. – CJ Jacobs Nov 26 '13 at 23:31
  • it wasn't suppose to ;) - Are the image stored within the application Jar or on the web server...? – MadProgrammer Nov 26 '13 at 23:32
  • I just have the folder on my desktop right now. – CJ Jacobs Nov 26 '13 at 23:34
  • i see that the path for the wood is in uppercase, and the other ones are in lower case. Are those the correct locations? Also, what does the code for getImage() and getCodeBase() look like? Are the images null or do they just not get painted? – kutschkem Nov 27 '13 at 09:00
  • I've made sure that the pathways are correct and I can still draw things into the canvas, I just can't load images. – CJ Jacobs Nov 27 '13 at 22:05
  • 1) 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). 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 27 '13 at 22:47

1 Answers1

0

You need to use BufferedImages.

BufferedImage wood = this.getClass().getResource("Resources/Images/Environment/woodfloor.png");

Keep in mind that the image files must be in the JAR or same directory as the code, relative to the class file(this).

Sources: Prior knowledge, loading BufferedImage with ClassLoader.getResource()

Community
  • 1
  • 1
Ethan McTague
  • 2,236
  • 3
  • 21
  • 53