Results 1 to 7 of 7
  1. #1
    Oleg is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default Problems with setBackground()

    What's up guys!

    It is my very first thread in here. I am a beginner in Java Programming.
    The program should display a FullScreen Window with a setBackground Color
    and some Message.

    at Line 13 in orange.java: I set the Background to PINK.
    However the screen remains BLACK. The Foreground is displayed correctly.
    Please help me to understand the Problem, why I can not set the Background color.

    Two files are attached.
    Attached Files Attached Files

  2. #2
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    I think the problem is that is your class orange, in the paint method, you don't call super.paint( g ); . If you place this statement on top of your paint method, the background will become pink.

    Hannes

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    It is my very first thread in here.
    To increase your chances of getting help you should create a Short, Self Contained, Compilable and Executable, Example Program (SSCCE), that demonstrates the incorrect behaviour.

  4. #4
    Oleg is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by hannes View Post
    I think the problem is that is your class orange, in the paint method, you don't call super.paint( g );
    I added super.paint( g ) as you said. Now the Background is always Gray, whatever I specify in Line 13 for setBackground(Color.***).:confused:

  5. #5
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    have a look at Painting in AWT and Swing. There you can find a explanation of how the painting in awt and swing works, and what methods you should override.

    Hannes

  6. #6
    Oleg is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    I got my Problem solved by a user from another forum!
    One should use ContentPane from JPanel.:) Still have to study that stuff carefully.
    Java Code:
    public void run(DisplayMode dm){
            getContentPane().setBackground(Color.BLUE);
            setForeground(Color.RED);
          ...
        }
    
        public void paint(Graphics g){
            super.paint(g); // Der super-Aufruf sollte bei überschriebenen paint*-Methoden immer die erste Zeile sein!
                if(g instanceof Graphics2D){
    ...
        }

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Oleg View Post
    I got my Problem solved by a user from another forum!
    One should use ContentPane from JPanel.:) Still have to study that stuff carefully.
    A JPanel doesn't have a content pane. What you should do is override the paintComponent( ... ) method, not the paint( ... ) method. As the first statement your method should call its overriden implementation to draw the entire background.

    kind regards,

    Jos
    Last edited by JosAH; 03-07-2010 at 05:10 PM.

Similar Threads

  1. JButton.setBackground() Problem
    By ellias2007 in forum AWT / Swing
    Replies: 2
    Last Post: 02-24-2010, 09:49 PM
  2. Gui problems
    By bulldog in forum Advanced Java
    Replies: 1
    Last Post: 12-11-2009, 12:35 PM
  3. having problems
    By pinkdiamondgail in forum New To Java
    Replies: 6
    Last Post: 02-06-2009, 11:35 PM
  4. many to many problems
    By cecily in forum JDBC
    Replies: 1
    Last Post: 08-02-2007, 05:51 PM
  5. problems with JPA
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •