...

View Full Version : How do I set font size in java



trigger_tre
12-06-2007, 06:29 PM
Hello I am working on an extra credit assignment. Its a simple road sign. I have completed it but I am confused on how to increase the font size.

Here is the component:


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;
import javax.swing.JComponent;

/**
A component that draws an alien face
*/
public class ExtraCreditComponent_TT extends JComponent
{
public void paintComponent(Graphics g)
{
// Recover Graphics2D
Graphics2D g2 = (Graphics2D) g;



// Construct a rectangle with rounded corners
g2.drawRoundRect( 150, 150, 300, 300, 10, 10 );

// Draw the red circle
Ellipse2D.Double circle = new Ellipse2D.Double(155, 155, 290, 290);
g2.setColor(Color.RED);
g2.fill(circle);
g2.draw(circle);

// Draw the white bar
Rectangle mouth = new Rectangle(175, 275, 250, 50);
g2.setColor(Color.WHITE);
g2.fill(mouth);

// Draw the greeting
g2.setColor(Color.WHITE);
g2.drawString("DO NOT", 200, 250);

// Draw the greeting
g2.setColor(Color.WHITE);
g2.drawString("Enter", 200, 350);
}
}

Here is my Viewer file:


import javax.swing.JFrame;

public class ExtraCreditViewer_TT
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setTitle("Do Not Enter Sign");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ExtraCreditComponent_TT component = new ExtraCreditComponent_TT();
frame.add(component);

frame.setVisible(true);
}
}


I want to increase the size of the font but I am confused and was unable to google a suitable answer.

Is there a g2.something that I can input the parameter for?

-trigger

Aradon
12-06-2007, 07:27 PM
Java World gives a pretty easy solution to understand as well as an explanation.

Java World (http://www.javaworld.com/javaworld/javatips/jw-javatip81.html)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum