如何将 DrawImage() 与图像的随机数组列表一起使用
How to use DrawImage() with a randomized arraylist of images
您好,我需要帮助使用 drawImage() 方法从保存图像的数组列表中绘制图像。它应该是随机的。起初它是一个随机字母浮动的程序,但在一些建议之后,我将字母的 drawString() 更改为 drawImage() 我的图像数组列表。更具体地说,这行代码就在这里 g.drawImage(PicList.get($ranNum), $ranNum, y, this);
我不希望任何人为我做家庭作业。我只需要一些帮助来解决这个问题。 NetBeans 没有显示任何错误,但是当我启动我的程序并且图像应该下降时,我得到一个 NullPointer 异常。 "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" 任何帮助将不胜感激。我知道的不多 Java 事情开始让我头疼了。
这是我的完整 classes。
编辑:我明白为什么会发生这个错误,我知道还有其他类似的问题。但是我的代码比那些例子复杂一点,它们现在对我没有帮助。
/******************************************************************************* **
* 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;
}
}
这里是 class 这些方法的来源:
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;
}
}
您似乎没有初始化列表。在您可以引用您的 PicList 之前( 应该 被命名为 picList 顺便说一句,因为它是一个字段名称而不是 class 名称),您需要初始化它。
您已经在 RandomImagePane() 中编写了它,但似乎没有调用它(同样应该将其命名为 initRandomImageList,小写字母和描述其用途的名称).
(暂且保留你的命名)修改 LearningLetterPanel 的构造函数:
LearningLetterPanel() {
try {
RandomImagePane();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
letterThread = new Thread(this);
letterThread.start();
}
捕获 IOException 的替代方法是让构造函数抛出 IOException,但您仍然需要在某处捕获它。我冒昧地删除了对 letterThread == null 的测试,因为在构造函数中 总是 这种情况(只要您只创建一个实例)
您好,我需要帮助使用 drawImage() 方法从保存图像的数组列表中绘制图像。它应该是随机的。起初它是一个随机字母浮动的程序,但在一些建议之后,我将字母的 drawString() 更改为 drawImage() 我的图像数组列表。更具体地说,这行代码就在这里 g.drawImage(PicList.get($ranNum), $ranNum, y, this);
我不希望任何人为我做家庭作业。我只需要一些帮助来解决这个问题。 NetBeans 没有显示任何错误,但是当我启动我的程序并且图像应该下降时,我得到一个 NullPointer 异常。 "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" 任何帮助将不胜感激。我知道的不多 Java 事情开始让我头疼了。
这是我的完整 classes。
编辑:我明白为什么会发生这个错误,我知道还有其他类似的问题。但是我的代码比那些例子复杂一点,它们现在对我没有帮助。
/******************************************************************************* **
* 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;
}
}
这里是 class 这些方法的来源:
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;
}
}
您似乎没有初始化列表。在您可以引用您的 PicList 之前( 应该 被命名为 picList 顺便说一句,因为它是一个字段名称而不是 class 名称),您需要初始化它。
您已经在 RandomImagePane() 中编写了它,但似乎没有调用它(同样应该将其命名为 initRandomImageList,小写字母和描述其用途的名称).
(暂且保留你的命名)修改 LearningLetterPanel 的构造函数:
LearningLetterPanel() {
try {
RandomImagePane();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
letterThread = new Thread(this);
letterThread.start();
}
捕获 IOException 的替代方法是让构造函数抛出 IOException,但您仍然需要在某处捕获它。我冒昧地删除了对 letterThread == null 的测试,因为在构造函数中 总是 这种情况(只要您只创建一个实例)