首页 > 解决方案 > JApplet 幻灯片中的公共空白绘制(图形 g)错误

问题描述

我正在尝试在 JApplet 中制作幻灯片,但在“public void paint (Graphics g)'s”上不断出现错误。我知道它已经过时了,但它适用于高中的课程。这是我第一次这样做,所以如果这是一个愚蠢的问题,请原谅。该项目只是在文本和图像周围有一个带有黄色框的图像。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class timers extends JApplet implements ActionListener
{
Timer timer1;
int seconds = 1;
Image picture1;
Image picture2;
Image picture3;
Image picture4;
Image picture5;
public void init ()
{
    // set applet size
    setSize (800, 600);
    //constructor Timer(int millidelay, ActionListener l)
    timer1 = new Timer (1000, this);
    picture1 = getImage (getDocumentBase (), "Red_Color.jpeg");
    picture2 = getImage (getDocumentBase (), "Color-blue.jpeg");
    picture3 = getImage (getDocumentBase (), "Color-yellow.jpeg");
    picture4 = getImage (getDocumentBase (), "green.png");
    picture5 = getImage (getDocumentBase (), "pink.jpeg");
    //
    repaint ();

}


public void start ()
{
    timer1.start ();
}


public void stop ()
{
    timer1.stop ();
}


public void destroy ()
{
    //System.exit(0);
}


public void paint (Graphics g)
{
    g.setColor (Color.BLACK);
    g.fillRect (0, 0, 800, 600);
    g.setFont (new Font ("Helvetica", Font.PLAIN, 20));
    g.setColor (Color.yellow);
    g.drawString ("This is the colour red", 270, 473);
    g.drawImage (picture1, 300, 200, 200, 200, this);
    g.setColor (Color.yellow);
    g.drawRect (275, 175, 251, 251);
    g.drawRect (269, 445, 265, 40);
} // end of paint



        public void paint (Graphics g)
        {
                g.setColor (Color.BLACK);
    g.fillRect (0, 0, 800, 600);
    g.setFont (new Font ("Helvetica", Font.PLAIN, 20));
    g.setColor (Color.yellow);
    g.drawString ("This is the colour blue", 270, 473);
    g.drawImage (picture2, 300, 200, 200, 200, this);
    g.setColor (Color.yellow);
    g.drawRect (275, 175, 251, 251);
    g.drawRect (269, 445, 265, 40);

  }




            public void actionPerformed (ActionEvent e)
{
    if (e.getSource () == timer1)


   // timer is firing
        // increment second counter
        seconds++;
        //call paint method
        repaint ();





        }
    }

标签: javanullpointerexceptionappletjapplet

解决方案


推荐阅读