#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929

    Question SetBackground() not working on mac


    I'm taking a stab at Java and built a little applet that uses a number of buttons with different background colors. I'm using the member function setBackground(). When I view the applet under windows or Linux everything comes out fine: the buttons are colored and correctly change as I interact with the applet. When someone viewed it on a mac, they saw no color differences and from the screenshot it appears that I'm not even getting the bland buttons normally associated with Java but some 3D-gradient overworked GUI monstrosity. I've tested it both with Button and JButton (no background colors appear for either on the mac) and also by writing my own paint() function (which works, but looks really ugly).

    Is there a way to get the mac to treat the buttons in the same way the PC does?
  2. #2
  3. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    423
    Welcome to the Dev Shed. In java, those "functions" you are talking about are called methods. =)

    Could you please post some code, that would help immensely, and maybe some screenshots of these "ugly" guis you are talking about.

    I don't know about making the mac buttons the same as windows, I will have to check.
    EDIT: I just kinda skimmed through THIS, but it looks like what you want.
    Last edited by tagmanadvance; May 11th, 2007 at 01:23 AM.
    "Java makes impossible things possible, but makes easy things difficult." - Somebody
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Thank you, that looks promising. I'll send that out and see if it makes a difference on the mac.

    The ugly GUI results from two things: First in that case I am just running paint() with nothing more than a fillRect() so I don't see a clean button (which could be fixed with a little work). The other problem is that in the following code the buttons will update appearance as the function executes if I use Button, when I implement my own paint() or use JButton the appearance doesn't change until after all the processing is done. I figure this is probably due to my blatant misuse of GUI elements and delays though and not any fault of Java's.

    In the meanwhile, here's some of the relevant code with some of the redundant/irrelevant bits chopped out. I have a two dimensional array of IndexButton (which is nothing more than a normal JButton that stores a few parameters). expandCross() sets the background color of these buttons to black in a + shape starting from some initial point.

    java Code:
     
    boolean expandCross( int x, int y ) {
    	boolean dir1,dir2,dir3,dir0;
    	dir0 = true;
    	dir1 = true;
    	dir2 = true;
    	dir3 = true;
     
    	// Check if initial square is OK and fill it
    	if( bgrid[x][y].valid ) {
    		bgrid[x][y].valid = false;
    		recolor( bgrid[x][y] );
    	}
    	else {
    		return(false);
    	}
     
    	// iterate through the squares N away orthogonally
    	for( int off = 1; off < SIZE_GRID; off++ )
    	{
    		// For each direction test if
    		// * we're inside the bounds
    		// * This direction is not already finished
    		// * The square is valid
    		if( x-off >= 0 && dir0 && bgrid[x-off][y].valid ) {
    			recolor( bgrid[x-off][y] );
    			bgrid[x-off][y].valid = false;
    		}
    		else
    			dir0 = false;
     
    // ... The above if-else repeats for the other three directions ...
     
    		// if we're finished in every direction, end
    		if( !(dir0 || dir1 || dir2 || dir3) ) {
    			break;
    		}
     
    		// delay
    		try { Thread t = new Thread(); t.sleep(100); }
    		catch( Exception ex ) { }
    	}
    	return(true);
    }
     
    void recolor( IndexButton b )
    {
    	if( b.type ) {
    		b.setBackground( new Color( 0.0f, 0.0f, 0.75f ) );
    	}
    	else {
    		b.setBackground( Color.BLACK );
    	}
    }
  6. #4
  7. Kage Bunshin
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    The Seven Seas Of Rhye
    Posts
    930
    Rep Power
    423
    Why not just use an image as the button background?

    EDIT: Nevermind, I see the whole point was to set the background color with Java.

    Well, you say it works with Windows and Linux. I would try setting the GUI to Javas default theme and see if that fixes it for you.

    Maybe THIS will help?
    Last edited by tagmanadvance; May 11th, 2007 at 11:26 AM.
    "Java makes impossible things possible, but makes easy things difficult." - Somebody
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Yes! That works.

    I added
    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    and was told it now looks right on a mac (it doesn't animate on mac, but that was bad code to begin with).

    Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo