You didn't say what the compiler error was specifically, but I'm betting it will say something like "undefined variable c".
In general methods can only access variables that are either passed to them as a parameter, declared in that method, or are global to the class. If we look closely at your code for your actionPerformed method we find that the variable c doesn't satisfy any of these conditions, and hence we cannot perform any actions on it.
c is declared in your init method which means that it can only be used within that method (init). This is why you get an error when you try to use it in the actionPerformed method. For further reading on Scope see this page http://en.wikiversity.org/wiki/Introduction_to_Programming/Scope
or google OOP/programming scope.
More to the point, to fix your error we need a way to access c in actionPerformed. One solution to this problem is to give c global scope using the following code
* Created on September 15, 2008, 10:24 AM
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
* @author teacher
public class Spring extends JApplet
//By declaring c here (in global scope) all methods within this class can
//access the c variable
public void init()
JButton button = new JButton("Spring");
//We still want to initialize c here, but it's already been declared so we
//can remove the "Container" from this line
c = getContentPane();
public void actionPerformed(ActionEvent e)
Random r = new Random();
Color randColor = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
The only caveat I can see with this code is that you must make sure that your init method is called before the actionPerformed method. If it's not then you'll probably get a null-pointer exception at runtime because c won't have been initialized before you try to use it.
I hope this helps.