所以这是一个java小程序,我不确定它为什么不呈现卡片(卡片文件名是任何数量的"types"数组,扩展名为".gif"。)我不知道的是如何将其渲染。我不知道我是做错了什么还是忘记了什么,但如果你发现问题,请告诉我。
import javax.swing.*;
import java.net.URL;
import java.applet.*;
import java.awt.*;
import java.awt.Graphics;
public class Assignment12 extends JApplet {
private final static int NUMBER_OF_CARDS = 52;
private static Image card1, card2, card3, card4, card5, card6, card7, card8, card9, card10;
private static String[] types = {"h1","h2","h3","h4","h5","h6","h7","h8","h9","h10","hj","hq","hk","c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","cj","cq","ck","s1","s2","s3","s4","s5","s6","s7","s8","s9","s10","sj","sq","sk","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","dj","dq","dk"};
public static void main (String[] args)
{
for(int i = 0; i < NUMBER_OF_CARDS; i++)
{
int index = (int) (Math.random() * NUMBER_OF_CARDS);
String temp = types[i];
types[i] = types[index];
types[index] = temp;
}
JFrame frame = new JFrame("Assignment12");
Assignment12 applet = new Assignment12();
applet.init();
frame.getContentPane().add(applet, BorderLayout.CENTER);
}
public void init()
{
card1 = getImage(getDocumentBase(), "images/" + types[0] + ".gif");
card2 = getImage(getDocumentBase(), "images/" + types[1] + ".gif");
card3 = getImage(getDocumentBase(), "images/" + types[2] + ".gif");
card4 = getImage(getDocumentBase(), "images/" + types[3] + ".gif");
card5 = getImage(getDocumentBase(), "images/" + types[4] + ".gif");
card6 = getImage(getDocumentBase(), "images/" + types[5] + ".gif");
card7 = getImage(getDocumentBase(), "images/" + types[6] + ".gif");
card8 = getImage(getDocumentBase(), "images/" + types[7] + ".gif");
card9 = getImage(getDocumentBase(), "images/" + types[8] + ".gif");
card10 = getImage(getDocumentBase(), "images/" + types[9] + ".gif");
repaint();
}
public void paint(Graphics g)
{
g.drawImage(card1, 10, 10, this);
}
}
如何让我的Java Applet显示卡片图像?
将其加载为小程序(这将是第一步)。一个简单的测试表明,上面看到的加载小程序的方式会使getDocumentBase()
方法返回null
。