Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

setBackground()

843807Aug 22 2010 — edited Aug 22 2010
Hi,
Could anybody help with this problem which involves the setBackground() the problem is is that the screen should be Pink and it just keeps showing Black and it doesnt matter what color I set the seBackground to? it just shows black.
package javaapplication1;

import java.awt.*;

import javax.swing.JFrame;

public class Main extends JFrame
{
   public static void main(String []args)
   {


         DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
         Main b = new Main();
         b.run(dm);
      }

   /* instance methods */
   public void run(DisplayMode dm)
   {
      
      setBackground(Color.PINK);

      setForeground(Color.BLUE);

      setFont(new Font("Arial",Font.PLAIN,24));

      Screen s = new Screen();
      try
      {
         s.setFullScreen(dm,this);
         try
         {
            Thread.sleep(5000);
         }
         catch(Exception ex){}
      }
      finally
      {
         s.restoreScreen();
      }
   }
   public void paint(Graphics g)
   {
      if (g instanceof Graphics2D)
      {
         Graphics2D g2 = (Graphics2D)g;
         g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      }
      g.drawString("Hello World",200,200);
   }

}
package javaapplication1;

import java.awt.*;

import javax.swing.JFrame;

public class Screen
{
	/* instance variables */
  private GraphicsDevice vc;
	/**
	 * Default constructor for objects of class Screen
	 */
  public Screen()
  {
      super();
      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
      vc = env.getDefaultScreenDevice();
  }


	/* instance methods */
   public void setFullScreen(DisplayMode dm, JFrame window)
   {
      window.setUndecorated(true);
      window.setResizable(false);
      vc.setFullScreenWindow(window);

      if(dm != null && vc.isDisplayChangeSupported())
      {
         try
         {
            vc.setDisplayMode(dm);
         }
         catch(Exception ex)
         {
         }
      }
   }

   public Window getFullScreen()
   {
      return vc.getFullScreenWindow();
   }

   public void restoreScreen()
   {
      Window w = vc.getFullScreenWindow();
      if(w != null)
      {
         w.dispose();
      }
      vc.setFullScreenWindow(null);
   }

}

Comments

843807
You're trying to set the background of the JFrame when you need to be setting the background of its contentPane. This can be solved by calling
getContentPane().setBackground(Color.PINK);
But more importantly I'm concerned that you're overriding the JFrame's paint method which you shouldn't be doing. Better still is to do all this work in a JPanel, including painting in the JPanel in it's paintComponent method, and setting the JPanel's background color. Then you can add this JPanel to the JFrame's contentPane in the BorderLayout.CENTER position, and you're all set.
843807
Also whenever you override paint (which should be rare), you usually should call the super's paint method first thing in the paint override method:
   public void paint(Graphics g) {
      super.paint(g);  // **** don't forget this ****
      if (g instanceof Graphics2D) {
         Graphics2D g2 = (Graphics2D) g;
         g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                  RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      }
      g.drawString("Hello World", 200, 200);
   }
Also, same for calling the super's paintComponent method if you are overriding the paintComponent of a JComponent such as a JPanel.
darrylburke
Note: This thread was originally posted in the [New To Java|http://forums.sun.com/forum.jspa?forumID=54] forum, but moved to this forum for closer topic alignment.
darrylburke
Encephalopathic wrote:
if (g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D) g;
}
I believe that since Java 1.2, every Graphics object is a Graphics2D. This is the most authentic reference to that I could find.
[JavaOne 2003 Notes|http://www.unidata.ucar.edu/staff/caron/java/JavaOne2003.html#Desktop] (scroll down to Swing Tips -- Graphics
Graphics Object
can always be cast Graphics2D g2d = (Graphics2D)g;


Then there are a few old threads in this forum that bear this out.
[http://forums.sun.com/thread.jspa?messageID=10875255#10875255]
As of Java 1.2 all Graphics objects, no matter how you obtained it, are Graphics2D objects. - Maxideon

[http://forums.sun.com/thread.jspa?messageID=2944067#2944067]
The reason it is valid to cast from Graphics to Graphics2D, is because Sun have said that all Graphics objects returned by the API in Java 1.2 or above will be a subclass of Graphics2D. - Abuse (Yes, that's really a username)

db
1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 19 2010
Added on Aug 22 2010
4 comments
1,025 views