Reputation: 258
My problem is that when trying to set the background color in a JApplet i'm trying to create, i am unable to get any color other than the default gray.
I would really appreciate it if someone was able to not only tell me whats wrong but also explain it. This technique was learned through a textbook and so, i want to understand the problem so that i know what is going on.
Any and all help is appreciated.
Thank you in advance,
SDG
public class QuoteApplet extends JApplet
{
public void paint (Graphics appPage)
{
setBackground(Color.YELLOW);
appPage.drawRect(65,55,255,100);
//page.drawRect(60,80,225,30);
//page.drawOval(75,65, 20, 20);
appPage.drawLine(30,30,30,100);
appPage.drawLine(40,30,40,100);
appPage.drawLine(55,45,15,85);
appPage.drawString("There once lived a man named Oedipus Rex.", 70, 70);
appPage.drawString("You may have heard about his odd complex.", 70, 90);
appPage.drawString("His name appears in Freud's index,", 70, 110);
appPage.drawString("'cause he loved his mother.", 70, 130);
appPage.drawString("-Tom Lehrer", 200, 150);
}
}
Upvotes: 3
Views: 2680
Reputation: 285405
You want to set the background color of the applet's contentPane, not the applet itself. So call getContentPane().setBackground(...)
. And you don't want to do this from within the paint method. Instead do it in init()
. In fact, it is rare that you'll ever want to override a JApplet's paint method, and certainly not here. You're much better drawing in the paintComponent(...)
method of a JPanel or other class that derives from JComponent and then adding that to the contentPane, or using it as the contentPane.
Upvotes: 6