如何使用DrawImage()与随机的图像arraylist

时间:2015-12-09 20:14:30

标签: java random arraylist drawimage

您好我需要帮助使用此保存图像的arraylist中的drawImage()方法绘制图像。它应该是随机的。起初它是一个浮动随机字母的程序,但在一些建议后,我改变了字母的drawString(),为我的图像arraylist drawImage()。更具体地说,这行代码就在g.drawImage(PicList.get($ranNum), $ranNum, y, this);  我不希望有人为我做功课。我只需要帮助解决这个问题。 NetBeans没有显示任何错误,但是当我启动我的程序并且图像应该掉落时,我得到一个NullPointer异常。 “线程中的异常”AWT-EventQueue-0“java.lang.NullPointerException”任何帮助都将不胜感激。我不太了解Java并且事情开始超越我的脑袋。 这是我的全班。 编辑:我理解为什么会发生这种错误,我知道还有其他问题。但是我的代码比那些例子稍微复杂一点,他们现在没有帮助我。

/*******************************************************************************    **
* 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;
}
  }

以下是这些方法来自的类:

    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;
}
}

1 个答案:

答案 0 :(得分:0)

因为您似乎没有初始化列表。在你可以引用你的PicList之前( 应该命名为picList btw,因为它是一个字段名而不是一个类名),你需要初始化它。

你已经在RandomImagePane()中写过,但似乎没有调用它(同样应该用小写命名为initRandomImage List ,并且名称描述其目的)。

(暂时坚持你的命名)修改LearningLetterPanel的构造者:

LearningLetterPanel()  {
    try {
        RandomImagePane();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    letterThread = new Thread(this);
    letterThread.start();
}

捕获IOException的替代方法是使构造函数抛出IOException,但是你仍然需要在某处捕获它。我冒昧地删除了letterThread == null的测试,因为总是构造函数中的情况(只要你只创建一个实例)