【问题标题】:Problems when drawing images in java applets在java小程序中绘制图像时的问题
【发布时间】:2014-03-21 14:45:10
【问题描述】:

我是 Java 新手,我正在创建一个小程序。我遇到了一些问题,我无法使用任何绘图函数绘制任何东西。我知道我的代码并不完美,但我将不胜感激。

这是我的代码:

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;
    }*/
    }





}

我创建了一个索引,所有文件都在正确的位置。

【问题讨论】:

  • 只是一个想法 - 考虑使用 for 循环,或者更好地使用 13 个条目填充自身的 Suite 类,而不是硬编码 104 行来填充卡片组。
  • 我不知道如何创建一个循环来更改每个循环的变量名称,我也不知道套件类是什么。请解释一下谢谢

标签: java applet


【解决方案1】:
Image c1 = getImage ( getDocumentBase (), "cards/c1.gif" );

这些行在paint(Graphics) 方法中。切勿尝试在绘画中执行 I/O 或其他耗时的任务(例如加载“装满”图像的“甲板”)!

图像应声明为类属性(在任何方法之外声明),如下所示:

Image c1;

然后在init()中加载。

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

【讨论】:

  • 还要记住 java.awt.Image 通常是无缓冲的。也就是说,在需要之前它不会加载图像数据。做你正在做的事情会丢弃之前的加载请求并重新开始,导致图像永远没有机会加载。
【解决方案2】:

我建议您修复代码以避免硬编码值。

您正在扩展 Applet 类,它本身定义了一个绘制方法,因此您必须首先调用基本绘制方法,使用super.paint(screen),另外我建议您检查您是否真的在加载您正在加载的图像到达那里。

另外,applet 的一个非常常见的错误是类没有被加载,因此请查看 Java 控制台以查看是否有任何加载器错误,作为建议,您可以将其设为 Frame 应用程序并进行翻译稍后将其发送到 Applet。

【讨论】:

    【解决方案3】:
    Here is the complete code to display the image
    
    Kindly refer through it
    /*Arpana*/
    
    import java.awt.*;
    import java.applet.*;
    
    /*
    <applet code="Img" width=500 height=500>
    </applet>
    */
    
    public class Img extends Applet
    {
        Image image;
        public void init()
        {
            image=getImage(getCodeBase(),"Appu.jpg");
        }
        public void paint(Graphics g)   
        {
            g.drawImage(image,0,0,this);
        }
    }
    

    【讨论】:

      【解决方案4】:
      Image picture;  
      picture = getImage(getDocumentBase(),"sonoo.jpg");  
      g.drawImage(picture, 30,30, this);  
      

      【讨论】:

        猜你喜欢
        • 2012-05-29
        • 1970-01-01
        • 1970-01-01
        • 2013-12-24
        • 1970-01-01
        • 2014-07-06
        • 2011-09-15
        • 2014-05-22
        • 2012-08-06
        相关资源
        最近更新 更多