Home arrow static arrow Java Programming [Archive] - How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?
This topic has 11 replies on 1 page.

Posts:15
Registered: 2/11/02
How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:29 AM



 
Hi,

Font has 3 constants for the style: BOLD, ITALIC and PLAIN;

We can construct an Font instance like that:
Font font = new Font(null, Font.BOLD | Font.ITALIC);
The constuctor allow us to mix of the constants.

Can anybody guide me how to split the BOLD and ITALIC ? I wanna to declare few constants and allow user to mix of some of them.

Thanks.
 

Posts:18,384
Registered: 21.03.00
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:33 AM (reply 1 of 11)



 
What???

Just write them as:
int bold = Font.BOLD;
int italic = Font.ITALIC;

Or??

/Kaj
 

Posts:15
Registered: 2/11/02
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:39 AM (reply 2 of 11)



 
Sorry, you are misunderstanding of what I ask.

I put it simpler,

I've few constants to describe Apple:
size (BIG, SMALL), color (RED, GREEN) and etc.

There is one method "setDescribe(int d)", u can code like that "setDescribe(RED | BIG)".

Then, how to split the parameter " i " and retrieve RED and BIG from it ?

Thanks.
 

Posts:5,904
Registered: 04/03/99
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:42 AM (reply 3 of 11)



 
Use the bitwise AND operator "&".
if( i & RED ) {    // i has the RED bit set.}

But I'd suggest a spot of googling and learn how (and why) this works before you go ahead. Search for "bitwise operators java".

Dave.
 

Posts:342
Registered: 5/22/98
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:42 AM (reply 4 of 11)



 
Use binary & operator:
if ((b & RED) == RED) {    // It's red}


Graeme
 

Posts:342
Registered: 5/22/98
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:47 AM (reply 5 of 11)



 
Use the bitwise AND operator "&".

Beat me to it.

And yes, I should have said 'bitwise' not binary. I'm really going senile.
Graeme
 

Posts:15
Registered: 2/11/02
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:52 AM (reply 6 of 11)



 
Hello,

There is one case:
1 | 3 = 3

And,

3 & 2 = 2

Is it an Expection ?
 

Posts:18,384
Registered: 21.03.00
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:54 AM (reply 7 of 11)



 
Hi,

The number that gets or:ed are always 1,2,4,8,16 (i.e powers of two). That us a must if you should be able to split them later on.

/Kaj
 

Posts:18,384
Registered: 21.03.00
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:55 AM (reply 8 of 11)



 
So, yes. That is expected.
/Kaj
 

Posts:15
Registered: 2/11/02
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 1:57 AM (reply 9 of 11)



 
Hi,

According to JavaDoc, BOLD, ITALIC and PLAIN are 0, 1, 2.
public static final int BOLD = 1
public static final int ITALIC = 2
public static final int PLAIN = 0

But they still can work in this method..
Do you have any ideas on it ?

Thanks.
 

Posts:18,384
Registered: 21.03.00
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 2:07 AM (reply 10 of 11)



 
Yes, as you can see, they are all powers of two. Look at them binary.

0 = 00000000
1 = 00000001
2 = 00000010

/Kaj
 

Posts:15
Registered: 2/11/02
Re: How to split constants from "new Font(null, Font.BOLD | Font.ITALIC)" ?  
Jul 19, 2004 2:14 AM (reply 11 of 11)



 
Hi kajbj,

I've known what do u mean.

thanks. :)
 
This topic has 11 replies on 1 page.