Hello I need help drawing an image using the drawImage() method from this arraylist that holds images. It is supposed to be random. At first it was a program that floated down random letters, but after some suggestions I changed drawString() for the letters, to drawImage() for my arraylist of images. More specifically this line of code right here g.drawImage(PicList.get($ranNum), $ranNum, y, this);
I do not expect anyone to do my homework for me. I just need some help figuring this out. NetBeans doesnt show any errors, but when I launch my program and the images are supposed to fall, I get a NullPointer exception. "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" Any help would be appreciated. I do not know much Java and things are starting to go over my head.
Here are my full classes.
EDIT: I understand why this error is happening, I know there are other questions like it. But my code is a little more complicated than those examples and they are not helping me right now.
/******************************************************************************* **
* LearningLetterPanel.java
* Panel class which which uses threads and overwrites the run method
* to display a panel which has letters that go from the top of the panel
* to the bottom. The colors are set each iteration to a new color.
* It is used by Panel class
*********************************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
class LearningLetterPanel extends JPanel implements Runnable {
public static Thread letterThread = null;
private int y = 10;
static long nLetterDropped = 1;
RandNum rn = new RandNum();
int $ranNum = rn.ranNum();
String $letter = rn.ranNumLetter();
Color $letterColor= rn.ranNumColor();
public java.util.List<Image> PicList; // here I am making a list to store my images.
public Image pic = null;
/***************************************************************************************
* main method in the class for starting and stopping the thread
************************************************************************************/
LearningLetterPanel() {
if (letterThread == null) {
letterThread = new Thread(this);
letterThread.start();
}
}
/***************************************************************************************
* Creates the thread and uses Thread.sleep to set the speed of the movement
******************************************************************************** ****/
public void run() {
Thread myThread = Thread.currentThread();
while (letterThread == myThread) {
try{
Thread.sleep(20);
}
catch (InterruptedException e){}
repaint();
}
}
/***************************************************************************************
* the paint method draws the letter based on color(ranNumLetter), location($ranNum and y)
* and speed (y += 3 with Thread.sleep from run())
*************************************************************************************/
@Override
public void paint(Graphics g) {
g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 48));
g.setColor(Color.white);
//g.drawString($letter, $ranNum, y);
g.drawImage(PicList.get($ranNum), $ranNum, y, this);
y += 3;
Dimension d = getSize();
if (y > (d.width - 10))
{y = 10;
LearningLetterPanel.nLetterDropped +=1;
$ranNum = rn.ranNum();
this.$letter = rn.ranNumLetter();
this.$letterColor = rn.ranNumColor();
}
g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 48));
g.setColor($letterColor);
//g.drawString($letter, $ranNum, y);
g.drawImage(PicList.get($ranNum), $ranNum, y, this);
}
public void RandomImagePane() throws IOException {
PicList = new ArrayList<Image>(25);
// here I am adding the images to the list
PicList.add(ImageIO.read(getClass().getResource("/images/aa.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/bb.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/cc.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/dd.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/ee.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/ff.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/gg.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/hh.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/ii.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/jj.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/kk.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/ll.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/mm.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/nn.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/oo.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/pp.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/qq.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/rr.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/ss.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/tt.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/uu.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/vv.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/ww.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/xx.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/yy.png")));
PicList.add(ImageIO.read(getClass().getResource("/images/zz.png")));
}
/***************************************************************************************
* sets thread to null which stops the thread
************************************************************************************/
public static void stop() {
letterThread = null;
}
/***************************************************************************************
* Returns the random letter when called
************************************************************************************/
public String getLetter() {
return this.$letter;
}
}
Here's a the class where these methods are from:
import java.util.*;
import java.awt.*;
import java.util.List;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/***************************************************************************************
* This class is to be used for random numbers, letters, or colors
* Using the Math.random class
************************************************************************************/
public class RandNum {
private String $alphabet="ABCDEFGHIJKLMNOPQRSTUVWXZY";
private int $width=640;
/******************************************************************************* ********
* This class is to be used for random numbers
* Using the Math.random class
************************************************************************************/
public int ranNum() {
int $ranNumber = 20 + (int)(Math.random() * $width);
return $ranNumber;
}
/***
* This method is to be used for random letters
* Using the Math.random class
*/
public String ranNumLetter() {
int $ranNumLetter = 0 + (int)(Math.random() * 26);
String $letter = $alphabet.substring($ranNumLetter, $ranNumLetter+1);
return $letter;
}
/****
* This method is to be used for colors
* Using the Math.random class for each of the three RGB
************************************************************************************/
public Color ranNumColor() {
int $ranColorRed = 0 + (int)(Math.random() * 256);
int $ranColorBlue = 0 + (int)(Math.random() * 256);
int $ranColorGreen = 0 + (int)(Math.random() * 256);
Color c = new Color($ranColorRed, $ranColorBlue, $ranColorGreen);
return c;
}
}