如何让我的Java Applet显示卡片图像?

时间:2015-03-19 19:09:24

标签: java swing graphics applet

所以这是一个java applet我不知道为什么它不呈现卡(卡片文件名是任何数量的“类型”数组加入“.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);

    }

}

1 个答案:

答案 0 :(得分:1)

  

如何让我的Java Applet显示卡片图像?

加载它就好像它是一个applet(这将是第一步)。一个简单的测试表明,加载上面看到的applet的方式使getDocumentBase()方法返回 null