简体   繁体   English

Java Applet中的.class错误

[英].class error in java applet

I am writing an applet that will randomly pick 10 cards and show them on the screen. 我正在编写一个applet,它将随机选择10张卡并在屏幕上显示它们。 However, I am receiving a .class error and a ; 但是,我收到一个.class错误和一个;错误。 needed error when I attempt to pass an String[]. 尝试传递String []时出现所需的错误。 Anyone help? 有人帮忙吗? This is my code: 这是我的代码:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.applet.Applet;
import java.lang.Math;
import java.util.Random;

public class unit12Assignment extends Applet
{
Image card1 ... card52;

public void init()
{
    card1 = getImage( getDocumentBase(), "c1.gif" );
    ...
    card52 = getImage( getDocumentBase(), "sk.gif" );
}

public void getCards()
{
    String cardNumber; 
    double cardRandom;
    int cardRandomNumber;
    String[] cardSelection = new String[10];
    Random ran = new Random();

    for (int number = 0; number <=  9; )
    {
        cardRandom = ran.nextInt(52) + 1;
        cardRandomNumber = (int) Math.round( cardRandom );

        if ( cardRandomNumber > 0 && cardRandomNumber <= 52 )
        { 
            cardNumber =  "card" + cardRandomNumber;
            number++;
        }
    }   
    paint( String[] cardSelection );
}

public void paint(Graphics g, String[] card)
{
    setBackground( Color.green );
    g.drawImage( card[0], 10, 10, this);
    g.drawImage( card[1], 90, 10, this);
    g.drawImage( card[2], 170, 10, this);
    g.drawImage( card[3], 250, 10, this);
}

} }

This line: 这行:

paint( String[] cardSelection );

should syntactically be 在语法上应该

paint( cardSelection );

You only need to write the type (for example String[] ) before a variable when you first declare it. 首次声明变量时,只需要在变量之前编写类型(例如String[] )。 From there it can just be referred to by its name. 从那里可以用它的名字来称呼它。

I also notice that paint takes a Graphics argument as well as a String[] , so you'll need to pass that in as well: 我还注意到paint接受了Graphics参数以及String[] ,因此您还需要传递它:

Graphics g = getGraphicsSomehow();
paint(g, cardSelection);

EDIT: see Andrew Thompson's answer for a disclaimer on using Graphics with an alternative solution. 编辑:请参阅安德鲁·汤普森(Andrew Thompson)的答案 ,以获取有关使用Graphics和其他解决方案的免责声明。

Anything involving getGraphics() is a fragile solution. 任何涉及getGraphics()事情都是一个脆弱的解决方案。

If the user drags another another app. 如果用户拖动另一个应用程序。 over the browser and covers the applet, then minimizes the other app., it is likely to erase the custom painted pixels. 在浏览器上并覆盖小程序,然后最小化其他应用程序。很可能会删除自定义绘制的像素。 See Performing Custom Painting for how to paint correctly (which breaks down to 'paint when told to do so'). 请参阅“ 执行自定义绘画”以获取有关如何正确绘画的信息(分解为“在需要时绘画”)。

An alternative is to use a BufferedImage for the rendering and display it in a JLabel . 一种替代方法是使用BufferedImage进行渲染并将其显示在JLabel Paint to the image whenever required, then repaint the label. 根据需要在图像上绘画,然后重新绘画标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM