gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:45:43 25 4
gpt4 key购买 nike

所以这是一个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);

}

}

最佳答案

How do I get my Java Applet to show the card images?

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

关于java - 如何让我的 Java Applet 显示卡片图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29153033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com