Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2006
    Posts
    290
    Thanks
    6
    Thanked 14 Times in 14 Posts

    How do I set font size in java

    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:

    Code:
    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:

    Code:
    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

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Java World gives a pretty easy solution to understand as well as an explanation.

    Java World
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •