Adding Backgrounds on JPanels

Could anyone help with the following code; I am trying to put two JPanels on a JLayeredPane and have one of them act as background; the code goes like this
/** The game has been implemented as part of the final
project for CSc322
*     @author Zengine Productions version 1
// This is the main frame
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SpaceBall
//To do the background just draw a JPanel inside
another JPanel just set the opacity of the outside one
to false and let
//it hold all the components of the game
public static void main(String[] args)
//declaring the buttons
JButton test=new JButton("test");
JButton test1=new JButton("test1");
JButton test2=new JButton("test2");
//declaring and setting the properties of the frame
JFrame SpaceBall= new JFrame("Space Ball"); // the
game window
SpaceBall.setSize(700,750);
//declaring the Panels
JLayeredPane bgPanel= new JLayeredPane(); // the
panel that holds the background image
JPanel fgPanel= new JPanel(); // the panel that
holds all the game objects
JPanel topPanel= new JPanel(); // the panel on top
of the game panel
JPanel sidePanel= new JPanel(); // the panel on the
side of the game panel
JPanel lowPanel= new JPanel(); // the panel that
will hold bgPanel and sidepanel
JPanel masterPanel= new JPanel(); // the panel that
holds everything to be added to the JFrame
//adding the buttons to the corresponding panels
fgPanel.add(test1);
sidePanel.add(test2);
topPanel.add(test);
// ImageIcon background= new
ImageIcon("images/background.jpg");
// JLabel backlabel = new JLabel(background);
// bgPanel.add(backlabel, new
Integer(Integer.MIN_VALUE));
backlabel.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
fgPanel.setOpaque(false);
bgPanel.add(fgPanel);
bgPanel.setForeground(Color.white);
//adding bgPanel and sidePanel to lowPanel
lowPanel.setLayout(new GridLayout(1,2));
lowPanel.add(bgPanel);
lowPanel.add(sidePanel);
//adding the Panels to the masterPanel
masterPanel.setLayout(new GridLayout(2,1));
masterPanel.add(topPanel);
masterPanel.add(lowPanel);
//getting the container of SpaceBall and adding the
Panels to it
Container cp=SpaceBall.getContentPane();
cp.add(masterPanel);
//displaying everything
SpaceBall.show();
WindowListener ClosingTheWindow=new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
SpaceBall.addWindowListener(ClosingTheWindow);
Could anyone point out any bug in the code that keeps it from displaying the button on the background Panel!
Thanks

Thanks Wekion but I think the editor messed up the format of my code; below is the exact one that I try to run but never get the result that I seek!
// This is the main frame
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SpaceBall
//To do the background just draw a JPanel inside another //JPanel just set the opacity of the outside one
//to false and let
//it hold all the components of the game
public static void main(String[] args)
//declaring the buttons
JButton test=new JButton("test");
JButton test1=new JButton("test1");
JButton test2=new JButton("test2");
//declaring and setting the properties of the frame
JFrame SpaceBall= new JFrame("Space Ball");
SpaceBall.setSize(700,650);
//declaring the Panels
JLayeredPane bgPanel= new JLayeredPane();
JPanel fgPanel= new JPanel();
JPanel topPanel= new JPanel();
JPanel sidePanel= new JPanel();
JPanel lowPanel= new JPanel();
JPanel masterPanel= new JPanel();
//adding the buttons to the corresponding panels
fgPanel.add(test1);
sidePanel.add(test2);
topPanel.add(test);
ImageIcon background= new ImageIcon("images/background.jpg");
JLabel backlabel = new JLabel(background);
backlabel.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
backlabel.add(test1);
bgPanel.add(backlabel, new Integer(0));
fgPanel.setOpaque(false);
bgPanel.add(fgPanel, new Integer(100));
bgPanel.moveToFront(fgPanel);
//adding bgPanel and sidePanel to lowPanel
lowPanel.setLayout(new GridLayout(1,2));
lowPanel.add(bgPanel);
lowPanel.add(sidePanel);
//adding the Panels to the masterPanel
masterPanel.setLayout(new GridLayout(2,1));
masterPanel.add(topPanel);
masterPanel.add(lowPanel);
//getting the container of SpaceBall and adding the Panels to it
Container cp=SpaceBall.getContentPane();
cp.add(masterPanel);
//displaying everything
SpaceBall.show();
WindowListener ClosingTheWindow=new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
SpaceBall.addWindowListener(ClosingTheWindow);

Similar Messages

  • Any one know how to set a picture background in JPanel

    I am study java at collage and need to know this for a project
    How to set a picture gif or bmp etc to a background in JPanel in Swing
    CAN ANYONE HELP!

    If you want to just add an image to a JPanel, create a JLabel and add the image to that, then add the JLabel to the JPanel. If you want it to be the background of the JPanel, check this out:
    http://manning.com/sbe/files/uts2/Chapter15html/Chapter15.htm
    15.2 is where you'll find your answer along with sample code. The whole book is useful, take a look through it if you're interested.

  • I added background music to the video clip, but I only hear noise, no video clip audio or the background music audio

    I added background music to the video clip in iMovie, the playback runs ok on the right corner preview screen, if I share it to movie clip or itunes or theather, I only hear noise, no video clip audio or the music audio
    If I don't put additional audio to the clip. audio effect or music background, the video plays normally
    I detached the audio from the video clip and deleted it, that way it was possible to listen to the music background and see the actual video clip, but no audio from the video clip.
    How can I have both?

    Hi
    My first thought is -
    Where are the original Audio file ?
    If it was on an Audio-CD then the CD is Ejected and one tries to play the movie - it can not find it = Silence.
    You are using an Non-Destructive Video editor - meaning that You have raw material - Video, Audio and photos but no Movie in Project - only a text document directing How You want it to be played. So if Audio is not where it was when this document were created - iMovie can not find it and e.g. audio goes silent.
    Yours thinking - Bengt W

  • Acrobat 9 Pro - Text Distortion on adding background

    Hello,
    I hope someone can help me with this as it's driving me up the wall!
    In ye olde days of version 8 my work flow used to go like this:
    Write XYZ in MS Word 2007.
    Export to PDF.
    Open up exported PDF and see the crisp and soft lovelyness of my chosen none standard font displayed in front of me.
    I would then add our companies background to all the pages in the PDF (a PDF I created full page from Illustrator CS3).
    Pop the front and rear covers on.
    Send to a client or whatever.
    Now we have upgraded to Acrobat 9 I have a bit of an issue.
    I go along the same route and all is fine until I add the background (I can even add the pages in before the background and it's fine).
    As soon as the background is added the font goes from crisp and smooth to bolder and jaggier and no matter what I do I just can't clean it out.
    At the moment we have only had to send out one document like this, but they are backing up until I can get this fixed, it's not something we can realistically show to a client when we are supposed to be graphic designers and pride ourselves on our output.
    Any ideas?
    Regards
    Paul

    Thanks for your suggestion! That actually did work pretty well.
    I had set up all the fields as non-rich text, cut and pasted the text into the fields, and then change the fields back to rich text to format them (ie superscript, subscript etc).
    Unfortunately, now I'm faced with another problem.
    I won't be able to add more "unformatted" text later on easily. It appears that to do so, I would have to change the fields to non-rich text which wipes out all the formatting I've already applied.
    Any suggestions?
    Cheers,
    Trish

  • Adding background image to JPanal...............PLEASE PLEASE HELP

    Hi,
    i am very very stuck in adding a image to a JPanel. here is my code could any one please show me where and how to add an image to this please. a sample code would be prefered.
    Thanks
    * card.java
    public class card extends javax.swing.JFrame {
    /** Creates new form card */
    public card() {
    initComponents();
    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    getContentPane().setLayout(new java.awt.GridBagLayout());
    setTitle("Card Game");
    addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent evt) {
    exitForm(evt);
    jPanel1.setLayout(new java.awt.GridBagLayout());
    jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    jLabel1.setIcon(new javax.swing.ImageIcon("card game implementation\\headingF1.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.insets = new java.awt.Insets(5, 145, 0, 205);
    jPanel1.add(jLabel1, gridBagConstraints);
    jButton1.setIcon(new javax.swing.ImageIcon("card game implementation\\Nigat\\helpbuttonF.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.ipadx = -36;
    gridBagConstraints.ipady = -10;
    gridBagConstraints.insets = new java.awt.Insets(83, 40, 130, 40);
    jPanel1.add(jButton1, gridBagConstraints);
    jButton2.setIcon(new javax.swing.ImageIcon("card game implementation\\Nigat\\newgame1.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.ipadx = -39;
    gridBagConstraints.ipady = -12;
    gridBagConstraints.insets = new java.awt.Insets(213, 40, 0, 40);
    jPanel1.add(jButton2, gridBagConstraints);
    jButton3.setIcon(new javax.swing.ImageIcon("card game implementation\\Nigat\\quit1.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.ipadx = -39;
    gridBagConstraints.ipady = -12;
    gridBagConstraints.insets = new java.awt.Insets(80, 40, 70, 40);
    jPanel1.add(jButton3, gridBagConstraints);
    jPanel2.setLayout(new java.awt.GridBagLayout());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridheight = 2;
    gridBagConstraints.ipadx = 420;
    gridBagConstraints.ipady = 400;
    gridBagConstraints.insets = new java.awt.Insets(43, 60, 40, 0);
    jPanel1.add(jPanel2, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 1);
    getContentPane().add(jPanel1, gridBagConstraints);
    pack();
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit(0);
    * @param args the command line arguments
    public static void main(String args[]) {
    new card().show();
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration

    My images loaded up just fine. I left them in and commented out. Try using the single forward slash below...
    public class CardTest extends javax.swing.JFrame {
        /** Creates new form card */
        public CardTest() {
            initComponents();
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
        private void initComponents() {
            java.awt.GridBagConstraints gridBagConstraints;
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jPanel2 = new javax.swing.JPanel();
            getContentPane().setLayout(new java.awt.GridBagLayout());
            setTitle("Card Game");
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    exitForm(evt);
            jPanel1.setLayout(new java.awt.GridBagLayout());
            jPanel1.setBackground(new java.awt.Color(255, 255, 255));
            jLabel1.setIcon(new javax.swing.ImageIcon(
                            //"images/T1.gif"));
                            "card game implementation/headingF1.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.gridwidth = 2;
            gridBagConstraints.insets = new java.awt.Insets(5, 145, 0, 205);
            jPanel1.add(jLabel1, gridBagConstraints);
            jButton1.setIcon(new javax.swing.ImageIcon(
                             //"images/T2.gif"));
                             "card game implementation/Nigat/helpbuttonF.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.ipadx = -36;
            gridBagConstraints.ipady = -10;
            gridBagConstraints.insets = new java.awt.Insets(83, 40, 130, 40);
            jPanel1.add(jButton1, gridBagConstraints);
            jButton2.setIcon(new javax.swing.ImageIcon(
                             //"images/T3.gif"));
                             "card game implementation/Nigat/newgame1.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.ipadx = -39;
            gridBagConstraints.ipady = -12;
            gridBagConstraints.insets = new java.awt.Insets(213, 40, 0, 40);
            jPanel1.add(jButton2, gridBagConstraints);
            jButton3.setIcon(new javax.swing.ImageIcon(
                             //"images/T4.gif"));
                             "card game implementation/Nigat/quit1.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 2;
            gridBagConstraints.ipadx = -39;
            gridBagConstraints.ipady = -12;
            gridBagConstraints.insets = new java.awt.Insets(80, 40, 70, 40);
            jPanel1.add(jButton3, gridBagConstraints);
            jPanel2.setLayout(new java.awt.GridBagLayout());
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.gridheight = 2;
            gridBagConstraints.ipadx = 420;
            gridBagConstraints.ipady = 400;
            gridBagConstraints.insets = new java.awt.Insets(43, 60, 40, 0);
            jPanel1.add(jPanel2, gridBagConstraints);
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 1);
            getContentPane().add(jPanel1, gridBagConstraints);
            pack();
        /** Exit the Application */
        private void exitForm(java.awt.event.WindowEvent evt) {
            System.exit(0);
         * @param args the command line arguments
        public static void main(String args[]) {
            new CardTest().setVisible(true);
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        // End of variables declaration
    }

  • Added Background does not appear in PDF

    I tried this and had problems - opened up a Catia sample in Toolkit, used 'Open Background' to add a jpeg background (blue waves). Worked fine, and when I exported to .u3d, then opened that, I could see that the background was there as you'd expect - behind the 3d Catia sample.
    However, when I placed the .u3d file into an Acrobat3D pdf, the background disappeared (just saw the default gray). Tried with other examples, same thing.
    Do you know why this is happening? Thanks.

    Thanks, sws
    I'll have to experiment - what is slightly confusing is that changing the background for one item seems to keep it there for the next. So maybe when I opened up the saved .u3d and 'saw' the background, it was not so much saved in the u3d, as being added by 3d Toolkit itself.
    I'll add it to my growing list of experiments.

  • Adding Background

    I have made a stand-alone app using JPanel and want to add a Image (tiled), instead of color, as a background. Can someone help me pse.

    create a custom JPanel, e.g.
    public class BackgroundJPanel extends JPanel {
         Image backImage;
         public BackgroundJPanel() {
             super();
             //load the image
          public void paint(Graphics g) {
               //g.drawImage(image, ....)
               //If you want to tile the image, repeat the image many times
    }Now, you can use this class instead of JPanel

  • Adding background image to navigation tabs

    I am adding a backgraound image to a navigation tab and so far have found the only way to do this is by modifying the HTMLTableCell class and adding a method that sets the background image. The closest existing method is setBgColor - which doesn't work.
    I'm sure there has to be a better way to do this?
    Thanks

    My favorite technique is to use CSS. Try this in a stylesheet:
    BODY { background-image: url(marble.gif);           background-color: #cccccc; } P { background-image: none;           background-color: #cccccc; }
    For more information visit http://css.nu/.
    You can apply that style to a <DIV> you use to bound your header, for instance.
    David Phipps
    Plumtree Software

  • Adding Image to JPanel?

    Hi all,
    I am new to Swing..
    How to add the "Image" to JPanel.
    if anyone knows please help me..
    Thanks

    Here's a long reply involving several classes, some obtained directly from Sun for reuse. These allow you to fill an image in a panel as the background to your frame. You can add components on top of this background image. If this isn't what you wanted, excuse my misunderstanding:
    First, you need a panel for your frame:
    <code>
    public class FramePanel extends JPanel
    * Initialized by the (first call to the) constructor. This
    * Fill is used to paint the entire panel.
    private static TiledFill tiledFill = null;
    /*Constructors */
    public FramePanel()
    super();
    setOpaque(true);
    * Create a TiledFill object to draw the
    * background from preferences properties file
    public void getBackgroundImage()
    setForeground(Color.BLACK);
    setBackground(Color.WHITE);
    if (tiledFill == null)
    try
    FileImageInputStream file = new FileImageInputStream(*your image's file location*);
    BufferedImage image = ImageIO.read(file);
    ImageFill fill = new ImageFill(image);
    tiledFill = new TiledFill(fill, image.getWidth(), image.getHeight());
    catch (IOException e)
    e.printStackTrace();
    JOptionPane pane = new JOptionPane("Could not retrieve image in FramePanel");
    pane.setVisible(true);
    * Paint the area within <code>g.getClipBounds()</code>
    * making sure that the new tiles are aligned with a tileWidth
    * by tileHeight grid whose origin is at 0,0.
    public void paintComponent(Graphics g)
    super.paintComponent(g);
    getBackgroundImage();
    /* To ensure that the tiles we paint are aligned with
    * a tileWidth X tileHeight grid whose origin is 0,0 we
    * enlarge the clipBounds rectangle so that its origin
    * is aligned with the origin of a tile and its size
    * is a multiple of the tile size.
    Rectangle clip = g.getClipBounds();
    int tw = tiledFill.getTileWidth();
    int th = tiledFill.getTileHeight();
    int x = (clip.x / tw) * tw;
    int y = (clip.y / th) * th;
    int w = (((clip.x + clip.width + tw - 1) / tw) * tw) - x;
    int h = (((clip.y + clip.height + th - 1) / th) * th) - y;
    Graphics gFill = g.create();
    tiledFill.paintFill(this, gFill, new Rectangle(x, y, w, h));
    gFill.dispose();
    </code>
    Now you need the fill and tiled fill classes size the image.
    <code>
    import java.awt.*;
    public class Fill
    public void paintFill(Component c, Graphics g, Rectangle r)
    g.setColor(c.getBackground());
    g.fillRect(r.x, r.y, r.width, r.height);
    public void paintFill(Container c, Graphics g)
    Insets insets = c.getInsets();
    int x = insets.left;
    int y = insets.top;
    int w = c.getWidth() - (insets.left + insets.right);
    int h = c.getHeight() - (insets.top + insets.bottom);
    paintFill(c, g, new Rectangle(x, y, w, h));
    public void paintFill(Component c, Graphics g, int x, int y, int w, int h)
    paintFill(c, g, new Rectangle(x, y, w, h));
    </code>
    <code>
    import java.awt.*;
    import java.awt.image.*;
    * Displays a single <code>BufferedImage</code>, scaled to fit the
    * <code>paintFill</code> rectangle.
    * <pre>
    * BufferedImage image = ImageIO.read(new File("background.jpg"));
    * final ImageFill imageFill = new ImageFill(image);
    * JPanel p = new JPanel() {
    * public c void paintComponent(Graphics g) {
    *     imageFill.paintFill(this, g);
    * </pre>
    * Note that animated gifs aren't supported as there's no image observer.
    public class ImageFill extends Fill
    private final static int IMAGE_CACHE_SIZE = 8;
    private BufferedImage image;
    private BufferedImage[] imageCache = new BufferedImage[IMAGE_CACHE_SIZE];
    private int imageCacheIndex = 0;
    * Creates an <code>ImageFill</code> that draws <i>image</i>
    * scaled to fit the <code>paintFill</code> rectangle
    * parameters.
    * @see #getImage
    * @see #paintFill
    public ImageFill(BufferedImage image)
    this.image = image;
    * Creates an "empty" ImageFill. Before the ImageFill can be
    * drawn with the <code>paintFill</code> method, the
    * <code>image</code> property must be set.
    * @see #setImage
    * @see #paintFill
    public ImageFill()
    this.image = null;
    * Returns the image that the <code>paintFill</code> method draws.
    * @return the value of the <code>image</code> property
    * @see #setImage
    * @see #paintFill
    public BufferedImage getImage()
    return image;
    * Set the image that the <code>paintFill</code> method draws.
    * @param image the new value of the <code>image</code> property
    * @see #getImage
    * @see #paintFill
    public void setImage(BufferedImage image)
    this.image = image;
    for (int i = 0; i < imageCache.length; i++)
    imageCache[i] = null;
    * Returns the actual width of the <code>BufferedImage</code>
    * rendered by the <code>paintFill</code> method. If the image
    * property hasn't been set, -1 is returned.
    * @return the value of <code>getImage().getWidth()</code> or -1 if
    * getImage() returns null
    * @see #getHeight
    * @see #setImage
    public int getWidth()
    BufferedImage image = getImage();
    return (image == null) ? -1 : image.getWidth();
    * Returns the actual height of the <code>BufferedImage</code>
    * rendered by the <code>paintFill</code> method. If the image
    * property hasn't been set, -1 is returned.
    * @return the value of <code>getImage().getHeight()</code> or -1 if
    * getImage() returns null
    * @see #getWidth
    * @see #setImage
    public int getHeight()
    BufferedImage image = getImage();
    return (image == null) ? -1 : image.getHeight();
    * Create a copy of image scaled to width,height w,h and
    * add it to the null element of the imageCache array. If
    * the imageCache array is full, then we replace the "least
    * recently used element", at imageCacheIndex.
    private BufferedImage createScaledImage(Component c, int w, int h)
    GraphicsConfiguration gc = c.getGraphicsConfiguration();
    BufferedImage newImage = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    boolean cacheOverflow = true;
    for (int i = 0; i < imageCache.length; i++)
    Image image = imageCache;
    if (image == null)
    imageCache[i] = newImage;
    cacheOverflow = false;
    break;
    if (cacheOverflow)
    imageCache[imageCacheIndex] = newImage;
    imageCacheIndex = (imageCacheIndex + 1) % imageCache.length;
    Graphics g = newImage.getGraphics();
    int width = image.getWidth();
    int height = image.getHeight();
    g.drawImage(image, 0, 0, w, h, 0, 0, width, height, null);
    g.dispose();
    return newImage;
    * Returns either the image itself or a cached scaled copy.
    private BufferedImage getFillImage(Component c, int w, int h)
    if ((w == getWidth()) && (h == getHeight()))
    return image;
    for (int i = 0; i < imageCache.length; i++)
    BufferedImage cimage = imageCache[i];
    if (cimage == null)
    break;
    if ((cimage.getWidth(c) == w) && (cimage.getHeight(c) == h))
    return cimage;
    return createScaledImage(c, w, h);
    * Draw the image at <i>r.x,r.y</i>, scaled to <i>r.width</i>
    * and <i>r.height</i>.
    public void paintFill(Component c, Graphics g, Rectangle r)
    if ((r.width > 0) && (r.height > 0))
    BufferedImage fillImage = getFillImage(c, r.width, r.height);
    g.drawImage(fillImage, r.x, r.y, c);
    </code>
    Now it's just a simple matter of creating a frame and making the frame panel the frame's content pane:
    <code>
    import java.awt.BorderLayout;
    import java.awt.HeadlessException;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    public class ImageFrame extends JFrame
    private FramePanel framePanel;
    //start of constructors
    public ImageFrame() throws HeadlessException
    super();
    init();
    public ImageFrame(String title) throws HeadlessException
    super(title);
    init();
    // end of constructors
    void init()
    try
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    catch (Exception e)
    System.out.println ("Unknown Look and Feel");
         framePanel = new FramePanel();
         this.getContentPane().add(framePanel, BorderLayout.CENTER);
         this.setContentPane(framePanel);
         ((JPanel)this.getContentPane()).setOpaque(false);
    </code>

  • Adding background to Acrobat DC file makes large file

    Help! In the past (as recently as Acrobat XI) I have made mail-merged envelopes using Microsoft Word, then added a background PDF of the envelope in Acrobat. In December a file of about 2700 pages was only a few megabytes. Today I made a file of only 1705 pages worth of addresses, added a similar-sized envelope background, and the file is almost a terabyte! What's going on?

    Then why -- when I do EXACTLY the same thing in either X or XI do I get a file that is only a few megabytes and works fine? In DC the same procedure produced a file that was almost a terabyte! Sorry, but something pretty basic has changed between XI and DC. Hope someone can pass this info to those who can do something.

  • Adding background image to Spry Menu Bar?

    Hi Everyone,
    I have a question concerning adding a background image to a horizontal spry menu.
    1) I am using Dreamweaver CS4.
    2) I am inserting this menu bar on a HTML document.
    3) I have created and am using an external style sheet (CSS)
    I am not sure on which style(s) ("ul.*) to edit in order to acheive this.
    I have a total of four(4) different images I would like to use, they are...
    1) Horizontal Menu Bar (main menu bar)
    2) Horizontal Menu Bar - Rollover
    3) Submenu
    4) Submenu - Rollover
    Also I would like the text to be centered on the button.
    I have somewhat acheived this using the instructions for "customize this widget", yet the image "repeats" because the button is larger than the image.
    I have not tried to center the text, this may be a very simple thing, if so just let me know.
    What I dont know, and was hoping someone could tell me, is which style(s) ("ul.*) to edit in order to acheive this.
    If anyone needs anymore information, just let me know and I will give it to you as soon as possible.
    Also I have attached three(3) of the four(4) pictures I would like to use. (this is the maximum)
    Any help will be greatly appreciated,
    Musicman1994

    I'd like to thank everyone with their help so far, but I am still having trouble.
    I have tried both suggestions and the results are...
    The one suggested by dhvani2511 gave me this...
    (The cursur is over "Item 1" it is not blue when a cursur is not over it.)
    I would like the image to "be" the button, not just "in" it,if that makes sense.
    I have slightly tried the one suggested by .V1 but being that I am new to dreamweaver, code, etc. I am not sure where to copy/paste the code that you gave me.
    Does anyone have anymore information?
    Thanks,
    Musicman1994

  • JScrollPane (added to a JPanel, which paints) is visible but does nothing

    Hi. I am writing a program that reads data from files and does paiting using the info. The top level window is a JFrame, which is divided into two parts - A Box which contains the buttons through which the user interacts and a JPanel on which the painting is done. I have added the JPanel to a JScrollPane, but have run into problems there. The JScrollPane is visible (both policies always visible) but it does nothing. I understand that when the drawing is fitting, the ScrollPane is not used. But even if I make the window small or draw something that is not visible in the normal are, the JScrollPane doesn't work. In fact, no knob is visible on either of the two scrollbars. I am pasting the relevant code below:
    //import
    public class MainWindow extends JFrame {
        public static void Main(String[] args) {
            new MainWindow();
        //Declare all the variables here.
        private Box buttionBox;
        private JScrollPane scroller;
        private HelloPanel drawingPanel;   
        //other variables
        //The constructor for the class MainWindow.
        public MainWindow() {
           initComponents();
            this.setLayout(new BorderLayout());
            this.setPreferredSize(new Dimension(900,670));
            //buttonBox containts the buttons - not very relevant to this problem.
            this.getContentPane().add(buttonBox, BorderLayout.WEST);
            //scroller is the JScrollPane
            this.getContentPane().add(scroller, BorderLayout.CENTER);       
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setTitle("My Title");       
            this.setVisible(true);
            this.pack();       
        public void initComponents() {
            buttonBox = Box.createVerticalBox();
            //instantiate the buttons here and add them to the ButtonBox.
            //The event listeners instantiated. Not relevant.       
            //The various components are assigned their appropriate event listeners here.
             //Not relevant.
    //Now adding all the buttons to the box with proper spacing.
            buttonBox.add(Box.createVerticalStrut(20));
            //This is the drawing panel on which the drawing will be done.
            drawingPanel = new HelloPanel();
            scroller = new JScrollPane(drawingPanel);
                    scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scroller.getHorizontalScrollBar().setUnitIncrement(10);
            scroller.getVerticalScrollBar().setUnitIncrement(10);
        //This inner class is used to define and implement the event listener for handling the checkboxes.
        private class checkBoxListener implements ItemListener{
            public void itemStateChanged(ItemEvent e){
                 drawingPanel.repaint();
                    //Implement actions. Irrelevant
        //This private class is used to define and implement the event listener for the buttons.
        private class buttonListener implements ActionListener {
            //Do this when the button which has an instance of this class as its listener is clicked.       
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == saveStructureButton){
                  //Implement action. Irrelvant.
        //The panel on which the drawings are done.
        private class HelloPanel extends JPanel {
            //This JPanel is used for drawing the image (for the purpose of saving to disk).
            JComponent c;    //For image. Irrelevant.
            public HelloPanel(){           
                c = this;   //This is necessary for drawing the image to be saved to disk.
                this.setBackground(Color.WHITE);
            //This is the method that actually paints all the drawings whenever a.
            //The shapes theselves can be defined somewhere else, but that paint method must be invoked from here.
            public void paintComponent(Graphics g){
                super.paintComponent(g);    //First of all, clear the panel.
                Graphics2D g2 = (Graphics2D) g;
                g2.fill(new Rectangle2D.Double(40,40,100,100));    //Just for this post.
                g2.drawString("Text", 750, 750);    //To test the scrollpane.
    } I hope this helps you get an idea of what I am trying to do and what the problem might be. Any help would be really appreciated. I have spent hours on this and I have no idea why it doesn't work.
    The actual code is much bigger, so if you need any extra information, please tell me.

    HelloPanel should provide a "public Dimension getPreferredSize()" method.
    With your code you create a simple JPanel and want to draw outside of it, but you never tell the JScrollPane that your HelloPanel is actually bigger than it seems, that's why it does not feel the need to add scroll bars.
    Here's the working code:
    //import
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    public class MainWindow extends JFrame {
        public static void main(String[] args) {
            new MainWindow();
        //Declare all the variables here.
        private Box buttionBox;
        private JScrollPane scroller;
        private HelloPanel drawingPanel; 
        //other variables
        //The constructor for the class MainWindow.
        public MainWindow() {
           initComponents();
            this.setLayout(new BorderLayout());
            this.setPreferredSize(new Dimension(900,670));
            //buttonBox containts the buttons - not very relevant to this problem.
            //this.getContentPane().add(buttonBox, BorderLayout.WEST);
            //scroller is the JScrollPane
            this.getContentPane().add(scroller, BorderLayout.CENTER);       
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setTitle("My Title");       
            this.setVisible(true);
            this.pack();       
        public void initComponents() {
            //buttonBox = Box.createVerticalBox();
            //instantiate the buttons here and add them to the ButtonBox.
            //The event listeners instantiated. Not relevant.       
            //The various components are assigned their appropriate event listeners here.
             //Not relevant.
    //Now adding all the buttons to the box with proper spacing.
            //buttonBox.add(Box.createVerticalStrut(20));
            //This is the drawing panel on which the drawing will be done.
            drawingPanel = new HelloPanel();
            scroller = new JScrollPane(drawingPanel);
                    scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scroller.getHorizontalScrollBar().setUnitIncrement(10);
            scroller.getVerticalScrollBar().setUnitIncrement(10);
        //This inner class is used to define and implement the event listener for handling the checkboxes.
        private class checkBoxListener implements ItemListener{
            public void itemStateChanged(ItemEvent e){
                 drawingPanel.repaint();
                    //Implement actions. Irrelevant
        //This private class is used to define and implement the event listener for the buttons.
        private class buttonListener implements ActionListener {
            //Do this when the button which has an instance of this class as its listener is clicked.       
            public void actionPerformed(ActionEvent e) {
                //if (e.getSource() == saveStructureButton){
                  //Implement action. Irrelvant.
        //The panel on which the drawings are done.
        private class HelloPanel extends JPanel {
            //This JPanel is used for drawing the image (for the purpose of saving to disk).
            JComponent c;    //For image. Irrelevant.
            public HelloPanel(){           
                c = this;   //This is necessary for drawing the image to be saved to disk.
                this.setBackground(Color.WHITE);
            //This is the method that actually paints all the drawings whenever a.
            //The shapes theselves can be defined somewhere else, but that paint method must be invoked from here.
            public void paintComponent(Graphics g){
                super.paintComponent(g);    //First of all, clear the panel.
                Graphics2D g2 = (Graphics2D) g;
                g2.fill(new Rectangle2D.Double(40,40,100,100));    //Just for this post.
                g2.drawString("Text", 750, 750);    //To test the scrollpane.
         public Dimension getPreferredSize()
         return new Dimension(750,750);
    }

  • How can i set  picture as background using JPanel

    hello can you kindly tell me how i can set picture as the background of my jpanel instead of using set
    setBackground(Color.bule)

    you can use a JLayeredPane ; put a JPanel containing a JLabel based on an ImageIcon on the lowest layer of your JLayeredPane
    then, use upper layers to add components
    edit: another possibility is to override the "paint()" method of your JPanel container, and use drawImage() to paint the picture as a background

  • Adding background music to video.

    What is the difference between music added that shows in green background under the videos and that in purple that covers top and bottom of video?  How do you get the purple one?

    Green indicates background music that is free floating as you add or delete clips. Purple indicates music pinned to a particular point in your video.
    This article explains how to pin and unpin your music:
    http://support.apple.com/kb/PH2259
    Matt

  • Adding iamge to JPanel but it is not display in it's original size.

    hi,
    I am trying to add Image to JPanel successfully, but it' s visible in panel as a small icon rather then it's original size.
    First i creating a panel class and after that adding this panel to JFrame. I am using BufferedImage. I have tried it all the way but it's not working.Anybody have any idea about the problem.
    Thanks

    rule #1 when you have problem with code: post the code! How do you expect people to help you if you give them nothing but the plea for help?
    rule #2: use the code tags (when you paste your code, select it and press the code button) to format it so it is easy to read.

Maybe you are looking for