views:

33

answers:

1
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Color;
import java.util.Random;

public class dots {
    public dots() {
        init();
    }
    public void init() {
        JFrame frame = new JFrame("Dots");
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        int scrWidth = (int) frame.getSize().getWidth();
        int scrHeight = (int) frame.getSize().getHeight();
        JFrame.getContentPane().add(panel);
        Random rand = new Random();
        Graphics g = panel.getGraphics();
        for (int i = 0; i < 18; i++) {
            g.setColor(i < 12 ? Color.YELLOW : Color.BLUE);
            g.drawOval(Random.nextInt(scrWidth),Random.nextInt(scrHeight),40,40);
        }
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        dots d = new dots();
    }
}

Alright, this makes no sense. When I try to compile this, I get

dots.java:19: non-static method getContentPane() cannot be referenced from a sta
tic context
                JFrame.getContentPane().add(panel);
                      ^
dots.java:24: non-static method nextInt(int) cannot be referenced from a static
context
                        g.drawOval(Random.nextInt(scrWidth),Random.nextInt(scrHe
ight),40,40);
                                         ^
dots.java:24: non-static method nextInt(int) cannot be referenced from a static
context
                        g.drawOval(Random.nextInt(scrWidth),Random.nextInt(scrHe
ight),40,40);
                                                                  ^
3 errors

I am obviously not operating in a static context, so I have no idea what is going on. Any help sincerely appreciated!

+4  A: 

Those are instance methods, so you need to call them on objects, not the class.

frame.getContentPane().add(panel);
// ...
g.drawOval(rand.nextInt(scrWidth), rand.nextInt(scrHeight),40,40);
Matthew Flaschen
Oh, I am so dumb. It has been a long day. Thanks so much for not flaming me.
David Watson
@David Watson, you should accept the answer.
mathepic