图中的Java小程序的图像时存在的问题 [英] Problems when drawing images in java applets

查看:95
本文介绍了图中的Java小程序的图像时存在的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Java和我创建的小程序。我遇到了一些问题,我不能得出使用任何的抽奖功能什么。我知道我的code不是完美的,但我会AP preciate任何帮助。

这是我的code:

 进口java.applet.Applet中;
进口java.awt.Graphics;
进口java.awt.Image中;
进口java.awt.Color中;公共类CardsJavaProgram扩展的Applet {    公共无效的init(){
    this.setBackground(Color.blue);    }
    公共无效漆(图形画面){
    INT X = 10;
    诠释Y = 10;
    INT X2 = 10;
    INT Y2 = 110;
        图像C1 =的getImage(getDocumentBase(),卡/ c1.gif);
        图像C2 =的getImage(getDocumentBase(),卡/ c2.gif);
        图像C3 =的getImage(getDocumentBase(),卡/ c3.gif);
        图像C4 =的getImage(getDocumentBase(),卡/ c4.gif);
        图像C5 =的getImage(getDocumentBase(),卡/ c5.gif);
        图像C6 =的getImage(getDocumentBase(),卡/ c6.gif);
        图像C7 =的getImage(getDocumentBase(),卡/ c7.gif);
        图像C8 =的getImage(getDocumentBase(),卡/ c8.gif);
        图片C9 =的getImage(getDocumentBase(),张卡/ c9.gif);
        图像C10 =的getImage(getDocumentBase(),卡/ c10.gif);
        图片CJ =的getImage(getDocumentBase(),张卡/ cj.gif);
        图片CQ =的getImage(getDocumentBase(),张卡/ cq.gif);
        图片CK =的getImage(getDocumentBase(),张卡/ ck.gif);
        图像H1 =的getImage(getDocumentBase(),卡/ h1.gif);
        图像H2 =的getImage(getDocumentBase(),卡/ h2.gif);
        图像H3 =的getImage(getDocumentBase(),卡/ h3.gif);
        图像H4 =的getImage(getDocumentBase(),卡/ h4.gif);
        图片H5 =的getImage(getDocumentBase(),张卡/ h5.gif);
        图像H6 =的getImage(getDocumentBase(),卡/ h6.gif);
        图像H7 =的getImage(getDocumentBase(),卡/ h7.gif);
        图片H8 =的getImage(getDocumentBase(),张卡/ h8.gif);
        图片H9 =的getImage(getDocumentBase(),张卡/ h9.gif);
        图像H10 =的getImage(getDocumentBase(),卡/ h10.gif);
        图片HJ =的getImage(getDocumentBase(),张卡/ hj.gif);
        图片HQ =的getImage(getDocumentBase(),张卡/ hq.gif);
        图像HK =的getImage(getDocumentBase(),张卡/ hk.gif);
        图像D1 =的getImage(getDocumentBase(),卡/ d1.gif);
        图像D2 =的getImage(getDocumentBase(),卡/ d2.gif);
        图像D3 =的getImage(getDocumentBase(),卡/ d3.gif);
        图像D4 =的getImage(getDocumentBase(),卡/ d4.gif);
        图像D5 =的getImage(getDocumentBase(),卡/ d5.gif);
        图像D6 =的getImage(getDocumentBase(),卡/ d6.gi​​f);
        图像D7 =的getImage(getDocumentBase(),卡/ d7.gif);
        图像D8 =的getImage(getDocumentBase(),卡/ d8.gif);
        图片D9 =的getImage(getDocumentBase(),张卡/ d9.gif);
        图像D10 =的getImage(getDocumentBase(),卡/ d10.gif);
        图片DJ =的getImage(getDocumentBase(),张卡/ dj.gif);
        图像DQ =的getImage(getDocumentBase(),卡/ dq.gif);
        图像DK =的getImage(getDocumentBase(),卡/ dk.gif);
        图像S1 =的getImage(getDocumentBase(),卡/ s1.gif);
        图像S2 =的getImage(getDocumentBase(),卡/ s2.gif);
        图像S3 =的getImage(getDocumentBase(),卡/ s3.gif);
        图像S4 =的getImage(getDocumentBase(),卡/ s4.gif);
        图像S5 =的getImage(getDocumentBase(),卡/ s5.gif);
        图像S6 =的getImage(getDocumentBase(),卡/ s6.gif);
        图片S7 =的getImage(getDocumentBase(),张卡/ s7.gif);
        图像S8 =的getImage(getDocumentBase(),卡/ s8.gif);
        图像S9 =的getImage(getDocumentBase(),卡/ s9.gif);
        图象S10 =的getImage(getDocumentBase(),卡/ s10.gif);
        图片SJ =的getImage(getDocumentBase(),张卡/ sj.gif);
        图像平方=的getImage(getDocumentBase(),卡/ sq.gif);
        图片SK =的getImage(getDocumentBase(),张卡/ sk.gif);        图片[] =卡新的图像[52];
        卡[0] = C1;
        卡[1] = C2;
        卡[2] = C3;
        卡[3] = C4;
        卡[4] = C5;
        卡[5] = C6;
        卡[6] = C7;
        卡[7] = C8;
        卡[8] = C9;
        卡[9] = C10;
        卡[10] = CJ;
        卡[11] = CQ;
        卡[12] = CK;
        卡[13] = H1;
        卡[14] = H2;
        卡[15] = H3;
        卡[16] = H4;
        卡[17] = H5;
        卡[18] = H6;
        卡[19] = H7;
        卡[20] = H8;
        卡[21] = H9;
        卡[22] = H10;
        卡[23] = HJ;
        卡[24] =总部;
        卡[25] = HK;
        卡[26] = D1;
        卡[27] ​​= D2;
        卡[28] = D3;
        卡[29] = D4;
        卡[30] = D5;
        卡[31] = D6;
        卡[32] = D7;
        卡[33] = D8;
        卡[34] = D9;
        卡[35] = D10;
        卡[36] = DJ;
        卡[37] = DQ;
        卡[38] = DK;
        卡[39] = S1;
        卡[40] = S2;
        卡[41] = S3;
        卡[42] = S4;
        卡[43] = S5;
        卡[44] = S6;
        卡[45] = S7;
        卡[46] = S8;
        卡[47] = S9;
        卡[48] = S10;
        卡[49] = 8.7;
        卡[50] =平方;
        卡[51] = SK;
    screen.drawImage(的getImage(getDocumentBase(),张卡/ c1.gif),100,100,这一点);
    screen.drawImage(的getImage(getDocumentBase(),卡/ c1.gif),10,10,这一点);
    screen.drawImage(的getImage(getDocumentBase(),张卡/ c1.gif),50,50,本);
    screen.drawString(测试,10,10,这一点);
    / *的for(int i = 0;我4;;我++){
        screen.drawImage(卡[(INT)(浮点)的Math.random()* 52 - 1],X,Y,这一点);
        X = X + 5;
    }
    对(INT I = 0; I&下; 4;我++){
        screen.drawImage(卡[(int)的(浮点)(的Math.random())* 52 - (int)的(1)],X2,Y2,本);
        X = X + 10;
    } * /
    }}

