Reputation: 147
So I'm doing this project to display certain fonts. The problem is that The original code works But When I try to use a scanner it doesn't
Original Code
public static void main(String[]args)
{
font("Times New Roman");
}
public static void font(String x){
World canvas = new World();//Creates a new world called canvas
Graphics picGraphics = canvas.getGraphics();//creates picGraphics which gets used in the world
picGraphics.setColor(Color.MAGENTA);//Color is set to Magenta
Font font = new Font(x,Font.BOLD,80);
picGraphics.setFont(font);//sets the font
//drawString parameters tell the string that is written and the coordinates to place the word
picGraphics.drawString("Font Tester",105,255);
canvas.repaint();
}
Now with the code with the scanner
public static void font2(){
World canvas = new World();//Creates a new world called canvas
Graphics picGraphics = canvas.getGraphics();//creates picGraphics which gets used in the world
picGraphics.setColor(Color.MAGENTA);//Color is set to Magenta
Scanner keyboard=new Scanner(System.in);
String x = keyboard.next();
Font font = new Font(x,Font.BOLD,80);
picGraphics.setFont(font);//sets the font
//drawString parameters tell the string that is written and the coordinates to place the word
picGraphics.drawString("Font Tester",105,255);
canvas.repaint();
}
As you see with that code I have a scanner so that the user can directly input the font they want into the program to be displayed. However whenever I use that new code with an example such as Time New Roman the font that displays in the window is not even Times new Roman but more like Arial... I have also tried putting the scanner in the main method which also doesn't give any different of a result
Help?
BTW: A lot of preset code has been added already so that codes such as World canvas = new World(); and other bits will help with the display of the pop up blank window where i can display the fonts
Thanks
Upvotes: 0
Views: 142
Reputation: 111409
The Scanner.next
method is returning only the first word of your input, which would be Times
, and the system can't find a font with that name.
You can use the nextLine
method to read an entire line with whitespace between the words:
String x = keyboard.nextLine();
Upvotes: 1