Tuesday, December 23, 2014

Display text diffrent and line using GUI and Create a frame in java pro



Display text in different fonts

import java.awt.*;
import java.awt.event.*
import javax.swing.*
public class Main extends JPanel
{
   String[] type = { "Serif","SansSerif"};
      int[] styles = { Font.Plain, Font.Italic, Font.Bold,
      Font.Italic + Font.Bold };
      String[] stylenames =
      { "Plain", "Italic", "Bold", "Bold & Italic" };
      public void paint(Graphics g)
     {
         for (int f = 0; f < type.length; f++)
         {
            for (int s = 0; s < styles.length; s++)
           {
               Font font = new Font(type[f], styles[s], 18);
               g.setFont(font);
               String name = type[f] + " " + stylenames[s];
               g.drawString(name, 20, (f * 4 + s + 1) * 20);
            }
         }
      }
      public static void main(String[] a)
     {
         JFrame f = new JFrame();
         f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
           {
               System.exit(0);
            }
         }
      );
      f.setContentPane(new Main());
      f.setSize(400,400);
      f.setVisible(true);
   }
}

Display line using GUI

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class Main extends JApplet 
{
   public void init() 
  {
      setBackground(Color.white);
      setForeground(Color.white);
   }
   public void paint(Graphics g) 
  {
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.key_antialiasing,
      RenderingHints.value_antialias_on);
      g2.setPaint(Color.gray);
      int x = 5;
      int y = 7;
      g2.draw(new Line2D.Double(x, y, 200, 200));
      g2.drawString("Line", x, 250);
   }
   public static void main(String s[]) 
   {
      JFrame f = new JFrame("Line");
      f.addWindowListener(new WindowAdapter() 
     {
         public void windowClosing(WindowEvent e) 
        {
            System.exit(0);
         }
      });
      JApplet applet = new Main();
      f.getContentPane().add("Center", applet);
      applet.init();
      f.pack();
      f.setSize(new Dimension(300, 300));
      f.setVisible(true);
   }
}

Create a frame

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel 
{
   public void paint(Graphics g) 
   {
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.key_antialiasing,
      RenderingHints.value_antialias_on);
      g2.setFont(new Font("Serif", Font.PLAIN, 48));
      paintHorizontallyCenteredText(g2, "Java Source", 200, 75);
      paintHorizontallyCenteredText(g2, "and", 200, 125);
      paintHorizontallyCenteredText(g2, "Support", 200, 175);
   }
   protected void paintHorizontallyCenteredText(Graphics2D g2,
   String s, float centerX, float baselineY) 
   {
      FontRenderContext frc = g2.getFontRenderContext();
      Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
      float width = (float) bounds.getWidth();
      g2.drawString(s, centerX - width / 2, baselineY);
   }
   public static void main(String[] args) 
  {
      JFrame f = new JFrame();
      f.getContentPane().add(new Main());
      f.setSize(450, 350);
      f.setVisible(true);
   }
}

No comments:

Post a Comment