我创建了一个指标,所有的文件都在正确的地方。


解决方案

 图像C1 =的getImage(getDocumentBase(),张卡/ c1.gif);

这些行是在油漆(图形)方法。不要试图做I / O或其他费时的任务(如加载一个全面的甲板图像)涂料!

图像应该声明为类属性(任何方法之外声明)是这样的:

 图像C1;

然后在加载的init()

  C1 =的getImage(getDocumentBase(),张卡/ c1.gif);

I am new to java and I am creating an applet. I ran into some problems where I cannot draw anything using any of the draw functions. I know my code is not perfect but I would appreciate any help.

This is my code:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;

public class CardsJavaProgram extends Applet {

    public void init(){
    this.setBackground( Color.blue );

    }
    public void paint( Graphics screen ){
    int x = 10;
    int y = 10;
    int x2 = 10;
    int y2 = 110;
        Image c1 = getImage ( getDocumentBase (), "cards/c1.gif" );
        Image c2 = getImage ( getDocumentBase (), "cards/c2.gif" );
        Image c3 = getImage ( getDocumentBase (), "cards/c3.gif" );
        Image c4 = getImage ( getDocumentBase (), "cards/c4.gif" );
        Image c5 = getImage ( getDocumentBase (), "cards/c5.gif" );
        Image c6 = getImage ( getDocumentBase (), "cards/c6.gif" );
        Image c7 = getImage ( getDocumentBase (), "cards/c7.gif" );
        Image c8 = getImage ( getDocumentBase (), "cards/c8.gif" );
        Image c9 = getImage ( getDocumentBase (), "cards/c9.gif" );
        Image c10 = getImage ( getDocumentBase (), "cards/c10.gif" );
        Image cj = getImage ( getDocumentBase (), "cards/cj.gif" );
        Image cq = getImage ( getDocumentBase (), "cards/cq.gif" );
        Image ck = getImage ( getDocumentBase (), "cards/ck.gif" );
        Image h1 = getImage ( getDocumentBase (), "cards/h1.gif" );
        Image h2 = getImage ( getDocumentBase (), "cards/h2.gif" );
        Image h3 = getImage ( getDocumentBase (), "cards/h3.gif" );
        Image h4 = getImage ( getDocumentBase (), "cards/h4.gif" );
        Image h5 = getImage ( getDocumentBase (), "cards/h5.gif" );
        Image h6 = getImage ( getDocumentBase (), "cards/h6.gif" );
        Image h7 = getImage ( getDocumentBase (), "cards/h7.gif" );
        Image h8 = getImage ( getDocumentBase (), "cards/h8.gif" );
        Image h9 = getImage ( getDocumentBase (), "cards/h9.gif" );
        Image h10 = getImage ( getDocumentBase (), "cards/h10.gif" );
        Image hj = getImage ( getDocumentBase (), "cards/hj.gif" );
        Image hq = getImage ( getDocumentBase (), "cards/hq.gif" );
        Image hk = getImage ( getDocumentBase (), "cards/hk.gif" );
        Image d1 = getImage ( getDocumentBase (), "cards/d1.gif" );
        Image d2 = getImage ( getDocumentBase (), "cards/d2.gif" );
        Image d3 = getImage ( getDocumentBase (), "cards/d3.gif" );
        Image d4 = getImage ( getDocumentBase (), "cards/d4.gif" );
        Image d5 = getImage ( getDocumentBase (), "cards/d5.gif" );
        Image d6 = getImage ( getDocumentBase (), "cards/d6.gif" );
        Image d7 = getImage ( getDocumentBase (), "cards/d7.gif" );
        Image d8 = getImage ( getDocumentBase (), "cards/d8.gif" );
        Image d9 = getImage ( getDocumentBase (), "cards/d9.gif" );
        Image d10 = getImage ( getDocumentBase (), "cards/d10.gif" );
        Image dj = getImage ( getDocumentBase (), "cards/dj.gif" );
        Image dq = getImage ( getDocumentBase (), "cards/dq.gif" );
        Image dk = getImage ( getDocumentBase (), "cards/dk.gif" );
        Image s1 = getImage ( getDocumentBase (), "cards/s1.gif" );
        Image s2 = getImage ( getDocumentBase (), "cards/s2.gif" );
        Image s3 = getImage ( getDocumentBase (), "cards/s3.gif" );
        Image s4 = getImage ( getDocumentBase (), "cards/s4.gif" );
        Image s5 = getImage ( getDocumentBase (), "cards/s5.gif" );
        Image s6 = getImage ( getDocumentBase (), "cards/s6.gif" );
        Image s7 = getImage ( getDocumentBase (), "cards/s7.gif" );
        Image s8 = getImage ( getDocumentBase (), "cards/s8.gif" );
        Image s9 = getImage ( getDocumentBase (), "cards/s9.gif" );
        Image s10 = getImage ( getDocumentBase (), "cards/s10.gif" );
        Image sj = getImage ( getDocumentBase (), "cards/sj.gif" );
        Image sq = getImage ( getDocumentBase (), "cards/sq.gif" );
        Image sk = getImage ( getDocumentBase (), "cards/sk.gif" );

        Image [] cards = new Image [ 52 ];
        cards [ 0 ] = c1;
        cards [ 1 ] = c2;
        cards [ 2 ] = c3;
        cards [ 3 ] = c4;
        cards [ 4 ] = c5;
        cards [ 5 ] = c6;
        cards [ 6 ] = c7;
        cards [ 7 ] = c8;
        cards [ 8 ] = c9;
        cards [ 9 ] = c10;
        cards [ 10 ] = cj;
        cards [ 11 ] = cq;
        cards [ 12 ] = ck;
        cards [ 13 ] = h1;
        cards [ 14 ] = h2;
        cards [ 15 ] = h3;
        cards [ 16 ] = h4;
        cards [ 17 ] = h5;
        cards [ 18 ] = h6;
        cards [ 19 ] = h7;
        cards [ 20 ] = h8;
        cards [ 21 ] = h9;
        cards [ 22 ] = h10;
        cards [ 23 ] = hj;
        cards [ 24 ] = hq;
        cards [ 25 ] = hk;
        cards [ 26 ] = d1;
        cards [ 27 ] = d2;
        cards [ 28 ] = d3;
        cards [ 29 ] = d4;
        cards [ 30 ] = d5;
        cards [ 31 ] = d6;
        cards [ 32 ] = d7;
        cards [ 33 ] = d8;
        cards [ 34 ] = d9;
        cards [ 35 ] = d10;
        cards [ 36 ] = dj;
        cards [ 37 ] = dq;
        cards [ 38 ] = dk;
        cards [ 39 ] = s1;
        cards [ 40 ] = s2;
        cards [ 41 ] = s3;
        cards [ 42 ] = s4;
        cards [ 43 ] = s5;
        cards [ 44 ] = s6;
        cards [ 45 ] = s7;
        cards [ 46 ] = s8;
        cards [ 47 ] = s9;
        cards [ 48 ] = s10;
        cards [ 49 ] = sj;
        cards [ 50 ] = sq;
        cards [ 51 ] = sk;
    screen.drawImage( getImage ( getDocumentBase (), "cards/c1.gif" ), 100, 100, this );
    screen.drawImage( getImage ( getDocumentBase (), "cards/c1.gif" ), 10, 10, this );
    screen.drawImage( getImage ( getDocumentBase (), "cards/c1.gif" ), 50, 50, this );
    screen.drawString( " test ", 10, 10, this );
    /*for (int i = 0; i < 4; i++){
        screen.drawImage (cards[( int ) ( float ) Math.random () * 52 - 1], x, y, this);
        x = x + 5;
    }
    for (int i = 0; i < 4; i++){
        screen.drawImage (cards [( int ) ( float ) ( Math.random () ) * 52 - ( int ) ( 1 )], x2, y2, this);
        x = x + 10;
    }*/
    }





}

I created an index and all the files are in the right place.

解决方案

Image c1 = getImage ( getDocumentBase (), "cards/c1.gif" );

These lines are in the paint(Graphics) method. Never try to do I/O or other time consuming tasks (like loading an a 'deck full' of images) in paint!

The images should be declared as class attributes (declared outside any method) something like this:

Image c1;

Then loaded in init().

c1 = getImage ( getDocumentBase (), "cards/c1.gif" );

这篇关于图中的Java小程序的图像时存在的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