Buttons on top of an image?

Ok, i've got a background image, currently being displayed by a custom canvas class that simply paints the image. I need add some buttons on top of this background image, but how can I do this? The canvas class doesn't allow bottons and the like to added to them, does it? Is there some other component I can extend, set the image as its background, and then add buttons to?
Thanks in advance.

import javax.swing.*;
import java.awt.*;
public class ImagePanel
public static void main(String argv[])
new ImagePanel();
public ImagePanel()
JFrame myFrame = new JFrame();
myFrame.setSize(300, 400);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.getContentPane().add(new MyPanel());
myFrame.pack();
myFrame.show();
public class MyPanel extends JPanel
ImageIcon aboutimage;
public MyPanel()
//JPanel buttonpanel = new JPanel();
aboutimage = new ImageIcon(getClass().getResource("about.gif"));
this.setOpaque(false);
JButton button = new JButton("OK");
this.add(button, BorderLayout.CENTER);
public void paint(Graphics g)
aboutimage.paintIcon(this, g, 0, 0);
super.paint(g);
public Dimension getPreferredSize()
return new Dimension(aboutimage.getIconWidth(),aboutimage.getIconHeight());
} //end-class-MyPanel
} //end-class-ImagePanel

Similar Messages

  • Prev Next Buttons on top of jQuery

    I have a scrolling banner and on the left and right of the image I want to place 2 buttons on top of the image that scrolls.
    I have coded CSS style position:absolute or even relative, which displays fine if jQuery is not used.
    How can I code this to jQuery as the previous and next always sits underneath the image and you cannot see it
    http://www.australiancheapholidays.com/site/
    thanks

    Give the 'dialog' <div> a position of relative:
    .dialog {
    position: relative;
    Then absolutely position your 'prev2' and 'next2' links (z-index, see below) will place the links on top of the banner images.
    #prev2 {
    position: absolute;
    top: 130px;
    left: 30px;
    z-index: 300;
    #next2 {
    position: absolute;
    top: 130px;
    left: 920px;
    z-index: 320;

  • Problem placing buttons on top of a background image

    My knowledge of Java isn't the greatest and I am currently having problems placing buttons on top of a background image within a JApplet (this is also my first encounter with Applets).
    I'm using a Card Layout in order to display a series of different screens upon request.
    The first card is used as a Splash Screen which is displayed for 5 seconds before changing to the Main Menu card.
    When the Main Menu card is called the background image is not shown but the button is.
    While the Applet is running no errors are shown in the console.
    Full source code can be seen below;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.net.*;
    // First extend JApplet
    public class WOT extends JApplet {
         //--------------- Variables are declared here -----------------
         private CardLayout contentCardLayout = new CardLayout();  // declare CardLayout
         private Container contentContain;     // declare content Container
         private JPanel contentCard, splashScreen, mainMenu, menuImage; // declare content Panels
         private JLabel splash, menu, map; // declare image labels
         private ImageIcon mapBtn; // declare ImageIcons
         private JButton mapOption; // declare option Buttons
         private Timer timer; // declare Timer
         private ActionListener actionListener, mapOptionListener; // declare ActionListener
    //--------------- Initialise Applet -----------------
      public void init() {
         //--------------- Set-up Card Layout -----------------
         contentCard = new JPanel(contentCardLayout); // assign card panels to CardLayout
         //--------------- Splash Screen -----------------
         splashScreen = new JPanel();
         splash = new JLabel(new ImageIcon(getClass().getResource("img/bg.gif")));
         splashScreen.add(splash);
         splashScreen.setSize(600,800);
         splashScreen.setLocation(0,0);
    //--------------- "View Map" Option Button -----------------
         mapBtn = new ImageIcon(getClass().getResource("img/map.gif"));
         mapOption = new JButton(mapBtn);
         mapOption.setBorder(null);
         mapOption.setContentAreaFilled(false);
         mapOption.setSize(150,66);
         mapOption.setLocation(150,450);
         mapOption.setOpaque(false);
         mapOption.setVisible(true);
    //--------------- Main Menu Screen -----------------
         //menuImage = new JPanel(null);
         //menuImage.add(mainMenu);
         //menuImage.setLocation(0,0);
         mainMenu = new JPanel(null);
         menu = new JLabel(new ImageIcon(getClass().getResource("img/menu.gif")));
         menu.setLocation(0,0);
         mainMenu.add(menu);
         //mainMenu.setBackground(Color.WHITE);
         mainMenu.setLocation(0,0);
         mainMenu.setOpaque(false);
         //mainMenu.setSize(150,66);
         mainMenu.add(mapOption);
         //--------------- Map Image Screen -----------------
         map = new JLabel(new ImageIcon(getClass().getResource("img/map.gif")));
         //--------------- Add Cards to CardLayout Panel -----------------
        contentCard.add(splashScreen, "Splash Screen");
         contentCard.add(mainMenu, "Main Menu");
         contentCard.add(map, "Map Image");
    //--------------- Set-up container -----------------
          contentContain = getContentPane(); // set container as content pane
          contentContain.setBackground(Color.WHITE); // set container background colour
           contentContain.setLocation(0,0);
           contentContain.setSize(600,800);
          contentContain.setLayout(new FlowLayout()); // set container layout
           contentContain.add(contentCard);  // cards added
           //--------------- Timer Action Listener -----------------
           actionListener = new ActionListener()
                    public void actionPerformed(ActionEvent actionEvent)
                             //--------------- Show Main Menu Card -----------------
                             contentCardLayout.show(contentCard, "Main Menu");
         //--------------- Map Option Button Action Listener -----------------
           mapOptionListener = new ActionListener()
                    public void actionPerformed(ActionEvent actionEvent)
                             //--------------- Show Main Menu Card -----------------
                             contentCardLayout.show(contentCard, "Map Image");
         //--------------- Timer -----------------               
         timer = new Timer(5000, actionListener);
         timer.start();
         timer.setRepeats(false);
    }Any help would be much appreciated!
    Edited by: bex1984 on May 18, 2008 6:31 AM

    1) When posting here, please use fewer comments. The comments that you have don't help folks who know Java read and understand your program and in fact hinder this ability, which makes it less likely that someone will in fact read your code and help you -- something you definitely don't want to have happen! Instead, strive to make your variable and method names as logical and self-commenting as possible, and use comments judiciously and a bit more sparingly.
    2) Try to use more methods and even classes to "divide and conquer".
    3) To create a panel with a background image that can hold buttons and such, you should create an object that overrides JPanel and has a paintComponent override method within it that draws your image using the graphics object's drawImage(...) method
    For instance:
    an image jpanel:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.net.URISyntaxException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    public class BackgroundImage
        // **** this will have to be changed for your program:
        private static final String IMAGE_PATH = "../../m02/a/images/Forest.jpg";
        private BufferedImage myImage = null;
        private JPanel imagePanel = new JPanel()
            @Override
            protected void paintComponent(Graphics g)
            {   // *** here is where I draw my image
                super.paintComponent(g);  // **** don't forget this!
                if (myImage != null)
                    g.drawImage(myImage, 0, 0, this);
        public BackgroundImage()
            imagePanel.setPreferredSize(new Dimension(600, 450));
            imagePanel.add(new JButton("Foobars Rule!"));
            try
                myImage = createImage(IMAGE_PATH);
            catch (IOException e)
                e.printStackTrace();
            catch (URISyntaxException e)
                e.printStackTrace();
        private BufferedImage createImage(String path) throws IOException,
                URISyntaxException
            URL imageURL = getClass().getResource(path);
            if (imageURL != null)
                return ImageIO.read(new File(imageURL.toURI()));
            else
                return null;
        public JPanel getImagePanel()
            return imagePanel;
    }and an applet that uses it:
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    public class BackgrndImageApplet extends JApplet
        @Override
        public void init()
            try
                SwingUtilities.invokeAndWait(new Runnable()
                    @Override
                    public void run()
                        getContentPane().add(new BackgroundImage().getImagePanel());
            catch (InterruptedException e)
                e.printStackTrace();
            catch (InvocationTargetException e)
                e.printStackTrace();
    }

  • How can I draw on top of an image?

    I'm using a JApplet with three JPanel's inside of it.
    Inside one of those JPanel's I would like to place an image and a button. When the button is clicked, I want to draw an oval on top of the image . Each subsequent time the button is clicked, the oval will move to a different location.
    So here are my questions:
    1) What should I use to draw the image? (a JLabel with an ImageIcon on it?)
    2) Could I simply use g.drawOval() in paintComponent() to directly draw on top of the image/JLabel?
    Any help will be greatly appriciated.

    Here's a sample to study;-import java.awt.*;
    import java.awt.event.*;
    public class DrawOnImage extends java.applet.Applet{
      int xPos, yPos;
      Image img;
      public void init() {
        add(new Label("Hello World") );
        Button press = new Button("press");
        add(press);
        press.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
            xPos = (int)(Math.random()*270);
            yPos = (int)(Math.random()*170)+30;
            repaint();
        img = getImage(getDocumentBase(), "anImage.JPG");
      public void paint(Graphics g){
        g.drawImage(img,0,30,this);
        if(yPos>=30)g.fillOval(xPos, yPos, 45, 45);
    }

  • Placing buttons on top of a rectangle..

    I have 5 button that i placed on my applet by specifying its exact location
    by using setBounds...for example, i have...
    btnOne.setBounds(15,159,120,40)...etc.
    I want to place these five buttons on top of a black rectangle, so that the
    black rectangle is serves as a background. How can I do this in Java? I also,
    wanted to place text over an image i have in my applet but am experience
    problems.
    thanx
    trin

    You can put the buttons in a panel and set the background color of the panel to black. Then you just have to add the panel to your applet.

  • Mac: "+" button on top will'nt resize PS to full screen

    On Photoshop cs5 MAC, the "+" button on top of window
    will not resize the current window to full screen, just to the size of the current open image.
    Is there any solution to that?
    Thanks in advance.

    View>Fit to Screen will just fit 1 side to screen (horizontal or vertical), and it will "zoom" the image to fit to screen.
    In cs4 the "+" button was resizing the entire "window" to full screen, letting the image size as it is, and the empty space around the image was the Grey color.
    Full screen mode will not help me, because i cant see tabs.

  • How can i place button on top right hand corner of the tabbed pane

    Hi all,
    i want to place button on top right hand corner of the tabbed pane, just run the code below, i have add button(it going to insert tab into tabbedpane), i want to place that tab into top right hand corner of the tabbedpane (not inside the tab itself). if i set tab policy setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT), it will give two button at right hand corner along with those buttons i want to and one more add button.
    please suggest so that i can move forward.
    Thanks in advance
    import java.awt.Dimension;
    import java.util.HashMap;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    * @author  Dayananda.BV
    public class TabpaneDemo extends javax.swing.JFrame {
        /** Creates new form TabpaneDemo */
        HashMap<Integer, tabpanel> panelMap = new HashMap<Integer, tabpanel>();
        public TabpaneDemo() {
            initComponents();
            createFloorPlan();
            getContentPane().setPreferredSize(new Dimension(400,400));
            jTabbedPane1.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        /** 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.
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
        private void initComponents() {
            jTabbedPane1 = new javax.swing.JTabbedPane();
            add_tab_button = new javax.swing.JButton();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            add_tab_button.setText("+");
            add_tab_button.setMargin(new java.awt.Insets(0, 0, 0, 0));
            add_tab_button.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    add_tab_buttonActionPerformed(evt);
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(308, Short.MAX_VALUE)
                    .addComponent(add_tab_button, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(67, 67, 67))
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(add_tab_button)
                    .addGap(8, 8, 8)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE))
            pack();
        }// </editor-fold>                       
        private void createFloorPlan(){
            tabpanel floorplan_Panel = new tabpanel(panelMap.size()+1);
            panelMap.put(floorplan_Panel.getTabIndex(), floorplan_Panel);
            jTabbedPane1.add(floorplan_Panel, floorplan_Panel.getTabName());
            jTabbedPane1.setSelectedIndex(jTabbedPane1.getTabCount()-1);
        private void add_tab_buttonActionPerformed(java.awt.event.ActionEvent evt) {                                              
            createFloorPlan();
         * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TabpaneDemo().setVisible(true);
        // Variables declaration - do not modify                    
        private javax.swing.JButton add_tab_button;
        private javax.swing.JTabbedPane jTabbedPane1;
        // End of variables declaration                  
        class tabpanel extends JPanel{
            private int tabIndex = 0;
            private String tabName ;
            public tabpanel(int tabIndex) {
                this.tabIndex = tabIndex;
                if(tabIndex >= 10) {
                    tabName = "Floor Map"+tabIndex;
                } else{
                    tabName = "Floor Map"+tabIndex+"  ";
            public int getTabIndex(){
                return tabIndex;
            public String getTabName(){
                return tabName;
    }Thanks
    Dayananda B V

    This part of tabbed pane is not customizable as it lies in the ComponentUI(TabbedpaneUI) portion of the tabbedpane.
    But I can point out the place u can change to bring the desired feature into effect.
    U can find an Inner class called ScrollableTabSupport
    Look for the methods createButtons where u can create extra buttons and add to the tabbed pane.
    The Inner class also implements actionListener where u can implement the action for the button u added.
    By this method U have to use your own extended TabbedPaneUI which u cant escape from.
    Hope this will help u.

  • When i try to scroll on a image webside it scips to the bottom and then to the top of the image w/ a vertical black line instead of the curser

    whaen i try to sroll on images on a website that has a gallery and where there is a description up top and a image in th middle and more txt on the sides the scrolling is extremly choppy when i use the arrow keys to scroll

    Have you upgraded to iPhoto 9.6 for compatibility with Yosemite? If not, try that first.
    It looks like iPhoto has lost the connection between the thumbnails and the original image files.
    This can be caused by a corrupted iPhoto library, or the originals have been deleted or moved.
    Try first to rebuild your iPhoto Library:
    If you do not have a current backup of the iPhoto library, make a copy of the library, but do not overwrite any previous backup.
    Launch iPhoto with the ⌥⌘-key combination (option-command) held down.
    Select "rebuild" from the first aid panel.  This may take a while for a large library.
    Can you now see your photos again?
    If not, rebuild the library with iPhoto Library manager as described by Old Toad:            Re: iphoto crashed

  • Hi I have an iPhone 4 and suddenly it stopped working. Could here notifications and nothing on the diisplay. Wake up  button on top is not working. so unable to restart the phone. Wanted to try restore from iTunes but Find my iPhone (iCloud) is on.

    Hi I have an iPhone 4 and suddenly it stopped working. Could here notifications and nothing on the diisplay. Wake up  button on top is not working. so unable to restart the phone. Wanted to try restore from iTunes but Find my iPhone (iCloud) is on. How to recover or restart my Phone

    Follow these steps to log into your iCloud account on your computer and deactivate "Find my iPhone" on your device:
    Remove an iOS device or Mac on which you can’t turn off Find My iPhone
    If you can’t turn off Find My iPhone on the device, turn off the device so it goes offline, then remove it from Find My iPhone on iCloud.com.
    Note:    You can also remove your iOS device by first erasing it—just follow the instructions below for removing an iOS device you don’t have. You can later restore the device from an iCloud or iTunes backup.
    Turn off the device you want to remove.
    Sign in to icloud.com/#find on another computer with your Apple ID (the one you use with iCloud). If you’re using another iCloud app, click the app’s name at the top of the iCloud.com window, then click Find My iPhone.
    Click All Devices, select the offline device, then click Remove from Account. If you don’t see Remove from Account, click All Devices again, then click the Delete button next to the device. If the device comes online again, it will reappear in Find My iPhone. If your device reappears, turn off Find My iPhone on the device (follow the instructions above for removing a device by turning off Find My iPhone), or if it’s an iOS device and you no longer have it, follow the instructions below for removing an iOS device you no longer have.
    Remove an iOS device you no longer have
    If you no longer have the iOS device because you gave it away or sold it, you need to remotely erase it before you can remove it.
    Sign in to icloud.com/#find with your Apple ID (the one you use with iCloud). If you’re using another iCloud app, click the app’s name at the top of the iCloud.com window, then click Find My iPhone.
    Click All Devices, then select the device.
    Click Erase [device], then enter your Apple ID password. Because the device isn’t lost, don’t enter a phone number or message.
    Note:    If you’re trying to erase a family member’s device, that person will need to enter his or her Apple ID password on this device. If the device is offline, the remote erase begins the next time it’s online. You’ll receive an email when the device is erased.
    When the device is erased, click Remove from Account.All your content is erased, and someone else can now activate the device.
    copied from: iCloud: Remove your device from Find My iPhone

  • I tried changing my password, and it changed to one that wasnt it, and i dont know it. So i tried it too many times and now its saying it is disabled, connect to itunes. but a problem is that my power button on top is broken. how to i fix it?

    I tried changing my password, and it changed to one that wasnt it, and i dont know it. So i tried it too many times and now its saying it is disabled, connect to itunes. but a problem is that my power button on top is broken. how to i fix it?

    Disabled
    Place the iOS device in Recovery Mode and then connect to your computer and restore via iTunes. The iPod will be erased.
    iOS: Wrong passcode results in red disabled screen                         
    If recovery mode does not work try DFU mode.                        
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings        
    For how to restore:
    iTunes: Restoring iOS software
    To restore from backup see:
    iOS: How to back up     
    If you restore from iCloud backup the apps will be automatically downloaded. If you restore from iTunes backup the apps and music have to be in the iTunes library since synced media like apps and music are not included in the backup of the iOS device that iTunes makes.
    You can redownload most iTunes purchases by:
    Downloading past purchases from the App Store, iBookstore, and iTunes Store        

  • How do I get rid of the thin line that goes across the top Persona background image.

    There is a 1-pixel wide light grey line across the top Persona background image between the navigation toolbar and the bookmarks toolbar from left to right. Nomatter whatever persona I have installed, it is there. Same thing also when i hover on any other Persona at Getpersonas.com. It is not there when using any other program and not when only showing the desktop. Only when using FF4. My PC is a HP Pavilion dv6000 laptop using Windows Vista. This is not a monitor issue nor a graphic card issue since it only shows in FF.

    for me, it happens on a maximized window. when i do restore to window, the line disappears.

  • How can I add callouts on top of anchored images in CS6?

    I am using InDesign CS6 on a MacBook. I have multiple anchored images in a long document. Some of the images require callouts, such as labels or arrows, which should display on top of the image and should always stay with the image as if these items were also anchored. Is there a way to layer images in InDesign so that they are all anchored?
    I tried grouping the callout objects with the anchored object, but you cannot select an anchored object and other objects at the same time. I know in Word you can create an image canvas and add multiple items to that, and they are all grouped together. Is there a way to do that in InDesign? Or would I have to anchor each callout along with its corresponding image?

    Peter, your advice was very helpful. However, the text boxes (callout labels) that are included in the grouped objects get distorted once the grouped objects are anchored. Each text box shows the red +. When I expand the text box to fit content, the text box expands vertically so that all of the text is below the originally anchored object.
    The other callouts (just lines) stay in place perfectly.  Any insight into why the text boxes don't behave the same way that the lines do?

  • Adding slide in caption on top of main image in gallery

    ok, now the same caption question from another angle...how
    would you get the caption to display on top of the main photo...and
    once that is accomplished, I want to add a slide effect *smile*
    I found a page that I want to recreate with spry...but its
    gonna be hard...I have the basics started, but cannot figure out
    how to put the text on TOP of the image
    http://www.nmdhsem.org/default.asp?CustComKey=270308&CategoryKey=274276&pn=Page&DomName=nm dhsem.org
    oh I am using the photo gallery demo, but hiding the select
    and gallery name so that just 3 images cycle on opening the page
    Edited: 08/28/2007 at 03:06:40 PM by karonz

    ok, so far I have this...I got the images to fade, but still
    would like to have a caption that fades in and out as well...
    any suggestions
    http://www.nmprc.state.nm.us/Copyindex.htm
    oh...and lol...I still have to get some images from NM
    instead of china *g*

  • "File could not be found" message on top of all images in Library:  How do I fix this?[was:Question]

    New to LR 5.2 Running OS X 10.8.5. Tried to save image in file folder. Now get this message at top of all images in Library: "File could not be found." How do I fix this?

    “Save” is not a term used with LR to create images.  What are you trying to do, exactly, in terms of LR menu options? 
    Are there questions marks on the thumbnails for the images that say File could not be found?
    Sometimes a screenshot can communicate things quicker than words.

  • My ipod screen has a white screen i have tried plugging it in to itunes and pressing the home button and top button together but it wont turn on! this has happened before, i should have never bought one what should i do?

    my ipod screen has went white i have tried plugging it into itunes and pressing the home button and top button together for 10 seconds but it wont work!! this has happened twice before already and i havent even had it a year yet, i should have listened to my sister and never have bought one!!! someone help please ?

    - Connect to computer and it it shows in iTune restore via iTunes
    - Then try letting the battery fully drain. After charging for an hour try the reset (both buttons) and connecting to computer and restore.

Maybe you are looking for