12, జనవరి 2014, ఆదివారం

JAVA-BEAN TUTORIAL



Manifest-Version: 1.0

Name: edu/unc/sunsite/cafeaulait/StringBean.class
Java-Bean: True 
after name and java-bean:  one space and end of the line u just press enter it also
play a crucial role 
 
be careful while writing the manifest file and as well as creation of jar file 


01/12/2014  12:05 PM               447 Colors$1.class
01/12/2014  12:05 PM             1,343 Colors.class
01/12/2014  12:05 PM             1,078 Colors.java
01/12/2014  03:01 PM                43 Colors.mft
D:\b>jar -cmf Colors.mft ramu.jar *.class


01/12/2014  02:30 PM             3,028 StringBean.class
01/12/2014  02:30 PM             2,511 StringBean.java
01/12/2014  03:10 PM                41 StringBean.mft 
 
creation of jar files  
01/12/2014  03:05 PM             3,305 ramu.jar
01/12/2014  02:34 PM             3,286 dayina.jar
               


D:\b>jar -cmf StringBean.mft dayina.jar *.class









example-1
import java.awt.*;
import java.awt.event.*;
public class Colors extends Canvas
{
  transient private Color color;
  private boolean rectangular;
  public Colors()
  {
    addMouseListener(new MouseAdapter()
     { 
       public void mousePressed(MouseEvent me)
       {
         change();
       } 
      });
     rectangular=false;
     setSize(200,100);
     change();
  }
 
 public boolean getRectangular()
 {
   return rectangular;
  }

 public void setRectangular(boolean flag)
 {
    this.rectangular=flag;
    repaint();
 }
 public void change()
 {
  color=randomColor();
  repaint();
 }

 private Color randomColor()
 { 
  int r=(int) (255*Math.random());
  int g=(int) (255*Math.random());
  int b=(int) (255*Math.random());
  return new Color(r, g, b);
 }
 
 public void paint(Graphics g)
 {
   Dimension d=getSize();
   int h=d.height;
   int w=d.width;
   g.setColor(color);
   if(rectangular)
    {
       g.fillRect(0, 0, w-1, h-1);
     }
    else
    {
      g.fillOval(0, 0, w-1, h-1);
    }
  }

} 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
example-2
package edu.unc.sunsite.cafeaulait;
import java.awt.*;
public class StringBean extends Component {

  // properties
  private String text;
  
  public static void main(String[] args) {
  
    Frame f = new Frame("String Beans");
    f.setLayout(new GridLayout(4, 1));
    f.setSize(300, 400);
    f.setLocation(100, 100);
    // test the default constructor
    StringBean sb = new StringBean();
    f.add(sb);
    
    sb = new StringBean("I am a bean!");
    f.add(sb);
    
    Font fo = new Font("Helvetica", Font.BOLD, 24);
    sb = new StringBean("I am a bold green bean!", Color.green, fo);
    f.add(sb);
    
    sb = new StringBean("I am an italic red bean!", Color.red, 
     "TimesRoman", Font.ITALIC, 18);
    f.add(sb);
    
    f.show();
    
  }
  
  public StringBean() {
    this("Hello", Color.black, new Font("Helvetica", Font.PLAIN, 12));
  }

  public StringBean(String text) {
    this(text, Color.black, new Font("Helvetica", Font.PLAIN, 12));
  }

  public StringBean(String text, Color c) {
    this(text, c, new Font("Helvetica", Font.PLAIN, 12));
  }

  public StringBean(String text, Color c, String fontName) {
    this(text, c, new Font(fontName, Font.PLAIN, 12));
  }

  public StringBean(String text, Color c, String fontName, int fontSize) {
    this(text, c, new Font(fontName, Font.PLAIN, fontSize));
  }

  public StringBean(String text, Color c, String fontName, int fontStyle, int fontSize) {
    this(text, c, new Font(fontName, fontStyle, fontSize));
  }

  public StringBean(String text, Font f) {
    this(text, Color.black, f);
  }

  public StringBean(String text, Color c, Font f) {
    this.text = text;
    super.setFont(f);
    super.setForeground(c);
  }

  public String getText() {
    return text;
  }

  public void setText(String s) {
    text = s;
  }

  public void paint(Graphics g) {
  
    Font f = super.getFont();    
    FontMetrics fm = super.getFontMetrics(f);
    g.setFont(f);
    g.setColor(super.getBackground());
    Rectangle r = super.getBounds();
    g.fillRect(r.x, r.y, r.width, r.height);
    g.setColor(super.getForeground());
    g.drawString(text, 5, fm.getMaxAscent());  

  }

  public Dimension getMinimumSize() {
    return getPreferredSize();
  }
  
  public Dimension getPreferredSize() {

    FontMetrics fm = super.getFontMetrics(super.getFont());
    return new Dimension(fm.stringWidth(text) + 10, fm.getMaxAscent() 
     + fm.getMaxDescent());
    
  }

}
 
 
if you succussfully execuate the concept u will see the output like below
 
 
 
 U 
http://aspen.ucs.indiana.edu/webtech/javabeans/examples/TextEditor/sunw/demo/TextEditor/index.html 

U CAN see at the above link a bean project 
 
 

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి