0

I am trying to do a slideshow in a JApplet and I keep getting an error on the "public void paint (Graphics g)'s." I know its outdated but its for a class in high school. This is my first time doing this so please im sorry if its a stupid question. The project is just to have an image with a yellow box around text and image.

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 ();





        }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • what is the error you get? Saying you "get an error", without saying which one, or when you get it (compile time/runtime) doesn't really help us – Stultuske Jun 19 '20 at 06:27
  • @Stultuske compilation error. – Eward000 Jun 19 '20 at 06:34
  • so, check the error message you get, and correct it – Stultuske Jun 19 '20 at 06:35
  • You have that method twice in the code you've shown. Is that the problem? – Stultuske Jun 19 '20 at 06:35
  • @Stultuske Duplicate declaration of method "paint" in type "timers" but if I get rid of the public void's then all the "g."'s become errors – Eward000 Jun 19 '20 at 06:48
  • you don't have to get rid of the "duplic void", you have that method two times, it is duplicated (this has nothing to do with the public modifier). Delete one of the two, and you'll be fine – Stultuske Jun 19 '20 at 07:04
  • @Stultuske Now its a run error that reads java.lang.NullPointerException at sun.java2d.SunGraphics2D.drawImage(Unknown Source) at sun.java2d.SunGraphics2D.drawImage(Unknown Source) at timers.paint(timers.java:55) at javax.swing.JApplet.update(Unknown Source) at sun.awt.RepaintArea.paint(Unknown Source) at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) – Eward000 Jun 19 '20 at 07:15
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 3) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and .. – Andrew Thompson Jun 20 '20 at 10:10
  • .. [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). 4) SO is a Q&A site, not a help desk. Each problem should be specific to one question, and be discussed on its own thread. – Andrew Thompson Jun 20 '20 at 10:11

0 Answers0