G4P Font for Textfield

@quark How would I change the font that a G4P Textfield uses?
Thanks

1 Like

here i play with it:
( note: the processing font used for text() and the G4P font are different things:

/** start from examples (c) 2015 Peter Lager */
// processing 3.5.3 // G4P V4.2.1 // win 7 64bit //

import java.awt.Font;
import g4p_controls.*;
GTextField txf1;
String startTextF = "G4P is a GUI control library created by Peter Lager";
String tfontname = "SimSun";   // change if not available on your PC
PFont  tfont;

void processingtextsetup() {
  String[] fontList = PFont.list();
  printArray(fontList);
  tfont = createFont(tfontname,15);
  textFont(tfont);  
}

void G4Ptextfieldsetup() {
  G4P.setGlobalColorScheme(GCScheme.PURPLE_SCHEME);
  txf1 = new GTextField(this, 10, 10, 400, 20);
  txf1.setFont(new Font(tfontname, Font.PLAIN,15));
  txf1.tag = "txf1";
  txf1.setPromptText(startTextF);  
}

public void setup() {
  size(500, 300);
//  processingtextsetup();  // optional
  G4Ptextfieldsetup();
}

public void draw() {
  background(200, 128, 200);
}

public void displayEvent(String name, GEvent event) {
  String extra = " event fired at " + millis() / 1000.0 + "s";
  print(name + "   ");
  switch(event) {
  case CHANGED:
    println("CHANGED " + extra);
    break;
  case SELECTION_CHANGED:
    println("SELECTION_CHANGED " + extra);
    break;
  case LOST_FOCUS:
    println("LOST_FOCUS " + extra);
    break;
  case GETS_FOCUS:
    println("GETS_FOCUS " + extra);
    break;
  case ENTERED:
    println("ENTERED " + extra);  
    break;
  default:
    println("UNKNOWN " + extra);
  }
}

public void handleTextEvents(GEditableTextControl textControl, GEvent event) { 
  displayEvent(textControl.tag, event);
}

2 Likes

As shown by @kll G4P text controls use a different approach to handling fonts than Processing. If you are wondering why, it is because G4P allows mixed fonts and styling in the same control, try out the G4P_TextAreaControl example that comes with the library, but that is another story.

You can use virtually any font that is stored on your computer the sketch code below has the folloing display.

46

If you run this example on your computer you will not get the same result because you probably don’t have some of the fonts used installed on your computer.


import java.awt.Font;

GTextField txf0, txf1, txf2, txf3;
Font f0, f1, f2, f3;

void setup() {
  size(400, 200);
  // Text field 0
  txf0 = new GTextField(this, 20, 20, 360, 20);
  txf0.setText("Bold italic text");
  f0 = new Font("Arial", Font.BOLD | Font.ITALIC, 14);
  txf0.setFont(f0);
  // Text field 1
  txf1 = new GTextField(this, 20, 50, 360, 20);
  txf1.setText("Consolas plain");
  f1 = new Font("Consolas", Font.PLAIN, 14);
  txf1.setFont(f1);
  // Text field 1
  txf2 = new GTextField(this, 20, 80, 360, 40);
  txf2.setText("Lucida Handwriting");
  f2 = new Font("Lucida Handwriting", Font.PLAIN, 20);
  txf2.setFont(f2);
  // Text field 1
  txf3 = new GTextField(this, 20, 140, 360, 40);
  txf3.setText("Dracula's blood");
  f3 = new Font("Blood of Dracula", Font.PLAIN, 24);
  txf3.setFont(f3);
}

void draw() {
  background(200, 200, 255);
}
2 Likes