Label with various effects : Label « Swing JFC « Java

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » LabelScreenshots 
Label with various effects
Label with various effects
 

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

public class JLabel2D extends JLabel {
  public static final int EFFECT_PLAIN = 0;

  public static final int EFFECT_GRADIENT = 1;

  public static final int EFFECT_IMAGE = 2;

  public static final int EFFECT_IMAGE_ANIMATION = 3;

  public static final int EFFECT_COLOR_ANIMATION = 4;

  protected int effectIndex = EFFECT_PLAIN;

  protected double shearFactor = 0.0;

  protected Color outlineColor;

  protected Stroke stroke;

  protected GradientPaint gradient;

  protected Image foregroundImage;

  protected Thread animator;

  protected boolean isRunning = false;

  protected int m_delay;

  protected int m_xShift;

  public JLabel2D() {
    super();
  }

  public JLabel2D(String text) {
    super(text);
  }

  public JLabel2D(String text, int alignment) {
    super(text, alignment);
  }

  public void setEffectIndex(int e) {
    effectIndex = e;
    repaint();
  }

  public int getEffectIndex() {
    return effectIndex;
  }

  public void setShearFactor(double s) {
    shearFactor = s;
    repaint();
  }

  public double getShearFactor() {
    return shearFactor;
  }

  public void setOutlineColor(Color c) {
    outlineColor = c;
    repaint();
  }

  public Color getOutlineColor() {
    return outlineColor;
  }

  public void setStroke(Stroke s) {
    stroke = s;
    repaint();
  }

  public Stroke getStroke() {
    return stroke;
  }

  public void setGradient(GradientPaint g) {
    gradient = g;
    repaint();
  }

  public GradientPaint getGradient() {
    return gradient;
  }

  public void setForegroundImage(Image img) {
    foregroundImage = img;
    repaint();
  }

  public Image getForegroundImage() {
    return foregroundImage;
  }

