d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Java Question
Prev12
Add Reply New Topic New Poll
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Sep 24 2013 08:58pm
Quote (SoftSpeaker @ Sep 24 2013 09:03pm)
Code
static final String FONT_NAMES[] = {"TimesRoman", "Courier", "Dialog"};
static final String FONTS[] = {TimesRoman, Courier, Dialog};


String typeFace = INITIAL_FACE;    // current typeface
int typeStyle = INITIAL_STYLE;      // current type style
int typeSize = INITIAL_SIZE;        // current type size

Font TimesRoman = new Font ("TimesRoman", typeStyle, typeSize);
Font Courier = new Font ("Courier", typeStyle, typeSize);
Font Dialog = new Font ("Dialog", typeStyle, typeSize);


So the declarations at the bottom don't help the ones in the array?


No, how are they linked at all?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 24 2013 09:26pm
Quote (SoftSpeaker @ Sep 24 2013 10:36pm)
I'm lost once again.


did you not see my post # 5? i show you how to use a font array
Member
Posts: 15,815
Joined: Aug 12 2009
Gold: 0.00
Sep 24 2013 09:36pm
Quote (carteblanche @ Sep 24 2013 11:26pm)
did you not see my post # 5? i show you how to use a font array


My java skills are very limited, just started this semester and I haven't been taught much. I attempted to use the array not sure if i did right.

Here is the latest version of the code, i commented out things that were making it not work: http://pastebin.com/kenXGbbJ

Code

String FONTS[] = {TimesRoman, Courier, Dialog};


Code

text.setFont(FONTS[faceFont.getSelectedIndex()]);
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 24 2013 09:49pm
Quote (SoftSpeaker @ Sep 24 2013 11:36pm)
My java skills are very limited, just started this semester and I haven't been taught much. I attempted to use the array not sure if i did right.

Here is the latest version of the code, i commented out things that were making it not work:  http://pastebin.com/kenXGbbJ

Code
String FONTS[] = {TimesRoman, Courier, Dialog};


Code
text.setFont(FONTS[faceFont.getSelectedIndex()]);


you declared that as a string. i assume that compiled because it calls toString() on your font objects. you must declare it as type Font.
Member
Posts: 15,815
Joined: Aug 12 2009
Gold: 0.00
Sep 24 2013 09:53pm
Quote (carteblanche @ Sep 24 2013 11:49pm)
you declared that as a string. i assume that compiled because it calls toString() on your font objects. you must declare it as type Font.


Wow I feel like an idiot.

Thanks for the explanation, it works now.
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Sep 25 2013 10:18am
Quote (SoftSpeaker @ Sep 24 2013 10:53pm)
Wow I feel like an idiot.

Thanks for the explanation, it works now.


As a stylistic note:

You are declaring an array like this:

Code
String array[] = ...;


Don't do this. It's a holdover from C (which Java borrowed a lot of it's syntax from) and the more commonly accepted and easier to read form is:

Code
String[] array = ...;


Also, try not to declare your non-static fields and variables with all caps.

Code
static String[] ARRAY = ...; // good
String[] ARRAY = ...; // not good
String[] array = ...; // good


Following general styling conventions for your language makes the code easier to read for others.

edit: Here's the style guide direct from the horse's (Oracle) mouth: http://www.oracle.com/technetwork/java/codeconv-138413.html

This post was edited by rockonkenshin on Sep 25 2013 10:19am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 25 2013 10:27am
Quote (rockonkenshin @ Sep 25 2013 12:18pm)

Also, try not to declare your non-static fields and variables with all caps.



i thought upper case is for final static? shrug
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Sep 25 2013 10:32am
Quote (carteblanche @ Sep 25 2013 11:27am)
i thought upper case is for final static? shrug


Generally speaking it is, yeah. Really if you are making a field static and non-final you are probably doing something very, very wrong.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 25 2013 11:13am
put your fonts in an array, then when you select a new font, get the index, that will be the font you selected in the array

Code
faceFont = new Choice( );
final Font fonts[] = { TimesRoman, Courier };

for ( Font font : fonts ) {
faceFont.add( font.getName( ) );
}

panel.add( faceFont );
faceFont.addItemListener( new ItemListener( ) {
public void itemStateChanged( ItemEvent event ) {
text.setFont( fonts[faceFont.getSelectedIndex( )] );

}
} );


This post was edited by labatymo on Sep 25 2013 11:13am
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll