Customized component : Customized Component « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Customized Component 




Customized component
Customized component


import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Clock extends JButton implements Customizer, Externalizable,
    Runnable {

  protected PropertyChangeSupport propertyChangeSupport;

  protected boolean isDigital = false;

  protected Calendar calendar = Calendar.getInstance();

  public Clock() {

    propertyChangeSupport = new PropertyChangeSupport(this);

    setBackground(Color.white);
    setForeground(Color.black);

    (new Thread(this)).start();
  }

  public void writeExternal(ObjectOutput outthrows IOException {
    out.writeBoolean(isDigital);
    out.writeObject(getBackground());
    out.writeObject(getForeground());
    out.writeObject(getPreferredSize());
  }

  public void readExternal(ObjectInput inthrows IOException,
      ClassNotFoundException {
    setDigital(in.readBoolean());
    setBackground((Colorin.readObject());
    setForeground((Colorin.readObject());
    setPreferredSize((Dimensionin.readObject());
  }

  public Dimension getPreferredSize() {
    return new Dimension(5050);
  }

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

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

  public void setDigital(boolean digital) {
    propertyChangeSupport.firePropertyChange("digital"new Boolean(
        isDigital)new Boolean(digital));
    isDigital = digital;
    repaint();
  }

  public boolean getDigital() {
    return isDigital;
  }

  public void addPropertyChangeListener(PropertyChangeListener lst) {
    if (propertyChangeSupport != null)
      propertyChangeSupport.addPropertyChangeListener(lst);
  }

  public void removePropertyChangeListener(PropertyChangeListener lst) {
    if (propertyChangeSupport != null)
      propertyChangeSupport.removePropertyChangeListener(lst);
  }

  public void setObject(Object bean) {
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Color colorRetainer = g.getColor();

    g.setColor(getBackground());
    g.fillRect(00, getWidth(), getHeight());
    getBorder().paintBorder(this, g, 00, getWidth(), getHeight());

    calendar.setTime(new Date())// get current time
    int hrs = calendar.get(Calendar.HOUR_OF_DAY);
    int min = calendar.get(Calendar.MINUTE);

    g.setColor(getForeground());
    if (isDigital) {
      String time = "" + hrs + ":" + min;
      g.setFont(getFont());
      FontMetrics fm = g.getFontMetrics();
      int y = (getHeight() + fm.getAscent()) 2;
      int x = (getWidth() - fm.stringWidth(time)) 2;
      g.drawString(time, x, y);
    else {
      int x = getWidth() 2;
      int y = getHeight() 2;
      int rh = getHeight() 4;
      int rm = getHeight() 3;

      double ah = ((doublehrs + min / 60.06.0 * Math.PI;
      double am = min / 30.0 * Math.PI;

      g.drawLine(x, y, (int) (x + rh * Math.sin(ah))(int) (y - rh
          * Math.cos(ah)));
      g.drawLine(x, y, (int) (x + rm * Math.sin(am))(int) (y - rm
          * Math.cos(am)));
    }

    g.setColor(colorRetainer);
  }

  public void run() {
    while (true) {
      repaint();
      try {
        Thread.sleep(30 1000);
      catch (InterruptedException ex) {
        break;
      }
    }
  }

  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    Clock c = new Clock();
    f.getContentPane().add("Center", c);
    f.pack();
    f.setSize(new Dimension(300300));
    f.show();

  }
}


           
       














Related examples in the same category
1.FontChooser dialogFontChooser dialog
2.Oval Panel
3.Ploygon ButtonPloygon Button
4.Demonstrating the Box ComponentDemonstrating the Box Component
5.The MyBean JavaBean Component
6.The TrafficLight Component
7.Alias BeanAlias Bean
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.