  public void startAnimation(int delay) {
    if (animator != null)
      return;
    m_delay = delay;
    m_xShift = 0;
    isRunning = true;
    animator = new Thread() {
      double arg = 0;
      public void run() {
        while (isRunning) {
          if (effectIndex == EFFECT_IMAGE_ANIMATION)
            m_xShift += 10;
          else if (effectIndex == EFFECT_COLOR_ANIMATION
              && gradient != null) {
            arg += Math.PI / 10;
            double cos = Math.cos(arg);
            double f1 = (+ cos2;
            double f2 = (- cos2;
            arg = arg % (Math.PI * 2);

            Color c1 = gradient.getColor1();
            Color c2 = gradient.getColor2();
            int r = (int) (c1.getRed() * f1 + c2.getRed() * f2);
            r = Math.min(Math.max(r, 0)255);
            int g = (int) (c1.getGreen() * f1 + c2.getGreen() * f2);
            g = Math.min(Math.max(g, 0)255);
            int b = (int) (c1.getBlue() * f1 + c2.getBlue() * f2);
            b = Math.min(Math.max(b, 0)255);
            setForeground(new Color(r, g, b));
          }
          repaint();
          try {
            sleep(m_delay);
          catch (InterruptedException ex) {
            break;
          }
        }
      }
    };
    animator.start();
  }

  public void stopAnimation() {
    isRunning = false;
    animator = null;
  }

  public void paintComponent(Graphics g) {
    Dimension d = getSize();
    Insets ins = getInsets();
    int x = ins.left;
    int y = ins.top;
    int w = d.width - ins.left - ins.right;
    int h = d.height - ins.top - ins.bottom;

    if (isOpaque()) {
      g.setColor(getBackground());
      g.fillRect(00, d.width, d.height);
    }
    paintBorder(g);

    Graphics2D g2 = (Graphics2Dg;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_QUALITY);

    FontRenderContext frc = g2.getFontRenderContext();
    TextLayout tl = new TextLayout(getText(), getFont(), frc);

    AffineTransform shear = AffineTransform.getShearInstance(shearFactor,
        0.0);
    Shape src = tl.getOutline(shear);
    Rectangle rText = src.getBounds();

    float xText = x - rText.x;
    switch (getHorizontalAlignment()) {
    case CENTER:
      xText = x + (w - rText.width2;
      break;
    case RIGHT:
      xText = x + (w - rText.width);
      break;
    }
    float yText = y + h / + tl.getAscent() 4;

    AffineTransform shift = AffineTransform.getTranslateInstance(xText,
        yText);
    Shape shp = shift.createTransformedShape(src);

    if (outlineColor != null) {
      g2.setColor(outlineColor);
      if (stroke != null)
        g2.setStroke(stroke);
      g2.draw(shp);
    }

    switch (effectIndex) {
    case EFFECT_GRADIENT:
      if (gradient == null)
        break;
      g2.setPaint(gradient);
      g2.fill(shp);
      break;

    case EFFECT_IMAGE:
      fillByImage(g2, shp, 0);
      break;

    case EFFECT_COLOR_ANIMATION:
      g2.setColor(getForeground());
      g2.fill(shp);
      break;

    case EFFECT_IMAGE_ANIMATION:
      if (foregroundImage == null)
        break;
      int wImg = foregroundImage.getWidth(this);
      if (m_xShift > wImg)
        m_xShift = 0;
      fillByImage(g2, shp, m_xShift - wImg);
      break;

    default:
      g2.setColor(getForeground());
      g2.fill(shp);
      break;
    }
  }

  protected void fillByImage(Graphics2D g2, Shape shape, int xOffset) {
    if (foregroundImage == null)
      return;
    int wImg = foregroundImage.getWidth(this);
    int hImg = foregroundImage.getHeight(this);
    if (wImg <= || hImg <= 0)
      return;
    g2.setClip(shape);
    Rectangle bounds = shape.getBounds();
    for (int xx = bounds.x + xOffset; xx < bounds.x + bounds.width; xx += wImg)
      for (int yy = bounds.y; yy < bounds.y + bounds.height; yy += hImg)
        g2.drawImage(foregroundImage, xx, yy, this);
  }
  public static void main(String argv[]) {
    JFrame f = new JFrame("2D Labels");
    f.setSize(600300);
    f.getContentPane().setLayout(new GridLayout(6155));
    f.getContentPane().setBackground(Color.white);
    Font bigFont = new Font("Helvetica", Font.BOLD, 24);

    JLabel2D lbl = new JLabel2D("Java Source and Support With Outline",
        JLabel.CENTER);
    lbl.setFont(bigFont);
    lbl.setForeground(Color.blue);
    lbl.setBorder(new LineBorder(Color.black));
    lbl.setBackground(Color.cyan);
    lbl.setOutlineColor(Color.yellow);
    lbl.setStroke(new BasicStroke(5f));
    lbl.setOpaque(true);
    lbl.setShearFactor(0.3);
    f.getContentPane().add(lbl);

    lbl = new JLabel2D("Java Source and Support With Color Gradient", JLabel.CENTER);
    lbl.setFont(bigFont);
    lbl.setOutlineColor(Color.black);
    lbl.setEffectIndex(JLabel2D.EFFECT_GRADIENT);
    GradientPaint gp = new GradientPaint(00, Color.red, 10050,
        Color.blue, true);
    lbl.setGradient(gp);
    f.getContentPane().add(lbl);

    lbl = new JLabel2D("Java Source and Support Filled With Image", JLabel.CENTER);
    lbl.setFont(bigFont);
    lbl.setEffectIndex(JLabel2D.EFFECT_IMAGE);
    ImageIcon icon = new ImageIcon("mars.gif");
    lbl.setForegroundImage(icon.getImage());
    lbl.setOutlineColor(Color.red);
    f.getContentPane().add(lbl);

    lbl = new JLabel2D("Java Source and Support With Image Animation", JLabel.CENTER);
    lbl.setFont(bigFont);
    lbl.setEffectIndex(JLabel2D.EFFECT_IMAGE_ANIMATION);
    icon = new ImageIcon("ocean.gif");
    lbl.setForegroundImage(icon.getImage());
    lbl.setOutlineColor(Color.black);
    lbl.startAnimation(400);
    f.getContentPane().add(lbl);

    lbl = new JLabel2D("Java Source and Support With Color Animation", JLabel.CENTER);
    lbl.setFont(bigFont);
    lbl.setEffectIndex(JLabel2D.EFFECT_COLOR_ANIMATION);
    lbl.setGradient(gp);
    lbl.setOutlineColor(Color.black);
    lbl.startAnimation(400);
    f.getContentPane().add(lbl);

    JLabel lbl1 = new JLabel("Plain Java Source and Support For Comparison", JLabel.CENTER);
    lbl1.setFont(bigFont);
    lbl1.setForeground(Color.black);
    f.getContentPane().add(lbl1);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);

    f.setVisible(true);

  }

}


           
         
  
Related examples in the same category
1. Create a JLabel with an image icon
2. Create JLabel component
3. Create a JLabel component
4. Putting HTML text on Swing componentsPutting HTML text on Swing components
5. Shows how displayed Mnemonic and labelFor properties work togetherShows how displayed Mnemonic and labelFor properties work together
6. A quick application to show a simple JLabelA quick application to show a simple JLabel
7. A JLabel that uses inline HTML to format its textA JLabel that uses inline HTML to format its text
8. Variations on a text and icon labelVariations on a text and icon label
9. A simple demonstration of text alignment in JLabelsA simple demonstration of text alignment in JLabels
10. Label with Image Label with Image
11. Label with HTML SampleLabel with HTML Sample
12. Label Text PositionLabel Text Position
13. LabelFor DemoLabelFor Demo
14. Label with IconLabel with Icon
15. Active Label SampleActive Label Sample
16. Swing Label DemoSwing Label Demo
17. Label alignmentLabel alignment
18. HTML labelHTML label
19. Label background, icon, alignLabel background, icon, align
20. Scrollable LabelScrollable Label
21. Grab and Drag image scroll labelGrab and Drag image scroll label
22. Animation label
23. Label for Label for
24. LabelFocusSample: setLabelForLabelFocusSample: setLabelFor
25. Adding Drag-and-Drop Support to a JLabel Component
26. Setting the Focus of a JTextField Component Using a JLabel Component
27. Adding an Icon to a JLabel Component
28. The text is horizontally and vertically centered
29. The text is right-justified and vertically centered
30. The text is left-justified and top-aligned
31. The text is right-justified and top-aligned
32. The text is left-justified and bottom-aligned
33. The text is right-justified and bottom-aligned
34. JLabel with more than one row
35. JLabel is for displaying text, images or both. It does not react to input events.
36. JSlider lets the user graphically select a value by sliding a knob within a bounded interval.
37. JLabel with lined border
38. Label with ImageIcon (Icon)
39. Multiline label (HTML)
40. Disable a label
41. Set the Preferred Size
42. Horizontal Alignment: CENTER
43. Vertical Alignment: CENTER
44. Horizontal Alignment: LEFT
45. Vertical Alignment: TOP
46. Horizontal Alignment: RIGHT
47. Vertical Alignment: BOTTOM
48. Associate JLabel component with a JTextField
49. A Label that uses inline HTML to format its text
50. A label with no indication it has been clicked
51. Solve the problem of text alignment in JLabels
52. Use a ScrollBar in both vertical and horizontal direction
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.