Example usage for java.awt Component getBackground

List of usage examples for java.awt Component getBackground

Introduction

In this page you can find the example usage for java.awt Component getBackground.

Prototype

@Transient
public Color getBackground() 

Source Link

Document

Gets the background color of this component.

Usage

From source file:Main.java

public static void drawImage(BufferedImage image, Graphics g, Component parent) {
    Dimension size = parent.getSize();
    Color background = parent.getBackground();
    if (image != null && size.width > 0) {
        double ratio = (double) image.getHeight(null) / image.getWidth(null);

        int effectiveWidth = 1;
        int effectiveHeight = (int) ratio;

        while (effectiveHeight < size.height && effectiveWidth < size.width) {
            effectiveWidth++;// w  w w  . j  a va2s .c  o m
            effectiveHeight = (int) (ratio * effectiveWidth);
        }

        g.setColor(background);
        g.fillRect(0, 0, size.width, size.height);

        int cornerx = Math.abs((size.width - effectiveWidth) / 2);
        int cornery = Math.abs((size.height - effectiveHeight) / 2);
        g.drawImage(image, cornerx, cornery, effectiveWidth + cornerx, effectiveHeight + cornery, 0, 0,
                image.getWidth(null), image.getHeight(null), null);
    }
}

From source file:Main.java

/**
 * Sets the background color for the specified <code>ButtonGroup</code> and
 * all the JCheckBox, JComboBox, JButton, and JRadioButton components that 
 * it contains to the same color.//from   w w  w .  java2  s  .  com
 * 
 * @param buttons the button group to set the background for.
 * @param bg the background color.
 */
public static void setBackground(ButtonGroup buttons, Color bg) {
    Enumeration<?> children = buttons.getElements();
    if (children == null) {
        return;
    }
    Component child;

    if (bg != null) {
        while (children.hasMoreElements()) {
            child = (Component) children.nextElement();
            if (!bg.equals(child.getBackground())
                    && ((child instanceof JCheckBox) || (child instanceof JComboBox)
                            || (child instanceof JButton) || (child instanceof JRadioButton))) {
                child.setBackground(bg);
            }
        }
    }
}

From source file:Main.java

/**
 * Sets the background color for the specified container and all the
 * JPanel, JLabel, JCheckBox, JComboBox, JTextField, JRadioButton, and
 * JScrollPane components that it contains to the same color.
 * //from  w  w  w  . jav a  2 s  .  c  om
 * @param c the container to set the background color of.
 * @param bg the background color.
 */
public static void setBackground(Container c, Color bg) {
    c.setBackground(bg);
    Component[] children = c.getComponents();
    if (children == null) {
        return;
    }
    Component child;
    int len = children.length;
    if (bg != null) {
        for (int i = 0; i < len; i++) {
            child = children[i];
            if (!bg.equals(child.getBackground())
                    && ((child instanceof JPanel) || (child instanceof JLabel) || (child instanceof JCheckBox)
                            || (child instanceof JComboBox) || (child instanceof JTextField)
                            || (child instanceof JRadioButton) || (child instanceof JScrollPane))) {
                child.setBackground(bg);
            }
        }
    } else {
        // Null background color case
        for (int i = 0; i < len; i++) {
            child = children[i];
            if ((child.getBackground() != null) && ((child instanceof JPanel) || (child instanceof JLabel)
                    || (child instanceof JCheckBox))) {
                child.setBackground(null);
            } //end if
        } // end for
    } //end if else
}

From source file:Arrow.java

public void paintIcon(Component c, Graphics g, int x, int y) {
    Color color = c == null ? Color.GRAY : c.getBackground();

    int dx = (int) (size / 2);
    int dy = descending ? dx : -dx;

    // Align icon (roughly) with font baseline.
    y = y + 5 * size / 6 + (descending ? -dy : 0);

    g.translate(x, y);/*from   w ww. j a  va 2  s. c  o m*/
    g.setColor(Color.GRAY);
    g.fillPolygon(new int[] { dx / 2, dx, 0 }, new int[] { dy, 0, 0 }, 3);
    g.translate(-x, -y);
    g.setColor(color);
}

From source file:org.jimcat.gui.borders.RoundedShadowBorder.java

