문제

This is the primary class

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.awt.event.*;


public class demo_image extends JApplet {

private Image offscreen;
private double wildcat_x;
private int wildcat_y;
public void init() {

    player();
    wildcat_x = 50;
    wildcat_y = 50;
}

public void paint(Graphics g) {
    Graphics gg =(Graphics2D)offscreen.getGraphics();
    Delay x = new Delay();
    gg.clearRect(0,0,getSize().width, getSize().height);
    gg.drawImage(offscreen,0,0,null);
    gg.drawString("Welcome to Java!!", 50, 60 );

    gg.drawImage(getImage(getDocumentBase(),"tyro.png"),(int)wildcat_x,wildcat_y, 250,300,this);
    wildcat_x+=2; 
    repaint();


    x.wait(30);

    gg.dispose();

}

public void player(){
    try{
        AudioClip b = getAudioClip( new URL(getCodeBase()+"track02.wav"));
        b.play();
    }
    catch(Exception e){
        System.out.println(e);
    }
}
}

This is the secondary class

public class Delay
{
  public void wait(int milliseconds)
{
    try
    {
        Thread.sleep(milliseconds);
    } 
    catch (Exception e)
    {
        // ignoring exception at the moment
    }
}
}
도움이 되었습니까?

해결책

  1. Don't override the paint() method. Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent) and then you add the panel to the applet

  2. Don't invoke repaint() from any painting method. This will cause an infinite loop.

  3. Don't use sleeping code inside a painting method. If you want animation then use a Swing Timer

  4. Don't read images from a painting method. The image should be read once when the class is created.

  5. Class name start with an upper case character. "demo_image" should be "DemoImage".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top