A button which paints on it one or more scaled arrows in one of the cardinal directions : Button « Swing Components « 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 Components » Button 




A button which paints on it one or more scaled arrows in one of the cardinal directions
   
//Revised from greef ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

/**
 * A button which paints on it one or more scaled arrows in one of the cardinal directions.
 @author Adrian BER
 */
public class ArrowIcon implements Icon {

    /** The cardinal direction of the arrow(s). */
    private int direction;

    /** The number of arrows. */
    private int arrowCount;

    /** The arrow size. */
    private int arrowSize;

    public ArrowIcon(int direction, int arrowCount, int arrowSize) {
        this.direction = direction;
        this.arrowCount = arrowCount;
        this.arrowSize = arrowSize;
    }

    /** Returns the cardinal direction of the arrow(s).
     @see #setDirection(int)
     */
    public int getDirection() {
        return direction;
    }

    /** Sets the cardinal direction of the arrow(s).
     @param direction the direction of the arrow(s), can be SwingConstants.NORTH,
     * SwingConstants.SOUTH, SwingConstants.WEST or SwingConstants.EAST
     @see #getDirection()
     */
    public void setDirection(int direction) {
        this.direction = direction;
    }

    /** Returns the number of arrows. */
    public int getArrowCount() {
        return arrowCount;
    }

    /** Sets the number of arrows. */
    public void setArrowCount(int arrowCount) {
        this.arrowCount = arrowCount;
    }

    /** Returns the arrow size. */
    public int getArrowSize() {
        return arrowSize;
    }

    /** Sets the arrow size. */
    public void setArrowSize(int arrowSize) {
        this.arrowSize = arrowSize;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        // paint the arrows
        int w = getIconWidth();
        int h = getIconHeight();
        for (int i = 0; i < arrowCount; i++) {
            paintArrow(g,
                    (x + w - arrowSize * (direction == SwingConstants.EAST
                            || direction == SwingConstants.WEST ? arrowCount : 1)) 2
                            + arrowSize * (direction == SwingConstants.EAST
                            || direction == SwingConstants.WEST ? i : 0),
                    (y + h - arrowSize * (direction == SwingConstants.EAST
                            || direction == SwingConstants.WEST ? : arrowCount)) 2
                            + arrowSize * (direction == SwingConstants.EAST
                            || direction == SwingConstants.WEST ? : i)
                    );
        }
    }

    public int getIconWidth() {
        return arrowSize * (direction == SwingConstants.EAST
                || direction == SwingConstants.WEST ? arrowCount : 3);
    }

    public int getIconHeight() {
        return arrowSize * (direction == SwingConstants.NORTH
                || direction == SwingConstants.SOUTH ? arrowCount : 3);
    }

    private void paintArrow(Graphics g, int x, int y) {
        int mid, i, j;

        j = 0;
        arrowSize = Math.max(arrowSize, 2);
        mid = (arrowSize / 21;

        g.translate(x, y);

        switch (direction) {
            case SwingConstants.NORTH:
                for (i = 0; i < arrowSize; i++) {
                    g.drawLine(mid - i, i, mid + i, i);
                }
                break;
            case SwingConstants.SOUTH:
                j = 0;
                for (i = arrowSize - 1; i >= 0; i--) {
                    g.drawLine(mid - i, j, mid + i, j);
                    j++;
                }
                break;
            case SwingConstants.WEST:
                for (i = 0; i < arrowSize; i++) {
                    g.drawLine(i, mid - i, i, mid + i);
                }
                break;
            case SwingConstants.EAST:
                j = 0;
                for (i = arrowSize - 1; i >= 0; i--) {
                    g.drawLine(j, mid - i, j, mid + i);
                    j++;
                }
                break;
        }

        g.translate(-x, -y);
    }
}

   
    
    
  














Related examples in the same category
1.Arrow Button
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.