/**
 * //from  w  w w  . j  av  a2s  .  c  o m
 * paint this type of border
 * 
 * @see org.jimcat.gui.borders.RoundedBorder#paintBorder(java.awt.Component,
 *      java.awt.Graphics, int, int, int, int)
 */
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    Color borderColor = c.getBackground();

    // check if old image is still useable
    if (imageBuffer == null || width != lastWidth || height != lastHeight
            || !ObjectUtils.equals(borderColor, lastColor)) {
        // update image buffer
        imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        lastHeight = height;
        lastWidth = width;
        lastColor = borderColor;

        int shadow_width = SHADOW_WIDTH * 2;

        int xb = Math.max(x + SHADOW_WIDTH - SHADOW_DISPLACEMENT.x, x);
        int yb = Math.max(y + SHADOW_WIDTH - SHADOW_DISPLACEMENT.y, y);

        int xs = Math.max(x, x + SHADOW_DISPLACEMENT.x);
        int ys = Math.max(y, y + SHADOW_DISPLACEMENT.y);

        BufferedImage border = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics imgG = border.createGraphics();
        super.paintBorder(c, imgG, xb, yb, width - shadow_width - Math.abs(SHADOW_DISPLACEMENT.x),
                height - shadow_width - Math.abs(SHADOW_DISPLACEMENT.y));
        imgG.dispose();

        BufferedImage shadow = shadowRenderer.createShadow(border);

        Graphics2D g2 = (Graphics2D) imageBuffer.getGraphics();
        g2.drawImage(shadow, xs, ys, null);
        g2.drawImage(border, xb, yb, null);
        g2.dispose();
    }
    g.drawImage(imageBuffer, x, y, null);
}

From source file:util.ui.PictureAreaIcon.java

public void paintIcon(final Component c, Graphics g, int x, int y) {
    if (mScaledIcon == null) {
        return;//from   w w w. j ava  2s .  c  om
    }

    y += 2;

    Color color = g.getColor();

    if (!colorsInEqualRange(c.getBackground(), c.getForeground()) && !mProgram.isExpired()) {
        g.setColor(c.getBackground());
        g.fillRect(x, y, getIconWidth(), getIconHeight() - 2);
    }

    g.setColor(color);
    g.drawRect(x, y, getIconWidth() - 1, getIconHeight() - 3);

    y += 3;
    x += 3;

    if (mIsGrayFilter && !mIsExpired && mProgram.isExpired()) {
        ImageFilter filter = new GrayFilter(true, 60);
        mScaledIcon
                .setImage(c.createImage(new FilteredImageSource(mScaledIcon.getImage().getSource(), filter)));
        mIsExpired = true;
    }

    if (c.getForeground().getAlpha() != 255) {
        ImageFilter filter = new RGBImageFilter() {
            public int filterRGB(int x, int y, int rgb) {
                if ((rgb & 0xff000000) != 0) {
                    return (rgb & 0xffffff) | (c.getForeground().getAlpha() << 24);
                }
                return rgb;
            }
        };

        mScaledIcon
                .setImage(c.createImage(new FilteredImageSource(mScaledIcon.getImage().getSource(), filter)));
    }

    mScaledIcon.paintIcon(c, g, x, y);

    /*
    if(!mProgram.isExpired()) {
      g.setColor(color);
    } else {
      g.setColor(color);
    }
    */
    mCopyrightText.paintIcon(null, g, x, y + mScaledIcon.getIconHeight());
    if (mDescriptionLines > 0 && mDescriptionText != null) {
        mDescriptionText.paintIcon(null, g, x,
                y + mScaledIcon.getIconHeight() + mCopyrightText.getIconHeight() + 1);
    }
    g.setColor(color);
}

From source file:edu.ku.brc.af.ui.forms.CarryForwardConfigDlg.java

@Override
public void createUI() {
    super.createUI();

    buildTableModel(mvParent);/*  w ww . java2s . co m*/

    model = new CFTableModel();
    table = new JTable(model) {
        //  Returning the Class of each column will allow different
        //  renderers to be used based on Class
        public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }

        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component c = super.prepareRenderer(renderer, row, column);
            if (!c.getBackground().equals(getSelectionBackground())) {
                CFWItem item = items.get(row);
                c.setBackground(item.getColor());
            }
            return c;
        }
    };

    UIHelper.makeTableHeadersCentered(table, false);

    //((DefaultTableCellRenderer)table.getColumnModel().getColumn(1).getCellRenderer()).setHorizontalAlignment(SwingConstants.CENTER);

    JScrollPane sp = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    contentPanel = sp;//new JPanel(new BorderLayout()));
    mainPanel.add(contentPanel, BorderLayout.CENTER);
}

From source file:org.nuclos.client.ui.collect.component.AbstractCollectableComponent.java

