Results 1 to 8 of 8
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default JFrame window doesnt change background color

    Hai!
    In the following code, this "game" still have same background color.
    PHP Code:
    class gameFrame extends JFrame implements Runnable { //
    	public void run(){
    		paintIt p = new paintIt();
    		add(p);
    	}
    	public gameFrame(){
    		
    		
    		
    		//paintIt p = new paintIt();
    		//add(p);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(1000,1500);
    		setTitle("Image test");
    		getContentPane().setBackground(Color.WHITE);
    		setVisible(true);
    	}
    }
    why?
    Im using a loop to update the images showed on it but Idk.. The frame is never white, always grey..
    How do I fix it?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    what is a paintIt object? Is it a JPanel? Realize that this is being repeatedly added to the JFrame's contentPane BorderLayout.CENTER and will cover up the original contentPane (unless it is not opaque). So it probably doesn't matter what color you set the contentPane as I'll bet you'll never see it.

    BTW, I'm not sure that I'd do my animation this way. I'd use one JPanel as an animation panel and then swap images in the same panel rather than creating and adding a bunch of components. Your way seems somewhat inefficient to me and may risk running out of memory.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    How do you run the program? Why is your JFrame a Runnable?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    ..and may risk running out of memory.
    That has actually already happened before in one of the OP's thread which they abandoned.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by r035198x View Post
    How do you run the program? Why is your JFrame a Runnable?
    My guess is it's a poor-man's Swing Timer. Better to use the real thing.

  6. #6
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Sry, but the runnable part was just leftovers..
    Heres the loop thingy:
    PHP Code:
    class MyTimerActionListener implements ActionListener {
      	public void actionPerformed(ActionEvent e) {
      		if (!(Player.Y+Player.SizeY+2> 400)){
      			Player.CharVel[1] += 2;
      		} else {
      			Player.CharVel[0] = (Player.CharVel[0]/Player.Friction);
      		}
        	if (Player.Y+Player.SizeY> 400){
        		
        		Player.CharVel[1] = -Player.CharVel[1]/2;
        		Player.Y = 400-Player.SizeY;
        	}
        	Player.Y += Player.CharVel[1];
    	   	Player.X += Player.CharVel[0]+Player.CharWalk[0];   
        	TestingArea.gf.repaint();
     	
      	}
      	
      	
    }

  7. #7
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    ahh, furbarable is right, the jpanel covers up the jframe
    so If i did setBackground on jpanel then it shows.
    Niceeee!!
    THanks all!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 4
    Last Post: 03-21-2009, 05:48 PM
  2. my code doesn't change background color
    By javanoobita in forum New To Java
    Replies: 5
    Last Post: 02-22-2009, 04:30 AM
  3. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 07:02 AM
  4. JButton onClick change color background
    By behrk2 in forum AWT / Swing
    Replies: 6
    Last Post: 07-09-2008, 04:54 PM
  5. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 10:43 AM

Posting Permissions

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