0
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;

public class snakeApplet extends Applet {

    private snakeCanvas c;

    public void init() {
        c.setPreferredSize(new Dimension(640, 480));
        c = new snakeCanvas();
        c.setVisible(true);
        c.setFocusable(true);
        this.add(c);
        this.setVisible(true);
        this.setSize(new Dimension(640, 480));
    }

    public void paint(Graphics g) {
        this.setSize(new Dimension(640, 480));
    }
}
Alexis Leclerc
  • 1,303
  • 1
  • 16
  • 26
user3223813
  • 1
  • 1
  • 1
  • 1
    What's the context? What details do you have about your error? What solutions did you try? Please elaborate your question in order to receive better help. – Alexis Leclerc Jan 22 '14 at 14:16
  • 1) Why code an applet? If it is due to spec. by teacher, 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/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. 3) The last two lines of the `init()` are not necessary. The last line should be replaced with the HTML setting the size. 4) The `paint(Graphics)` override will not only cause an infinite loop, but.. – Andrew Thompson Jan 23 '14 at 07:46
  • ..will cause everything else to not be painted. Remove it. – Andrew Thompson Jan 23 '14 at 07:46

1 Answers1

3

You forgot to ask a question but here is an issue

c = new snakeCanvas();
c.setPreferredSize(new Dimension(640, 480));

The variable c needs to be initialized before any methods can be invoked on the instance

Reimeus
  • 158,255
  • 15
  • 216
  • 276