protected final static void setBackground(Component c, NuclosScript ns, final Collectable clct,
        EntityMetaDataVO meta, boolean isEnabled) {
    try {//from  w  w  w.  j  av  a  2s.co  m
        String rgb = Integer.toHexString(c.getBackground().getRGB());
        rgb = rgb.substring(2, rgb.length());
        Object o = ScriptEvaluator.getInstance().eval(ns, new CollectableScriptContext(clct), "#" + rgb);

        if (o instanceof String) {
            Color color = Color.decode((String) o);
            if (isEnabled) {
                c.setBackground(color);
            } else {
                c.setBackground(new Color(Math.max(0, color.getRed() - (color.getRed() * 15 / 100)),
                        Math.max(0, color.getGreen() - (color.getGreen() * 15 / 100)),
                        Math.max(0, color.getBlue() - (color.getBlue() * 15 / 100))));
            }
        }
    } catch (Exception ex) {
        LOG.warn(ex);
    }
}

From source file:dk.dma.epd.common.prototype.gui.voct.VOCTAdditionalInfoPanel.java

/**
 * {@inheritDoc}/*from  w  w w  .  j a v  a 2 s  .  c  om*/
 */
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHints(GraphicsUtil.ANTIALIAS_HINT);

    // Define the content rectangle
    int x0 = pointerLeft ? pad + pointerWidth : pad;
    RoundRectangle2D.Double content = new RoundRectangle2D.Double(x0, pad, width - 2 * pad - pointerWidth,
            height - 2 * pad, cornerRadius, cornerRadius);

    // Define the pointer triangle
    int xp = pointerLeft ? pad + pointerWidth : width - pad - pointerWidth;
    int yp = pad + height - pointerFromBottom;
    Polygon pointer = new Polygon();
    pointer.addPoint(xp, yp);
    pointer.addPoint(xp, yp - pointerHeight);
    pointer.addPoint(xp + pointerWidth * (pointerLeft ? -1 : 1), yp - pointerHeight / 2);

    // Combine content rectangle and pointer into one area
    Area area = new Area(content);
    area.add(new Area(pointer));

    // Fill the pop-up background
    Color col = pointerLeft ? c.getBackground().darker() : c.getBackground().brighter();
    g2.setColor(col);
    g2.fill(area);

}

From source file:com._17od.upm.gui.MainWindow.java

