Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Setbackground doesn't work for me Solution for it?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Setbackground doesn't work for me Solution for it?

    I'm currently trying to make a game with Eclipse I have ran into one problem.

    When I try and set a background color Nothing shows up, I think it's because "setBackground" is bug for me.
    Is there another way instead of using "setBackground"?

    package Default.Package;
     
    import javax.swing.*;
    import java.awt.*;
     
     
     
     
    public class AC extends JComponent {
     
        public void ContentPane() {
            setBackground(Color.PINK);
        }
     
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
            Graphics2D g2d = (Graphics2D) g;
     
            //Begin drawing
        }
     
        public static void main(String[] args) {
            JFrame gui = new JFrame("Window Title");
            AC cp = new AC();
     
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setSize(800, 600);
            gui.setResizable(false);
            gui.setLocationRelativeTo(null);
     
            gui.setContentPane(cp);
            gui.setVisible(true);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Setbackground doesn't work for me Solution for it?

    Can you post a small simple program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Setbackground doesn't work for me Solution for it?

    package Default.Package;
     
    import javax.swing.*;
    import java.awt.*;
     
     
     
     
    public class AC extends JComponent {
     
        public void ContentPane() {
            setBackground(Color.PINK);
        }
     
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
            Graphics2D g2d = (Graphics2D) g;
     
            //Begin drawing
        }
     
        public static void main(String[] args) {
            JFrame gui = new JFrame("Window Title");
            AC cp = new AC();
     
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setSize(800, 600);
            gui.setResizable(false);
            gui.setLocationRelativeTo(null);
     
            gui.setContentPane(cp);
            gui.setVisible(true);
        }
    }

    This is my setup. When I click Run.Ac.java I can get the name on the top right. but I can't get anything to show up on the screen/client area.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Setbackground doesn't work for me Solution for it?

    Your code does not demonstrate your error leaving us completely in the dark as to figuring out how to help you. Consider creating and posting an SSCCE.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Setbackground doesn't work for me Solution for it?

    Add some debugging code to print out the values used in the paintComponent() method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] .equals doesn't work?
    By Purple01 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 14th, 2012, 03:20 AM
  2. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 03:25 AM
  3. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 10:19 PM
  4. serato_sync command .. doesn't work
    By bizzyj237 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 15th, 2011, 03:38 AM
  5. pathSeparatorChar doesn't work
    By ordonezc78 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 12:25 PM