Confused about painting in Java2D

HI, I'm trying to get a simple app - note I don't want to do this in an Applet - that displays some text on screen and then exits when a key is pressed. I don't know how to get it to paint if I just call paint() from the class constructor it complains about the parameter.

paint(java.awt.Graphics) cannot be applied to ()

If I try to call paint from the calling class I get the same problem.

Do I need a JPanel and if so, what for? Where should I use paint() and not paintComponent() or vice versa and what's the difference? I've seen pieces of code which draw without a JPanel or JFrame but I don't know how those work. Here's what I've got so far:

publicclass Controller

{

public CallingClass

{

Intro intro =new Intro();

}

publicstaticvoid main(String[] args)

{

Controller callingClass =new CallingClass;

}

}

import java.awt.*;

import java.awt.Color;

publicclass Intro

{

public Intro()

{

paint();

}

publicvoid paint(Graphics g)

{

setBackgrond(Color.black);

g.setColor(Color.green);

g.drawString("Blah",512,20);

g.drawString("Blah",20,60);

g.drawString("Blah",20,80);

g.drawString("Blah",20,120);

g.drawString("Blah",20,140);

g.drawString("Blah",20,160);

g.drawString("Blah",20,180);

g.drawString("Press F to start or q to quit",20,200);

}

publicvoid keyDown(Event e,int key)

{

switch(key)

{

case'F':

//MainClass mainClass = new MainClass();//ignore this

break;

case'Q':

System.exit(0);

break;

}

}

}

I know I sound like a noob but I'm having trouble finding this sort of information.

[3282 byte] By [Hard_Wireda] at [2007-11-27 0:28:05]
# 1

You didn't post enough code, and what you did post has syntax errors i.e. Controller callingClass = new CallingClass;

Try having your Intro class extend JPanel and call repaint() (instead of paint() or paintComponent()). Of course you'll need to add the Intro to a JFrame or something...

BinaryDigita at 2007-7-11 22:28:41 > top of Java-index,Java Essentials,New To Java...
# 2
public void keyDown(Event e, int key)Don't use this method. Use a KeyListener instead.
CaptainMorgan08a at 2007-7-11 22:28:41 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks for the advice, I just used the keyDown() method so I wouldn't have to override the methods for the KeyListener interface. I followed BinaryDigit's advice, but I'm still confused, do I need both a JFrame and JPanel to draw to the screen? Am I right in saying that a JPanel resided in a JFrame?

Thanks again for the responses.

Hard_Wireda at 2007-7-11 22:28:41 > top of Java-index,Java Essentials,New To Java...
# 4

> I just used the keyDown()

> method so I wouldn't have to override the methods for

> the KeyListener interface.

Well you should still use a KeyListener.

> I followed BinaryDigit's

> advice, but I'm still confused, do I need both a

> JFrame and JPanel to draw to the screen?

You should have your Intro class extend JPanel (and override paintComponent(), not paint()). You should then use that JPanel as the content pane in a JFrame. And remember to call repaint(), not paint() or paintComponent().

CaptainMorgan08a at 2007-7-11 22:28:41 > top of Java-index,Java Essentials,New To Java...