Example usage for javax.swing JComponent getWidth

List of usage examples for javax.swing JComponent getWidth

Introduction

In this page you can find the example usage for javax.swing JComponent getWidth.

Prototype

@BeanProperty(bound = false)
public int getWidth() 

Source Link

Document

Returns the current width of this component.

Usage

From source file:Utils.java

/** 
 * Renders a component into an image, which is useful for playing with the component's 
 * resultant image in special effects or transitions
 *
 * @param component The component to render
 * @return A buffered image with the rendered component. 
 *//*w ww .j av a  2s . c  o  m*/
public static BufferedImage renderComponentToImage(JComponent component) {
    //Create the image
    BufferedImage image = createCompatibleImage(component.getWidth(), component.getHeight());

    //Render the component onto the image
    Graphics graphics = image.createGraphics();
    //      component.update(graphics);
    component.paint(graphics);
    graphics.dispose();
    return image;
}

From source file:Main.java

/**
 * Sets where a component should be painted. It uses the 
 * {@code JComponent}'s width and height and excludes the {@code Insets}.
 * // w ww . ja v a  2s  .c om
 * @param c the component
 * @param viewRect the bounds for painting the component
 */
public static void setViewBounds(JComponent c, Rectangle viewRect) {
    Insets i = c.getInsets();

    viewRect.x = i.left;
    viewRect.y = i.top;
    viewRect.width = c.getWidth() - (i.right + viewRect.x);
    viewRect.height = c.getHeight() - (i.bottom + viewRect.y);
}

From source file:Main.java

public static void openDialogNextToParent(final JComponent parentComponent, final JDialog dialog) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override/*ww w .j av a  2  s .  co  m*/
        public void run() {
            if (parentComponent != null) {
                Point parentLocation = parentComponent.getLocationOnScreen();
                parentLocation.x += parentComponent.getWidth();
                dialog.setLocation(parentLocation);
            }
            dialog.setVisible(true);
            dialog.setSize(dialog.getPreferredSize());
        }
    });
}

From source file:Main.java

/**
 * Returns a string abbreviated according to the length of the available space
 * in a component.//  ww w  .jav  a  2 s .c om
 * @param str A string which may need abbreviating.
 * @param component The component the string will be rendered in.
 * @return a string abbreviated according to the length of the available space
 */
public static String abbreviate(String str, JComponent component) {
    String result = "";
    if (component != null) {
        Graphics g = component.getGraphics();
        FontMetrics fm = g.getFontMetrics(component.getFont());
        int stringSize = SwingUtilities.computeStringWidth(fm, str);
        final int border = 48;
        int availableWidth = component.getWidth() - border;
        if (stringSize > availableWidth) {
            final int avCharWidth = fm.charWidth('x');
            final int alwaysChop = 5;
            final int charsToChop = alwaysChop + ((stringSize - availableWidth) / avCharWidth);
            final int leftPos = (str.length() - charsToChop) / 2;
            final int maxLength = str.length() - charsToChop;
            final int left = leftPos > 0 ? leftPos : 0;
            final int len = maxLength > left ? maxLength : left + 1;
            result = abbreviate(str, left, len);
        } else {
            result = str;
        }
    }
    return result;
}

From source file:PaintUtils.java

/**
 * Paints a top to bottom gradient fill over the component bounds
 * from color1 to color2./*from w  w  w  .  ja  v  a  2 s  .c o m*/
 */
public static void paintGradient(Graphics g, JComponent comp, Color color1, Color color2) {
    GradientPaint paint = new GradientPaint(0, 0, color1, 0, comp.getHeight(), color2, true);
    Graphics2D g2 = (Graphics2D) g;
    Paint oldPaint = g2.getPaint();
    g2.setPaint(paint);
    g2.fillRect(0, 0, comp.getWidth(), comp.getHeight());
    g2.setPaint(oldPaint);
}

From source file:Main.java

@Override
public void paint(Graphics g, JComponent c) {
    super.paint(g, c);
    Dimension d = l.getPreferredSize();
    int x = c.getWidth() - d.width - 5;
    SwingUtilities.paintComponent(g, l, rubberStamp, x, 2, d.width, d.height);
}

From source file:Myopia.java

@Override
public void paint(Graphics g, JComponent c) {
    int w = c.getWidth();
    int h = c.getHeight();

    if (w == 0 || h == 0) {
        return;/* w w w. ja  va2s .  c  om*/
    }

    // Only create the offscreen image if the one we have
    // is the wrong size.
    if (mOffscreenImage == null || mOffscreenImage.getWidth() != w || mOffscreenImage.getHeight() != h) {
        mOffscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    }

    Graphics2D ig2 = mOffscreenImage.createGraphics();
    ig2.setClip(g.getClip());
    super.paint(ig2, c);
    ig2.dispose();

    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(mOffscreenImage, mOperation, 0, 0);
}

From source file:probe.com.model.util.SwingToImageGenerator.java

public String generateHeatMap(JComponent component) {

    BufferedImage heatMapImg = new BufferedImage((component.getWidth()), (component.getHeight()),
            BufferedImage.TYPE_INT_ARGB);
    Graphics g = heatMapImg.getGraphics();
    component.paint(g);/*  w w w . j a v a2 s. c o m*/
    return generateEncodedImg(heatMapImg);
}

From source file:org.jfree.graphics2d.demo.SwingUIToSVGDemo.java

@Override
public void actionPerformed(ActionEvent e) {
    JComponent c = (JComponent) getContentPane().getComponent(0);
    SVGGraphics2D g2 = new SVGGraphics2D(c.getWidth(), c.getHeight());
    c.paint(g2);/*from www .  j  a v  a  2 s  .  c  o  m*/
    File f = new File("SwingUIToSVGDemo.svg");
    try {
        SVGUtils.writeToSVG(f, g2.getSVGElement());
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:Wallpaper.java

@Override
public void paint(Graphics g, JComponent c) {
    super.paint(g, c);

    Graphics2D g2 = (Graphics2D) g.create();

    int w = c.getWidth();
    int h = c.getHeight();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
    g2.setPaint(new GradientPaint(0, 0, Color.yellow, 0, h, Color.red));
    g2.fillRect(0, 0, w, h);/*w w w.j  a  v  a2s.  c  om*/

    g2.dispose();
}