private void addComponentsToPane() {

    // Ensure the layout manager is a BorderLayout
    if (!(getContentPane().getLayout() instanceof GridBagLayout)) {
        getContentPane().setLayout(new GridBagLayout());
    }//from  w ww.  j  a va2  s  .com

    // Create the menubar
    setJMenuBar(createMenuBar());

    GridBagConstraints c = new GridBagConstraints();

    // The toolbar Row
    c.gridx = 0;
    c.gridy = 0;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    c.insets = new Insets(0, 0, 0, 0);
    c.weightx = 0;
    c.weighty = 0;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.HORIZONTAL;
    Component toolbar = createToolBar();
    getContentPane().add(toolbar, c);

    // Keep the frame background color consistent
    getContentPane().setBackground(toolbar.getBackground());

    // The seperator Row
    c.gridx = 0;
    c.gridy = 1;
    c.anchor = GridBagConstraints.LINE_START;
    c.insets = new Insets(0, 0, 0, 0);
    c.weightx = 1;
    c.weighty = 0;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.HORIZONTAL;
    getContentPane().add(new JSeparator(), c);

    // The search field row
    searchIcon = new JLabel(Util.loadImage("search.gif"));
    searchIcon.setDisabledIcon(Util.loadImage("search_d.gif"));
    searchIcon.setEnabled(false);
    c.gridx = 0;
    c.gridy = 2;
    c.anchor = GridBagConstraints.LINE_START;
    c.insets = new Insets(5, 1, 5, 1);
    c.weightx = 0;
    c.weighty = 0;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE;
    getContentPane().add(searchIcon, c);

    searchField = new JTextField(15);
    searchField.setEnabled(false);
    searchField.setMinimumSize(searchField.getPreferredSize());
    searchField.getDocument().addDocumentListener(new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            // This method never seems to be called
        }

        public void insertUpdate(DocumentEvent e) {
            dbActions.filter();
        }

        public void removeUpdate(DocumentEvent e) {
            dbActions.filter();
        }
    });
    searchField.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                dbActions.resetSearch();
            } else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                // If the user hits the enter key in the search field and
                // there's only one item
                // in the listview then open that item (this code assumes
                // that the one item in
                // the listview has already been selected. this is done
                // automatically in the
                // DatabaseActions.filter() method)
                if (accountsListview.getModel().getSize() == 1) {
                    viewAccountMenuItem.doClick();
                }
            }
        }
    });
    c.gridx = 1;
    c.gridy = 2;
    c.anchor = GridBagConstraints.LINE_START;
    c.insets = new Insets(5, 1, 5, 1);
    c.weightx = 0;
    c.weighty = 0;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE;
    getContentPane().add(searchField, c);

    resetSearchButton = new JButton(Util.loadImage("stop.gif"));
    resetSearchButton.setDisabledIcon(Util.loadImage("stop_d.gif"));
    resetSearchButton.setEnabled(false);
    resetSearchButton.setToolTipText(Translator.translate(RESET_SEARCH_TXT));
    resetSearchButton.setActionCommand(RESET_SEARCH_TXT);
    resetSearchButton.addActionListener(this);
    resetSearchButton.setBorder(BorderFactory.createEmptyBorder());
    resetSearchButton.setFocusable(false);
    c.gridx = 2;
    c.gridy = 2;
    c.anchor = GridBagConstraints.LINE_START;
    c.insets = new Insets(5, 1, 5, 1);
    c.weightx = 1;
    c.weighty = 0;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE;
    getContentPane().add(resetSearchButton, c);

    // The accounts listview row
    accountsListview = new JList();
    accountsListview.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    accountsListview.setSelectedIndex(0);
    accountsListview.setVisibleRowCount(10);
    accountsListview.setModel(new SortedListModel());
    JScrollPane accountsScrollList = new JScrollPane(accountsListview, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    accountsListview.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent e) {
            // If the listview gets focus, there is one ore more items in
            // the listview and there is nothing
            // already selected, then select the first item in the list
            if (accountsListview.getModel().getSize() > 0 && accountsListview.getSelectedIndex() == -1) {
                accountsListview.setSelectionInterval(0, 0);
            }
        }
    });
    accountsListview.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            dbActions.setButtonState();
        }
    });
    accountsListview.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                viewAccountMenuItem.doClick();
            }
        }
    });
    accountsListview.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                viewAccountMenuItem.doClick();
            }
        }
    });
    // Create a shortcut to delete account functionality with DEL(delete)
    // key

    accountsListview.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DELETE) {

                try {
                    dbActions.reloadDatabaseBefore(new DeleteAccountAction());
                } catch (InvalidPasswordException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (ProblemReadingDatabaseFile e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }
        }
    });

    c.gridx = 0;
    c.gridy = 3;
    c.anchor = GridBagConstraints.CENTER;
    c.insets = new Insets(0, 1, 1, 1);
    c.weightx = 1;
    c.weighty = 1;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.BOTH;
    getContentPane().add(accountsScrollList, c);

    // The "File Changed" panel
    c.gridx = 0;
    c.gridy = 4;
    c.anchor = GridBagConstraints.CENTER;
    c.insets = new Insets(0, 1, 0, 1);
    c.ipadx = 3;
    c.ipady = 3;
    c.weightx = 0;
    c.weighty = 0;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.BOTH;
    databaseFileChangedPanel = new JPanel();
    databaseFileChangedPanel.setLayout(new BoxLayout(databaseFileChangedPanel, BoxLayout.X_AXIS));
    databaseFileChangedPanel.setBackground(new Color(249, 172, 60));
    databaseFileChangedPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    JLabel fileChangedLabel = new JLabel("Database file changed");
    fileChangedLabel.setAlignmentX(LEFT_ALIGNMENT);
    databaseFileChangedPanel.add(fileChangedLabel);
    databaseFileChangedPanel.add(Box.createHorizontalGlue());
    JButton reloadButton = new JButton("Reload");
    reloadButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                dbActions.reloadDatabaseFromDisk();
            } catch (Exception ex) {
                dbActions.errorHandler(ex);
            }
        }
    });
    databaseFileChangedPanel.add(reloadButton);
    databaseFileChangedPanel.setVisible(false);
    getContentPane().add(databaseFileChangedPanel, c);

    // Add the statusbar
    c.gridx = 0;
    c.gridy = 5;
    c.anchor = GridBagConstraints.CENTER;
    c.insets = new Insets(0, 1, 1, 1);
    c.weightx = 1;
    c.weighty = 0;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.HORIZONTAL;
    getContentPane().add(statusBar, c);

}